三字棋的优化 电脑会堵棋电脑会向赢棋的方向走

接着上一篇说

由于上次的三字棋电脑不会堵棋 也不会向赢棋的方向走! 

为了让电脑不显得呆我们对代码进行了优化

相比上一篇三子棋来说 只动了game()和加了两段判断代码

首先在玩家下完棋之后我们判断一下此时电脑下棋会不会赢 如果会赢 电脑向会赢的那个点位下棋

然后返回一个2 ,不会赢 !电脑就不下棋 ,就返回一个1!代码如下

int computer_win(char playboard[ROW][COL], int row, int col)
{
	int x = 1;
	if (playboard[0][0] == playboard[0][1] && playboard[0][1] == '#')
	{
		playboard[0][2] = '#';
		x = 2;
	}
	if (playboard[0][2] == playboard[0][1] && playboard[0][1] == '#' )
	{
		playboard[0][0] = '#';
		x = 2;
	}
	if (playboard[1][0] == playboard[1][1] && playboard[1][1] == '#' )
	{
		playboard[1][2] = '#';
		x = 2;
	}
	if (playboard[1][1] == playboard[1][2] && playboard[1][1] == '#')
	{
		playboard[1][0] = '#';
		x = 2;
	}
	if (playboard[2][0] == playboard[2][1] && playboard[2][1] == '#')
	{
		playboard[2][2] = '#';
		x = 2;
	}
	if (playboard[2][2] == playboard[2][1] && playboard[2][1] == '#')
	{
		playboard[2][0] = '#';
		x = 2;
	}
	if (playboard[0][0] == playboard[1][1] && playboard[1][1] == '#')
	{
		playboard[2][2] = '#';
		x = 2;
	}
	if (playboard[1][1] == playboard[2][2] && playboard[1][1] == '#')
	{
		playboard[0][0] = '#';
		x = 2;
	}
	if (playboard[0][2] == playboard[1][1] && playboard[1][1] == '#')
	{
		playboard[2][0] = '#';
		x = 2;
	}
	if (playboard[1][1] == playboard[2][0] && playboard[1][1] == '#')
	{
		playboard[0][2] = '#';
		x = 2;
	}
	if (playboard[0][0] == playboard[0][2] && playboard[0][0] == '#')
	{
		playboard[0][1] = '#';
		x = 2;
	}
	if (playboard[0][0] == playboard[2][0] && playboard[0][0] == '#')
	{
		playboard[1][0] = '#';
		x = 2;
	}
	if (playboard[2][0] == playboard[2][2] && playboard[2][0] == '#')
	{
		playboard[2][1] = '#';
		x = 2;
	}
	if (playboard[2][2] == playboard[0][2] && playboard[2][2] == '#')
	{
		playboard[1][2] = '#';
		x = 2;
	}
	if (playboard[0][0] == playboard[2][2] && playboard[2][2] == '#')
	{
		playboard[1][1] = '#';
		x = 2;
	}
	if (playboard[0][2] == playboard[2][0] && playboard[2][0] == '#')
	{
		playboard[1][1] = '#';
		x = 2;
	}
	return x;
}

通过返回值来判断电脑是否会赢!  如果电脑赢了 ,那么电脑就走了一步棋,我们也就不用进行下一步 ,游戏结束!电脑没赢!那么电脑就没下棋!这个时候 我们判断电脑会不会输棋!那么代码如下

