五子棋(人机对弈)

五子棋(人机对弈)

人事三杯酒,流年一局棋。 ------《句》李远

1.既然是棋,先得有棋盘

--先画个棋盘--
void draw_map()
{
	int i, j;
	for(i=0; i<H; i++)
		for(j=0; j<W; j++){
			gotoxy(i,j);
			printf("-");		
	}
	for(i=0; i<H; i++)
		gotoxy(i,W),
		printf("%2d",i);
	for(i=0; i<W; i++,i++)		//游戏最小单位为两个字符故i连续++
		gotoxy(H,i),
		printf("%2d",i/2);
	gotoxy(0,50);
	printf("是五子棋");
	gotoxy(5,50);
	printf("输入坐标为四位字符型《前两位为行*后两位为列》");
	gotoxy(6,50);
	printf("                        (0--19)    (0--19)");
}

2.有了棋盘,还得有棋子,人机对弈,顾名思义,黑先白后,人执黑,人机执白

--map存棋子状态,下标表示棋子位置,数值为0表示空,为1表示玩家已下棋子,为2表示人机已下棋子--
--chess存棋子坐标,第二个下标为0表示棋子行坐标,为1表示棋子列坐标--
int map[H][W];		//棋盘状态
int chess[H*W/4][2], ai_chess[H*W/4][2];		//玩家/人机棋子坐标
int chess_num, ai_chess_num;		//玩家/人机落子总数

3.棋盘和棋子准备妥当,接下来就是下棋的状态了

--这里用scanf函数来实现下棋的动作--
--输入四位字符型数值,前两位表示行坐标,后两位表示列坐标--
--满足落子的条件才可落子(不能落子到棋盘外,也不能落子在已落子处)--
void in_chess()
{
	int i;
	ret:;
	gotoxy(10,50);
	printf("请输入落子坐标:");
	scanf("%4d:",&data);
	data_now[chess_num] = data;
	gotoxy(15,50);
	printf("当前输入坐标为:");
	printf("%4d",data_now[chess_num]);
	if(((data/100)<20)&&((data%100)<20)){		//输入坐标必须在棋盘内才打印棋子
		for(i=0 ; i<=chess_num; i++)					//输入坐标不能是已经落子的位置
			if(((data/100)==chess[i][0])&&((data%100)==chess[i][1]))
				goto ret;
		for(i=0 ; i<=ai_chess_num; i++)				//也不能落在人机已经落子位置
			if(((data/100)==ai_chess[i][0])&&((data%100)==ai_chess[i][1]))
				goto ret;			
		out_chess();
		chess_num++;
	}
	Ai_chess();			//玩家落子后人机落子
	a = 0;			//标识置零,进入清空输入坐标函数
}
void if_chess()
{
	gotoxy(10,65);
	printf("    ");
	a = 1;
}
void out_chess()
{
	chess[chess_num][0] = data_now[chess_num]/100;			//整除得到高二位数值即输入行坐标
	chess[chess_num][1] = data_now[chess_num]%100;			//取余得到低二位数值即输入列坐标
	gotoxy(chess[chess_num][0],chess[chess_num][1]*2);			//*2理解如上
	printf("@@");
	map[chess[chess_num][0]][chess[chess_num][1]*2] = 1;
}

4.接下来就需要判断胜负了

--遍历棋盘--
--找到第一个棋子位置--
--判断这个棋子右边相邻四个位置是否是同阵营棋子--
	--如果是,达成五子相连条件,游戏胜利--
	--如果不是,继续判断下边四个位置与右下四个位置与左下四个位置是否是同阵营棋子--
		(这里只粘贴了一行的判断条件,完整代码如后)
