简易三子棋

本文档展示了如何创建一个3x3的三子棋游戏,包括头文件定义、游戏逻辑实现、玩家与电脑的下棋策略以及游戏结果判断。玩家和电脑轮流下棋,电脑具有简单的判断策略以阻止玩家获胜。游戏结束时会根据棋盘状态判断胜负或和局。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先创建一个utili.h头文件,包含编写代码时所需要的头文件。

//防止头文件重复引入
#ifndef _UTILI_H_
#define _UTILI_H_

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

#endif // !_UTILI_H_

再创建game.h头文件,声明进行游戏所需的函数,以及游戏棋盘大小,棋子的定义。利用二维数组,创建一个3x3的棋盘。定义玩家棋子为’x‘,电脑棋子为’o‘。

#ifndef _GAME_H_
#define _GAME_H_

#include"utili.h"

#define ROW 3
#define COL 3

#define PLAYER_CHESS_SYMBAL 'x'
#define COMPUTER_CHESS_SYMBAL 'o'

//函数声明
void startGame();										//开始游戏
void initChessBoard(char chessBoard[ROW][COL]);			//初始化棋盘
void displayChessBoard(char chessBoard[ROW][COL]);		//显示棋盘
void playerMove(char chessBoard[ROW][COL]);				//玩家下棋
void computerMove(char chessBoard[ROW][COL]);			//电脑下棋
char CheckResult(char chessBoard[ROW][COL]);			//判断结果
int computerJudge(char chessBoard[ROW][COL]);			//电脑判断

#endif // !_GAME_H_

创建game.c源文件,编写进行游戏所需函数

初始化棋盘,将二维数组中的元素置为空格

void initChessBoard(char chessBoard[ROW][COL])
{
	for (int i = 0; i < ROW; ++i)
	{
		for (int j = 0; j < COL; ++j)
			chessBoard[i][j] = ' ';
	}
}

显示棋盘

void displayChessBoard(char chessBoard[ROW][COL])
{
	for (int i = 0; i < ROW; ++i)
	{
		printf("|  %c  |  %c  |  %c  |\n", chessBoard[i][0],
										   chessBoard[i][1],
										   chessBoard[i][2]);
		if (i < ROW - 1)
			printf("  ---   ---   --- \n");
	}
}

玩家下棋,玩家通过输入数组下标来进行下棋。函数应当对玩家输入的下标进行判断,判断是否该位置已被占用,或者输入的下标超出界限。

void playerMove(char chessBoard[ROW][COL])
{
	int r, c;
	printf("玩家下棋\n");
	printf("输入位置坐标:");
	while (1)
	{
		scanf("%d%d", &r, &c);
		if (r < 0 || r >= ROW || c < 0 || c >= COL)
		{
			printf("输入有误,请重新输入:\n");
			continue;
		}
		if (chessBoard[r][c] != ' ')
		{
			printf("该位置已被占用,请选择其他位置\n");
			continue;
		}
		chessBoard[r][c] = PLAYER_CHESS_SYMBAL;
		break;
	}
}

电脑下棋,通过随机数来实现电脑下棋。

这里增加一个电脑判断函数,使电脑可以根据玩家下棋情况来进行下棋,若玩家还差一颗棋子就胜出时,电脑优先选择这颗棋子位置,不让玩家胜出,函数返回1;若不符合上述情况,函数返回0。

int computerJudge(char chessBoard[ROW][COL])
{
	//行判断
	for (int i = 0; i < ROW; ++i)
	{
		if ((chessBoard[i][0] == chessBoard[i][1]) && chessBoard[i][2] == ' ' && chessBoard[i][0] == PLAYER_CHESS_SYMBAL)
		{
			chessBoard[i][2] = COMPUTER_CHESS_SYMBAL;
			return 1;
		}
		if ((chessBoard[i][0] == chessBoard[i][2]) && chessBoard[i][1] == ' ' && chessBoard[i][0] == PLAYER_CHESS_SYMBAL)
		{
			chessBoard[i][1] = COMPUTER_CHESS_SYMBAL;
			return 1;
		}
		if ((chessBoard[i][1] == chessBoard[i][2]) && chessBoard[i][0] == ' ' && chessBoard[i][1] == PLAYER_CHESS_SYMBAL)
		{
			chessBoard[i][0] = COMPUTER_CHESS_SYMBAL;
			return 1;
		}
	}
	//列判断
	for (int i = 0; i < COL; ++i)
	{
		if ((chessBoard[0][i] == chessBoard[1][i]) && chessBoard[2][i] == ' ' && chessBoard[0][i] == PLAYER_CHESS_SYMBAL)
		{
			chessBoard[2][i] = COMPUTER_CHESS_SYMBAL;
			return 1;
		}
		if ((chessBoard[0][i] == chessBoard[2][i]) && chessBoard[1][i] == ' ' && chessBoard[0][i] == PLAYER_CHESS_SYMBAL)
		{
			chessBoard[1][i] = COMPUTER_CHESS_SYMBAL;
			return 1;
		}
		if ((chessBoard[1][i] == chessBoard[2][i]) && chessBoard[0][i] == ' ' && chessBoard[0][i] == PLAYER_CHESS_SYMBAL)
		{
			chessBoard[0][i] = COMPUTER_CHESS_SYMBAL;
			return 1;
		}
	}
	//对角线判断
	if ((chessBoard[1][1] == chessBoard[0][0]) && chessBoard[2][2] == ' ' && chessBoard[1][1] == PLAYER_CHESS_SYMBAL)
	{
		chessBoard[2][2] = COMPUTER_CHESS_SYMBAL;
		return 1;
	}
	if ((chessBoard[1][1] == chessBoard[2][2]) && chessBoard[0][0] == ' ' && chessBoard[1][1] == PLAYER_CHESS_SYMBAL)
	{
		chessBoard[0][0] = COMPUTER_CHESS_SYMBAL;
		return 1;
	}
	if ((chessBoard[0][0] == chessBoard[2][2]) && chessBoard[1][1] == ' ' && chessBoard[0][0] == PLAYER_CHESS_SYMBAL)
	{
		chessBoard[1][1] = COMPUTER_CHESS_SYMBAL;
		return 1;
	}
	if ((chessBoard[1][1] == chessBoard[0][2]) && chessBoard[2][0] == ' ' && chessBoard[1][1] == PLAYER_CHESS_SYMBAL)
	{
		chessBoard[2][0] = COMPUTER_CHESS_SYMBAL;
		return 1;
	}
	if ((chessBoard[1][1] == chessBoard[2][0]) && chessBoard[0][2] == ' ' && chessBoard[1][1] == PLAYER_CHESS_SYMBAL)
	{
		chessBoard[0][2] = COMPUTER_CHESS_SYMBAL;
		return 1;
	}
	if ((chessBoard[0][2] == chessBoard[2][0]) && chessBoard[1][1] == ' ' && chessBoard[0][2] == PLAYER_CHESS_SYMBAL)
	{
		chessBoard[1][1] = COMPUTER_CHESS_SYMBAL;
		return 1;
	}
	//不满足上述判断
	return 0;
}
void computerMove(char chessBoard[ROW][COL])
{
	int r, c;
	srand(time(NULL));
	if (!computerJudge(chessBoard))
	{
		while (1)
		{
			r = rand() % ROW;
			c = rand() % COL;
			if (chessBoard[r][c] != ' ')
			{
				continue;
			}
			chessBoard[r][c] = COMPUTER_CHESS_SYMBAL;
			break;
		}
	}
}

判断结果。玩家胜出,返回’x‘;电脑胜出,返回’o‘;和棋,返回’h‘;继续下棋,返回’c‘。

