游戏思路
- 初始化棋盘
- 打印棋盘
- 玩家走
- 打印棋盘
- 判断输、赢、平局
- 电脑走
- 打印棋盘
- 判断输、赢、平局
如此3–8步循环直至游戏结束
实现这个小游戏我们通过test.c测试源文件,game.c游戏源文件以及game.h头文件构成。
测试代码:
#define _CRT_SECURE_NO_WARNINGS 1
//测试文件 test.c
#include <tx.h> //这是自己写的头文件,其中包括了常用的stdio.h等
#include "game.h"
void menu()
{
printf("***************************\n");
printf("******* 1.play ******\n");
printf("******* 0.exit ******\n");
printf("***************************\n");
}
void game()
{
char board[ROW][COL] = { 0 }; //棋盘
char ret = 0;
InitBoard(board, ROW, COL);
PrintBoard(board, ROW, COL);
while (1)
{
PlayerMove(board, ROW, COL); // 玩家走
//判断输赢
ret = IsWin(board, ROW, COL);
if (ret != ' ')
{
break;
}
PrintBoard(board, ROW, COL); //打印
ComputerMove(board, ROW, COL); //电脑走
//判断
ret = IsWin(board, ROW, COL);
if (ret != ' ')
{
break;
}
PrintBoard(board, ROW, COL); //打印
}
if (ret == 'O')
{
printf("电脑赢^\n");
}
else if (ret == 'X')
{
printf("玩家赢^\n");
}
else if (ret == 'P')
{
printf("平局^\n");
}
PrintBoard(board, ROW, COL);
}
void test()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请选择:\n");
scanf("%d",&input);
switch (input)
{
case 1:
//玩游戏
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误,重新选择!\n");
break;
}
} while (input);
}
int main()
{
test();
return 0;
}
游戏代码:
#define _CRT_SECURE_NO_WARNINGS 1
//game.c 存放游戏相关的实现代码
#include "game.h"
void InitBoard(char board[ROW][COL], int row, int col)
{
memset(board, ' ', row*col*sizeof(board[0][0]));//初始化为空格
}
void PrintBoard(char board[ROW][COL], int row, int col)
{
int i = 0;//行
int j = 0;//列
//1.打印┏━┳━┳━┓
printf("┏");
for (j = 0; j < col - 1; j++)
{
printf("━┳");
}
printf("━┓\n");
//2.
for (i = 0; i < row; i++)
{
//(1)打印┃ ┃ ┃ ┃
printf("┃");
for (j = 0; j < col; j++)
{
printf("%c", board[i][j]);
printf(" ┃");
}
printf("\n");
//(2)打印┣━╋━╋━┫
if (i < col - 1)
{
printf("┣");
for (j = 0; j < col - 1; j++)
{
printf("━╋");
}
printf("━┫\n");
}
//(3)打印┗━┻━┻━┛
else
{
printf("┗");
for (j = 0; j<col - 1; j++)
{
printf("━┻");
}
printf("━┛\n");
}
}
}
//玩家走
void PlayerMove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("玩家走->\n");
while (1)
{
printf("请输入坐标->\n");
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] = 'X';
break;
}
else
{
printf("该坐标被占用\n");
}
}
else
{
//坐标非法
printf("该坐标非法\n");
}
}
}
//判断棋盘是否占满 占满则平局
static 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 < row; j++)
{
if (board[i][j] == ' ')
{
return 0;
}
}
}
return 1;
}
//电脑赢 返回'O'
//玩家赢 返回'X'
//平局 返回'P'
//继续游戏 返回' '
//胜利条件:一行、列、对角线三个棋子相同
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] != ' ')
{
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];
}
//平局
if (IsFull(board, row, col) == 1)
{
return 'P';
}
return ' ';
}
//电脑走
void ComputerMove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("电脑走->\n");
while (1)
{
x = rand() % row; // rand 0--32767
y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = 'O';
break;
}
}
}
其中头文件game.h包括:
#ifndef __GAME_H__
#define __GAME_H__
#define ROW 3
#define COL 3
#include <tx.h>
void InitBoard(char board[ROW][COL], int row, int col);
void PrintBoard(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 IsWin(char board[ROW][COL], int row, int col);
#endif //__GAME_H__