C语言 三子棋(含完整 代码详解)

三子棋是一种民间传统游戏,又叫九宫棋、圈圈叉叉、一条龙、井字棋等。将正方形对角线连起来,双方在3X3棋盘上轮流每次各下一子,一方将三个棋子连成一条直线或斜线便获胜。

我们使用VS2015进行编写

目录

utili.h

game.h

game.c

gamemain.c


系统头文件

utili.h

#ifndef _UTILI_H_
#define _UTILI_H_

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

#endif /* _UTILI_H_ */

游戏头文件声明

game.h

#ifndef _GAME_H_
#define _GAME_H_

#include"utili.h"

//开始游戏
void StartGame();

//初始化函数
void InitGame();

//显示棋盘
void ShowChessBoard();

//玩家下棋
void PlayerMove();

//电脑下棋
void ComputerMove();

//判断棋盘是否满
bool IsFullChessBoard();

//检查结果
char CheakResult();

#endif /* _GAME_H_ */

游戏各项功能实现函数

game.c

#include"game.h"

//宏定义
#define ROW 3
#define COL 3

//定义棋盘
static char chess_board[ROW][COL];

void InitGame()
{
	for (int i = 0; i<ROW; ++i)
	{
		for (int j = 0; j<COL; ++j)
		{
			chess_board[i][j] = ' ';
		}
	}
}

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

void PlayerMove()
{
	printf("玩家落子.\n");
	int x, y;
	while (1)
	{
		printf("请输入一组坐标(row col):>");
		scanf("%d %d", &x, &y);

		if (chess_board[x][y] == ' ')
		{
			chess_board[x][y] = 'x';
			break;
		}
		else
		{
			printf("输入的位置被占用,请重新输入.....\n");
		}
	}
}

void ComputerMove()
{
	int x, y;
	while (1)
	{
		srand(time(0));
		x = rand() % ROW;  //0 1 2
		y = rand() % COL;  //0 1 2

		if (chess_board[x][y] == ' ')
		{
			chess_board[x][y] = 'o';
			break;
		}
	}
}

bool IsFullChessBoard()
{
	for (int i = 0; i<ROW; ++i)
	{
		for (int j = 0; j<COL; ++j)
		{
			if (chess_board[i][j] == ' ')
				return false;
		}
	}
	return true;
}

//返回 x :玩家赢
//返回 o :电脑赢
//返回 c :继续
//返回 h :和棋
char CheakResult()
{
	//判断行
	for (int i = 0; i<ROW; ++i)
	{
		if (chess_board[i][0] != ' '
			&& chess_board[i][0] == chess_board[i][1]
			&& chess_board[i][0] == chess_board[i][2])
			return chess_board[i][0];
	}

	//判断列
	for (int i = 0; i<COL; ++i)
	{
		if (chess_board[0][i] != ' '
			&& chess_board[0][i] == chess_board[1][i]
			&& chess_board[0][i] == chess_board[2][i])
			return chess_board[0][i];
	}

	//判断斜线
	if (chess_board[1][1] != ' '
		&& chess_board[0][0] == chess_board[1][1]
		&& chess_board[0][0] == chess_board[2][2])
		return chess_board[1][1];
	if (chess_board[1][1] != ' '
		&& chess_board[0][2] == chess_board[1][1]
		&& chess_board[0][2] == chess_board[2][0])
		return chess_board[1][1];

	//和棋
	if (IsFullChessBoard())
		return 'h';

	//继续
	return 'c';
}

void StartGame()
{
	//1、初始化
	InitGame();

	//2、进入游戏
	char winner;
	while (1)
	{
		//3、显示棋盘
		ShowChessBoard();

		//4、玩家下棋
		PlayerMove();
		//5、检查结果
		winner = CheakResult();
		if (winner != 'c')
			break;

		//6、电脑下棋
		ComputerMove();
		//7、检查结果
		winner = CheakResult();
		if (winner != 'c')
			break;
	}

	//8、判断胜负
	ShowChessBoard();
	if (winner == 'x')
		printf("恭喜玩家,你赢了.\n");
	if (winner == 'o')
		printf("很遗憾,你输了.\n");
	if (winner == 'h')
		printf("势均力敌,是否继续游戏[Y/N].\n");
}

游戏主函数

gamemain.c

#include"game.h"

int main()
{
	int select = 1;
	while (select)
	{
		printf("*******************************\n");
		printf("*        简 易 三 子 棋       *\n");
		printf("*******************************\n");
		printf("*  [1] Play        [0] Exit   *\n");
		printf("*******************************\n");
		printf("请选择:>");
		scanf("%d", &select);

		if (select == 0)
			break;

		if (select != 1)
		{
			printf("输入有误,请重新输入.......\n");
			continue;
		}

		//开始游戏
		StartGame();
	}
	printf("************ GoodBye *********\n");
	return 0;
}

运行代码:

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值