思路
主要以模块化的函数实现井字棋不同的功能
主要功能
初始化棋盘,在每次玩游戏之前,有必要初始化
打印棋盘,要让玩家能看得到棋盘长什么样子
玩家回合,总得让玩家下棋吧
电脑回合,玩家和玩家对弈也不是不可以
判断胜利,不然要一直玩下去吗
游戏菜单
void menu()
{
printf("****************************************\n");
printf("***************1. 进入游戏**************\n");
printf("***************2. 退出游戏**************\n");
printf("****************************************\n");
}
简简单单,没啥功能
主函数
int main()
{
int input;
srand((unsigned)time(NULL));
do
{
menu();
printf("请选择:>");
scanf("%d",&input);
switch (input)
{
case 1:
printf("游戏开始啦!\n");
game();
break;
case 2:
printf("退出游戏咯,拜拜\n");
break;
default:
printf("输入错误,请重新输入:>");
}
} while (input != 2);
return 0;
}
要有输入,并且判断玩家的意图是玩还是不玩,玩的话是需要调用游戏的函数
游戏函数
void game()
{
char board[ROW][COL] = {0};
char ret;
Init(board, ROW, COL);
print(board, ROW, COL);
while (1)
{
player_round(board, ROW, COL);
print(board, ROW, COL);
ret = check_win(board, ROW, COL);
if (ret != 'c')
break;
computer_round(board, ROW, COL);
print(board, ROW, COL);
ret = check_win(board, ROW, COL);
if (ret != 'c')
break;
}
if (ret == 'x')
printf("恭喜你,你赢啦!\n");
else if (ret == 'o')
printf("你输咯,再来一局吧\n");
else
printf("平局啦,电脑真厉害\n");
}
首先初始化棋盘,打印一下让玩家选择下在哪个位置,然后判断是否赢,然后电脑下棋,判断是否赢,如果有赢的或者占满了,就退出这个循环,最后再判断是哪一种情况结束了游戏
初始化棋盘
void Init(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 print(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++)
{
printf(" ");
printf("%c", board[i][j]);
printf(" ");
if (j < col - 1)
{
printf("|");
}
}
printf("\n");
if (i < row - 1)
{
for (j = 0; j < col; j++)
{
printf("---");
if (j < col - 1)
{
printf("|");
}
}
}
printf("\n");
}
printf("\n");
}
玩家回合
void player_round(char board[ROW][COL], int row, int col)//玩家回合
{
int x;
int y;
do
{
printf("轮到你下棋啦\n");
printf("请输入坐标:>");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = 'x';
break;
}
else
{
printf("这里已经下过啦,换一个位置吧!\n");
}
}
else
{
printf("你输入的位置不太对哦,换一个位置吧!\n");
}
} while (1);
}
电脑回合
void computer_round(char board[ROW][COL], int row, int col)//电脑回合
{
printf("轮到电脑下棋咯\n");
while (1)
{
int x = rand() % 3;
int y = rand() % 3;
if (board[x][y] != ' ')
continue;
else
{
board[x][y] = 'o';
break;
}
}
}
是否赢了
char check_win(char board[ROW][COL], int row, int col)//判断胜利
{
int a = 0;
int b = 0;
for (a = 0; a < row; a++)
{
if (board[a][0] == board[a][1] && board[a][2] == board[a][1] && board[a][0] != ' ')
{
return board[a][0];
}
}
for (b = 0; b < col; b++)
{
if (board[0][b] == board[1][b] && board[1][b] == board[2][b] && board[0][b] != ' ')
{
return board[0][b];
}
}
if (board[0][0] == board[1][1] && board[0][0] == board[2][2] && board[0][0] != ' ')
return board[0][0];
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != ' ')
return board[0][2];
if (1 == check_full(board, row, col))
return 'f';
return 'c';
}
是否满了
int check_full(char board[ROW][COL], int row, int col)//判断平局
{
int i, j;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (board[i][j] == ' ')
return 0;
}
}
return 1;
}