首先简单介绍一下三子棋的规则:在九宫格棋盘上,那一方三个棋子连成一线即判定内一方胜利(连成一线指:一行或一列或对角线)。
我们使用多文件编程,多文件编程便于修改和维护代码,便于复用代码
头文件:game.h //包含函数各种声明,宏定义
源文件:test.c //主要负责测试整个代码
源文件:game.c //包含各种函数的定义
代码实现:
1.首先打印出菜单选项
void menu()
{
printf("**********************************\n");
printf("**********************************\n");
printf("***** 0.play *************\n");
printf("***** 1.exit *************\n");
printf("**********************************\n");
}
//然后在主函数中调用menu菜单函数
menu();
do
{
printf("请输入要选的的选项: ");
scanf("%d",&input);
switch (input)
{
case 0:
game();
break;
case 1:
printf("退出游戏");
break;
default:
printf("请输入要选的选项:\n");
break;
}
} while (input);
2.创建一个二维数组board用来储存三子棋的元素首先对其初始化
void Initboard(char board[ROW][COL], int row, int col)
{
//memset(&board[0][0], ' ', row*col * sizeof(board[0][0])); //也可以使用函数对数组进行初始化
//memset() 库函数,头文件为 #include , 参数分别为,首地址,需要设置的字符,需要设置的字符个数
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
board[i][j] = ' ';
}
}
}
3.创建Displayboard函数打印九宫格棋盘
void Displayboard(char board[ROW][COL], int row, int col) //打印棋盘
{
int i;
int j;
for (i = 0; i < row; i++)
{
for (j = 0; j < col ; j++)
{
printf(" %c ", board[i][j]);
if (j == col - 1)
continue;
printf("|");
}
printf("\n");
if (i < row - 1)
printf("-----------\n");
}
}
4.玩家和电脑落子,使用Playermove和Computermove函数来对board数组进行更改,模拟落子情况
此处考虑到玩家输入的坐标一般为 1至3 所以将判断条件改为 x>=1 y>=1 ,但是在下个判断条件要将x y坐标减一
void Playermove(char board[ROW][COL]) //玩家输入坐标
{
while (1)
{
int x = 0;
int y = 0;
printf("请玩家输入坐标");
scanf_s("%d %d", &x, &y);
if (x <= ROW && x >= 1 && y <= COL && y >= 1) //判断输入的坐标是否越界
{
if (board[x - 1][y - 1] == ' ') //输入的坐标必须为空才可以落子
{
board[x - 1][y - 1] = '*';
break;
}
else
printf("坐标被占用,请重新输入\n");
}
else
printf("坐标有误,请重新输入\n");
}
}
void Computermove(char board[ROW][COL]) //电脑输入坐标
{
int x = 0;
int y = 0;
while (1)
{
x = rand() % 3; //使用rand函数生成随机数并取余得到电脑落子坐标
y = rand() % 3;
if (board[x][y] == ' ')
{
printf("电脑输入坐标%d %d\n", x+1, y+1);
board[x][y] = '#';
break; //打印后跳出循环
}
}
}
5.玩家和电脑每落一次子,就要进行一次判断是否获胜,使用Iswin函数判断是否获胜
根据返回值来进行判断游戏结果(此处定义为 1为玩家获胜 0为电脑获胜 2为平局(返回值可以任意更改))
int Iswin(char board[ROW][COL], int row, int col) //判断谁获胜
{
int i;
int j;
//1为玩家获胜 0为电脑获胜 2为平局
for (i = 0; i < row; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] == '*') //判断同一行是否相等
return 1;
else if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] == '#')
return 0;
}
for (j = 0; j < col; j++)
{
if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[1][j] == '*') //判断同一列是否相等
return 1;
else if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[1][j] == '#')
return 0;
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] == '*') //判断左对角线是否相等
return 1;
else if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] == '#')
return 0;
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] == '*') //判断右对角线是否相等
return 1;
else if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] == '#')
return 0;
if (Full(board, ROW, COL)) //判断是否平局
return 2;
return 3; //返回3为继续进行游戏
}
下面上源代码
test.c //测试整个代码
# define _CRT_SECURE_NO_WARNINGS
#include"game.h"
void menu()
{
printf("**********************************\n");
printf("**********************************\n");
printf("***** 0.play *************\n");
printf("***** 1.exit *************\n");
printf("**********************************\n");
}
void game()
{
char board[ROW][COL] = { 0 };
int ret;
Initboard(board, ROW, COL); //用Initboard函数初始化数组,并用‘ ’初始化数组
printf("游戏开始\n");
while (1)
{
Playermove(board); //玩家输入坐标
Displayboard(board, ROW, COL);
ret = Iswin(board, ROW, COL); //判断是否获胜
if (ret == 1)
{
printf("玩家获胜");
break;
}
else if (ret == 0)
{
printf("电脑获胜");
break;
}
else if (ret == 2)
{
printf("平局");
break;
}
printf("\n\n");
Computer(board); //电脑输入坐标
Displayboard(board, ROW, COL);
ret = Iswin(board, ROW, COL); //判断是否获胜
if (ret == 1)
{
printf("玩家获胜");
break;
}
else if (ret == 0)
{
printf("电脑获胜");
break;
}
else if (ret == 2)
{
printf("平局");
break;
}
printf("\n\n");
}
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
menu();
do
{
printf("请输入要选的的选项: ");
scanf("%d",&input);
switch (input)
{
case 0:
game();
break;
case 1:
printf("退出游戏");
break;
default:
printf("请输入要选的选项:\n");
break;
}
} while (input);
}
game.c //函数的定义实现
#include"game.h"
void Initboard(char board[ROW][COL], int row, int col)
{
//memset(&board[0][0], ' ', row*col * sizeof(board[0][0]));
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;
int j;
for (i = 0; i < row; i++)
{
for (j = 0; j < col ; j++)
{
printf(" %c ", board[i][j]);
if (j == col - 1)
continue;
printf("|");
}
printf("\n");
if (i < row - 1)
printf("-----------\n");
}
}
void Playermove(char board[ROW][COL]) //玩家输入坐标
{
while (1)
{
int x = 0;
int y = 0;
printf("请玩家输入坐标");
scanf_s("%d %d", &x, &y);
if (x <= ROW && x >= 1 && y <= COL && y >= 1)
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '*';
break;
}
else
printf("坐标被占用,请重新输入\n");
}
else
printf("坐标有误,请重新输入\n");
}
}
void Computer(char board[ROW][COL]) //电脑输入坐标
{
int x = 0;
int y = 0;
while (1)
{
x = rand() % 3;
y = rand() % 3;
if (board[x][y] == ' ')
{
printf("电脑输入坐标%d %d\n", x+1, y+1);
board[x][y] = '#';
break; //打印后跳出循环
}
}
}
int Iswin(char board[ROW][COL], int row, int col) //判断谁获胜
{
int i;
int j;
//1为玩家获胜 0为电脑获胜 2为平局
for (i = 0; i < row; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] == '*') //判断同一行是否相等
return 1;
else if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] == '#')
return 0;
}
for (j = 0; j < col; j++)
{
if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[1][j] == '*') //判断同一列是否相等
return 1;
else if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[1][j] == '#')
return 0;
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] == '*') //判断左对角线是否相等
return 1;
else if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] == '#')
return 0;
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] == '*') //判断右对角线是否相等
return 1;
else if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] == '#')
return 0;
if (Full(board, ROW, COL)) //判断是否平局
return 2;
return 3; //返回3为继续进行游戏
}
int Full(char board[ROW][COL], int row, int col) //判断棋盘是否下满
{
int i;
int j;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (board[i][j] == ' ')
return 0; //此时棋盘未满
}
}
return 1; //此时棋盘已满
}
game.h //函数声明和宏定义
#include
#include
#include
#include
#define ROW 3
#define COL 3
void Initboard(char board[ROW][COL], int row, int col);
void Displayboard(char board[ROW][COL], int row, int col);
void Playermove(char board[ROW][COL]);
void Computer(char board[ROW][COL]);
int Iswin(char board[ROW][COL], int row, int col);
int Full(char board[ROW][COL], int row, int col);