game.h:自定义头文件,用于:
//防止头文件被重复包含
#pragma once
//头文件的包含
#include
#include
#include
//符号的定义:使棋盘的大小可以跟着row和col的改变而改变
#define ROW 5
#define COL 5
//函数的声明
//棋盘初始化
void BoardInit(char arr[ROW][COL], int row, int col);
//打印棋盘
void BoardPrint(char arr[ROW][COL], int row, int col);
//玩家下棋
void PlayerMove(char arr[ROW][COL], int row, int col);
//电脑下棋
void ComputerMove(char arr[ROW][COL], int row, int col);
//判断输赢
char IsWin(char arr[ROW][COL], int row, int col);
//判断棋盘是否满了
int IsFull(char board[ROW][COL], int row, int col);
test.c:用于游戏逻辑的测试
#define _CRT_SECURE_NO_WARNINGS 1
//自定义头文件的包含
#include"game.h"
void menu()
{
printf("================================\n");
printf("========= 1. play ==========\n");
printf("========= 0. exit ==========\n");
printf("================================\n");
}
//游戏逻辑的实现
void game()
{
//定义一个二维数组来存储下棋的数据
char arr[ROW][COL] = { 0 };
//棋盘初始化
BoardInit(arr, ROW, COL);
//打印棋盘
BoardPrint(arr, ROW, COL);
char ch = 0;
while (1)
{
//玩家下棋
PlayerMove(arr, ROW, COL);
//打印棋盘
BoardPrint(arr, ROW, COL);
//判断输赢
ch = IsWin(arr, ROW, COL);
if (ch != 'C')
break;
//电脑下棋
ComputerMove(arr, ROW, COL);
//打印棋盘
BoardPrint(arr, ROW, COL);
//判断输赢
ch = IsWin(arr, ROW, COL);
if (ch != 'C')
break;
}
if (ch == '*')
printf("直接拿下!\n");
else if (ch == '#')
printf("你竟然打不过人机!\n");
else
printf("平局,得加油啊!\n");
}
int main()
{
int input = 0;
//设置随机数种子
srand((unsigned int)time(NULL));
do {
//菜单
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input) {
case 1:
//玩游戏
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("输入错误,请重新输入!\n");
break;
}
} while (input);
return 0;
}
game.c:游戏功能的实现
#define _CRT_SECURE_NO_WARNINGS 1
//自定义头文件的包含
#include"game.h"
//函数的定义
//棋盘初始化
void BoardInit(char arr[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
arr[i][j] = ' ';
}
}
}
//打印棋盘
void BoardPrint(char arr[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
//打印分割竖向分割
for (j = 0; j < col; j++)
{
printf(" %c ", arr[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 PlayerMove(char arr[ROW][COL], int row, int col)
{
//获取玩家坐标
int x = 0;
int y = 0;
printf("玩家下棋\n");
while (1)
{
printf("请输入坐标:>");
scanf("%d %d", &x, &y);
//判断坐标合法性
if ((x >= 1 && x <= row) && (y >= 1 && y <= col))
{
//把玩家坐标对应数组下标
x -= 1;
y -= 1;
//判断坐标是否被占用
if (arr[x][y] == ' ')
{
arr[x][y] = '*'; //假设玩家为*号
break;
}
else
{
printf("该坐标已被占用\n");
}
}
else
{
printf("坐标非法\n");
}
}
}
//电脑下棋
void ComputerMove(char arr[ROW][COL], int row, int col)
{
printf("电脑下棋\n");
while (1)
{
//在主函数生成种子srand
//随机生成范围内的坐标
int x = rand() % row;
int y = rand() % col;
//判断坐标是否被占用
if (arr[x][y] == ' ')
{
arr[x][y] = '#'; //假设电脑为#号
break;
}
}
}
//判断输赢
char IsWin(char board[ROW][COL], int row, int col)
{
/*
* 约定返回*代表玩家赢
* 返回#代表电脑赢
* 返回D代表平局
* 返回C代表继续
*/
int i = 0;
int j = 0;
//判断行
for (i = 0; i < row; i++)
{
int count = 0; //标记相同棋子的个数
for (j = 0; j < col - 1; j++)
{
if (board[i][j] == board[i][j + 1] && board[i][j] != ' ')
count++;
}
if (count == col - 1) //一次判断有两个棋子
return board[i][j];
}
//判断列
for (i = 0; i < col; i++)
{
int count = 0;
for (j = 0; j < row - 1; j++)
{
if (board[j][i] == board[j + 1][i] && board[j][i] != ' ')
{
count++;
}
}
if (count == row - 1)
return board[j][i];
}
//判断两条斜边
//第一条
int count = 0;
for (i = 0, j = 0; i < row - 1 && j < col - 1; i++, j++)
{
if (board[i][j] == board[i + 1][j + 1] && board[i][j] != ' ')
count++;
}
if (count == row - 1)
return board[i][j];
//第二条
count = 0; //把count重新置为0(易错)
for (i = 0, j = col - 1; i < row && j >= 0; i++, j--)
{
if (board[i][j] == board[i + 1][j - 1] && board[i][j] != ' ')
count++;
}
if (count == row - 1)
return board[i][j];
//判断棋盘是否满了
if (IsFull(board, row, col))
{
return 'D';
}
//如果上述情况都没有返回,游戏继续
return 'C';
}
//判断棋盘是否满了
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; //有空格就返回0
}
}
return 1;
}
void BoardInit(char arr[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
arr[i][j] = ' ';
}
}
}
void BoardPrint(char arr[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
//打印分割竖向分割
for (j = 0; j < col; j++)
{
printf(" %c ", arr[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 PlayerMove(char arr[ROW][COL], int row, int col)
{
//获取玩家坐标
int x = 0;
int y = 0;
printf("玩家下棋\n");
while (1)
{
printf("请输入坐标:>");
scanf("%d %d", &x, &y);
//判断坐标合法性
if ((x >= 1 && x <= row) && (y >= 1 && y <= col))
{
//把玩家坐标对应数组下标
x -= 1;
y -= 1;
//判断坐标是否被占用
if (arr[x][y] == ' ')
{
arr[x][y] = '*'; //假设玩家为*号
break;
}
else
{
printf("该坐标已被占用\n");
}
}
else
{
printf("坐标非法\n");
}
}
}
void ComputerMove(char arr[ROW][COL], int row, int col)
{
printf("电脑下棋\n");
while (1)
{
//在主函数生成种子srand
//随机生成范围内的坐标
int x = rand() % row;
int y = rand() % col;
//判断坐标是否被占用
if (arr[x][y] == ' ')
{
arr[x][y] = '#'; //假设电脑为#号
break;
}
}
}
char IsWin(char board[ROW][COL], int row, int col)
{
/*
* 约定返回*代表玩家赢
* 返回#代表电脑赢
* 返回D代表平局
* 返回C代表继续
*/
int i = 0;
int j = 0;
//判断行
for (i = 0; i < row; i++)
{
int count = 0; //标记相同棋子的个数
for (j = 0; j < col - 1; j++)
{
if (board[i][j] == board[i][j + 1] && board[i][j] != ' ')
count++;
}
if (count == col - 1) //一次判断有两个棋子
return board[i][j];
}
//判断列
for (i = 0; i < col; i++)
{
int count = 0;
for (j = 0; j < row - 1; j++)
{
if (board[j][i] == board[j + 1][i] && board[j][i] != ' ')
{
count++;
}
}
if (count == row - 1)
return board[j][i];
}
//判断两条斜边
//第一条
int count = 0;
for (i = 0, j = 0; i < row - 1 && j < col - 1; i++, j++)
{
if (board[i][j] == board[i + 1][j + 1] && board[i][j] != ' ')
count++;
}
if (count == row - 1)
return board[i][j];
//第二条
count = 0; //把count重新置为0(易错)
for (i = 0, j = col - 1; i < row && j >= 0; i++, j--)
{
if (board[i][j] == board[i + 1][j - 1] && board[i][j] != ' ')
count++;
}
if (count == row - 1)
return board[i][j];
//判断棋盘是否满了
if (IsFull(board, row, col))
{
return 'D';
}
//如果上述情况都没有返回,游戏继续
return 'C';
}
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; //有空格就返回0
}
}
return 1;
}