游戏规则
文件管理
三子棋的代码并不是很简单,所以需要文件分类管理。
game.h:主要存放函数的声明
test.c:三子棋的基本逻辑
game.c:游戏的实现
游戏实现代码讲解
开始前的准备
打印菜单:1开始游戏,0退出游戏
void menu()
{
printf("*****************************\n");
printf("******** 1. play *********\n");
printf("******** 0. exit *********\n");
printf("*****************************\n");
}
用switch语句实现选择的功能
int main()
{
int input = 0;
srand(time(NULL));
do
{
menu();
printf("请选择: ");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误,请重新选择\n");
break;
}
} while (input);
return 0;
}
srand(time(NULL)); 初始化随机数生成器,参数传入一个永远不会和之前相等的时间戳,在之后的rand调用才会生成不同的结果。(固定用法,不需要理解,直接记忆)
game.h中的声明:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#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], int row, int col);
//电脑下棋
void ComputerMove(char board[ROW][COL], int row, int col);
//判断输赢
//玩家赢 - '*'
//电脑赢 - '#'
//平局 - 'Q'
//继续 - 'C'
char IsWin(char board[ROW][COL], int row, int col);
游戏的逻辑
1. 初始化棋盘
将行和列设置成可以宏定义,方便我们后续直接替换
#define ROW 3
#define COL 3
将棋盘初始化为空格
void InitBoard(char board[ROW][COL], int row, int col)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
board[i][j] = ' ';
}
}
}
2. 展示棋盘
void DisplayBoard(char board[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
//1. 打印数据
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)
{
for (j = 0; j < col; j++)
{
printf("---");
if (j < col - 1)
printf("|");
}
printf("\n");
}
}
}
若为3行3列打印出来的格式为:
3. 玩家下棋
void PlayerMove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("玩家下棋: \n");
while (1)
{
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] = '*';
break;
}
else
{
printf("坐标被占用,不能落子,请重新输入\n");
}
}
else
{
printf("坐标非法,请重新输入\n");
}
}
}
4. 电脑下棋
void ComputerMove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("电脑下棋: \n");
while (1)
{
x = rand() % row;//余数是0 ~ (row-1),正好符合我们的要求
y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
5. 判断棋盘是否满了
int IsFull(char board[ROW][COL], int row, int col)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
if (board[i][j] == ' ')
return 0;
}
}
return 1;
}
6. 判断游戏输赢
直接用棋子代表输赢, * 表示玩家赢, # 表示电脑赢
如果棋盘满了则为平局,否则就继续
char IsWin(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[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 (IsFull(board, row, col) == 1)
return 'Q';
//以上情况均不是,继续游戏
return 'C';
}
7. 总结
void game()
{
char board[ROW][COL];
InitBoard(board, ROW, COL);
DisplayBoard(board, ROW, COL);
char ret = 0;
while (1)
{
//玩家下棋
PlayerMove(board, ROW, COL);
DisplayBoard(board, ROW, COL);
//判断输赢
ret = IsWin(board, ROW, COL);
if (ret != 'C')
break;
//电脑下棋
ComputerMove(board, ROW, COL);
DisplayBoard(board, ROW, COL);
//判断输赢
ret = IsWin(board, ROW, COL);
if (ret != 'C')
break;
}
if (ret == '*')
printf("玩家赢啦\n");
else if (ret == '#')
printf("电脑赢了\n");
else
printf("平局\n");
}
游戏实操
出现菜单栏,选择1开始游戏:
先打印空棋盘,然后输入我们的坐标,电脑自动下棋:
省略中间过程...
完整代码
game.h
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#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], int row, int col);
//电脑下棋
void ComputerMove(char board[ROW][COL], int row, int col);
//判断输赢
//玩家赢 - '*'
//电脑赢 - '#'
//平局 - 'Q'
//继续 - 'C'
char IsWin(char board[ROW][COL], int row, int col);
test.c
#include "game.h"
void menu()
{
printf("*****************************\n");
printf("******** 1. play *********\n");
printf("******** 0. exit *********\n");
printf("*****************************\n");
}
void game()
{
char board[ROW][COL];
InitBoard(board, ROW, COL);
DisplayBoard(board, ROW, COL);
char ret = 0;
while (1)
{
//玩家下棋
PlayerMove(board, ROW, COL);
DisplayBoard(board, ROW, COL);
//判断输赢
ret = IsWin(board, ROW, COL);
if (ret != 'C')
break;
//电脑下棋
ComputerMove(board, ROW, COL);
DisplayBoard(board, ROW, COL);
//判断输赢
ret = IsWin(board, ROW, COL);
if (ret != 'C')
break;
}
if (ret == '*')
printf("玩家赢啦\n");
else if (ret == '#')
printf("电脑赢了\n");
else
printf("平局\n");
}
int main()
{
int input = 0;
srand(time(NULL));
do
{
menu();
printf("请选择: ");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误,请重新选择\n");
break;
}
} while (input);
return 0;
}
game.c
#include "game.h"
//初始化棋盘为空格
void InitBoard(char board[ROW][COL], int row, int col)
{
for (int i = 0; i < row; i++)
{
for (int 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. 打印数据
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)
{
for (j = 0; j < col; j++)
{
printf("---");
if (j < col - 1)
printf("|");
}
printf("\n");
}
}
}
void PlayerMove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("玩家下棋: \n");
while (1)
{
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] = '*';
break;
}
else
{
printf("坐标被占用,不能落子,请重新输入\n");
}
}
else
{
printf("坐标非法,请重新输入\n");
}
}
}
void ComputerMove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("电脑下棋: \n");
while (1)
{
x = rand() % row;//余数是0 ~ (row-1),正好符合我们的要求
y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
int IsFull(char board[ROW][COL], int row, int col)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
if (board[i][j] == ' ')
return 0;
}
}
return 1;
}
char IsWin(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[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 (IsFull(board, row, col) == 1)
return 'Q';
//以上情况均不是,继续游戏
return 'C';
}
好啦!以上就是全部内容啦!感谢你看到这里,有不懂的可以评论区留言哦。