--若都不是,继续遍历棋盘,找到第二个棋子位置,再次判断,直到遍历完整个棋盘--
--再重复一遍流程,判断人机是否获胜--
		(由于从左上挨个挨个棋子遍历,所以只需要判断右边,右下,下边,左下四个方向的棋子连接情况)
	for(i=0; i<H; i++)
		for(j=0; j<W; j+=2){
			if(map[i][j] == 1){
				if((map[i+1][j] == 1)&&(map[i+2][j] == 1)&&(map[i+3][j] == 1)&&(map[i+4][j] == 1)){					
					for(k=0; k<5; k++){
						gotoxy(i+k,j);
						printf("  ");
					}
					Sleep(1000);		//延时1秒
					system("cls");		//清屏
					gotoxy(10,10);
					printf("win!");
					system("pause");	//暂停
				}

5.最后是程序核心==>实现低智能人机运算

--就是一个对棋盘中出现的各种情况人机不同的落子方式--
--面对不同的棋盘情况,需要优先处理更“急”的情况,所以不同的判断语句有一个先后顺序--
--当满足了一种可以落子的情况,落子后需要马上跳到函数底端(跳出多层循环体/判断语句),这里用了goto--
	(人机一次只能落一个子)
--怎么判断棋盘情况其实和判断胜负的情况差不多,都是先遍历棋盘,再根据不同的棋盘情况落子--
有两个缺点
--人机代码比较沉长,因为调用了goto,无法函数嵌套,整个代码块可读性比较差,判断的情况也较少,人机智能程度不高--
--落子在棋盘边缘的情况没有一个很好地考虑,整个程序还有待提高--

以下是源代码

#include<stdio.h>
#include<windows.h>

#define W 40	//棋盘宽
#define H 20	//棋盘高

int map[H][W];		//棋盘状态
int chess[H*W/4][2], ai_chess[H*W/4][2];		//玩家/人机棋子坐标
int chess_num, ai_chess_num;		//玩家/人机落子总数
int data_now[H*W/2];		//存放输入坐标
int data;		//输入坐标暂存变量
int a;		//判断标识

/*******移动光标函数********/
void gotoxy(int i,int j)      
{
    COORD position={j,i};
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),position);
}
void init();		//初始化
void draw_map();		//画棋盘
void out_chess();		//打印玩家落子
void Ai_chess();		//人机落子
void in_chess();		//棋子坐标输入与显示
void if_chess();		//清空输入坐标
void deleter();			//胜负判断
int main()
{
	init();
	draw_map();
	while(1){
		if(a)
			in_chess();
		if(!a)
			if_chess();
		deleter();
	}
	return 0;
}

void init()
{
	/**********gotoxy()初始化***********/
	CONSOLE_CURSOR_INFO cursor_info = {1, 0};
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
	data = 0;
	a = 1;
}

void draw_map()
{
	int i, j;
	for(i=0; i<H; i++)
		for(j=0; j<W; j++){
			gotoxy(i,j);
			printf("-");		
	}
	for(i=0; i<H; i++)
		gotoxy(i,W),
		printf("%2d",i);
	for(i=0; i<W; i++,i++)		//游戏最小单位为两个字符故i连续++
		gotoxy(H,i),
		printf("%2d",i/2);
	gotoxy(0,50);
	printf("是五子棋");
	gotoxy(5,50);
	printf("输入坐标为四位字符型《前两位为行*后两位为列》");
	gotoxy(6,50);
	printf("                        (0--19)    (0--19)");
}

void in_chess()
{
	int i;
	ret:;
	gotoxy(10,50);
	printf("请输入落子坐标:");
	scanf("%4d:",&data);
	data_now[chess_num] = data;
	gotoxy(15,50);
	printf("当前输入坐标为:");
	printf("%4d",data_now[chess_num]);
	if(((data/100)<20)&&((data%100)<20)){		//输入坐标必须在棋盘内才打印棋子
		for(i=0 ; i<=chess_num; i++)					//输入坐标不能是已经落子的位置
			if(((data/100)==chess[i][0])&&((data%100)==chess[i][1]))
				goto ret;
		for(i=0 ; i<=ai_chess_num; i++)				//也不能落在人机已经落子位置
			if(((data/100)==ai_chess[i][0])&&((data%100)==ai_chess[i][1]))
				goto ret;			
		out_chess();
		chess_num++;
	}
	Ai_chess();			//玩家落子后人机落子
	a = 0;			//标识置零,进入清空输入坐标函数
}

/********其实这个函数可以整合到上一个函数*********/
/********分开两个函数写以便于更清晰的逻辑*********/
void if_chess()
{
	gotoxy(10,65);
	printf("    ");
	a = 1;
}

void out_chess()
{
	chess[chess_num][0] = data_now[chess_num]/100;			//整除得到高二位数值即输入行坐标
	chess[chess_num][1] = data_now[chess_num]%100;			//取余得到低二位数值即输入列坐标
	gotoxy(chess[chess_num][0],chess[chess_num][1]*2);			//*2理解如上
	printf("@@");
	map[chess[chess_num][0]][chess[chess_num][1]*2] = 1;
}

