【C语言】3子棋游戏,

最近写了一个三子棋的游戏,然后我们来看一下。

主函数:

int main()
{	
	int start = 1;
	char lchess = 0;
	char cchess = 0;
	char chess[5][11];
while(start)
{	
	int n = 1;
	int p = 1;
	start = 1;
	printf("---------------开始游戏-------------\n");
	for(cchess = 0;cchess < 5;cchess++)
	{
		for(lchess = 0;lchess < 11;lchess++)
		{
			if(!(cchess % 2))
			{
				if(lchess == 3||lchess == 7)
					chess[cchess][lchess] = '|';
				else
					chess[cchess][lchess] = ' ';
			}
			else
			{
				if(lchess == 3||lchess == 7)
					chess[cchess][lchess] = '|';
				else
					chess[cchess][lchess] = '-';
			}
		}
	}
while(n)
	{
		print(chess);
		playChess(chess,&p);
		if(p == 1)
			computerChess(chess);
		check(chess,&n);
	}
	print(chess);
	printf("请问是否继续游戏:是1,否0\n");
	scanf("%d",&start);
}
	return 0;
}

玩家走棋:

int playChess(char ch[5][11],int *p)
{	
	int i = 0;
	int j = 0;
	*p = 1;
	printf("请走棋");
	scanf("%d %d",&i,&j);
	if(i <= 0||i > 3)
	{
		printf("输入错误\n");
		*p = 0;
		return 0;
	}
	if(j <= 0||j > 3)
	{	
		printf("输入错误\n");
		*p = 0;
		return 0;
	}
	i = (i-1)*2;
	switch(j)
	{
	case 1:
		if(ch[i][1] != 'X'||ch[i][1] != '0')
			ch[i][1] = 'X';
		break;
	case 2:
		if(ch[i][5] != 'X'||ch[i][5] != '0')
			ch[i][5] = 'X';
		break;
	case 3:
		if(ch[i][9] != 'X'||ch[i][9] != '0')
			ch[i][9] = 'X';
		break;
	default:
		break;	
	}
	return 0;
}

电脑走棋:如果想实现较高的AI的话,可以自己一一吧算法推导,然后添加进去,我就进行了简单的棋子填充:

void computerChess(char ch[5][11])
{	
	int i = 0;
	int j = 0;
	for(i = 0;i < 5;i+=2)
	{
		for(j = 1;j < 11;j+=4)
		{
			if(ch[i][j] == 'X'||ch[i][j] == '0'	)
				continue;
			else 
				ch[i][j] = '0';
				return;
		}
	}
}

胜率判断:

int check(char ch[5][11],int *n)
{	
	int *p = n;
	int i = 0;
	int j = 0;
	int count = 0;
	for(i = 0;i < 5;i+=2)
	{
		for(j = 1;j < 11;j+=4)
		{
			if(ch[i][j] == 'X'||ch[i][j] == '0'	)
				count++;
		}
	}
	if(count == 9)
	{
		printf("棋盘已满平局,游戏结束");
		*p = 0;
	}
	for(i = 0;i < 5;i+=2)
	{
		if(ch[i][1] == ch[i][5]&&ch[i][5] == ch[i][9]&&ch[i][1] != ' ')
		{
			if(ch[i][1] == 'X')
				printf("玩家获胜\n");
			else
				printf("电脑获胜\n");
			*p = 0;
		}
	}
	for(j = 1;j < 11;j += 4)
	{
		if(ch[0][j] == ch[2][j]&&ch[2][j] ==ch[4][j]&&ch[0][j] != ' ')
		{
			if(ch[0][j] == 'X')
				printf("玩家获胜\n");
			else
				printf("电脑获胜\n");
			*p = 0;
		}

	}
	if(ch[0][1] == ch[2][5]&& ch[2][5] == ch[4][9]&&ch[0][1] != ' ')
	{
			if(ch[0][1] == 'X')
				printf("玩家获胜\n");
			else
				printf("电脑获胜\n");
			*p = 0;

	}
	if(ch[4][1] == ch[2][5] &&ch[2][5]== ch[0][9]&&ch[0][9] != ' ')
	{
			if(ch[4][1] == 'X')
				printf("玩家获胜\n");
			else
				printf("电脑获胜\n");
			*p = 0;
	}
	
	return 0;
}


大概就这样,代码很简单,有兴趣可以自己复制试试,注意加上头文件还有函数的声明。也可以自己写一个头文件函数调用,这样调理比较清楚一点。

然后在附加一个strstr()函数的重写:

#include <stdio.h>

char *mystrstr(const char *s1,const char *s2)
{
	char *spd = (char *) s1;
    if(*s1==0)	//s1 = 0		
    {
        if(*s2)			//s2 = 0
            return (char*)NULL;
        return (char*)s1;
    }
    while(*spd)
    {
        int i=0;
        while(1)
        {
            if(s2[i]==0)
                return spd;
            if(s2[i]!=spd[i])
                break;				//完全遍历,d e n den ,den den,输入前面。子字符大于父字符则寻找不到
            i++;
        }
        spd++;
    }
    return (char*)NULL;
}


本文出自 “剩蛋君” 博客,请务必保留此出处http://memory73.blog.51cto.com/10530560/1678872

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值