//test.c
#include
void menu()//打印菜单
{
printf("******1.开始游戏*******\n");
printf("**********************\n");
printf("******0.退出游戏*******\n");
printf("**********************\n");
}
void game()
{
}
int main()
{
int input = 0;
do
{
menu();
printf("请选择:");
scanf("%d", &input);
switch (input)
{
case 1:
printf("\n游戏开始\n");
game();//游戏运行
printf("\n游戏结束\n");
break;
case 0:
printf("退出游戏");
break;
default:
printf("选择错误请重新输入:\n");
}
} while (input);
return 0;
}
#include
代码习惯:分步进行,边写边调试,在调试中完善每一个模块
井字棋盘⇨数据的存储需要一个ROW*COL在头文件//game.h定义,方便后期改动(3*3)二维数组
//game.h
#define ROW 3
#define COl 3
define定义的常量标识符句尾没有分号“;” 头文件里的函数声明句尾有“;”
//game.c
void init_board(char board[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++)
{
board[i][j] = ' ';//初始化为空格
}
}
}
void display_board(char board[ROW][COL], int row, int col)//二维数组不可以省略列!
{
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++)
{
printf(" %c ", board[i][j]);
if (j < col - 1)
{
printf("|");
}
}
printf("\n");
if (i < col - 1)
{
for (j = 0; j < col; j++)
{
printf("---");
if (j < col - 1)
{
printf("|");
}
}
printf("\n");
}
}
}
//test.c
void game()
{
char board[ROW][COL] = { 0 };
//初始化棋盘
init_board(board, ROW, COL);
//打印棋盘
display_board(board, ROW, COL);
}
二维数组不可以省略列!
board[i][j] = ' ';//初始化为空格
//game.h
#include
#include
//初始化棋盘
void init_board(char board[ROW][COL], int row, int col);
//打印棋盘
void display_board(char board[ROW][COL], int row, int col);
//玩家下棋
void player_move(char board[ROW][COL], int row, int col);
//电脑下棋
void computer_move(char board[ROW][COL], int row, int col);
//game.c
//玩家下棋
void player_move(char board[ROW][COL], int row, int col)
{
int i = 0, j = 0;
printf("请输入坐标:");
while (1)
{
scanf("%d %d", &i, &j);
if (i <= row && j <= col)
{
if (' ' == board[i - 1][j - 1])
{
board[i - 1][j - 1] = '*';
break;
}
else
{
printf("该坐标已被占用,请重新输入:");
}
}
else
{
printf("该坐标非法,请重新输入:");
}
}
}
//电脑下棋
void computer_move(char board[ROW][COL], int row, int col)
{
printf("电脑下棋\n");
while (1)
{
int i = rand() % row, j = rand() % col;//生成随机数坐标,注意坐标的范围
if (' ' == board[i][j])
{
board[i][j] = '#';
break;
}
}
}
//test.c
void game()
{
char board[ROW][COL] = { 0 };
//初始化棋盘
init_board(board, ROW, COL);
//打印棋盘
display_board(board, ROW, COL);
//玩游戏
while (1)
{
//玩家下棋
player_move(board, ROW, COL);
//电脑下棋
computer_move(board, ROW, COL);
//打印棋盘
display_board(board, ROW, COL);
}
}
int main()
{
int input = 0;
srand((unsigned)time(NULL));//调用随机数生成器
do
{
menu();
printf("请选择:");
scanf("%d", &input);
switch (input)
{
case 1:
printf("\n游戏开始\n");
game();//游戏运行
printf("\n游戏结束\n");
break;
case 0:
printf("退出游戏");
break;
default:
printf("选择错误请重新输入:\n");
}
} while (input);
return 0;
}
//game.c
//判断棋盘是否已满
int is_full(char board[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++)
{
if (' ' == board[i][j])
{
return 0;
}
}
}
return 1;
}
//判断游戏状态
char is_win(char board[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)//判断一行
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ')
{
return board[i][0];
}
}
for (i = 0; i < col; i++)//判断一列
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ')
{
return board[0][i];
}
}
//判断对角线
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
{
return board[1][1];
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
{
return board[1][1];
}
//判断是否平局
if ( 1 == is_full(board, row, col) )
{
return 'D';
}
return 'C';
}
//test.c
void game()
{
char board[ROW][COL] = { 0 };
//初始化棋盘
init_board(board, ROW, COL);
//打印棋盘
display_board(board, ROW, COL);
//玩游戏
while (1)
{
//玩家下棋
player_move(board, ROW, COL);
display_board(board, ROW, COL);
printf("\n");
//判断游戏状态
if ('C'!= is_win(board, ROW, COL))
{
break;
}
//电脑下棋
computer_move(board, ROW, COL);
display_board(board, ROW, COL);
printf("\n");
//判断游戏状态
if ('C' != is_win(board, ROW, COL))
{
break;
}
}
if ('*' == is_win(board, ROW, COL))
{
printf("玩家获胜\n");
}
if ('#' == is_win(board, ROW, COL))
{
printf("电脑获胜\n");
}
display_board(board, ROW, COL);
}
完整源码