代码如下:
//打印开始界面
void menu()
{
printf("-----------------------------------\n");
printf("------ 1.noughts-and-crosses ------\n");
printf("-----------------------------------\n");
printf("------ 2.Five in a Row ------\n");
printf("-----------------------------------\n");
printf("------ 0.exit ------\n");
printf("-----------------------------------\n");
printf("\n");
}
这里是三子棋游戏函数代码:
void game_NAC()
{
int computer, player;//记录获胜次数
char ret;//
computer = player = 0;
char ChessBoard[3][3];
//初始化棋盘
InitBoard(ChessBoard, 3, 3);
//打印棋盘
PrintBorad(ChessBoard, 3, 3);
//游戏开始:
while (1)
{
//玩家移动:
PlayerMove(ChessBoard, 3, 3);
//打印棋盘
PrintBorad(ChessBoard, 3, 3);
//判断输赢:
ret = fun(ChessBoard);
//如果不是继续,那么打印结算
if (ret != 'C')
{
printf_end(&computer, &player, ret);
printf("Whether to continue:(Y/N)");
char flag;
scanf("%c", &flag);
while (getchar() != '\n');
//玩家选择是否继续
if (flag == 'N' || flag == 'n')
{
break;
}
//再来一次:初始化棋盘
else
{
InitBoard(ChessBoard, 3, 3);
}
}
//电脑移动:
ComputerMove(ChessBoard, 3, 3);
//打印棋盘:
PrintBorad(ChessBoard, 3, 3);
//判断输赢:
ret = fun(ChessBoard);
//如果不是继续,那么打印结算,并且判断是否继续游戏:
if (ret != 'C')
{
printf_end(&computer, &player, ret);
printf("Whether to continue:(Y/N)");
char flag;
scanf("%c", &flag);
while (getchar() != '\n');
//玩家选择是否继续
if (flag == 'N' || flag == 'n')
{
break;
}
//再来一次:初始化棋盘
else
{
InitBoard(ChessBoard, 3, 3);
}
}
}
return;
}
注:代码中包含了很多其他的函数,将在后续提到
//打印开始界面
void menu()
{
printf("-----------------------------------\n");
printf("------ 1.noughts-and-crosses ------\n");
printf("-----------------------------------\n");
printf("------ 2.Five in a Row ------\n");
printf("-----------------------------------\n");
printf("------ 0.exit ------\n");
printf("-----------------------------------\n");
printf("\n");
}
//游戏:
void game_NAC()
{
int computer, player;//记录获胜次数
char ret;//
computer = player = 0;
char ChessBoard[3][3];
//初始化棋盘
InitBoard(ChessBoard, 3, 3);
//打印棋盘
PrintBorad(ChessBoard, 3, 3);
//游戏开始:
while (1)
{
//玩家移动:
PlayerMove(ChessBoard, 3, 3);
//打印棋盘
PrintBorad(ChessBoard, 3, 3);
//判断输赢:
ret = fun(ChessBoard);
//如果不是继续,那么打印结算
if (ret != 'C')
{
printf_end(&computer, &player, ret);
printf("Whether to continue:(Y/N)");
char flag;
scanf("%c", &flag);
while (getchar() != '\n');
//玩家选择是否继续
if (flag == 'N' || flag == 'n')
{
break;
}
//再来一次:初始化棋盘
else
{
InitBoard(ChessBoard, 3, 3);
}
}
//电脑移动:
ComputerMove(ChessBoard, 3, 3);
//打印棋盘:
PrintBorad(ChessBoard, 3, 3);
//判断输赢:
ret = fun(ChessBoard);
//如果不是继续,那么打印结算,并且判断是否继续游戏:
if (ret != 'C')
{
printf_end(&computer, &player, ret);
printf("Whether to continue:(Y/N)");
char flag;
scanf("%c", &flag);
while (getchar() != '\n');
//玩家选择是否继续
if (flag == 'N' || flag == 'n')
{
break;
}
//再来一次:初始化棋盘
else
{
InitBoard(ChessBoard, 3, 3);
}
}
}
return;
}
//三子棋判断输赢(只能用于三子棋)
char fun(char Board[][3])
{
//三行判断:
for (int i = 0; i < 3; i++)
{
if (Board[i][0] == Board[i][1] && Board[i][0] == Board[i][2]&& Board[i][0]!=' ')
{
return Board[i][0];
}
}
//三列判断:
for (int i = 0; i < 3; i++)
{
if (Board[0][i] == Board[1][i] && Board[0][i] == Board[2][i] && Board[0][i] != ' ')
{
return Board[0][i];
}
}
//两条对角判断:
if (Board[0][0] == Board[1][1] && Board[0][0] == Board[2][2] && Board[1][1] != ' ')
{
return Board[1][1];
}
if (Board[2][0] == Board[1][1] && Board[0][2] == Board[1][1] && Board[1][1] != ' ')
{
return Board[1][1];
}
//平局判断:
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (Board[i][j] == ' ')
{
return 'C';
}
}
}
//最后则为平局:
return 'D';
}
//数组初始化:这里的 SIZE_MAX 必须大于col
void InitBoard(char Board[][SIZE_MAX], int row, int col)
{
//利用二维数组的连续性,来初始化,一共要初始化row*col个元素
for (int j = 0; j < row * col; j++)
{
Board[0][j] = ' ';
}
}
//棋盘打印:利用了与二维数组自动推导的原理进行打印棋盘
void PrintBorad(char Board[][SIZE_MAX], int row, int col)
{
//一共要打印row*col个元素
for (int j = 0; j < row * col; j++)
{
//逐个打印
printf(" %c ", Board[0][j]);
//判断1:结尾不打印 |
if ((j + 1) % col != 0)
{
printf("|");
}
//判断2:一行打印完了之后,打印分隔符
if ((j + 1) % col == 0&& j < row * col-1)
{
//先换行
printf("\n");
//打印分割符号:3*col是对应空格,col-1是分割符所占
for (int i = 0; i < 3 * col+col-1; i++)
{
printf("-");
}
//打印换行,开始打印下一行代码
printf("\n");
}
}
printf("\n");
}
//玩家移动:
void PlayerMove(char Board[][SIZE_MAX], int row, int col)
{
int r,c;
printf("please input row and col:");
//提示用户输入格式
printf("eg:1,1:");
scanf("%d,%d", &r, &c);
//清除缓存区字符
while (getchar() != '\n');
while (1)
{
//判断是否输入在棋盘内
if (r >= 1 && r <= row && c >= 1 && c <= col)
{
//判断该位置是否被占(这里也是根据坐标,将二维数组转为一维)
if (Board[0][(r - 1) * col + c - 1] == ' ')
{
Board[0][(r - 1) * col + c - 1] = '*';
//用户完成下棋,退出循环
break;
}
else
{
//如果被占,再次读取坐标
printf("The location is occupied, please re-enter:");
scanf("%d,%d", &r, &c);
while (getchar() != '\n');
}
}
//输入错误,重新输入
else
{
printf("Input error, please re-enter:");
scanf("%d,%d", &r, &c);
while (getchar() != '\n');
}
}
}
//电脑移动:
void ComputerMove(char Board[][SIZE_MAX], int row, int col)
{
printf("computer move:\n");
int r, c;
r = Gen_rand_num(1, row);
c = Gen_rand_num(1, col);
//注:这里生成的随机数是符合范围要求的,所以不用再判断是否在棋盘内
while (1)
{
//判断该位置是否被占(这里也是根据坐标,将二维数组转为一维)
if (Board[0][(r - 1) * col + c - 1] == ' ')
{
Board[0][(r - 1) * col + c - 1] = '#';
//电脑完成下棋,退出循环
break;
}
else
{
//如果被占,再次生成随机坐标
r = Gen_rand_num(1, row);
c = Gen_rand_num(1, col);
}
}
}
//随机数生成函数,范围是min-max
int Gen_rand_num(int min,int max)
{
srand(time(0));
int num;
//生成一个min-max之间的随机数:核心要领是明白,取模留下的是谁
num = rand() % (max-min+1)+min;
return num;
}
//计算并且打印游戏结算界面
void printf_end(int* computer, int* player,char ret)
{
if (ret == '*')
{
printf("player win\n");
(*player)++;
}
if (ret == '#')
{
printf("computer win\n");
(*computer)++;
}
if (ret == 'D')
{
printf("Draw the game\n");
}
//打印计分牌:
printf("player:%d , computer:%d\n",*player,*computer);
}
比如:game.h中存放如下声明:
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#define SIZE_MAX 400 //这里表示数组中的元素个数最大值,棋盘最大是20*20
void menu();
void game_NAC();
void game_FIR();
void InitBoard(char Board[][SIZE_MAX], int row, int col);
void PrintBorad(char Board[][SIZE_MAX], int row, int col);
void PlayerMove(char Board[][SIZE_MAX], int row, int col);
void ComputerMove(char Board[][SIZE_MAX], int row, int col);
int Gen_rand_num(int min, int max);
char IsWin(char Board[][SIZE_MAX], int row, int col);
void printf_end(int* computer, int* player,char ret);
char fun(char Board[][3]);
game.c为函数库,函数定义的代码都在上面发过了,这是截图:
test.c中运行代码:注意需要引用game.h,因为所有的函数声明都在其中
#include"game.h"
int main()
{
//打印界面:
menu();
//提示语:
//printf("please input the number:");
do
{
printf("please input the game you want to play:");
int input = -1;
scanf("%d", &input);
//用户输入进行选择:
switch (input)
{
case 0:
printf("exit game\n");
break;
case 1:
game_NAC();
break;
case 2:
game_FIR();
break;
//错误数据重新输入
default:
menu();
printf("error! please input 0/1/2:");
break;
}
//输入0则退出游戏
if (input == 0)
{
break;
}
} while (1);
return 0;
}
代码运行截图如下: