初学c语言的小游戏三子棋
目录
一、三子棋游戏规则
二、设计思路
三、代码部分
四、运行
五、总结
三子棋是3*3的九宫格,玩家和电脑每人各下一次,判断谁先将三颗子给连成一条线,则为成功,在编程时我们会先将这九个空格填充为空格,由玩家先行下棋,电脑后下棋,然后进行判断胜负。
1、设计一个开始游戏的菜单
2、创建一个初始化棋盘
3、显示棋盘
4、玩家下棋
5、电脑下棋
6、判断结果
7、开始游戏
1、Game.h(t头文件)
#include"common.h"
//初始化游戏
void InitGame();
//显示棋盘
void ShowChessBoard();
//玩家下棋
void PlayerMove();
//电脑下棋
void ComputerMove();
//判断结果
char CheckResult();
//开始游戏
void StartGame();
2、common.h
#include
#include
#include
3、game.c(源文件,是各个函数的代码的实现)
#include"Game.h"
#define ROW 3
#define COL 3
static char board[ROW][COL];
void InitGame()
{
for (int i = 0; i < ROW; ++i)
{
for (int j = 0; j < COL; ++j)
{
board[i][j] = ' ';
}
}
}
void ShowChessBoard()
{
for (int i = 0; i < ROW; ++i)
{
printf("|%c |%c |%c |\n", board[i][0],
board[i][1],
board[i][2]);
if(i!=ROW-1)
printf(" --- --- ---\n");
}
}
void PlayerMove()
{
printf("请玩家落子:\n");
int row, col;
while (1)
{
printf("请输入棋子的坐标(row:col):");
scanf_s("%d %d", &row, &col);
if (board[row - 1][col - 1] != ' ')
{
printf("输入的位置被占用,请重新输入\n");
continue;
}
board[row-1][col-1] = 'x';
break;
}
}
void ComputerMove()
{
int row, col;
srand(time(0));
while (1)
{
//随机产生位置
row = rand() % ROW;
col = rand() % COL;
if (board[row][col] == ' ')
{
board[row][col] = 'o';
break;
}
}
}
bool _IsFullChessBoard()
{
for (int i = 0; i < ROW; ++i)
{
for (int j = 0; j < COL; ++j)
{
if (board[i][j] == ' ')
return false;
}
}
return true;
}
//x:玩家赢 o:电脑赢 h:和其 c:继续
char CheckResult()
{
//检查行
for (int i = 0; i < ROW; ++i)
{
if (board[i][0]!=' '&& board[i][0] == board[i][1] && board[i][0] == board[i][2])
return board[i][0];
}
//检查列
for (int j = 0; j < COL; ++j)
{
if (board[0][j]!=' '&&board[0][j] == board[1][j] && board[0][j] == board[2][j])
return board[0][j];
}
//判断斜线
if (board[1][1]!=' '&&((board[0][0] == board[1][1] && board[0][0] == board[2][2])
|| (board[0][2] == board[1][1] && board[0][2] == board[2][0])))
return board[1][1];
//是否和棋
if (_IsFullChessBoard())
return 'h';
//继续下棋
return 'c';
}
void StartGame()
{
//1、初始化游戏
InitGame();
ShowChessBoard();
char winner;
//2、进入游戏
while (1)
{
//3显示棋盘
ShowChessBoard();
//4、玩家下棋
PlayerMove();
//5、判断结果
winner = CheckResult();
if (winner != 'c')
break;
//6、电脑下棋
ComputerMove();
//7、判断结果
winner = CheckResult();
if (winner != 'c')
break;
}
//8、宣布结果
if (winner == 'x')
printf("恭喜你!玩家胜利\n");
else if (winner == 'o')
printf("很遗憾!电脑胜利\n");
else
{
printf("平局,是否继续[y/n]\n");
}
}
4、gameMain.c(主函数,负责调用各个函数来最终实现程序的功能)
#include"Game.h"
#define ROW 3
#define COL 3
static char board[ROW][COL];
void InitGame()
{
for (int i = 0; i < ROW; ++i)
{
for (int j = 0; j < COL; ++j)
{
board[i][j] = ' ';
}
}
}
void ShowChessBoard()
{
for (int i = 0; i < ROW; ++i)
{
printf("|%c |%c |%c |\n", board[i][0],
board[i][1],
board[i][2]);
if(i!=ROW-1)
printf(" --- --- ---\n");
}
}
void PlayerMove()
{
printf("请玩家落子:\n");
int row, col;
while (1)
{
printf("请输入棋子的坐标(row:col):");
scanf_s("%d %d", &row, &col);
if (board[row - 1][col - 1] != ' ')
{
printf("输入的位置被占用,请重新输入\n");
continue;
}
board[row-1][col-1] = 'x';
break;
}
}
void ComputerMove()
{
int row, col;
srand(time(0));
while (1)
{
//随机产生位置
row = rand() % ROW;
col = rand() % COL;
if (board[row][col] == ' ')
{
board[row][col] = 'o';
break;
}
}
}
bool _IsFullChessBoard()
{
for (int i = 0; i < ROW; ++i)
{
for (int j = 0; j < COL; ++j)
{
if (board[i][j] == ' ')
return false;
}
}
return true;
}
//x:玩家赢 o:电脑赢 h:和其 c:继续
char CheckResult()
{
//检查行
for (int i = 0; i < ROW; ++i)
{
if (board[i][0]!=' '&& board[i][0] == board[i][1] && board[i][0] == board[i][2])
return board[i][0];
}
//检查列
for (int j = 0; j < COL; ++j)
{
if (board[0][j]!=' '&&board[0][j] == board[1][j] && board[0][j] == board[2][j])
return board[0][j];
}
//判断斜线
if (board[1][1]!=' '&&((board[0][0] == board[1][1] && board[0][0] == board[2][2])
|| (board[0][2] == board[1][1] && board[0][2] == board[2][0])))
return board[1][1];
//是否和棋
if (_IsFullChessBoard())
return 'h';
//继续下棋
return 'c';
}
void StartGame()
{
//1、初始化游戏
InitGame();
ShowChessBoard();
char winner;
//2、进入游戏
while (1)
{
//3显示棋盘
ShowChessBoard();
//4、玩家下棋
PlayerMove();
//5、判断结果
winner = CheckResult();
if (winner != 'c')
break;
//6、电脑下棋
ComputerMove();
//7、判断结果
winner = CheckResult();
if (winner != 'c')
break;
}
//8、宣布结果
if (winner == 'x')
printf("恭喜你!玩家胜利\n");
else if (winner == 'o')
printf("很遗憾!电脑胜利\n");
else
{
printf("平局,是否继续[y/n]\n");
}
}
1、选择开始或退出
2、输入棋子的位置
3、输入棋子坐标
4、判断输赢·
5、选择继续游戏或者退出
这个代码还有许多需要改进的地方,大家可以去尝试一下!