int computer_lose(char playboard[ROW][COL], int row, int col)
{
	int x = 1;
	if (playboard[0][0] == playboard[0][1] && playboard[0][1] == '*' && playboard[0][2] == ' ')
	{
		playboard[0][2] = '#';
		x = 2;
	}
	if (playboard[0][2] == playboard[0][1] && playboard[0][1] == '* ' && playboard[0][0] == ' ')
	{
		playboard[0][0] = '#';
		x = 2;
	}
	if (playboard[1][0] == playboard[1][1] && playboard[1][1] != ' ' && playboard[1][2] == ' ')
	{
		playboard[1][2] = '#';
		x = 2;
	}
	if (playboard[1][1] == playboard[1][2] && playboard[1][1] != ' ' && playboard[1][0] == ' ')
	{
		playboard[1][0] = '#';
		x = 2;
	}
	if (playboard[2][0] == playboard[2][1] && playboard[2][1] != ' ' && playboard[2][2] == ' ')
	{
		playboard[2][2] = '#';
		x = 2;
	}
	if (playboard[2][2] == playboard[2][1] && playboard[2][1] != ' ' && playboard[2][0] == ' ')
	{
		playboard[2][0] = '#';
		x = 2;
	}
	if (playboard[0][0] == playboard[1][1] && playboard[1][1] != ' ' && playboard[2][2] == ' ')
	{
		playboard[2][2] = '#';
		x = 2;
	}
	if (playboard[1][1] == playboard[2][2] && playboard[1][1] != ' ' && playboard[0][0] == ' ')
	{
		playboard[0][0] = '#';
		x = 2;
	}
	if (playboard[0][2] == playboard[1][1] && playboard[1][1] != ' ' && playboard[2][0] == ' ')
	{
		playboard[2][0] = '#';
		x = 2;
	}
	if (playboard[1][1] == playboard[2][0] && playboard[1][1] != ' ' && playboard[0][2] == ' ')
	{
		playboard[0][2] = '#';
		x = 2;
	}
	if (playboard[0][0] == playboard[0][2] && playboard[0][0] == '*' && playboard[0][1] == ' ')
	{
		playboard[0][1] = '#';
		x = 2;
	}
	if (playboard[0][0] == playboard[2][0] && playboard[0][0] == '*' && playboard[1][0] == ' ')
	{
		playboard[1][0] = '#';
		x = 2;
	}
	if (playboard[2][0] == playboard[2][2] && playboard[2][0] == '*' && playboard[2][1] == ' ')
	{
		playboard[2][1] = '#';
		x = 2;
	}
	if (playboard[2][2] == playboard[0][2] && playboard[2][2] == '*' && playboard[1][2] == ' ')
	{
		playboard[1][2] = '#';
		x = 2;
	}
	if (playboard[0][0] == playboard[2][2] && playboard[2][2] == '*' && playboard[1][1] == ' ')
	{
		playboard[1][1] = '#';
		x = 2;
	}
	if (playboard[0][2] == playboard[2][0] && playboard[2][0] == '*' && playboard[1][1] == ' ')
	{
		playboard[1][1] = '#';
		x = 2;
	}
	return x;
}

一样的如果电脑会输 ,那么电脑会走一步棋阻止他输棋,并返回一个2 ,如果电脑不会输 ,同样这个时候电脑就不下棋 并返回一个1,

在电脑判断自己是否会赢棋里面会返回一个值  如果返回的是1电脑就没有下棋,如果返回的是2,电脑就下棋了,因为电脑只能走一步棋!所以我们就要判断一下电脑只要走棋了后面就不能再走棋了。代码如下

if (n_1 == 1)
		{
			n_2 = computer_lose(playboard, ROW, COL);
		}

这里我们创建了两个变量! 一个n_1用来存放电脑是否会赢棋而返回的值,一个n_2用来存放电脑是否会输棋而返回的值,

不管电脑是为了赢棋而走棋,还是是为了阻止输棋而走棋,电脑都只能走一步棋,所以我们这就要判断一下 如果电脑两步都没下棋电脑就随机走一步

if (n_1 == 2)
  {
     n_2 = 2;
  }
if (n_2 == 2)
  {
     n_1 = 2;
  }
if (n_1 == 1||n_2 == 1)
  {
     computer_move(playboard, ROW, COL);
  }

最终的代码如下

