c语言实现五子棋(真人对战+人机大战)
作者:互联网
废话不多说,直接上代码!
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ROW 10//宏定义行数为3 ROW为行,LIE为列(不会列的英文)
#define LIE 10//宏定义列数为3,也可以改为大棋盘,但要更改游戏机制
void aBoard(char board[ROW][LIE], int row, int lie);//初始化棋子
void printBoard(char board[ROW][LIE], int row, int lie);// 打印棋盘
void Playergo1(char board[ROW][LIE], int row, int lie); //玩家1走棋
void Playergo2(char board[ROW][LIE], int row, int lie); //玩家2走棋
void Computergo(char board[ROW][LIE], int row, int lie);//电脑走
char ifWin(char board[ROW][LIE], int row, int lie);//判断输赢
void menu(void);//菜单
void test(void);//
void game1(void);//核心
void game2(void);//核心
int main(void)
{
test();
return 0;
}
void game1(void)
{
char p = 0;
char board[ROW][LIE] = { 0 };//我也不知道为什么不能用‘ ’;
//初始化棋子为空格
aBoard(board, ROW, LIE);
//打印棋盘
printBoard(board, ROW, LIE);
while (2)//非0的数字就可以
{
//玩家1下棋
Playergo1(board, ROW, LIE);
printBoard(board, ROW, LIE);
//判断玩家1是否胜利
p = ifWin(board, ROW, LIE);
if (p != 'C')
{
break;
}
//玩家2下棋
Playergo2(board, ROW, LIE);
printBoard(board, ROW, LIE);
//判断玩家2是否胜利
p = ifWin(board, ROW, LIE);
if (p != 'C')
{
break;
}
}
if (p == 'O')
{
printf("PLAY1 WIN\n");
printf("\n");
}
else if (p == 'X')
{
printf("PLAY2 WIN!\n");
printf("\n");
}
}
void game2(void)
{
char p = 0;
char board[ROW][LIE] = { 0 };//我也不知道为什么不能用‘ ’;
//初始化棋子为空格
aBoard(board, ROW, LIE);
//打印棋盘
printBoard(board, ROW, LIE);
while (2)//非0的数字就可以
{
//玩家1下棋
Playergo1(board, ROW, LIE);
printBoard(board, ROW, LIE);
//判断玩家1是否胜利
p = ifWin(board, ROW, LIE);
if (p != 'C')
{
break;
}
//电脑下棋
Computergo(board, ROW, LIE);
printBoard(board, ROW, LIE);
//判断电脑是否胜利
p = ifWin(board, ROW, LIE);
if (p != 'C')
{
break;
}
}
if (p == 'O')
{
printf("PLAY1 WIN\n");
printf("\n");
}
else if (p == 'X')
{
printf("COMPUTERT WIN!\n");
printf("\n");
}
}
//游戏菜单
void menu(void)
{
printf("--------------------------------------------------------------\n");
printf("--------------------------------------------------------------\n");
printf("*****输入1.真人对战 输入2.人机大战 输入0.退出游戏*****\n");
printf("--------------------------------------------------------------\n");
printf("--------------------------------------------------------------\n");
}
//初始化棋盘
void aBoard(char board[ROW][LIE], int row, int lie)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < lie; j++)
{
board[i][j] = ' ';
}
}
}
//打印棋盘
void printBoard(char board[ROW][LIE], int row, int lie)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < lie; j++)
{
if (j < lie - 1)
{
printf(" %c |", board[i][j]);
}
else
printf(" %c ", board[i][j]);
}
printf("\n");
if (i < row - 1)
{
for (int k = 0; k < lie; k++)
{
if (k < lie - 1)
{
printf("---|");
}
else
{
printf("---");
}
}
}
printf("\n");
}
}
//玩家1输入
void Playergo1(char board[ROW][LIE], int row, int lie)
{
int x = 0;
int y = 0;
while (1)//同上
{
printf("\n请输入您的坐标,坐标之间需要空格间隔开\n");
scanf_s("%d%d", &x, &y);
printf("\n");
//判断xy是否合法,一个是是否超出,一个是是否被占用
if (x > 0 && x < row + 1 && y>0 && y < lie + 1)
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = 'O';
break;
}
else
{
printf("坐标被占用,请重新输入\n");
printf("\n");
}
}
else
{
printf("超出坐标范围,输入的横纵都坐标为1到3;\n\n");
}
}
}
//玩家2输入
void Playergo2(char board[ROW][LIE], int row, int lie)
{
int x = 0;
int y = 0;
while (1)//同上
{
printf("\n请输入您的坐标,坐标之间需要空格间隔开\n");
scanf_s("%d%d", &x, &y);
printf("\n");
//判断xy是否合法,一个是是否超出,一个是是否被占用
if (x > 0 && x < row + 1 && y>0 && y < lie + 1)
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = 'X';
break;
}
else
{
printf("坐标被占用,请重新输入\n");
printf("\n");
}
}
else
{
printf("超出坐标范围,输入的横纵都坐标为1到3;\n\n");
}
}
}
//电脑人机
void Computergo(char board[ROW][LIE], int row, int lie)
{
int x = 0;
int y = 0;
printf("本电脑走的飞快\n\n");
while (1)
{
x = rand() % row;
y = rand() % lie;
if (board[x][y] == ' ')
{
board[x][y] = 'X';
break;
}
}
}
//判断胜负
char ifWin(char board[ROW][LIE], int row, int lie)
{
int i = 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][1] != ' ')
{
return board[i][1];
}
}
//判断列
for (i = 0; i < lie; i++)
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[2][i] == board[3][i] && board[3][i] == board[4][i] && board[1][i] != ' ')
{
return board[1][i];
}
}
//判断对角线
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[2][2] == board[3][3] && board[3][3] == board[4][4] && board[1][1] != ' ')
{
return board[1][1];
}
if (board[2][0] == board[1][1] && board[1][1] == board[0][2] && board[1][1] == board[0][3] && board[1][1] == board[3][0] && board[1][1] == board[0][4] && board[1][1] == board[4][0] && board[1][1] != ' ')
{
return board[1][1];
}
return 'C';
}
void test(void)
{
srand((unsigned int)time(NULL));
int input = 0;
do
{
menu();
printf("\n");
printf("系统提示:请选择:>>>");
scanf_s("%d", &input);
printf("\n");
switch (input)
{
case 1:
game1();
break;
case 2:
game2();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("非法输入,请重新输入\n");
break;
}
} while (input);
}
标签:LIE,int,void,五子棋,对战,board,人机,printf,ROW 来源: https://blog.csdn.net/weixin_44606789/article/details/120788192