勤时当勉励 岁月不待人
C/C++ 游戏开发
如果你是从现在关注的老粉的话,你可能会有点疑惑“how old are you?”(怎么老是你?)
唉,没办法我也不想的,但是月末了参加新星计划和2023年博客之星的评选只能更新的勤快一点喽!废话不多说,咱们直接开始吧!
今天带来的内容是五子棋项目的具体实现,其实咱们上回已经实现了三子棋,
链接如下:
【C语言】三子棋详解(包教包会的那种)
但是呢,上回咱们的三子棋其实还有非常多需要优化的地方,我们今天就来优化一下并把它改为五子棋。
#include
#include
#include
#define Col 3
#define Row 3
void InitBoard(char board[Row][Col], int row, int col);//初始化棋盘
void PrintBoard(char board[Row][Col], int row, int col);//打印棋盘
void PlayBoard(char board[Row][Col], int row, int col);//玩游戏
void ComputerBoard(char board[Row][Col], int row, int col);//电脑下
int IsFull(char board[Row][Col], int row, int col);//棋盘是否已满
int Is_Win(char board[Row][Col], int row, int col);//判断谁赢
#include"game.h"
void InitBoard(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++)
{
board[i][j] = ' ';
}
}
}
void PrintBoard(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++)
{
printf(" %c ", board[i][j]);
if (j < col - 1)
printf("|");
}
printf("\n");
if (i < row - 1)
{
int j = 0;
for (j = 0; j < col; j++)
{
printf("---");
if (j < col - 1)
printf("|");
}
printf("\n");
}
}
}
void PlayBoard(char board[Row][Col], int row, int col)
{
int x, y = 0;
while (1)
{
printf("玩家下棋>:\n");
printf("请输入要下的坐标,中间用空格隔开\n");
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 ComputerBoard(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;//生成0-col-1的数
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
int IsFull(char board[Row][Col], int row, int col)
{
int i ,j;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (board[i][j] == ' ')
return 0;
}
}
return 1;
}
int Is_Win(char board[Row][Col], int row, int col)
{
int x = 0;
int y = 0;
//竖着
if (board[x][0] == board[x][1] && board[x][1] == board[x][2] && board[x][0] !=' ')
{
return board[x][0];
}
//横着
if (board[0][y] == board[1][y] && board[1][y] == board[2][y] && board[0][y] != ' ')
{
return board[0][y];
}
//对角线
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
return board[1][1];
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
return board[1][1];
if (IsFull(board, row, col) == 1)
{
return 'Q';
}
return 'C';
}
#include"game.h"
void menu()//打印菜单
{
printf("*****************************\n");
printf("*********** 1.play **********\n");
printf("*********** 0.exit **********\n");
printf("*****************************\n");
}
void game()
{
char board[Row][Col] = { 0 };
InitBoard(board, Row, Col);
PrintBoard(board,Row, Col);
char ret = 0;
while (1)
{
PlayBoard(board, Row, Col);
PrintBoard(board, Row, Col);
ret = Is_Win(board, Row, Col);
if (ret != 'C')
break;
ComputerBoard(board, Row, Col);
PrintBoard(board, Row, Col);
ret = Is_Win(board, Row, Col);
if (ret != 'C')
break;
}
if (ret == '*')
printf("玩家赢\n");
if (ret == '#')
printf("电脑赢\n");
if (ret == 'Q')
printf("平局\n");
}
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;
}
#define Col 10
#define Row 10
int IsFull(char board[Row][Col], int row, int col)
{
int i ,j;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (board[i][j] == ' ')
return 0;
}
}
return 1;
}
int Is_Win(char board[Row][Col], int row, int col)
{
int x = 0;
int y = 0;
//竖着
if (board[x][0] == board[x][1] && board[x][1] == board[x][2] && board[x][0] !=' ')
{
return board[x][0];
}
//横着
if (board[0][y] == board[1][y] && board[1][y] == board[2][y] && board[0][y] != ' ')
{
return board[0][y];
}
//对角线
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
return board[1][1];
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
return board[1][1];
if (IsFull(board, row, col) == 1)
{
return 'Q';
}
return 'C';
}
//竖着
if (board[x][0] == board[x][1] && board[x][1] == board[x][2] && board[x][0] !=' ')
{
return board[x][0];
}
//横着
if (board[0][y] == board[1][y] && board[1][y] == board[2][y] && board[0][y] != ' ')
{
return board[0][y];
}
//对角线
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
return board[1][1];
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
return board[1][1];
int Is_Win(char board[Row][Col], int row, int col)
{
int i = 0;
int j = 0;
//竖着
if (j < col - 4)//防止越界
if (board[i][j] == board[i][j + 1] && board[i][j] ==board[i][j + 2]&& board[i][j] == board[i][j + 3] && board[i][j]== board[i][j + 4])
return board[i][j];
//横着
if (i < row - 4)
if(board[i][j] == board[i][j + 1] && board[i][j] ==board[i][j + 2]&& board[i][j] == board[i][j + 3] && board[i][j]== board[i][j + 4])
return board[i][j];
//左对角线连成五子
if (i < row - 4 && j < col - 4)
if (board[i][j] ==board[i + 1][j + 1] && board[i][j] ==board[i + 2][j + 2]&& board[i][j] == board[i + 3][j + 3] && board[i][j] == board[i + 4][j + 4])
return board[i][j];
// 右对角线连成五子
if (i < row - 4 && j > 4)
if (board[i][j] == board[i + 1][j - 1] &&board[i][j] ==board[i + 2][j - 2]&& board[i][j] == board[i + 3][j - 3] && board[i][j] == board[i + 4][j - 4])
return board[i][j];
if (IsFull(board, row, col) == 1)
{
return 'Q';
}
return 'C';
}
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
#include
#define Col 10
#define Row 10
void InitBoard(char board[Row][Col], int row, int col);//初始化棋盘
void PrintBoard(char board[Row][Col], int row, int col);//打印棋盘
void PlayBoard(char board[Row][Col], int row, int col);//玩游戏
void ComputerBoard(char board[Row][Col], int row, int col);//电脑下
int IsFull(char board[Row][Col], int row, int col);//棋盘是否已满
int Is_Win(char board[Row][Col], int row, int col);//判断谁赢
#include"game.h"
void menu()//打印菜单
{
printf("*****************************\n");
printf("*********** 1.play **********\n");
printf("*********** 0.exit **********\n");
printf("*****************************\n");
}
void game()
{
char board[Row][Col] = { 0 };
InitBoard(board, Row, Col);
PrintBoard(board, Row, Col);
char ret = 0;
while (1)
{
PlayBoard(board, Row, Col);
PrintBoard(board, Row, Col);
ret = Is_Win(board, Row, Col);
if (ret != 'C')
break;
ComputerBoard(board, Row, Col);
PrintBoard(board, Row, Col);
ret = Is_Win(board, Row, Col);
if (ret != 'C')
break;
}
if (ret == '*')
printf("玩家赢\n");
if (ret == '#')
printf("电脑赢\n");
if (ret == 'Q')
printf("平局\n");
}
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;
}
#include"game.h"
void InitBoard(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++)
{
board[i][j] = ' ';
}
}
}
void PrintBoard(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++)
{
printf(" %c ", board[i][j]);
if (j < col - 1)
printf("|");
}
printf("\n");
if (i < row - 1)
{
int j = 0;
for (j = 0; j < col; j++)
{
printf("---");
if (j < col - 1)
printf("|");
}
printf("\n");
}
}
}
void PlayBoard(char board[Row][Col], int row, int col)
{
int x, y = 0;
while (1)
{
printf("玩家下棋>:\n");
printf("请输入要下的坐标,中间用空格隔开\n");
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 ComputerBoard(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;//生成0-col-1的数
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
int IsFull(char board[Row][Col], int row, int col)
{
int i, j;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (board[i][j] == ' ')
return 0;
}
}
return 1;
}
int Is_Win(char board[Row][Col], int row, int col)
{
int i = 0;
int j = 0;
//竖着
if (j < col - 4)//防止越界
if (board[i][j] == board[i][j + 1] && board[i][j] ==board[i][j + 2]&& board[i][j] == board[i][j + 3] && board[i][j]== board[i][j + 4])
return board[i][j];
//横着
if (i < row - 4)
if(board[i][j] == board[i][j + 1] && board[i][j] ==board[i][j + 2]&& board[i][j] == board[i][j + 3] && board[i][j]== board[i][j + 4])
return board[i][j];
//左对角线连成五子
if (i < row - 4 && j < col - 4)
if (board[i][j] ==board[i + 1][j + 1] && board[i][j] ==board[i + 2][j + 2]&& board[i][j] == board[i + 3][j + 3] && board[i][j] == board[i + 4][j + 4])
return board[i][j];
// 右对角线连成五子
if (i < row - 4 && j > 4)
if (board[i][j] == board[i + 1][j - 1] &&board[i][j] ==board[i + 2][j - 2]&& board[i][j] == board[i + 3][j - 3] && board[i][j] == board[i + 4][j - 4])
return board[i][j];
if (IsFull(board, row, col) == 1)
{
return 'Q';
}
return 'C';
}
新人博主创作不易,如果感觉文章内容对你有所帮助的话不妨三连一下这个新人博主再走呗。你们的支持就是我更新的动力!!!
(可莉请求你们三连支持一下博主!!!点击下方评论点赞收藏帮帮可莉吧)