/*************人机判断如何落子****************/
/*********先遍历棋盘再判断落子情况************/
/********goto用于跳出多层循环判断语句*********/
void Ai_chess()
{
	int i, j;
	for(i=0; i<H; i++){					//第一次遍历
		for(j=0; j<W; j+=2){
			if(map[i][j] == 2){
				if((map[i+1][j] == 2)&&(map[i+2][j] == 2)&&(map[i+3][j] == 2)){			//第一种情况,如果人机已有四个棋子按列连着
					if((map[i-1][j]==0)&&(map[i+4][j]==0)){				//四个棋子两边都为空
						gotoxy(i+4,j);
						printf("##");
						ai_chess[ai_chess_num][0] = i+4;
						ai_chess[ai_chess_num][1] = j;
						map[ai_chess[ai_chess_num][0]][ai_chess[ai_chess_num][1]] = 2;
						ai_chess_num++;
						goto here;						//已落子已保存数据即跳到函数底端
					}
					if((map[i-1][j]==0)&&(map[i+4][j]!=0)){					//四个棋子一边为空就落子另一边
						gotoxy(i-1,j);
						printf("##");
						ai_chess[ai_chess_num][0] = i-1;
						ai_chess[ai_chess_num][1] = j;
						map[ai_chess[ai_chess_num][0]][ai_chess[ai_chess_num][1]] = 2;
						ai_chess_num++;
						goto here;
					}
					if((map[i-1][j]!=0)&&(map[i+4][j]==0)){					//同上,另一边棋子为空就落子这一边
						gotoxy(i+4,j);
						printf("##");
						ai_chess[ai_chess_num][0] = i+4;
						ai_chess[ai_chess_num][1] = j;
						map[ai_chess[ai_chess_num][0]][ai_chess[ai_chess_num][1]] = 2;
						ai_chess_num++;
						goto here;
					}
				}					//第一种情况没有生效则判断下一种情况
				if((map[i][j+2] == 2)&&(map[i][j+4] == 2)&&(map[i][j+6] == 2)){				//四个棋子按行连着
					if((map[i][j-2]==0)&&(map[i][j+8]==0)){
						gotoxy(i,j+8);
						printf("##");
						ai_chess[ai_chess_num][0] = i;
						ai_chess[ai_chess_num][1] = j+8;
						map[ai_chess[ai_chess_num][0]][ai_chess[ai_chess_num][1]] = 2;
						ai_chess_num++;
						goto here;
					}
					if((map[i][j-2]==0)&&(map[i][j+8]!=0)){
						gotoxy(i,j-2);
						printf("##");
						ai_chess[ai_chess_num][0] = i;
						ai_chess[ai_chess_num][1] = j-2;
						map[ai_chess[ai_chess_num][0]][ai_chess[ai_chess_num][1]] = 2;
						ai_chess_num++;
						goto here;
					}
					if((map[i][j-2]!=0)&&(map[i][j+8]==0)){
						gotoxy(i,j+8);
						printf("##");
						ai_chess[ai_chess_num][0] = i;
						ai_chess[ai_chess_num][1] = j+8;
						map[ai_chess[ai_chess_num][0]][ai_chess[ai_chess_num][1]] = 2;
						ai_chess_num++;
						goto here;
					}
				}
				if((map[i+1][j+2] == 2)&&(map[i+2][j+4] == 2)&&(map[i+3][j+6] == 2)){				//四个棋子按右下斜连着
					if((map[i-1][j-2]==0)&&(map[i+4][j+8]==0)){
						gotoxy(i+4,j+8);
						printf("##");
						ai_chess[ai_chess_num][0] = i+4;
						ai_chess[ai_chess_num][1] = j+8;
						map[ai_chess[ai_chess_num][0]][ai_chess[ai_chess_num][1]] = 2;
						ai_chess_num++;
						goto here;
					}
					if((map[i-1][j-2]==0)&&(map[i+4][j+8]!=0)){
						gotoxy(i-1,j-2);
						printf("##");
						ai_chess[ai_chess_num][0] = i-1;
						ai_chess[ai_chess_num][1] = j-2;
						map[ai_chess[ai_chess_num][0]][ai_chess[ai_chess_num][1]] = 2;
						ai_chess_num++;
						goto here;
					}
					if((map[i-1][j-2]!=0)&&(map[i+4][j+8]==0)){
						gotoxy(i+4,j+8);
						printf("##");
						ai_chess[ai_chess_num][0] = i+4;
						ai_chess[ai_chess_num][1] = j+8;
						map[ai_chess[ai_chess_num][0]][ai_chess[ai_chess_num][1]] = 2;
						ai_chess_num++;
						goto here;
					}
				}
				if((map[i+1][j-2] == 2)&&(map[i+2][j-4] == 2)&&(map[i+3][j-6] == 2)){				//四个棋子按左下斜连着
					if((map[i-1][j+2]==0)&&(map[i+4][j-8]==0)){
						gotoxy(i+4,j-8);
						printf("##");
						ai_chess[ai_chess_num][0] = i+4;
						ai_chess[ai_chess_num][1] = j-8;
						map[ai_chess[ai_chess_num][0]][ai_chess[ai_chess_num][1]] = 2;
						ai_chess_num++;
						goto here;
					}
					if((map[i-1][j+2]==0)&&(map[i+4][j-8]!=0)){
						gotoxy(i-1,j+2);
						printf("##");
						ai_chess[ai_chess_num][0] = i-1;
						ai_chess[ai_chess_num][1] = j+2;
						map[ai_chess[ai_chess_num][0]][ai_chess[ai_chess_num][1]] = 2;
						ai_chess_num++;
						goto here;
					}
					if((map[i-1][j+2]!=0)&&(map[i+4][j-8]==0)){
						gotoxy(i+4,j-8);
						printf("##");
						ai_chess[ai_chess_num][0] = i+4;
						ai_chess[ai_chess_num][1] = j-8;
						map[ai_chess[ai_chess_num][0]][ai_chess[ai_chess_num][1]] = 2;
						ai_chess_num++;
						goto here;
					}
				}
			}
		}
	}
	for(i=0; i<H; i++){						//再次遍历
		for(j=0; j<W; j+=2){
			if(map[i][j] == 1){
				if((map[i+1][j] == 1)&&(map[i+2][j] == 1)&&(map[i+3][j] == 1)){			//堵玩家已经有四个棋子按列连着(垂死挣扎)
					if((map[i-1][j]==0)&&(map[i+4][j]==0)){
						gotoxy(i+4,j);
						printf("##");
						ai_chess[ai_chess_num][0] = i+4;
						ai_chess[ai_chess_num][1] = j;
						map[ai_chess[ai_chess_num][0]][ai_chess[ai_chess_num][1]] = 2;
						ai_chess_num++;
						goto here;
					}
					if((map[i-1][j]==0)&&(map[i+4][j]!=0)){				//这四个棋子有一端已经封住(可以抢救)
						gotoxy(i-1,j);
						printf("##");
						ai_chess[ai_chess_num][0] = i-1;
						ai_chess[ai_chess_num][1] = j;
						map[ai_chess[ai_chess_num][0]][ai_chess[ai_chess_num][1]] = 2;
						ai_chess_num++;
						goto here;
					}
					if((map[i-1][j]!=0)&&(map[i+4][j]==0)){				//四个棋子另一端被封住(抢救有效)
						gotoxy(i+4,j);
						printf("##");
						ai_chess[ai_chess_num][0] = i+4;
						ai_chess[ai_chess_num][1] = j;
						map[ai_chess[ai_chess_num][0]][ai_chess[ai_chess_num][1]] = 2;
						ai_chess_num++;
						goto here;
					}
				}
				if((map[i][j+2] == 1)&&(map[i][j+4] == 1)&&(map[i][j+6] == 1)){			//堵玩家已经有四个棋子按行连着
					if((map[i][j-2]==0)&&(map[i][j+8]==0)){
						gotoxy(i,j+8);
						printf("##");
						ai_chess[ai_chess_num][0] = i;
						ai_chess[ai_chess_num][1] = j+8;
						map[ai_chess[ai_chess_num][0]][ai_chess[ai_chess_num][1]] = 2;
						ai_chess_num++;
						goto here;
					}
					if((map[i][j-2]==0)&&(map[i][j+8]!=0)){
						gotoxy(i,j-2);
						printf("##");
						ai_chess[ai_chess_num][0] = i;
						ai_chess[ai_chess_num][1] = j-2;
						map[ai_chess[ai_chess_num][0]][ai_chess[ai_chess_num][1]] = 2;
						ai_chess_num++;
						goto here;
					}
					if((map[i][j-2]!=0)&&(map[i][j+8]==0)){
						gotoxy(i,j+8);
						printf("##");
						ai_chess[ai_chess_num][0] = i;
						ai_chess[ai_chess_num][1] = j+8;
						map[ai_chess[ai_chess_num][0]][ai_chess[ai_chess_num][1]] = 2;
						ai_chess_num++;
						goto here;
					}
				}
				if((map[i+1][j+2] == 1)&&(map[i+2][j+4] == 1)&&(map[i+3][j+6] == 1)){			//堵玩家已经有四个棋子按右下斜连着
					if((map[i-1][j-2]==0)&&(map[i+4][j+8]==0)){
						gotoxy(i+4,j+8);
						printf("##");
						ai_chess[ai_chess_num][0] = i+4;
						ai_chess[ai_chess_num][1] = j+8;
						map[ai_chess[ai_chess_num][0]][ai_chess[ai_chess_num][1]] = 2;
						ai_chess_num++;
						goto here;
					}
					if((map[i-1][j-2]==0)&&(map[i+4][j+8]!=0)){
						gotoxy(i-1,j-2);
						printf("##");
						ai_chess[ai_chess_num][0] = i-1;
						ai_chess[ai_chess_num][1] = j-2;
						map[ai_chess[ai_chess_num][0]][ai_chess[ai_chess_num][1]] = 2;
						ai_chess_num++;
						goto here;
					}
					if((map[i-1][j-2]!=0)&&(map[i+4][j+8]==0)){
						gotoxy(i+4,j+8);
						printf("##");
						ai_chess[ai_chess_num][0] = i+4;
						ai_chess[ai_chess_num][1] = j+8;
						map[ai_chess[ai_chess_num][0]][ai_chess[ai_chess_num][1]] = 2;
						ai_chess_num++;
						goto here;
					}
				}
				if((map[i+1][j-2] == 1)&&(map[i+2][j-4] == 1)&&(map[i+3][j-6] == 1)){			//堵玩家已经有四个棋子按左下斜连着
					if((map[i-1][j+2]==0)&&(map[i+4][j-8]==0)){
						gotoxy(i+4,j-8);
						printf("##");
						ai_chess[ai_chess_num][0] = i+4;
						ai_chess[ai_chess_num][1] = j-8;
						map[ai_chess[ai_chess_num][0]][ai_chess[ai_chess_num][1]] = 2;
						ai_chess_num++;
						goto here;
					}
					if((map[i-1][j+2]==0)&&(map[i+4][j-8]!=0)){
						gotoxy(i-1,j+2);
						printf("##");
						ai_chess[ai_chess_num][0] = i-1;
						ai_chess[ai_chess_num][1] = j+2;
						map[ai_chess[ai_chess_num][0]][ai_chess[ai_chess_num][1]] = 2;
						ai_chess_num++;
						goto here;
					}
					if((map[i-1][j+2]!=0)&&(map[i+4][j-8]==0)){
						gotoxy(i+4,j-8);
						printf("##");
						ai_chess[ai_chess_num][0] = i+4;
						ai_chess[ai_chess_num][1] = j-8;
						map[ai_chess[ai_chess_num][0]][ai_chess[ai_chess_num][1]] = 2;
						ai_chess_num++;
						goto here;
					}
				}
				if((map[i+1][j] == 1)&&(map[i+2][j] == 1)){				//玩家三个棋子连着必须堵一边
					if((map[i-1][j]==0)&&(map[i+3][j]==0)){
						if(i == 0){						//落在边界情况
							gotoxy(i+3,j);
							printf("##");
							ai_chess[ai_chess_num][0] = i+3;
							ai_chess[ai_chess_num][1] = j;
							map[ai_chess[ai_chess_num][0]][ai_chess[ai_chess_num][1]] = 2;
							ai_chess_num++;
							goto here;
						}
						if(i == H-1){
							gotoxy(i-1,j);
							printf("##");
							ai_chess[ai_chess_num][0] = i-1;
							ai_chess[ai_chess_num][1] = j;
							map[ai_chess[ai_chess_num][0]][ai_chess[ai_chess_num][1]] = 2;
							ai_chess_num++;
							goto here;
						}
						else							//不在边界情况
						{
							int s;
							s = rand()%2;			//因为两边为空可以用个伪随机数随机落子在一边
							gotoxy(i-1+4*s,j);			//必要性不大后面就没有用了
							printf("##");
							ai_chess[ai_chess_num][0] = i-1+4*s;
							ai_chess[ai_chess_num][1] = j;
							map[ai_chess[ai_chess_num][0]][ai_chess[ai_chess_num][1]] = 2;
							ai_chess_num++;
							goto here;
						}
					}
				}
				if((map[i][j+2] == 1)&&(map[i][j+4] == 1)){
					if((map[i][j-2]==0)&&(map[i][j+6]==0)){
						if(j == 0){
							gotoxy(i,j+6);
							printf("##");
							ai_chess[ai_chess_num][0] = i;
							ai_chess[ai_chess_num][1] = j+6;
							map[ai_chess[ai_chess_num][0]][ai_chess[ai_chess_num][1]] = 2;
							ai_chess_num++;
							goto here;
						}
						if(j == W-2){
							gotoxy(i,j-2);
							printf("##");
							ai_chess[ai_chess_num][0] = i;
							ai_chess[ai_chess_num][1] = j-2;
							map[ai_chess[ai_chess_num][0]][ai_chess[ai_chess_num][1]] = 2;
							ai_chess_num++;
							goto here;
						}
						else
						{
							int s;
							s = rand()%2;
							gotoxy(i,j-2+8*s);
							printf("##");
							ai_chess[ai_chess_num][0] = i;
							ai_chess[ai_chess_num][1] = j-2+8*s;
							map[ai_chess[ai_chess_num][0]][ai_chess[ai_chess_num][1]] = 2;
							ai_chess_num++;
							goto here;
						}
					}
				}
				if((map[i+1][j+2] == 1)&&(map[i+2][j+4] == 1)){
					if((map[i-1][j-2]==0)&&(map[i+3][j+6]==0)){
						if(i == 0){									//斜边的边界判断不够完善(右下)												
							gotoxy(i+3,j+6);
							printf("##");
							ai_chess[ai_chess_num][0] = i+3;
							ai_chess[ai_chess_num][1] = j+6;
							map[ai_chess[ai_chess_num][0]][ai_chess[ai_chess_num][1]] = 2;
							ai_chess_num++;
							goto here;
						}
						if(i == H-1){
							gotoxy(i-1,j-2);
							printf("##");
							ai_chess[ai_chess_num][0] = i-1;
							ai_chess[ai_chess_num][1] = j-2;
							map[ai_chess[ai_chess_num][0]][ai_chess[ai_chess_num][1]] = 2;
							ai_chess_num++;
							goto here;
						}
						else
						{
							int s;
							s = rand()%2;
							gotoxy(i-1+4*s,j-2+8*s);
							printf("##");
							ai_chess[ai_chess_num][0] = i-1+4*s;
							ai_chess[ai_chess_num][1] = j-2+8*s;
							map[ai_chess[ai_chess_num][0]][ai_chess[ai_chess_num][1]] = 2;
							ai_chess_num++;
							goto here;
						}
					}
				}
				if((map[i+1][j-2] == 1)&&(map[i+2][j-4] == 1)){
					if((map[i-1][j+2]==0)&&(map[i+3][j-6]==0)){
						if(i == 0){										//左下										
							gotoxy(i+3,j-6);
							printf("##");
							ai_chess[ai_chess_num][0] = i+3;
							ai_chess[ai_chess_num][1] = j-6;
							map[ai_chess[ai_chess_num][0]][ai_chess[ai_chess_num][1]] = 2;
							ai_chess_num++;
							goto here;
						}
						if(i == H-1){
							gotoxy(i-1,j+2);
							printf("##");
							ai_chess[ai_chess_num][0] = i-1;
							ai_chess[ai_chess_num][1] = j+2;
							map[ai_chess[ai_chess_num][0]][ai_chess[ai_chess_num][1]] = 2;
							ai_chess_num++;
							goto here;
						}
						else
						{
							int s;
							s = rand()%2;
							gotoxy(i-1+4*s,j+2+8*(-s));
							printf("##");
							ai_chess[ai_chess_num][0] = i-1+4*s;
							ai_chess[ai_chess_num][1] = j+2+8*(-s);
							map[ai_chess[ai_chess_num][0]][ai_chess[ai_chess_num][1]] = 2;
							ai_chess_num++;
							goto here;
						}
					}
				}
			}	
		}
	}
	for(i=0; i<H; i++){				//第三次遍历
		for(j=0; j<W; j+=2){			//这次遍历是判断人机自身了
			if(map[i][j] == 2){
				if((map[i+1][j] == 2)&&(map[i+2][j] == 2)){				//人机有三个棋子按列连着
					if((map[i-1][j] == 0)&&(map[i+3][j] == 0)){
						gotoxy(i-1,j);
						printf("##");
						ai_chess[ai_chess_num][0] = i-1;
						ai_chess[ai_chess_num][1] = j;
						map[ai_chess[ai_chess_num][0]][ai_chess[ai_chess_num][1]] = 2;
						ai_chess_num++;
						goto here;
					}
				}
				if((map[i][j+2] == 2)&&(map[i][j+4] == 2)){				//按行连着
					if((map[i][j-2] == 0)&&(map[i][j+6] == 0)){
						gotoxy(i,j-2);
						printf("##");
						ai_chess[ai_chess_num][0] = i;
						ai_chess[ai_chess_num][1] = j-2;
						map[ai_chess[ai_chess_num][0]][ai_chess[ai_chess_num][1]] = 2;
						ai_chess_num++;
						goto here;
					}
				}
				if((map[i+1][j+2] == 2)&&(map[i+2][j+4] == 2)){				//斜右下连着
					if((map[i-1][j-2] == 0)&&(map[i+3][j+6] == 0)){
						gotoxy(i-1,j-2);
						printf("##");
						ai_chess[ai_chess_num][0] = i-1;
						ai_chess[ai_chess_num][1] = j-2;
						map[ai_chess[ai_chess_num][0]][ai_chess[ai_chess_num][1]] = 2;
						ai_chess_num++;
						goto here;
					}
				}
				if((map[i+1][j-2] == 2)&&(map[i+2][j-4] == 2)){				//斜左下连着
					if((map[i-1][j+2] == 0)&&(map[i+3][j-6] == 0)){
						gotoxy(i-1,j+2);
						printf("##");
						ai_chess[ai_chess_num][0] = i-1;
						ai_chess[ai_chess_num][1] = j+2;
						map[ai_chess[ai_chess_num][0]][ai_chess[ai_chess_num][1]] = 2;
						ai_chess_num++;
						goto here;
					}
				}		
			}
		}
	}
	for(i=0; i<H; i++){				//第四次遍历
		for(j=0; j<W; j+=2){			//还是判断人机自身(可以和第三次遍历放在一起,放在第三次末尾)
			if(map[i][j] == 2){
				if(map[i+1][j] == 2){			//人机有两个棋子按列连着
					if((map[i-1][j] == 0)&&(map[i+2][j] == 0)){
						gotoxy(i-1,j);
						printf("##");
						ai_chess[ai_chess_num][0] = i-1;
						ai_chess[ai_chess_num][1] = j;
						map[ai_chess[ai_chess_num][0]][ai_chess[ai_chess_num][1]] = 2;
						ai_chess_num++;
						goto here;
					}
				}
				if(map[i][j+2] == 2){				//按行连着
					if((map[i][j-2] == 0)&&(map[i][j+4] == 0)){
						gotoxy(i,j-2);
						printf("##");
						ai_chess[ai_chess_num][0] = i;
						ai_chess[ai_chess_num][1] = j-2;
						map[ai_chess[ai_chess_num][0]][ai_chess[ai_chess_num][1]] = 2;
						ai_chess_num++;
						goto here;
					}
				}
				if(map[i+1][j+2] == 2){				//按斜右下连着
					if((map[i-1][j-2] == 0)&&(map[i+2][j+4] == 0)){
						gotoxy(i-1,j-2);
						printf("##");
						ai_chess[ai_chess_num][0] = i-1;
						ai_chess[ai_chess_num][1] = j-2;
						map[ai_chess[ai_chess_num][0]][ai_chess[ai_chess_num][1]] = 2;
						ai_chess_num++;
						goto here;
					}
				}	
				if(map[i+1][j-2] == 2){				//按斜左下连着
					if((map[i-1][j+2] == 0)&&(map[i+2][j-4] == 0)){
						gotoxy(i-1,j+2);
						printf("##");
						ai_chess[ai_chess_num][0] = i-1;
						ai_chess[ai_chess_num][1] = j+2;
						map[ai_chess[ai_chess_num][0]][ai_chess[ai_chess_num][1]] = 2;
						ai_chess_num++;
						goto here;
					}
				}		
			}
		}
	}
	for(i=0; i<H; i++){				//再遍历一次
		for(j=0; j<W; j+=2){			//这次只判断棋盘上有一个棋子了
			if(map[i][j] == 2){				
				if((map[i-1][j] == 0)&&(map[i+1][j] == 0)){				//虽然只有一个棋子,但还是判断一下四种关系
					gotoxy(i-1,j);								//因为人机落子的位置往往都挨着玩家的落子
					printf("##");
					ai_chess[ai_chess_num][0] = i-1;
					ai_chess[ai_chess_num][1] = j;
					map[ai_chess[ai_chess_num][0]][ai_chess[ai_chess_num][1]] = 2;
					ai_chess_num++;
					goto here;
				}
				if((map[i][j-2] == 0)&&(map[i][j+2] == 0)){
					gotoxy(i,j-2);
					printf("##");
					ai_chess[ai_chess_num][0] = i;
					ai_chess[ai_chess_num][1] = j-2;
					map[ai_chess[ai_chess_num][0]][ai_chess[ai_chess_num][1]] = 2;
					ai_chess_num++;
					goto here;
				}
				if((map[i-1][j-2] == 0)&&(map[i+1][j+2] == 0)){
					gotoxy(i-1,j-2);
					printf("##");
					ai_chess[ai_chess_num][0] = i-1;
					ai_chess[ai_chess_num][1] = j-2;
					map[ai_chess[ai_chess_num][0]][ai_chess[ai_chess_num][1]] = 2;
					ai_chess_num++;
					goto here;
				}
				if((map[i-1][j+2] == 0)&&(map[i+1][j-2] == 0)){
					gotoxy(i-1,j+2);
					printf("##");
					ai_chess[ai_chess_num][0] = i-1;
					ai_chess[ai_chess_num][1] = j+2;
					map[ai_chess[ai_chess_num][0]][ai_chess[ai_chess_num][1]] = 2;
					ai_chess_num++;
					goto here;
				}
			}
		}
	}
	for(i=0; i<H; i++){			//最后一次遍历
		for(j=0; j<W; j+=2){			//最后一种情况,人机还未在棋盘上落子
			if(map[i][j] == 1){
				int s;
				s = rand()%2;
				gotoxy(i-1+2*s,j);
				printf("##");
				ai_chess[ai_chess_num][0] = i-1+2*s;
				ai_chess[ai_chess_num][1] = j;
				map[ai_chess[ai_chess_num][0]][ai_chess[ai_chess_num][1]] = 2;
				ai_chess_num++;
				goto here;
			}
		}
	}
	here:;
}

