一、创建游戏初始界面
三子棋,也就是井字棋。构思游戏大致模块,再一一实现每个函数,首先创建test.c用于创建初始界面
1.1 进入游戏
int main()
{
int input = 0;
do {
menu();
printf("请选择->");
scanf("%d", &input);
switch (input)
{
case 1:
printf("开始游戏\n");
game();
break;
case 0:printf("退出游戏\n");
break;
default:
printf("选择错误,请重新选择\n");
break;
}
} while (input);
return 0;
}
1.2游戏菜单
void menu()
{
printf("*****************************\n");
printf("****** 1. play ******\n");
printf("****** 0. exit ******\n");//0为假,用于终止循环
printf("*****************************\n");
}
1.3游戏的实现
void game()
{
char board[ROW][COL] = { 0 };//创建一个数组用于存放玩家和电脑下的棋
InitBoard(board, ROW, COL);//初始化棋盘
DisplayBoard(board, ROW, COL);//显示棋盘
char ret;
while (1)
{
PlayerMove(board, ROW, COL);//玩家下棋
DisplayBoard(board, ROW, COL);
ret = is_win(board, ROW, COL);
if (ret != 'C')//判断棋盘是否满
break;
ComputerMove(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 if (ret == 'Q')
printf("平局\n");
}
二、创建存放头文件的文件
创建game.h用于存放头文件并且声明自定义函数
#define ROW 3
#define COL 3
#include <stdio.h>
#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);
char is_win(char board[ROW][COL], int row, int col);
三、游戏主体的实现
创建game.c用于实现游戏主体
3.1 初始化棋盘函数
void InitBoard(char board[ROW][COL], int row, int col)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
board[i][j] = ' ';
}
}
//或者memset(&board[0][0],' ',row*col*sizeof(board[0][0]));(注意需要包含头文件string.h)
}
3.2打印棋盘函数
void DisplayBoard(char board[ROW][COL], int row, int col)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
printf(" %c ", board[i][j]);
if (j < col - 1)
printf("|");//打印分割列
}
printf("\n");
if (i < row - 1)
for (int j = 0; j < col; j++)
{
printf("---");//打印分割行
if (j < col - 1)
printf("|");
}
printf("\n");
}
}
3.3 玩家下棋函数
void PlayerMove(char board[ROW][COL], int row, int col)
{
int x = 0, y = 0;
printf("玩家下棋\n");
printf("请输入坐标->");
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] = '*';
else
printf("坐标被占用,请重新输入\n");
}
else
printf("坐标非法,请重新输入\n");
}
3.4 电脑下棋函数
由于电脑下棋是随机的,因此需要用rand函数实现,而rand函数和srand配套使用,后面会把srand函数放置在主函数中
void ComputerMove(char board[ROW][COL], int row, int col)
{
int x = 0, y = 0;
printf("电脑下棋->\n");
while (1)//如果生成的随机位置未被占用,电脑下棋,否则重新生成
{
x = rand() % row;
y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
3.5 判断输赢函数
char is_win(char board[ROW][COL], int row, int col)
//玩家赢,返回'*'
//电脑赢,返回'#'
//平局,返回'Q'
//继续,返回'C'
{
for (int i = 0; i < row; i++)//判断行
{
if (board[i][0] == board[i][1] && board[i][2] == board[i][1] && board[i][0] != ' ')
return board[i][0];
}
for (int i = 0; i < col; i++)//判断列
{
if (board[0][i] == board[1][i] && board[2][i] == board[1][i] && board[0][i] != ' ')
return board[i][i];
}
if ((board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ') || (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != ' '))
//判断对角线
return board[1][1];
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
if (board[i][j] == ' ')
{
return 'C';
}
}
}
return 'Q';
}
四、完整代码
4.1 test.c文件
#define _CRT_SECURE_NO_WARNINGS
#include "game.h"
void menu()
{
printf("*****************************\n");
printf("****** 1. play ******\n");
printf("****** 0. exit ******\n");//0为假,用于终止循环
printf("*****************************\n");
}
void game()
{
char board[ROW][COL] = { 0 };
InitBoard(board, ROW, COL);//初始化棋盘
DisplayBoard(board, ROW, COL);//显示棋盘
char ret;
while (1)
{
PlayerMove(board, ROW, COL);//玩家下棋
DisplayBoard(board, ROW, COL);
ret = is_win(board, ROW, COL);
if (ret != 'C')//判断棋盘是否满
break;
ComputerMove(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 if (ret == 'Q')
printf("平局\n");
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));//时间戳生成随机数,srand头文件stdlib.h,time头文件time.h
do {
menu();
printf("请选择->");
scanf("%d", &input);
switch (input)
{
case 1:
printf("开始游戏\n");
game();
break;
case 0:printf("退出游戏\n");
break;
default:
printf("选择错误,请重新选择\n");
break;
}
} while (input);
return 0;
}
4.2 game.h文件
#define _CRT_SECURE_NO_WARNINGS
#define ROW 3
#define COL 3
#include <stdio.h>
#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);
char is_win(char board[ROW][COL], int row, int col);
4.3 game.c文件
#define _CRT_SECURE_NO_WARNINGS
#include "game.h"
void InitBoard(char board[ROW][COL], int row, int col)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
board[i][j] = ' ';
}
}
//或者memset(&board[0][0],' ',row*col*sizeof(board[0][0]));(注意需要包含头文件string.h)
}
void DisplayBoard(char board[ROW][COL], int row, int col)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
printf(" %c ", board[i][j]);
if (j < col - 1)
printf("|");//打印分割列
}
printf("\n");
if (i < row - 1)
for (int 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, y = 0;
printf("玩家下棋\n");
printf("请输入坐标->");
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] = '*';
else
printf("坐标被占用,请重新输入\n");
}
else
printf("坐标非法,请重新输入\n");
}
void ComputerMove(char board[ROW][COL], int row, int col)
{
int x = 0, y = 0;
printf("电脑下棋->\n");
while (1)//如果生成的随机位置未被占用,电脑下棋,否则重新生成
{
x = rand() % row;
y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
char is_win(char board[ROW][COL], int row, int col)
//玩家赢,返回'*'
//电脑赢,返回'#'
//平局,返回'Q'
//继续,返回'C'
{
for (int i = 0; i < row; i++)//判断行
{
if (board[i][0] == board[i][1] && board[i][2] == board[i][1] && board[i][0] != ' ')
return board[i][0];
}
for (int i = 0; i < col; i++)//判断列
{
if (board[0][i] == board[1][i] && board[2][i] == board[1][i] && board[0][i] != ' ')
return board[i][i];
}
if ((board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ') || (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != ' '))
//判断对角线
return board[1][1];
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
if (board[i][j] == ' ')
{
return 'C';
}
}
}
return 'Q';
}