参考了B站UP主 比特鹏哥 的教程视频
链接:https://www.bilibili.com/video/BV1oi4y1g7CF?t=464&p=17
简单实现三子棋+改进后多子棋
/*
缺点:
1.胜负判断只能判断棋盘为3 × 3
2.电脑下棋是随机下
*/
#include <stdio.h> //格式化输入输出函数库
#include <stdlib.h> //伪随机数生成函数库
#include <time.h> //每个生成数不同
#define ROW 3 //棋盘横
#define COL 3 //棋盘纵
void menu(void); //菜单
void game(void); //游戏逻辑
void InitBoard(char board[ROW][COL],int row,int col); //初始化棋盘
void DrawBoard(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 Win(char board[ROW][COL],int row,int col); //胜负判断
int Full(char board[ROW][COL],int row,int col); //平局
int main(void)
{
int input = 0;
srand((unsigned int)time(NULL));//使用系统时间作为随机值种子
do {
menu();
printf("请选择:>>> ");
scanf("%d",&input);
printf("\n");
switch (input)
{
case 1:
game();
break;
case 0:
printf("垃圾游戏,不玩了!!!\n");
break;
default:
printf("FBI >>> 输入错误,重新输入!!!\n");
break;
}
printf("\n");
}while(input);
return 0;
}
//打印菜单函数
void menu(void)
{
printf("*************************\n");
printf("****** 1.开始游戏 *******\n");
printf("****** 0.退出游戏 *******\n");
printf("*************************\n");
printf("\n");
return;
}
//游戏函数
void game (void)
{
char Board[ROW][COL] = {0};
//初始化棋盘
InitBoard(Board,ROW,COL);
//打印棋盘
DrawBoard(Board,ROW,COL);
while (1)
{
//玩家输入
PlayerMove(Board,ROW,COL);
if (Win(Board,ROW,COL) != 'C')
{
break;
}
//电脑输入
ComputerMove(Board,ROW,COL);
if (Win(Board,ROW,COL) != 'C')
{
break;
}
}
if ('*' == Win(Board,ROW,COL))
{
printf("恭喜你赢了!!!\n");
}
else if ('#' == Win(Board,ROW,COL))
{
printf("你输了!!!\n");
}
else
{
printf("平局!!!\n");
}
return;
}
//初始化棋盘
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] = ' ';
}
}
return;
}
//打印棋盘
void DrawBoard(char board[ROW][COL],int row,int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
//打印 1 | 1 | 1 棋盘
for (j = 0; j < col; j++)
{
if (j < col - 1)
{
printf(" %c |",board[i][j]);
}
else
{
printf(" %c ",board[i][j]);
}
}
printf("\n"); //换行
//打印---|---|---棋盘
if (i < row - 1)
{
for(j = 0;j < col; j++)
{
if (j < col - 1)
{
printf("---|");
}
else
{
printf("---");
}
}
}
printf("\n");
}
return;
}
//玩家输入
void PlayerMove(char board[ROW][COL],int row,int col)
{
int x = 0;
int y = 0;
while (1)
{
printf("请输入您要下的坐标: >>> ");
scanf("%d%d",&x,&y);
printf("\n");
//判断xy是否合法,溢出+占用
if (x > 0 && x - 1 < row && y > 0 && y - 1 < col)
{
if (board[x-1][y-1] == ' ')
{
board[x-1][y-1] = '*';
break;
}
else
{
printf("这下过了,憨憨!!!重新输入!\n");
printf("\n");
}
}
else
{
printf("FBI 非法输入!!! 重新输入!\n");
printf("\n");
}
}
DrawBoard(board,row,col);
return;
}
//电脑输入
void ComputerMove(char board[ROW][COL],int row,int col)
{
int x = 0;
int y = 0;
printf("电脑下棋时间.....\n");
printf("\n");
while (1)
{
x = rand() % row;//库函数中的伪随机数
y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
DrawBoard(board,row,col);
if ('#' != Win(board,row,col))
{
printf("我下完了,到你了!!!\n");
printf("\n");
}
return;
}
//胜负判断
char Win(char board[ROW][COL],int row,int col)
{
int i = 0;
//行
for (i = 0; i < row; i++)
{
if (board[i][0] == board[i][1] && board[i][0]== board[i][2] && board[i][1] == board[i][2] && board[i][1] != ' ')
{
return board[i][0];
}
}
//列
for (i = 0; i < row; i++)
{
if (board[0][i] == board[1][i] && board[0][i]== board[2][i] && board[1][i] == board[2][i] && board[0][i] != ' ')
{
return board[0][i];
}
}
//对角
if (board[1][1] == board[2][2] && board[2][2]== board[0][0] && board[1][1] == board[0][0] && board[1][1] != ' ')
{
return board[0][0];
}
if (board[0][2] == board[1][1] && board[2][0]== board[1][1] && board[0][2] == board[2][0] && board[1][1] != ' ')
{
return board[2][0];
}
//平局
if (Full(board,row,col) == 1)
{
return 'Q';
}
return 'C';
}
//平局
int 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;
}
改进后
/*
改进:
1.棋盘添加坐标。
2.更改棋盘胜负判断,实现多子判胜负。
*/
#include <stdio.h> //格式化输入输出函数库
#include <stdlib.h> //伪随机数生成函数库
#include <time.h> //每个生成数不同
#define ROW 10 //棋盘横
#define COL 10 //棋盘纵
#define NUMBER 5 //多子
void menu(void); //菜单
void game(void); //游戏逻辑
void InitBoard(char board[ROW][COL],int row,int col); //初始化棋盘
void DrawBoard(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 Win(char board[ROW][COL],int row,int col); //胜负判断
int Full(char board[ROW][COL],int row,int col); //平局
int main(void)
{
int input = 0;
srand((unsigned int)time(NULL));//使用系统时间作为随机值种子
do {
menu();
printf("请选择:>>> ");
scanf("%d",&input);
printf("\n");
switch (input)
{
case 1:
game();
break;
case 0:
printf("垃圾游戏,不玩了!!!\n");
break;
default:
printf("FBI >>> 输入错误,重新输入!!!\n");
break;
}
printf("\n");
}while(input);
return 0;
}
//打印菜单函数
void menu(void)
{
printf("*************************\n");
printf("****** 1.开始游戏 *******\n");
printf("****** 0.退出游戏 *******\n");
printf("*************************\n");
printf("\n");
return;
}
//游戏函数
void game (void)
{
char Board[ROW][COL] = {0};
//初始化棋盘
InitBoard(Board,ROW,COL);
//打印棋盘
DrawBoard(Board,ROW,COL);
while (1)
{
//玩家输入
PlayerMove(Board,ROW,COL);
if (Win(Board,ROW,COL) != 'C')
{
break;
}
//电脑输入
ComputerMove(Board,ROW,COL);
if (Win(Board,ROW,COL) != 'C')
{
break;
}
}
if ('*' == Win(Board,ROW,COL))
{
printf("恭喜你赢了!!!\n");
}
else if ('#' == Win(Board,ROW,COL))
{
printf("你输了!!!\n");
}
else
{
printf("平局!!!\n");
}
return;
}
//初始化棋盘
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] = ' ';
}
}
return;
}
//打印棋盘
void DrawBoard(char board[ROW][COL],int row,int col)
{
int i = 0;
int j = 0;
//棋盘上方坐标
for (i = 0; i <= col; i++)
{
printf (" %02d ",i);
}
printf("\n");
printf("\n");
for (i = 0; i < row; i++)
{
//棋盘左边坐标
printf(" %02d ",i + 1);
//打印 1 | 1 | 1 棋盘
for (j = 0; j < col; j++)
{
if (j < col - 1)
{
printf(" %c |",board[i][j]);
}
else
{
printf(" %c ",board[i][j]);
}
}
printf("\n"); //换行
printf(" ");
//打印---|---|---棋盘
if (i < row - 1)
{
for(j = 0;j < col; j++)
{
if (j < col - 1)
{
printf("---|");
}
else
{
printf("---");
}
}
}
printf("\n");
}
return;
}
//玩家输入
void PlayerMove(char board[ROW][COL],int row,int col)
{
int x = 0;
int y = 0;
while (1)
{
printf("请输入您要下的坐标: >>> ");
scanf("%d%d",&x,&y);
printf("\n");
//判断xy是否合法,溢出+占用
if (x > 0 && x - 1 < row && y > 0 && y - 1 < col)
{
if (board[x-1][y-1] == ' ')
{
board[x-1][y-1] = '*';
break;
}
else
{
printf("这下过了,憨憨!!!重新输入!\n");
printf("\n");
}
}
else
{
printf("FBI 非法输入!!! 重新输入!\n");
printf("\n");
}
}
DrawBoard(board,row,col);
return;
}
//电脑输入
void ComputerMove(char board[ROW][COL],int row,int col)
{
int x = 0;
int y = 0;
printf("电脑下棋时间.....\n");
printf("\n");
while (1)
{
x = rand() % row;//库函数中的伪随机数
y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
DrawBoard(board,row,col);
if ('#' != Win(board,row,col))
{
printf("我下完了,到你了!!!\n");
printf("\n");
}
return;
}
//胜负判断
char Win(char board[ROW][COL],int row,int col)
{
int x = 0;
int y = 0;
int i = 0;
for (x = 0; x < row; x++)
{
for (y = 0; y < col; y++)
{
//比较(x,y)和(x,y + 1)(x,y + 2)(x,y + 3)(x ,y +(NUMBER-1))相同继续,不同跳出
//纵向
for (i = 0;i < NUMBER - 1; i++)
{
if (board[x][y] == board[x][y+i] && board[x][y] != ' ')
{
continue;
}
else
{
break; //只跳出当前for
}
}
if (board[x][y] == board[x][y+i] && board[x][y] != ' ')
{
return board[x][y];
}
//横向
for (i = 0;i < NUMBER - 1; i++)
{
if (board[x][y] == board[x+i][y] && board[x][y] != ' ')
{
continue;
}
else
{
break;
}
}
if (board[x][y] == board[x+i][y] && board[x][y] != ' ')
{
return board[x][y];
}
//撇
for (i = 0;i < NUMBER - 1; i++)
{
if (board[x][y] == board[x-i][y+i] && board[x][y] != ' ')
{
continue;
}
else
{
break;
}
}
if (board[x][y] == board[x-i][y+i] && board[x][y] != ' ')
{
return board[x][y];
}
for (i = 0;i < NUMBER - 1; i++)
{
if (board[x][y] == board[x+i][y-i] && board[x][y] != ' ')
{
continue;
}
else
{
break;
}
}
if (board[x][y] == board[x+i][y-i] && board[x][y] != ' ')
{
return board[x][y];
}
//捺
for (i = 0;i < NUMBER - 1; i++)
{
if (board[x][y] == board[x-i][y-i] && board[x][y] != ' ')
{
continue;
}
else
{
break;
}
}
if (board[x][y] == board[x-i][y-i] && board[x][y] != ' ')
{
return board[x][y];
}
for (i = 0;i < NUMBER - 1; i++)
{
if (board[x][y] == board[x+i][y+i] && board[x][y] != ' ')
{
continue;
}
else
{
break;
}
}
if (board[x][y] == board[x+i][y+i] && board[x][y] != ' ')
{
return board[x][y];
}
}
}
//平局
if (Full(board,row,col) == 1)
{
return 'Q';
}
return 'C';
}
//平局
int 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;
}