void deleter()
{
	//先判断玩家是否获胜
	int i, j, k;
	for(i=0; i<H; i++)
		for(j=0; j<W; j+=2){
			if(map[i][j] == 1){
				if((map[i+1][j] == 1)&&(map[i+2][j] == 1)&&(map[i+3][j] == 1)&&(map[i+4][j] == 1)){					
					for(k=0; k<5; k++){
						gotoxy(i+k,j);
						printf("  ");
					}
					Sleep(1000);		//延时1秒
					system("cls");		//清屏
					gotoxy(10,10);
					printf("win!");
					system("pause");	//暂停
				}
				if((map[i][j+2] == 1)&&(map[i][j+4] == 1)&&(map[i][j+6] == 1)&&(map[i][j+8] == 1)){					
					for(k=0; k<5; k++){
						gotoxy(i,j+2*k);
						printf("  ");
					}
					Sleep(1000);
					system("cls");
					gotoxy(10,10);
					printf("win!");
					system("pause");
				}
				if((map[i+1][j+2] == 1)&&(map[i+2][j+4] == 1)&&(map[i+3][j+6] == 1)&&(map[i+4][j+8] == 1)){					
					for(k=0; k<5; k++){
						gotoxy(i+k,j+2*k);
						printf("  ");
					}
					Sleep(1000);
					system("cls");
					gotoxy(10,10);
					printf("win!");
					system("pause");
				}
				if((map[i+1][j-2] == 1)&&(map[i+2][j-4] == 1)&&(map[i+3][j-6] == 1)&&(map[i+4][j-8] == 1)){					
					for(k=0; k<5; k++){
						gotoxy(i+k,j-2*k);
						printf("  ");
					}
					Sleep(1000);
					system("cls");
					gotoxy(10,10);
					printf("win!");
					system("pause");
				}
			}
	}
	//判断人机是否获胜
	for(i=0; i<H; i++)
		for(j=0; j<W; j+=2){
			if(map[i][j] == 2){
				if((map[i+1][j] == 2)&&(map[i+2][j] == 2)&&(map[i+3][j] == 2)&&(map[i+4][j] == 2)){					
					for(k=0; k<5; k++){
						gotoxy(i+k,j);
						printf("  ");
					}
					Sleep(1000);
					system("cls");
					gotoxy(10,10);
					printf("fail!");
					system("pause");
				}
				if((map[i][j+2] == 2)&&(map[i][j+4] == 2)&&(map[i][j+6] == 2)&&(map[i][j+8] == 2)){					
					for(k=0; k<5; k++){
						gotoxy(i,j+2*k);
						printf("  ");
					}
					Sleep(1000);
					system("cls");
					gotoxy(10,10);
					printf("fail!");
					system("pause");
				}
				if((map[i+1][j+2] == 2)&&(map[i+2][j+4] == 2)&&(map[i+3][j+6] == 2)&&(map[i+4][j+8] == 2)){					
					for(k=0; k<5; k++){
						gotoxy(i+k,j+2*k);
						printf("  ");
					}
					Sleep(1000);
					system("cls");
					gotoxy(10,10);
					printf("fail!");
					system("pause");
				}
				if((map[i+1][j-2] == 2)&&(map[i+2][j-4] == 2)&&(map[i+3][j-6] == 2)&&(map[i+4][j-8] == 2)){					
					for(k=0; k<5; k++){
						gotoxy(i+k,j-2*k);
						printf("  ");
					}
					Sleep(1000);
					system("cls");
					gotoxy(10,10);
					printf("fail!");
					system("pause");
				}
			}
	}
}
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值