每天进步一点点,坚持带来大改变!!!
目录
前言:
实现思路:
1.代码搭建:
2.框架搭建:
3.游戏功能实现:
1.创建棋盘并用空格初始化:
2.打印棋盘:
3.玩家下棋:
4.电脑下棋:
5.判断输赢:
7.整体代码:
8.玩游戏链接:
之前我们已经学习了函数,循环和数组,接下来我们使用这些C语言知识来搭建一个五子棋游戏。
test.c文件实现整个游戏框架和思路,game.c文件实现整个游戏的具体游戏功能,game.h文件实现整个游戏需要的声明。
让玩家来选择开始游戏,或者退出游戏,为了让玩家玩完一把继续下一把,采用do……while循环
void menu()
{
printf("*********************\n");
printf("**1.play 0.exit **\n");
printf("*********************\n");
}
int main()
{
int input = 0;
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()函数内实现
#define Row 5
#define Col 5//创建棋盘
char board[Row][Col] = { 0 };//初始化棋盘:用空换
void Initboard(char board[Row][Col], int row, int col) { int i = 0; for (i = 0; i < row; i++) { int j = 0; for (j = 0; j < col; j++) { board[i][j] = ' '; } } }
思路:1.先打印" %c "然后打印"|"
2.先打印"---",然后打印"|"
思路:首先从键盘录入两个坐标,判断坐标的合法性,如果是空格则赋值"*"(玩家下的棋子);
思路:调用rand—>srand—>time—>函数产生随机值:在空格的地方赋值"#";
注:rand和srand 头文件#include
time函数头文件#include
使用rand函数需要在主函数调用一下sranf函数;
思路:如果返回"*"则代表玩家赢,返回"#"则代表电脑赢,返回"Q"则代表平局,返回"C"则代表继续
//满了,返回1
//不满,返回0
char Is_Full(char board[Row][Col], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (board[i][j] == ' ')
{
return 0;
}
}
}
return 1;
}
char Is_Win(char board[Row][Col], int row, int col)
{
//横五行
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] &&
board[i][2] == board[i][3] && board[i][3] == board[i][4] &&
board[i][4] != ' ')
{
return board[i][2];
}
}
//列五行
for (j = 0; j < col; j++)
{
if (board[0][j] == board[1][j] && board[1][j] == board[2][j] &&
board[2][j] == board[3][j] && board[3][j] == board[4][j] &&
board[4][j] != ' ')
return board[1][j];
}
//对角线
if (board[0][0] == board[1][1] && board[2][2] == board[2][2] &&
board[2][2] == board[3][3] && board[3][3] == board[4][4] &&
board[4][4] != ' ')
{
return board[1][1];
}
if (board[0][4] == board[1][3] && board[1][3] == board[2][2] &&
board[2][2] == board[3][1] && board[3][1] == board[4][0] &&
board[4][0] != ' ')
return board[0][4];
//没有人赢,平局
if (Is_Full(board, Row, Col))
{
return 'Q';
}
//游戏继续
return 'C';
}
test.c
#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
void menu()
{
printf("*********************\n");
printf("**1.play 0.exit **\n");
printf("*********************\n");
}
void game()
{
char ret = 0;
//创建棋盘
char board[Row][Col] = { 0 };
//初始化棋盘
Initboard(board, Row, Col);
//打印棋盘
Displayboard(board, Row, Col);
while (1)
{
//玩家下棋
PlayerMove(board, Row, Col);
ret = Is_Win(board, Row, Col);
if (ret != 'C')
{
break;
}
//电脑下棋
ComputerMove(board, Row, Col);
Is_Win(board, Row, Col);
if (ret != 'C')
{
break;
}
Displayboard(board, Row, Col);
}
if (ret == '*')
{
printf("玩家赢\n");
}
else if (ret == '#')
{
printf("电脑赢\n");
}
else if (ret == 'Q')
{
printf("平局\n");
}
Displayboard(board, Row, Col);
}
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
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.h
#include
#include
#include
#define Row 5
#define Col 5
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);
char Is_Win(char board[Row][Col], int row, int col);
game.c
#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
void Initboard(char board[Row][Col], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++)
{
board[i][j] = ' ';
}
}
}
void Displayboard(char board[Row][Col], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
printf(" %c ", board[i][j]);
if (j < col - 1)
{
printf("|");
}
}
printf("\n");
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("玩家下棋,请输入坐标:");
while (1)
{
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");
break;
}
}
else
{
printf("坐标非法,请重新输入:\n");
break;
}
}
}
void ComputerMove(char board[Row][Col], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
x = rand() % 5;
//随机产生:0 1 2 3 4;
y = rand() % 5;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
//满了,返回1
//不满,返回0
char Is_Full(char board[Row][Col], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (board[i][j] == ' ')
{
return 0;
}
}
}
return 1;
}
char Is_Win(char board[Row][Col], int row, int col)
{
//横五行
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] &&
board[i][2] == board[i][3] && board[i][3] == board[i][4] &&
board[i][4] != ' ')
{
return board[i][2];
}
}
//列五行
for (j = 0; j < col; j++)
{
if (board[0][j] == board[1][j] && board[1][j] == board[2][j] &&
board[2][j] == board[3][j] && board[3][j] == board[4][j] &&
board[4][j] != ' ')
return board[1][j];
}
//对角线
if (board[0][0] == board[1][1] && board[2][2] == board[2][2] &&
board[2][2] == board[3][3] && board[3][3] == board[4][4] &&
board[4][4] != ' ')
{
return board[1][1];
}
if (board[0][4] == board[1][3] && board[1][3] == board[2][2] &&
board[2][2] == board[3][1] && board[3][1] == board[4][0] &&
board[4][0] != ' ')
return board[0][4];
//没有人赢,平局
if (Is_Full(board, Row, Col))
{
return 'Q';
}
//游戏继续
return 'C';
}
1651207073891-CSDN直播
每一段努力的过程都是值得被尊重的,没有天生完美的人生,只有努力过越来越好的生活。