#define _CRT_SECURE_NO_WARNINGS
#define ROW 3
#define COL 3
#include<stdio.h>
#include<time.h>
void isitboard(char playboard[ROW][COL], int row, int col);
void bgboard(char playboard[ROW][COL], int row, int col);
void play_move(char playboard[ROW][COL], int row, int col);
void computer_move(char playboard[ROW][COL], int row, int col);
char check(char playboard[ROW][COL], int row, int col);
int computer_lose (char playboard[ROW][COL], int row, int col);
int computer_win(char playboard[ROW][COL], int row, int col);
#include "game.h"
void menu()
{
	printf("############################\n");
	printf("############################\n");
	printf("######## 1.play 0.exit######\n");
	printf("############################\n");
	printf("############################\n");
}
void game()
{
	int n_1 = 0;
	int n_2 = 0;
	char coll = ' ';
	char playboard[ROW][COL] = {0};
	isitboard(playboard, ROW, COL);//初始化
	bgboard(playboard, ROW, COL);//打印棋盘
	while (1)
	{
		play_move(playboard, ROW, COL);
		bgboard(playboard, ROW, COL);
		coll = check(playboard, ROW, COL);
		if (coll != 'C')
			break;
		n_1 = computer_win(playboard, ROW, COL);//判断电脑会不会赢 
		if (n_1 == 1)
		{
			n_2 = computer_lose(playboard, ROW, COL);
		}
		if (n_1 == 2)
		{
			n_2 = 2;
		}
		if (n_2 == 2)
		{
			n_1 = 2;
		}
		if (n_1 == 1||n_2 == 1)
		{
			computer_move(playboard, ROW, COL);
		}
		bgboard(playboard, ROW, COL);
		coll = check(playboard, ROW, COL);
		if (coll != 'C')
			break;
	}//computer_move();
	if (coll == '*')
		printf("玩家赢\n");
	if (coll == '#')
		printf("电脑赢\n");
	if (coll == 'Q')
		printf("平局\n");
}
int main()
{
	int input = 0;
	srand((unsigned int) time(NULL));
	do
	{
		menu();
		printf("请输入\n");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			printf("三子棋\n");
			game();
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("输入错误请重新输入\n");
			break;
		}

	} while (input);
	return 0;
}
#include"game.h"
void isitboard(char playboard[ROW][COL], int row, int col)//存入空格
{
	int i = 0;
	for (i = 0; i < row; i++)
	{
		int j = 0;
		for (j = 0; j < col; j++)
		{
			playboard[i][j] = ' ';
		}
	}
}
void bgboard(char playboard[ROW][COL], int row, int col)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		j = 0;
		for (j = 0; j < col;j++)
		{
			printf(" %c ",playboard[i][j]);
			if(j<col-1)
			   printf("|");
		}
		printf("\n");
		if (i < row - 1)
		{
			for (j = 0; j < col; j++)
			{
				printf("---");
				if (j < col - 1)
					printf("|");
			}
			printf("\n");
		}
	}
}
void play_move(char playboard[ROW][COL], int row, int col)
{
	int x = 0;
	int y = 0;
	while (1)
	{
		printf("请输入坐标\n");
		scanf("%d %d", &x, &y);
		if (0<= x-1 && x-1<row && 0<=y-1&&y-1< col)
		{
			if (playboard[x - 1][y - 1] == ' ')
			{
				playboard[x-1][y-1] = '*';
				break;
			}
			else
				printf("坐标被占用请重新输入\n");
		}
		else
			printf("输入错误\n");
	}
}
void computer_move(char playboard[ROW][COL], int row, int col)
{
	int x = 0;
	int y = 0;
	printf("电脑走\n");
	while (1)
	{
			x = rand() % row;
			y = rand() % col;
			if (playboard[x][y] == ' ')
			{
				playboard[x][y] = '#';
				break;
			}
		
	}
}
int full(char playboard[ROW][COL], int row, int col)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		int j = 0;
		for (j = 0; j < col; j++)
		{
			if (playboard[i][j] == ' ')
			{
				return 1;
			}
		}
	}
	return 0;
}
char check(char playboard[ROW][COL], int row, int col)
{
	int i = 0;
	int ret = 0;
	ret = full(playboard, ROW, COL);
	
		for (i = 0; i < row; i++)
		{
			if (playboard[i][0] == playboard[i][1] && playboard[i][0] == playboard[i][2]&& playboard[i][2]!=' ')
				return playboard[i][0];
		}
		for (i = 0; i < col; i++)
		{
			if (playboard[0][i] == playboard[1][i] && playboard[0][i] == playboard[2][i] && playboard[2][i]!=' ')
				return playboard[0][i];
		}
		if (playboard[0][0] == playboard[1][1] && playboard[1][1] == playboard[2][2] && playboard[2][2] != ' ')
			return playboard[1][1];
		if (playboard[0][2] == playboard[1][1] && playboard[1][1] == playboard[2][0] && playboard[2][0] != ' ')
			return playboard[1][1];
	
		if (ret == 0)
		{
			return 'Q';
		}
		return'C';
}
int computer_lose(char playboard[ROW][COL], int row, int col)
{
	int x = 1;
	if (playboard[0][0] == playboard[0][1] && playboard[0][1] == '*' && playboard[0][2] == ' ')
	{
		playboard[0][2] = '#';
		x = 2;
	}
	if (playboard[0][2] == playboard[0][1] && playboard[0][1] == '* ' && playboard[0][0] == ' ')
	{
		playboard[0][0] = '#';
		x = 2;
	}
	if (playboard[1][0] == playboard[1][1] && playboard[1][1] != ' ' && playboard[1][2] == ' ')
	{
		playboard[1][2] = '#';
		x = 2;
	}
	if (playboard[1][1] == playboard[1][2] && playboard[1][1] != ' ' && playboard[1][0] == ' ')
	{
		playboard[1][0] = '#';
		x = 2;
	}
	if (playboard[2][0] == playboard[2][1] && playboard[2][1] != ' ' && playboard[2][2] == ' ')
	{
		playboard[2][2] = '#';
		x = 2;
	}
	if (playboard[2][2] == playboard[2][1] && playboard[2][1] != ' ' && playboard[2][0] == ' ')
	{
		playboard[2][0] = '#';
		x = 2;
	}
	if (playboard[0][0] == playboard[1][1] && playboard[1][1] != ' ' && playboard[2][2] == ' ')
	{
		playboard[2][2] = '#';
		x = 2;
	}
	if (playboard[1][1] == playboard[2][2] && playboard[1][1] != ' ' && playboard[0][0] == ' ')
	{
		playboard[0][0] = '#';
		x = 2;
	}
	if (playboard[0][2] == playboard[1][1] && playboard[1][1] != ' ' && playboard[2][0] == ' ')
	{
		playboard[2][0] = '#';
		x = 2;
	}
	if (playboard[1][1] == playboard[2][0] && playboard[1][1] != ' ' && playboard[0][2] == ' ')
	{
		playboard[0][2] = '#';
		x = 2;
	}
	if (playboard[0][0] == playboard[0][2] && playboard[0][0] == '*' && playboard[0][1] == ' ')
	{
		playboard[0][1] = '#';
		x = 2;
	}
	if (playboard[0][0] == playboard[2][0] && playboard[0][0] == '*' && playboard[1][0] == ' ')
	{
		playboard[1][0] = '#';
		x = 2;
	}
	if (playboard[2][0] == playboard[2][2] && playboard[2][0] == '*' && playboard[2][1] == ' ')
	{
		playboard[2][1] = '#';
		x = 2;
	}
	if (playboard[2][2] == playboard[0][2] && playboard[2][2] == '*' && playboard[1][2] == ' ')
	{
		playboard[1][2] = '#';
		x = 2;
	}
	if (playboard[0][0] == playboard[2][2] && playboard[2][2] == '*' && playboard[1][1] == ' ')
	{
		playboard[1][1] = '#';
		x = 2;
	}
	if (playboard[0][2] == playboard[2][0] && playboard[2][0] == '*' && playboard[1][1] == ' ')
	{
		playboard[1][1] = '#';
		x = 2;
	}
	return x;
}

