目录
一、介绍
二、游戏实现
设计菜单
设计游戏流程
初始化游戏
显示棋盘
下棋
判断结果
三、整体代码
common.h
Game.h
Game.c
三子棋,民间传统游戏,又叫九宫棋、圈圈叉叉、一条龙等。将正方形对角线连起来,相对两边依次摆上三个双方棋子,只要将自己的三个棋子走成一条线,对方就算输了。如果两个人都掌握了技巧,那么一般来说就是平棋。一般来说,下在中间最有利,下在角上次之,下在边上再次之。最大的好处就是随便找个地方就可以玩这个简单而有趣的游戏了。
首先我们需要一个主函数main来设计游戏首页面菜单并来选择进入游戏或者退出游戏。在运行程序时可能玩家会输入错误,所以用while(1)来死循环游戏菜单页面。
#include"Game.h"
int main()
{
//设置界面
system("title 简易三子棋");
system("mode con cols=32 lines=32");
system("color F0");
int select = 1;
while (select)
{
printf("********************************\n");
printf("* 简 易 三 子 棋 *\n");
printf("********************************\n");
printf("* [1]开始 [0]退出 *\n");
printf("********************************\n");
printf("请选择:>");
scanf("%d", &select);
if (select == 0)
break;
if (select != 1)
{
printf("输入有误!\n");
continue;
}
//开始游戏
BeginGame();
}
printf("游戏结束,欢迎下次来玩!\n");
return 0;
}
由运行截图可见,输入错误数字则会死循环,输入0则退出程序。
进入游戏后编写一个BeginGame函数来设计游戏流程,其中可以随机让电脑先下棋或者让玩家先下棋,和棋后判断你是否要重来。
int BeginGame()
{
//1 初始化游戏
InitializeGame();
char winner;
//2 随机下棋者
int Return = RandomChessPlayer();
if (Return == 0)
{
//3 进入游戏
while (1)
{
//4 显示棋盘
ShowChessBoard();
//5 玩家下棋
PlayerMove();
//6 判断结果
winner = CheckResult();
if (winner != 'c')
break;
//7 电脑下棋
ComputerMove();
//8 判断结果
winner = CheckResult();
if (winner != 'c')
break;
}
}
else
{
while (1)
{
//4 电脑下棋
ComputerMove();
//5 判断结果
winner = CheckResult();
if (winner != 'c')
break;
//6 显示棋盘
ShowChessBoard();
//7 玩家下棋
PlayerMove();
//8 判断结果
winner = CheckResult();
if (winner != 'c')
break;
}
}
//9 宣布结果
if (winner == 'x')
printf("恭喜你,玩家胜利.\n");
else if (winner == 'o')
printf("很遗憾,电脑胜利.\n");
else
{
printf("和棋,是否继续[y/n]:");
while (1)
{
char a[1];
scanf("%s", a);
if (a[0] == 'y')
BeginGame();
else if (a[0] == 'n')
break;
else
printf("输入有误,清重新输入:>");
}
}
return 0;
}
以下是对游戏流程的细化实现
我们可以用二维数组来确定一个3×3的版块,把3×3数组都初始化为空字符。
void InitializeGame()
{
for (int i = 0; i|
将3×3数组打印出来,一个简易的棋盘就做好了
void ShowChessBoard()
{
for (int i = 0; i|
设立玩家下棋为字符‘x’,电脑下棋为字符‘o’;玩家来通过坐标实现下棋,电脑先确定是否玩家下一步可能获胜,如果可能获胜,则下到玩家可能获胜的位置,否则随机下棋。
void PlayerMove()
{
printf("请玩家落子:\n");
int row, col;
while (1)
{
printf("请输入棋子的坐标(row:col):>");
scanf("%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)
{
//下玩家可能赢的位置
if (board[0][0] == 'x' && board[0][1] == 'x' && board[0][2] == ' ')
{
board[0][2] = 'o';
break;
}
else if (board[0][2] == 'x' && board[0][1] == 'x' && board[0][0] == ' ')
{
board[0][0] = 'o';
break;
}
else if (board[0][2] == 'x' && board[0][0] == 'x' && board[0][1] == ' ')
{
board[0][1] = 'o';
break;
}
else if (board[1][0] == 'x' && board[1][1] == 'x' && board[1][2] == ' ')
{
board[1][2] = 'o';
break;
}
else if (board[1][2] == 'x' && board[1][1] == 'x' && board[1][0] == ' ')
{
board[1][0] = 'o';
break;
}
else if (board[1][2] == 'x' && board[1][0] == 'x' && board[1][1] == ' ')
{
board[1][1] = 'o';
break;
}
else if (board[2][0] == 'x' && board[2][1] == 'x' && board[2][2] == ' ')
{
board[2][2] = 'o';
break;
}
else if (board[2][2] == 'x' && board[2][1] == 'x' && board[2][0] == ' ')
{
board[2][0] = 'o';
break;
}
else if (board[2][2] == 'x' && board[2][0] == 'x' && board[2][1] == ' ')
{
board[2][1] = 'o';
break;
}
else if (board[0][0] == 'x' && board[1][0] == 'x' && board[2][0] == ' ')
{
board[2][0] = 'o';
break;
}
else if (board[0][0] == 'x' && board[2][0] == 'x' && board[1][0] == ' ')
{
board[1][0] = 'o';
break;
}
else if (board[1][0] == 'x' && board[2][0] == 'x' && board[0][0] == ' ')
{
board[0][0] = 'o';
break;
}
else if (board[0][1] == 'x' && board[1][1] == 'x' && board[2][1] == ' ')
{
board[2][1] = 'o';
break;
}
else if (board[0][1] == 'x' && board[2][1] == 'x' && board[1][1] == ' ')
{
board[1][1] = 'o';
break;
}
else if (board[1][1] == 'x' && board[2][1] == 'x' && board[0][1] == ' ')
{
board[0][1] = 'o';
break;
}
else if (board[0][2] == 'x' && board[1][2] == 'x' && board[2][2] == ' ')
{
board[2][2] = 'o';
break;
}
else if (board[0][2] == 'x' && board[2][2] == 'x' && board[1][2] == ' ')
{
board[1][2] = 'o';
break;
}
else if (board[1][2] == 'x' && board[2][2] == 'x' && board[0][2] == ' ')
{
board[0][2] = 'o';
break;
}
else if (board[0][0] == 'x' && board[1][1] == 'x' && board[2][2] == ' ')
{
board[2][2] = 'o';
break;
}
else if (board[0][0] == 'x' && board[2][2] == 'x' && board[1][1] == ' ')
{
board[1][1] = 'o';
break;
}
else if (board[1][1] == 'x' && board[2][2] == 'x' && board[0][0] == ' ')
{
board[0][0] = 'o';
break;
}
else if (board[0][2] == 'x' && board[1][1] == 'x' && board[2][0] == ' ')
{
board[2][0] = 'o';
break;
}
else if (board[0][2] == 'x' && board[2][0] == 'x' && board[1][1] == ' ')
{
board[1][1] = 'o';
break;
}
else if (board[2][0] == 'x' && board[1][1] == 'x' && board[0][2] == ' ')
{
board[0][2] = 'o';
break;
}
else
{
//随机产生位置
row = rand() % ROW;
col = rand() % COL;
if (board[row][col] == ' ')
{
board[row][col] = 'o';
break;
}
}
}
}
结果分为四种(玩家赢x,电脑赢o,和棋h,未下满继续c),当玩家或者电脑的棋子连成一行或者一列又或者连成对角线时,有一方获胜;当棋盘下满时,应判断为平局,所以判断输赢的函数就需要返回值。
#include
#include
#include
#include"common.h"
//初始化游戏
void InitializeGame();
//显示棋盘
void ShowChessBoard();
//玩家下棋
void PlayerMove();
//电脑下棋
void ComputerMove();
//判断结果
char CheckResult();
//开始游戏
void StartGame();
#include"Game.h"
#define ROW 3
#define COL 3
static char board[ROW][COL];
void InitializeGame()
{
for (int i = 0; i");
scanf("%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)
{
//下玩家可能赢的位置
if (board[0][0] == 'x' && board[0][1] == 'x' && board[0][2] == ' ')
{
board[0][2] = 'o';
break;
}
else if (board[0][2] == 'x' && board[0][1] == 'x' && board[0][0] == ' ')
{
board[0][0] = 'o';
break;
}
else if (board[0][2] == 'x' && board[0][0] == 'x' && board[0][1] == ' ')
{
board[0][1] = 'o';
break;
}
else if (board[1][0] == 'x' && board[1][1] == 'x' && board[1][2] == ' ')
{
board[1][2] = 'o';
break;
}
else if (board[1][2] == 'x' && board[1][1] == 'x' && board[1][0] == ' ')
{
board[1][0] = 'o';
break;
}
else if (board[1][2] == 'x' && board[1][0] == 'x' && board[1][1] == ' ')
{
board[1][1] = 'o';
break;
}
else if (board[2][0] == 'x' && board[2][1] == 'x' && board[2][2] == ' ')
{
board[2][2] = 'o';
break;
}
else if (board[2][2] == 'x' && board[2][1] == 'x' && board[2][0] == ' ')
{
board[2][0] = 'o';
break;
}
else if (board[2][2] == 'x' && board[2][0] == 'x' && board[2][1] == ' ')
{
board[2][1] = 'o';
break;
}
else if (board[0][0] == 'x' && board[1][0] == 'x' && board[2][0] == ' ')
{
board[2][0] = 'o';
break;
}
else if (board[0][0] == 'x' && board[2][0] == 'x' && board[1][0] == ' ')
{
board[1][0] = 'o';
break;
}
else if (board[1][0] == 'x' && board[2][0] == 'x' && board[0][0] == ' ')
{
board[0][0] = 'o';
break;
}
else if (board[0][1] == 'x' && board[1][1] == 'x' && board[2][1] == ' ')
{
board[2][1] = 'o';
break;
}
else if (board[0][1] == 'x' && board[2][1] == 'x' && board[1][1] == ' ')
{
board[1][1] = 'o';
break;
}
else if (board[1][1] == 'x' && board[2][1] == 'x' && board[0][1] == ' ')
{
board[0][1] = 'o';
break;
}
else if (board[0][2] == 'x' && board[1][2] == 'x' && board[2][2] == ' ')
{
board[2][2] = 'o';
break;
}
else if (board[0][2] == 'x' && board[2][2] == 'x' && board[1][2] == ' ')
{
board[1][2] = 'o';
break;
}
else if (board[1][2] == 'x' && board[2][2] == 'x' && board[0][2] == ' ')
{
board[0][2] = 'o';
break;
}
else if (board[0][0] == 'x' && board[1][1] == 'x' && board[2][2] == ' ')
{
board[2][2] = 'o';
break;
}
else if (board[0][0] == 'x' && board[2][2] == 'x' && board[1][1] == ' ')
{
board[1][1] = 'o';
break;
}
else if (board[1][1] == 'x' && board[2][2] == 'x' && board[0][0] == ' ')
{
board[0][0] = 'o';
break;
}
else if (board[0][2] == 'x' && board[1][1] == 'x' && board[2][0] == ' ')
{
board[2][0] = 'o';
break;
}
else if (board[0][2] == 'x' && board[2][0] == 'x' && board[1][1] == ' ')
{
board[1][1] = 'o';
break;
}
else if (board[2][0] == 'x' && board[1][1] == 'x' && board[0][2] == ' ')
{
board[0][2] = 'o';
break;
}
else
{
//随机产生位置
row = rand() % ROW;
col = rand() % COL;
if (board[row][col] == ' ')
{
board[row][col] = 'o';
break;
}
}
}
}
bool _IsFullChessBoard()
{
for (int i = 0; i");
}
}
return 0;
}
Main.c
#include"Game.h"
int main()
{
//设置界面
system("title 简易三子棋");
system("mode con cols=32 lines=32");
system("color F0");
int select = 1;
while (select)
{
printf("********************************\n");
printf("* 简 易 三 子 棋 *\n");
printf("********************************\n");
printf("* [1]开始 [0]退出 *\n");
printf("********************************\n");
printf("请选择:>");
scanf("%d", &select);
if (select == 0)
break;
if (select != 1)
{
printf("输入有误!\n");
continue;
}
//开始游戏
BeginGame();
}
printf("游戏结束,欢迎下次来玩!\n");
return 0;
}