三子棋游戏(c语言实现)

</pre>一说到写个三子棋游戏,首先我们得想到有个棋盘初始化和棋盘打印函数。<p></p><p>接下来,玩游戏阶段,人玩游戏,电脑玩游戏,各写一个函数,每次下完棋都得判断有没有人赢了游戏,若有人赢了,游戏退出,否则继续游戏。在这过程中,还有可能棋盘满</p><p>了,所以写个判断棋盘是否满的的函数,棋盘满了,程序结束,没人赢,此时就平局。</p><p>当然了,还有一些辅助函数,比如game函数,print_manu函数,只是起到封装的作用,也防止main函数过长。</p><p>程序亮点:巧妙地运用了逗号表达式(在电脑下棋游戏中),但是,得用个计数器,while语句执行一次就好,不然电脑就耍赖了。</p><p>                   程序有一定的容错性。</p><p>程序缺陷:游戏只能一次性玩一局。</p><p>                   只写了人机游戏,没有人人游戏。这个需要之后继续实现。</p><p> <span style="background-color:rgb(255,102,102)"> game.c文件</span></p><pre name="code" class="objc">#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"

int chess_full(char chessBoard[3][3])//棋盘判满函数
{
	int i = 0;
	int j = 0;
	for (i = 0;i < 3;i++)
	{
		for (j = 0;j < 3;j++)
		{
			if (chessBoard[i][j] == ' ')
				return 0;
		}
	}
	return 1;
}
void init(char chessBoard[3][3])//棋盘初试化函数
{
	int i = 0;
	int j = 0;
	for (i = 0; i < 3; i++)
	{
		for (j = 0; j < 3; j++)
		{
			chessBoard[i][j] = ' ';
		}
	}

	distchessBoard(chessBoard);
}
void distchessBoard(char chessBoard[3][3])//打印棋盘函数
{
	int i = 0;
	for (i = 0;i < 3;i++)//打印棋盘
	{
		printf(" %c | %c | %c \n", chessBoard[i][0], chessBoard[i][1], chessBoard[i][2]);
		if (i != 2)
			printf("---|---|---\n");//pchessBoard
	}
}
void manPlay(char chessBoard[3][3])//人下棋函数
{
	if (chess_full(chessBoard) == 1)
	{
		exit(0);
	}
	int line = 0;
	int column = 0;
	do
	{
		printf("请输入你的棋子的位置(注意最小下标是0哦):");
		scanf("%d%d", &line, &column);
		while (chessBoard[line][column] != ' ')
			if ((line < 0) || (line > 2) || (column < 0) || (column > 2))
			{
				printf("对不起,您输入的是无效的位置!");
			}
		chessBoard[line][column] = 'X';
	} while (chessBoard[line][column] == ' ');
	distchessBoard(chessBoard);
}
void pcplay(char chessBoard[3][3])//电脑下棋函数
{
	if (chess_full(chessBoard) == 1)
	{
		exit(0);
	}
	printf("电脑下棋中...\n");
	int line = 0;
	int column = 0;
	int count = 0;
	while (line = rand() % 3, column = rand() % 3, chessBoard[line][column] == ' ')
	{
		count++;
		chessBoard[line][column] = 'Y';
		if (count == 1)
		{
			break;
		}
	}
	distchessBoard(chessBoard);
}
int judge(char chessBoard[3][3])
{
	int i = 0;
	if ((chessBoard[0][0] == chessBoard[1][1]) && (chessBoard[1][1] == chessBoard[2][2]))
	{
		if (chessBoard[1][1] == 'X')
		{
			printf("玩家赢了\n");
			return 1;
		}
		if (chessBoard[1][1] == 'Y')
		{
			printf("对方赢了\n");
			return 1;
		}
	}
	if ((chessBoard[0][2] == chessBoard[1][1]) && (chessBoard[1][1] == chessBoard[2][0]))
	{
		if (chessBoard[1][1] == 'X')
		{
			printf("玩家赢了\n");
			return 1;
		}
		if (chessBoard[1][1] == 'Y')
		{
			printf("对方赢了\n");
			return 1;
		}
	}
	for (i = 0;i < 3;i++)
	{
		if ((chessBoard[i][1] == chessBoard[i][0]) && (chessBoard[i][1] == chessBoard[i][2]))
		{
			if (chessBoard[i][1] == 'X')
			{
				printf("玩家赢了\n");
				return 1;
			}
			if (chessBoard[i][1] == 'Y')
			{
				printf("对方赢了\n");
				return 1;
			}
		}

	}
	for (i = 0;i < 3;i++)
	{
		if ((chessBoard[0][i] == chessBoard[1][i]) && (chessBoard[1][i] == chessBoard[2][i]))
		{
			if (chessBoard[0][i] == 'X')
			{
				printf("玩家赢了\n");
				return 1;
			}
			if (chessBoard[0][i] == 'Y')
			{
				printf("对方赢了\n");
				return 1;
			}
		}
	}
	return 0;
}
void game(char chessBoard[3][3])
{
	//char chessBoard[3][3];
	int i = 0;
	int ret = 0;
	init(chessBoard);//调用初始化函数
	while ((chess_full(chessBoard) == 0))
	{
		manPlay(chessBoard);
		pcplay(chessBoard);
		ret = judge(chessBoard);
		if (ret == 1)
			break;
	}
	if (ret == 0)
	{
		printf("平局\n");
	}
}

void print_manu()
{
	printf("----------------欢迎进入三子棋系统-------------------\n");
	printf("*****************************************************\n");
	printf("**********************1.play*************************\n");
	printf("**********************2.exit*************************\n");
	printf("*****************************************************\n");
}
<pre name="code" class="objc"><span style="background-color: rgb(255, 102, 102);">game.h文件</span>
 
</pre><pre name="code" class="objc">#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<malloc.h>
#include<time.h>
#include<stdlib.h>

int chess_full(char chessBoard[3][3]);//棋盘判满函数
void init(char chessBoard[3][3]);//棋盘初试化函数
void distchessBoard(char chessBoard[3][3]);//打印棋盘函数
void manPlay(char chessBoard[3][3]);//人下棋函数
void pcplay(char chessBoard[3][3]);//电脑下棋函数
int judge(char chessBoard[3][3]);//评判胜负函数
void game(char chessBoard[3][3]);//游戏函数
void print_manu();//打印菜单函数

 
test.c文件 
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"

int main()
{
	print_manu();
	char chessBoard[3][3];
	printf("请输入你的选择>");
	int choose = 0;
	scanf("%d", &choose);
	switch (choose)
	{
	case 1:
		game(chessBoard);
		break;
	case 0:
		exit(0);
		break;
	default:
		printf("input error");
	}
	system("pause");
	return 0;
}

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值