int computer_win(char playboard[ROW][COL], int row, int col)
{
	int x = 1;
	if (playboard[0][0] == playboard[0][1] && playboard[0][1] == '#')
	{
		playboard[0][2] = '#';
		x = 2;
	}
	if (playboard[0][2] == playboard[0][1] && playboard[0][1] == '#' )
	{
		playboard[0][0] = '#';
		x = 2;
	}
	if (playboard[1][0] == playboard[1][1] && playboard[1][1] == '#' )
	{
		playboard[1][2] = '#';
		x = 2;
	}
	if (playboard[1][1] == playboard[1][2] && playboard[1][1] == '#')
	{
		playboard[1][0] = '#';
		x = 2;
	}
	if (playboard[2][0] == playboard[2][1] && playboard[2][1] == '#')
	{
		playboard[2][2] = '#';
		x = 2;
	}
	if (playboard[2][2] == playboard[2][1] && playboard[2][1] == '#')
	{
		playboard[2][0] = '#';
		x = 2;
	}
	if (playboard[0][0] == playboard[1][1] && playboard[1][1] == '#')
	{
		playboard[2][2] = '#';
		x = 2;
	}
	if (playboard[1][1] == playboard[2][2] && playboard[1][1] == '#')
	{
		playboard[0][0] = '#';
		x = 2;
	}
	if (playboard[0][2] == playboard[1][1] && playboard[1][1] == '#')
	{
		playboard[2][0] = '#';
		x = 2;
	}
	if (playboard[1][1] == playboard[2][0] && playboard[1][1] == '#')
	{
		playboard[0][2] = '#';
		x = 2;
	}
	if (playboard[0][0] == playboard[0][2] && playboard[0][0] == '#')
	{
		playboard[0][1] = '#';
		x = 2;
	}
	if (playboard[0][0] == playboard[2][0] && playboard[0][0] == '#')
	{
		playboard[1][0] = '#';
		x = 2;
	}
	if (playboard[2][0] == playboard[2][2] && playboard[2][0] == '#')
	{
		playboard[2][1] = '#';
		x = 2;
	}
	if (playboard[2][2] == playboard[0][2] && playboard[2][2] == '#')
	{
		playboard[1][2] = '#';
		x = 2;
	}
	if (playboard[0][0] == playboard[2][2] && playboard[2][2] == '#')
	{
		playboard[1][1] = '#';
		x = 2;
	}
	if (playboard[0][2] == playboard[2][0] && playboard[2][0] == '#')
	{
		playboard[1][1] = '#';
		x = 2;
	}
	return x;
}

到这里也就完成了!!!!!

但是我写的代码好长!有点啰嗦希望有大佬可以指正!

谢谢啦!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值