作者简介:大家好,我是大杉。
个人主页:爱编程的大杉
支持我:点赞+收藏~不迷路
✔系列专栏:c语言入门每日一题
(❁´◡`❁)每日格言:如果足够勇敢说再见,生活便会奖励你一个新的开始。
题目描述:用c语言实现三子棋游戏。
三子棋又叫九宫格游戏,是以二维数组为基础设计的一种人机下棋博弈的游戏,其中先将棋子连成串的为获胜者
#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 playMove(char board[ROW][COL], int row, int col);
//电脑移动
void computerMove(char board[ROW][COL], int row, int col);
//判断游戏是否有输赢
char Iswin(char board[ROW][COL], int row, int col);
不要忘记要加入#include"game.h"的头文件,是多文件关联
void initboard(char board[ROW][COL], int row, int col)
{
int i, j;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
board[i][j] = ' ';//空格要用单引号框起来
}
}
}
/显示棋盘
void displayboard(char board[ROW][COL], int row, int col)
{
int i;
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)
{
for (j=0;j<col;j++)
{
printf("---");
if (j < col-1)
{
printf("|");
}
}
printf("\n");
}
}
}
void playMove(char board[][COL], int row, int col)
{
printf("玩家走\n");
while (1)
{
int i = 0;
int j = 0;
printf("请输入一个坐标\n");
scanf_s("%d %d", &i, &j);
if (i >= 1 && i <= row && j >= 1 && j <= col)
{
//判断棋盘是否被占位
if (board[i - 1][j - 1] != ' ')
{
printf("坐标已经被占用\n");
//board[i - 1][j - 1] = '*';
//break;
}
else
{
board[i - 1][j - 1] = '*';
break;
}
}
else
{
printf("输入的坐标非法\n");
}
}
}
void computerMove(char board[ROW][COL], int row, int col)
{
printf("电脑走\n");
while (1)
{
int x = rand() % ROW;
int y = rand() % ROW;
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;
}
char Iswin(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][1] == board[i][2] && board[1][0] != ' ')
{
return board[i][0];
}
}
//判断三列
for (i = 0; i < col; i++)
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][i] != ' ')
{
return board[0][i];
}
}
//判断对角线
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][2] && board[1][1] != ' ')
{
return board[1][1];
}
//判断平局
int ret = isfull(board, ROW, COL);
if (ret == 1)
{
return'Q';
}
return 'c';
}
//判断平局
int ret = isfull(board, ROW, COL);
if (ret == 1)
{
return'Q';
}
return 'c';
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 2:
printf("退出游戏\n");
break;
default:
printf("输入错误\n");
break;
}
} while (input);
return 0;
}
void menu()
{
printf("*********************\n");
printf("*****1.play *********\n");
printf("*****2.exit *********\n");
printf("*********************\n");
}
void game()
{
//存储数据,二维数组
char board[ROW][COL];
//初始化数组;
initboard(board, ROW, COL);
displayboard(board, ROW, COL);
int ret = 0;
while (1)//研究一下
{
//玩家下棋
playMove(board, ROW, COL);
displayboard(board, ROW, COL);
//displayboard(board, ROW, COL);
ret = Iswin(board, ROW, COL);
if (ret != 'c')
{
break;
}
//电脑下棋
computerMove(board, ROW, COL);
ret=Iswin(board, ROW, COL);
if (ret != 'c')
{
break;
}
displayboard(board, ROW, COL);
}
if (ret == '*')
{
printf("玩家赢了\n");
}
if (ret == '#')
{
printf("电脑赢了\n");
}
if (ret == 'Q')
{
printf("平局\n");
}
if (ret == 'c')
{
printf("游戏继续\n");
}
}
以上就是实现三子棋的分模块代码执行了,有过有更为优化的方式,还请大佬们能指点一二