char CheckResult(char chessBoard[ROW][COL])
{
	//检查行
	for (int i = 0; i < ROW; ++i)
	{
		if (chessBoard[i][0] == chessBoard[i][1] && chessBoard[i][1] == chessBoard[i][2])
		{
			if (chessBoard[i][0] == PLAYER_CHESS_SYMBAL)
				return 'x';
			if (chessBoard[i][0] == COMPUTER_CHESS_SYMBAL)
				return 'o';
		}
	}
	//检查列
	for (int i = 0; i < COL; ++i)
	{
		if (chessBoard[0][i] == chessBoard[1][i] && chessBoard[1][i] == chessBoard[2][i])
		{
			if (chessBoard[0][i] == PLAYER_CHESS_SYMBAL)
				return 'x';
			if (chessBoard[0][i] == COMPUTER_CHESS_SYMBAL)
				return 'o';
		}
	}
	//检查对角线
	if (chessBoard[0][0] == chessBoard[1][1] && chessBoard[1][1] == chessBoard[2][2])
	{
		if (chessBoard[0][0] == PLAYER_CHESS_SYMBAL)
			return 'x';
		if (chessBoard[0][0] == COMPUTER_CHESS_SYMBAL)
			return 'o';
	}
	if (chessBoard[0][2] == chessBoard[1][1] && chessBoard[1][1] == chessBoard[2][0])
	{
		if (chessBoard[0][2] == PLAYER_CHESS_SYMBAL)
			return 'x';
		if (chessBoard[0][2] == COMPUTER_CHESS_SYMBAL)
			return 'o';
	}
	//检查是否和棋
	int count = 0;
	for (int i = 0; i < ROW; ++i)
	{
		for (int j = 0; j < COL; ++j)
		{
			if (chessBoard[i][j] != ' ')
				++count;
		}
	}
	if (count == ROW * COL)
		return 'h';
	//继续
	return 'c';
}

开始游戏。将前面的函数结合在一起,实现游戏的运行。

这里用一个随机数first,来决定谁先下棋。

void startGame()
{
	char chessBoard[ROW][COL];
	srand(time(NULL));
	//给出随机数first,决定谁先下棋
	int first = rand() % 2 + 1;
	//初始化棋盘
	initChessBoard(chessBoard);
	char winner = '\0';
	if (first % 2 == 0)
	{
		printf("电脑先下棋\n");
		computerMove(chessBoard);
		displayChessBoard(chessBoard);
	}
	else
	{
		printf("玩家先下棋\n");
		//显示棋盘
		displayChessBoard(chessBoard);
	}
	while (1)
	{
		//玩家下棋
		playerMove(chessBoard);
		winner = CheckResult(chessBoard);
		if (winner == 'x' || winner == 'h')
			break;
		//电脑下棋
		computerMove(chessBoard);
		//显示棋盘
		displayChessBoard(chessBoard);
		winner = CheckResult(chessBoard);
		if (winner == 'o' || winner == 'h')
			break;
	}
	if (winner == 'x')
		printf("玩家赢\n");
	else
		if (winner == 'o')
			printf("电脑赢\n");
		else
			if (winner == 'h')
				printf("和棋\n");
			else printf("程序出错\n");
	printf("---------------------------------------\n");
	printf("是否进行下一句游戏?\n");
}

最后,创建main.c源文件,进行菜单编写,开始游戏以及退出。

#include"game.h"

int main()
{
	int select = 0;
	while (1)
	{
		printf("************************\n");
		printf("*  简  易  三  子  棋  *\n");
		printf("************************\n");
		printf("*         1.play       *\n");
		printf("*         0.exit       *\n");
		printf("************************\n");
		printf("请选择:");
		scanf("%d", &select);
		switch (select)
		{
		case 0:exit(0);
		case 1:startGame();
			break;
		default:printf("输入错误,请重新输入\n");
			break;
		}
	}
	return 0;
}

运行结果:

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值