C语言之三子棋游戏(棋盘)

test.c

#define _CRT_SECURE_NO_WARNINGS 1

//测试三子棋

#include"game.h"

void menu()

{

printf("**********************************\n");

printf("***** 1.play 0.exit ****\n");

printf("**********************************\n");

}

void game()//游戏实现

{

//数组-存放走出的棋盘信息

char board[ROW][COL] = { 0 };//全部是空格

//初始化棋盘

InitBoard(board,ROW,COL);

//打印棋盘

DisplayBoard(board, ROW, COL);

}

void test()

{

int input = 0;

do

{

menu();

printf("请选择:>");

scanf("%d", &input);

switch (input)

{

case 1:

game();

break;

case 0:

printf("退出游戏\n");

break;

default:

printf("选择错误,请重新选择!\n");

break;

}

} while (input);

}

int main()

{

test();

return 0;

}


game.h

#define ROW 3

#define COL 3

#include<stdio.h>

//声明

void InitBoard(char board[ROW][COL],int row,int col);

void DisplayBoard(char board[ROW][COL],int row,int col);


game.c

#include"game.h"

void InitBoard(char board[ROW][COL], int row, int col)

{

int i = 0;

int j = 0;

for (i = 0; i < row; i++)

{

for (j = 0; j < col; j++)

{

board[i][j] = ' ';

}

}

}

void DisplayBoard(char board[ROW][COL], int row, int col)

{

int i = 0;

for (i = 0; i < row; i++)

{

//1.打印一行的数据

//printf(" %c | %c | %c \n");

//printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][2]);

int j = 0;

for (j = 0; j < col; j++)

{

printf(" %c ", board[i][j]);

if (j < col - 1)

printf("|");

}

printf("\n");

//2.打印分割行

//if (i < row - 1)

//{

// printf("---|---|---\n");

//}

if (i < row - 1)

{

for (j = 0; j < col; j++)

{

printf("---");

if(j<col-1)

printf("|");

}

}

printf("\n");

}

}

相关推荐
uppp»4 分钟前
深入理解 Java 反射机制:获取类信息与动态操作
java·开发语言
玩电脑的辣条哥2 小时前
Python如何播放本地音乐并在web页面播放
开发语言·前端·python
楼台的春风3 小时前
【GPIO详解及实践示例】
c语言·stm32·单片机·嵌入式硬件·mcu·物联网·嵌入式
ll7788115 小时前
LeetCode每日精进:20.有效的括号
c语言·开发语言·算法·leetcode·职场和发展
Jackson@ML6 小时前
Python数据可视化简介
开发语言·python·数据可视化
赵琳琅7 小时前
Java语言的云计算
开发语言·后端·golang
lly2024067 小时前
jQuery 杂项方法
开发语言
赵琳琅7 小时前
MDX语言的安全开发
开发语言·后端·golang
coding_rui7 小时前
链表(C语言版)
c语言·数据结构·链表
开开又心心的学嵌入式7 小时前
C语言——指针进阶应用
c语言·开发语言