大家好,首先祝大家五一快乐,当然有很多大学生像作者一样五一并没有放假,说多了都是泪啊,不过没事,我们还有周末!废话不多说了,今天我们一起来研究一下三子棋,虽然在c站三子棋已经烂大街了,但是每个人都有自己与众不同的地方,值得我们学习。所以我们可以博采众长,吸收别人优秀的地方,提高自己的水平。
我们在写一个项目的时候,如果这个项目非常复杂,不建议大家上来就干代码的,因为你有很大的可能会出错。我们应该仔细思考游戏的逻辑,如何实现这个项目。
1.我们需要一个菜单控制游戏,让玩家做出选择
2.游戏最好可以让玩家一直玩,除非玩家退出游戏
3.三子棋需要一个棋盘,我们打印一个棋盘(通过二维数组)
4.玩家下棋,输入坐标(也可以电脑先下棋)
5.打印棋盘,观察坐标,(是否有错误)
6.电脑下棋,打印棋盘,这部分我们可以优化一下,让我们很难赢电脑
7.判断输赢,打印棋盘,观察坐标
当我们写的代码比较多的时候,建议大家使用模块化的设计,我们这里分成三个部分:
test.c
主函数部分,对游戏的逻辑进行测试运行
game.h
库函数头文件的包含
行列的自定义设置
函数的定义
game.c
游戏逻辑的具体实现
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void menu()
{
printf("**************************************\n");
printf("******** 1. play(智能模式) *********\n");
printf("******** 2. play(智障模式) *********\n");
printf("******** 0. exit(退出游戏) *********\n");
printf("**************************************\n");
}
void game1()
{ //存储数据 - 二维数组
char board[ROW][COL] = { 0 };
//初始化棋盘 - 初始化空格
initBoard(board, ROW, COL);
//打印棋盘 - 本质是打印数组的内容
displayBoard(board, ROW, COL);
//下棋
char ret = 0;
while(1)
{
playerMove(board, ROW, COL);
displayBoard(board, ROW, COL);
//判断玩家是否赢得游戏
ret = isWin(board, ROW, COL);
if(ret != 'c')
{
break;
}
computerMove(board, ROW, COL);
displayBoard(board, ROW, COL);
//判断电脑是否赢得游戏
ret = isWin(board, ROW, COL);
if (ret != 'c')
{
break;
}
}
switch (ret)
{
case 'O':
printf("你赢了,也就一般般吧\n");
break;
case 'X':
printf("你竟然输给了人工智障,哈哈\n");
break;
case 'p':
printf("平局,好好反思一下你自己\n");
break;
displayBoard(board, ROW, COL);
}//效果差不多
/*if (ret == 'o')
{
printf("玩家赢了\n");
}
else if (ret == 'x')
{
printf("电脑赢了\n");
}
else
{
printf("平局\n");
}*/
}
void game2()
{ //存储数据 - 二维数组
char board[ROW][COL] = { 0 };
//初始化棋盘 - 初始化空格
initBoard(board, ROW, COL);
//打印棋盘 - 本质是打印数组的内容
displayBoard(board, ROW, COL);
//下棋
char ret = 0;
while (1)
{
SmartComputerMove(board, ROW, COL);
displayBoard(board, ROW, COL);
ret = isWin(board, ROW, COL);
//判断电脑是否赢得游戏
if (ret != 'c')
{
break;
}
playerMove(board, ROW, COL);
displayBoard(board, ROW, COL);
//判断玩家是否赢得游戏
ret = isWin(board, ROW, COL);
if (ret != 'c')
{
break;
}
}
switch (ret)
{
case 'O':
printf("您赢了,就算是阿尔法狗也不是您的对手\n");
break;
case 'X':
printf("你输了,没事,等于你已经和大神过过手了\n");
break;
case 'p':
printf("平局,小伙子,你非常有前途\n");
break;
displayBoard(board, ROW, COL);
}
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请选择模式:>");
printf("\n人工智障:1 | 人工智能:2 | 退出游戏:0\n");
scanf("%d", &input);
switch (input)
{
case 1:
game1();
break;
case 2:
game2();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("输入错误,请重新输入\n");
break;
}
} while (input);
return 0;
}
//防止头文件被重复包含
#pragma once
//头文件的包含
#include
#include
#include
//定义行和列(可以随意改变)
#define ROW 3
#define COL 3
//初始化棋盘
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);
//困难模式
void SmartComputerMove(char board[ROW][COL], int row, int col);
//判断棋盘满没满
int isFull(char board[ROW][COL], int row, int col);
//1.玩家赢 - O
//2.电脑赢 - X
//3.平局 - p
//4.游戏继续 - c
//判断输赢
char isWin(char board[ROW][COL], int row, int col);
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void initBoard(char board[ROW][COL], int row, int col)
{
for (int i = 0; i < row; i++)
{
for (int 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(int 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("玩家走:>\n");
while(1)
{
printf("请输入下棋坐标:>");
scanf("%d %d", &x, &y);
if (x > 0 && x <= row && y > 0 && y <= col)
{
//判断坐标是否被占用
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = 'O';
break;
}
else
{
printf("坐标已被占用,请重新输入\n");
break;
}
}
else
{
printf("坐标错误,请重新输入\n");
}
}
}
void computerMove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("电脑走:>\n");
while (1)
{
x = rand() % row;
y = rand() % col;
//不用判断合法性
if(board[x][y] == ' ')
{
board[x][y] = 'X';
break;
}
}
}
int isFull(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++)
{
if(board[i][j] == ' ')
{
return 0;//棋盘没满
}
}
}
return 1;//棋盘满了
}
char isWin(char board[ROW][COL], int row, int col)
{
//判断三行
for (int i = 0; i < row; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' ')
{
return board[i][1];
}
}
//判断三列
for (int j = 0; j < col; j++)
{
if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[1][j] != ' ')
{
return board[0][j];
}
}
//判断对角线
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
{
return board[0][0];
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
{
return board[0][2];
}
//判断平局
int ret = isFull(board, row, col);
if (ret == 1)
{
return 'p';
}
return 'c';
}
void SmartComputerMove(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][0] == 'X' && board[i][2] == ' ')
{
board[i][2] = 'X';
}
if (board[i][0] == board[i][2] && board[i][0] == 'X' && board[i][1] == ' ')
{
board[i][1] = 'X';
}
if (board[i][1] == board[i][2] && board[i][1] == 'X' && board[i][0] == ' ')
{
board[i][0] = 'X';
}
}
//判断每一列是否有两个相连的棋子,如果有,且第三个棋格为空,则落棋
for (j = 0; j < col; j++)
{
if (board[0][j] == board[1][j] && board[0][j] == 'X' && board[2][j] == ' ')
{
board[2][j] = 'X';
}
if (board[0][j] == board[2][j] && board[0][j] == 'X' && board[1][j] == ' ')
{
board[1][j] = 'X';
}
if (board[1][j] == board[2][j] && board[1][j] == 'X' && board[0][j] == ' ')
{
board[0][j] = 'X';
}
}
//判断两条对角线是否有两个相连的棋子,如果有,且第三个棋格为空,则落棋
{
//第一条
if (board[0][0] == board[1][1] && board[0][0] == 'X' && board[2][2] == ' ')
{
board[2][2] = 'X';
}
if (board[0][0] == board[2][2] && board[0][0] == 'X' && board[1][1] == ' ')
{
board[1][1] = 'X';
}
if (board[1][1] == board[2][2] && board[1][1] == 'X' && board[0][0] == ' ')
{
board[0][0] = 'X';
}
//第二条
if (board[0][2] == board[1][1] && board[0][2] == 'X' && board[2][0] == ' ')
{
board[2][0] = 'X';
}
if (board[0][2] == board[2][0] && board[0][2] == 'X' && board[1][1] == ' ')
{
board[1][1] = 'X';
}
if (board[1][1] == board[2][0] && board[1][1] == 'X' && board[0][2] == ' ')
{
board[0][2] = 'X';
}
//如果上面都没落子,说明不符合赢的条件
int i = 0;
int j = 0;
//判断每一行是否有两个相连的棋子,如果有,且第三个棋格为空,则堵棋
for (i = 0; i < row; i++)
{
if (board[i][0] == board[i][1] && board[i][0] == 'O' && board[i][2] == ' ')
{
board[i][2] = 'X';
goto end;
}
if (board[i][0] == board[i][2] && board[i][0] == 'O' && board[i][1] == ' ')
{
board[i][1] = 'X';
goto end;
}
if (board[i][1] == board[i][2] && board[i][1] == 'O' && board[i][0] == ' ')
{
board[i][0] = 'X';
goto end;
}
}
//判断每一列是否有两个相连的棋子,如果有,且第三个棋格为空,则堵棋
for (j = 0; j < col; j++)
{
if (board[0][j] == board[1][j] && board[0][j] == 'O' && board[2][j] == ' ')
{
board[2][j] = 'X';
goto end;
}
if (board[0][j] == board[2][j] && board[0][j] == 'O' && board[1][j] == ' ')
{
board[1][j] = 'X';
goto end;
}
if (board[1][j] == board[2][j] && board[1][j] == 'O' && board[0][j] == ' ')
{
board[0][j] = 'X';
goto end;
}
}
//判断两条对角线是否有两个相连的棋子,如果有,且第三个棋格为空,则堵棋
{
//第一条
if (board[0][0] == board[1][1] && board[0][0] == 'O' && board[2][2] == ' ')
{
board[2][2] = 'X';
goto end;
}
if (board[0][0] == board[2][2] && board[0][0] == 'O' && board[1][1] == ' ')
{
board[1][1] = 'X';
goto end;
}
if (board[1][1] == board[2][2] && board[1][1] == 'O' && board[0][0] == ' ')
{
board[0][0] = 'X';
goto end;
}
//第二条
if (board[0][2] == board[1][1] && board[0][2] == 'O' && board[2][0] == ' ')
{
board[2][0] = 'X';
goto end;
}
if (board[0][2] == board[2][0] && board[0][2] == 'O' && board[1][1] == ' ')
{
board[1][1] = 'X';
goto end;
}
if (board[1][1] == board[2][0] && board[1][1] == 'O' && board[0][2] == ' ')
{
board[0][2] = 'X';
goto end;
}
//如果上面都没返回,说明不符合堵棋,下棋的条件
else
{
computerMove(board, ROW, COL);
}
}
}
int ret = 0;
end://判断平局
ret = isFull(board, row, col);
if (ret == 1)
{
return 'p';
}
return 'c';
}
void initBoard(char board[ROW][COL], int row, int col)
{
for (int i = 0; i < row; i++)
{
for (int 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(int 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("玩家走:>\n");
while(1)
{
printf("请输入下棋坐标:>");
scanf("%d %d", &x, &y);
if (x > 0 && x <= row && y > 0 && y <= col)
{
//判断坐标是否被占用
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = 'O';
break;
}
else
{
printf("坐标已被占用,请重新输入\n");
break;
}
}
else
{
printf("坐标错误,请重新输入\n");
}
}
}
void computerMove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("电脑走:>\n");
while (1)
{
x = rand() % row;
y = rand() % col;
//不用判断合法性
if(board[x][y] == ' ')
{
board[x][y] = 'X';
break;
}
}
}
void SmartComputerMove(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][0] == 'X' && board[i][2] == ' ')
{
board[i][2] = 'X';
}
if (board[i][0] == board[i][2] && board[i][0] == 'X' && board[i][1] == ' ')
{
board[i][1] = 'X';
}
if (board[i][1] == board[i][2] && board[i][1] == 'X' && board[i][0] == ' ')
{
board[i][0] = 'X';
}
}
//判断每一列是否有两个相连的棋子,如果有,且第三个棋格为空,则落棋
for (j = 0; j < col; j++)
{
if (board[0][j] == board[1][j] && board[0][j] == 'X' && board[2][j] == ' ')
{
board[2][j] = 'X';
}
if (board[0][j] == board[2][j] && board[0][j] == 'X' && board[1][j] == ' ')
{
board[1][j] = 'X';
}
if (board[1][j] == board[2][j] && board[1][j] == 'X' && board[0][j] == ' ')
{
board[0][j] = 'X';
}
}
//判断两条对角线是否有两个相连的棋子,如果有,且第三个棋格为空,则落棋
{
//第一条
if (board[0][0] == board[1][1] && board[0][0] == 'X' && board[2][2] == ' ')
{
board[2][2] = 'X';
}
if (board[0][0] == board[2][2] && board[0][0] == 'X' && board[1][1] == ' ')
{
board[1][1] = 'X';
}
if (board[1][1] == board[2][2] && board[1][1] == 'X' && board[0][0] == ' ')
{
board[0][0] = 'X';
}
//第二条
if (board[0][2] == board[1][1] && board[0][2] == 'X' && board[2][0] == ' ')
{
board[2][0] = 'X';
}
if (board[0][2] == board[2][0] && board[0][2] == 'X' && board[1][1] == ' ')
{
board[1][1] = 'X';
}
if (board[1][1] == board[2][0] && board[1][1] == 'X' && board[0][2] == ' ')
{
board[0][2] = 'X';
}
//如果上面都没落子,说明不符合赢的条件
int i = 0;
int j = 0;
//判断每一行是否有两个相连的棋子,如果有,且第三个棋格为空,则堵棋
for (i = 0; i < row; i++)
{
if (board[i][0] == board[i][1] && board[i][0] == 'O' && board[i][2] == ' ')
{
board[i][2] = 'X';
goto end;//防止多次落子的问题
}
if (board[i][0] == board[i][2] && board[i][0] == 'O' && board[i][1] == ' ')
{
board[i][1] = 'X';
goto end;
}
if (board[i][1] == board[i][2] && board[i][1] == 'O' && board[i][0] == ' ')
{
board[i][0] = 'X';
goto end;
}
}
//判断每一列是否有两个相连的棋子,如果有,且第三个棋格为空,则堵棋
for (j = 0; j < col; j++)
{
if (board[0][j] == board[1][j] && board[0][j] == 'O' && board[2][j] == ' ')
{
board[2][j] = 'X';
goto end;
}
if (board[0][j] == board[2][j] && board[0][j] == 'O' && board[1][j] == ' ')
{
board[1][j] = 'X';
goto end;
}
if (board[1][j] == board[2][j] && board[1][j] == 'O' && board[0][j] == ' ')
{
board[0][j] = 'X';
goto end;
}
}
//判断两条对角线是否有两个相连的棋子,如果有,且第三个棋格为空,则堵棋
{
//第一条
if (board[0][0] == board[1][1] && board[0][0] == 'O' && board[2][2] == ' ')
{
board[2][2] = 'X';
goto end;
}
if (board[0][0] == board[2][2] && board[0][0] == 'O' && board[1][1] == ' ')
{
board[1][1] = 'X';
goto end;
}
if (board[1][1] == board[2][2] && board[1][1] == 'O' && board[0][0] == ' ')
{
board[0][0] = 'X';
goto end;
}
//第二条
if (board[0][2] == board[1][1] && board[0][2] == 'O' && board[2][0] == ' ')
{
board[2][0] = 'X';
goto end;
}
if (board[0][2] == board[2][0] && board[0][2] == 'O' && board[1][1] == ' ')
{
board[1][1] = 'X';
goto end;
}
if (board[1][1] == board[2][0] && board[1][1] == 'O' && board[0][2] == ' ')
{
board[0][2] = 'X';
goto end;
}
//如果上面都没返回,说明不符合堵棋,下棋的条件
else
{
computerMove(board, ROW, COL);
}
}
}
int ret = 0;
end://判断平局
ret = isFull(board, row, col);
if (ret == 1)
{
return 'p';
}
return 'c';
}
int isFull(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++)
{
if(board[i][j] == ' ')
{
return 0;//棋盘没满
}
}
}
return 1;//棋盘满了
}
char isWin(char board[ROW][COL], int row, int col)
{
//判断三行
for (int i = 0; i < row; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' ')
{
return board[i][1];
}
}
//判断三列
for (int j = 0; j < col; j++)
{
if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[1][j] != ' ')
{
return board[0][j];
}
}
//判断对角线
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
{
return board[0][0];
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
{
return board[0][2];
}
//判断平局
int ret = isFull(board, row, col);
if (ret == 1)
{
return 'p';
}
return 'c';
}
我们可以发现简单模式下,电脑的下法是非常笨拙的,因为是随机的坐标。我们可以改一下,比如困难模式,用穷举的方法,判断我方是否有两颗棋子在一条线上,如果有则堵死;并且判断电脑的棋子是否有两颗在一条线上,如果有则落子。一定程度上增加了难度,不太容易赢,平局比较多。因为电脑前两颗棋子是随机的,所以也就是看运气。感兴趣的小伙伴可以复制过去体验一下,可能会有BUG,我暂时还没有发现!!!如果你发现了,请一定联系我改正。另外,这个程序只适用于三子棋,其实也可以改,不过我懒写的了,你可以搜一下,等以后闲了再说吧,孩子要好好学习了,不然要挂科了!!!