代码呈现
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 board[ROW][COL] = { 0 };
InitBoard(board, ROW, COL);//下棋--初始化棋盘
DisplayBoard(board,ROW,COL);//显示棋盘
char ret = 0;
ret = is_win(board, ROW, COL);
while (1)
{
player_move(board, ROW, COL);//玩家下
DisplayBoard(board, ROW, COL);//显示棋盘
ret = is_win(board, ROW, COL);
if (ret != 'C')
{
break;
}
computer_move(board, ROW, COL);//玩家下
DisplayBoard(board, ROW, COL);//显示棋盘
ret = is_win(board, ROW, COL);//判断输赢
if (ret != 'C')
{
break;
}
}
if (ret == '*')
{
printf("玩家赢\n");
}
else if (ret == '#')
{
printf("电脑赢\n");
}
else
{
printf("平局");
}
}
void test()
{
int input = 0;
srand((unsigned int)time(NULL));//时间戳
do
{
menu();
printf("请选择>:");
scanf("%d", &input);
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 游戏的实现逻辑
//实现游戏的各个函数
#define _CRT_SECURE_NO_WARNINGS 1
#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] = ' ';//双引号会有bug,单引号没有,试试看吧~~
}
}
}
//展示棋盘格子
void DisplayBoard(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++)
{
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 player_move(char board[ROW][COL], int row, int col)
{
printf("玩家下棋>:");
int x = 0;
int y = 0;
while (1)
{
scanf("%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("该坐标被占用,请重新输入!\n");
}
}
else
{
printf("坐标非法,请重新输入!\n");
}
}
}
//电脑下棋
void computer_move(char board[ROW][COL], int row, int col)
{
printf("电脑下棋>\n");
int x = 0;
int y = 0;
x = rand() % ROW;
y = rand() % COL;
while (1)
{
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
int is_full(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;
}
//判断输赢
//返回'*'玩家赢,返回'#'电脑赢,返回'Q'平局,返回'C'游戏继续
char is_win(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[2][0] == board[1][1] && board[1][1] == board[0][2] && board[1][1] != ' ')
{
return board[1][1];
}
//判断平局
if (1 == is_full(board, row, col))
{
return 'Q';
}
return 'C';
}
//game.h 游戏实现函数的声明
//在每个源文件都需要引用这个头文件
#pragma once
#define ROW 3
#define COL 3
#include <stdlib.h>
#include <stdio.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 player_move(char board[ROW][COL], int row, int col);//玩家下棋
void computer_move(char board[ROW][COL], int row, int col);//电脑下棋
char is_win(char board[ROW][COL], int row, int col);//判断输赢
注意提示
1、需用到两个源文件加一个头文件
2、函数定义、函数声明、函数使用的参数须保持一致,否则会报错
3、使用宏定义是为了方便修改棋盘的大小
4、电脑随机下棋用时间戳生成随机数:srand((unsigned int)time(NULL));//时间戳