文章目录
game.h
编译环境
VS2019
游戏界面
test6.c
#include <stdio.h>//std standard input output
#include <string.h>
#include <game.h>
//测试三子棋游戏
void menu()
{
printf("**********************\n");
printf("******1paly,0end******\n");
printf("**********************\n");
}
void game()
{
char board[ROW][COL];
InitBoard(board, ROW, COL);//所以是传入这三个形参
DisplayBoard(board, ROW, COL);//一个数组最基本的就三点:名字,行,列
char ret = 0;//接受游戏状态
int ret_flag = 0;
while (1)
{
PlayerMove(board, ROW, COL);
//判断玩家是否赢得游戏
ret = IsWin(board, ROW, COL);
ret_flag = IsFull(board, ROW, COL);
//if (ret != 'C')
// break;
if (ret == '*' && ret_flag)
{
printf("玩家赢了\n");
break;
}
else if (ret == '#' && ret_flag)
{
printf("电脑赢了\n");
break;
}
else if (ret == 'Q' && ret_flag)
{
printf("平局\n");
break;
}
ComputerMove(board, ROW, COL);
DisplayBoard(board, ROW, COL);//一个数组最基本的就三点:名字,行,列
}
}
void test()
{
int input = 0;
srand((unsigned int)time(NULL));
menu();
printf("请选择数字:");
scanf_s("%d", &input);//假如这里写成了"%d\n",于是此时第一次输入1+回车无反应,第二次输入1+回车才有反应,只有按下回车再松开时数字才赋给input
//printf("444\n");//上面为啥是那样,我也不是很明白,记住吧
do
{
switch (input)
{
case 1:
game();
printf("三子棋\n");
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("请重新选择\n");
break;
}
} while (input);//这里不填入数字,系统报错
}
int main()
{
test();
return 0;
}
game.c
#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 DisplayBoard(char board[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
//printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][2]);
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 PlayerMove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("玩家走,");
printf("请输入坐标:");
//之所以用到这个while(1)循环是因为,假如我第一次输入一个坐标,电脑输入一个坐标
//然后你第二次输入一个与第一次相同的坐标,此时打印“该坐标已经被占用”,此时电脑仍然输入第二个坐标
//结果就是,你输入了一个坐标,电脑输入了两个坐标,因为必须加入死循环,使得每次必须输入一个坐标
while (1)
{
scanf_s("%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("该坐标已经被占用,请重新输入;");
continue;
}
}
else
{
printf("请输入合法坐标:");
continue;
}
}
}
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] = '#';
break;
}
}
}
int IsFull(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;//棋盘满了
}
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[i][1] != ' ')//board[i][1] != ' '判断三个中任意一个即可
{
return board[i][1];
}
}
//判断三列
for (i = 0; i < col; i++)
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][i] != ' ')
{
return board[1][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][0] && board[1][1] != ' ')
{
return board[1][1];
}
//判断平局
//如果棋盘满了返回1, 不满返回0
int ret = IsFull(board, row, col);
if (ret == 1)
{
return 'Q';
}
//继续
return 'C';
}
game.h
#define ROW 3
#define COL 3
#include <stdlib.h>
#include <time.h>
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);
//1. 玩家赢了 - *
//2. 电脑赢了 - #
//3. 平局 - Q
//4. 游戏继续 - C
//判断游戏是否有输赢
char IsWin(char board[ROW][COL], int row, int col);
int IsFull(char board[ROW][COL], int row, int col);