[Java backgammon game -Ai algorithm to achieve succinctly and] advanced algorithms preliminary -02--

Advanced algorithms preliminary

  • Go to their own interests the biggest road
  • It is difficult to have a panic, abandon it to victory in this chess game for his revenge

Collision detection algorithm and schematic stride

Here Insert Picture Description


Reverse calculate the weight

Each calculation finished weights must conduct a reverse weight calculation

  • Weight value: weight on both sides of the weight value is added

Weight calculation code - here or take the first section of the code:

/*
           x 和 y  代表坐标
        * xx :x方向需要增加的值
        * yy :y方向需要增加的值
        *
        * 例如xx = -1 yy= -1
        * 代表需要获取(x-1,y-1)的棋子
        *
        *  xx = 1 yy= 1
        * 代表需要获取(x+1,y+1)的棋子
        *
        * */
 /**
     * 计算权重
     * @param x x坐标
     * @param y y坐标
     * @param xx x方向
     * @param yy y方向
     * @param size 缓存变量如果当前检测是棋子是你的棋子
     * 				那么就会保存这个变量继续递归获取下一个位置的权重
     * @param c 自己的棋子颜色
     * @return 返回计算后的权重
     */
    private int ishas(int x,int y,int xx,int yy,int size ,char c){
    	//边缘检测防止超出棋盘位置
        if((x==0&&xx==-1)|| (x==15&&xx==1) || (y==0&&yy==-1) || (y== 15&&yy==1)) return size;

		
        if(table[x+xx][y+yy] == c){
            return ishas(x+xx,y+yy,xx,yy,size+1,c);
        }
        return size;
    }

Reverse calculate the weight pseudocode

//之前计算权重的代码
旧计算权重(x,y,你棋子的颜色 简称->颜色){
	//xx 和 yy 代表方向 这里不细说了
	//这里 0 代表默认权重 由于它是递归实现的 所以 给一个 0 让他累加
	return ishas(x,y,xx,yy,0,颜色);//返回的只是一个方向的权重计算
}

新计算权重(x,y,你棋子的颜色 简称->颜色){
	//这里的 -xx 和 -yy 就是对xx和yy进行相反的方向运算
	int n = ishas(x,y,xx,yy,0,颜色)+ishas(x,y,-xx,-yy,0,颜色);
	return n;
}



Impact checking

If it is detected by detecting the opponent recursive pieces

  • His weight-1 because the opponent can block you directly
  • If his weight is 4 then Lazi, ending the game

Collision detection code

Collision detection is calculated on the weight before weight optimization

Original code
 private int ishas(int x,int y,int xx,int yy,int size ,char c){
        if((x==0&&xx==-1)|| (x==15&&xx==1) || (y==0&&yy==-1) || (y== 15&&yy==1)) return size;

        if(table[x+xx][y+yy] == c){
            return ishas(x+xx,y+yy,xx,yy,size+1,c);
        }
        return size;
    }

After the collision detection optimization

//本方法可以进行对手棋子检测  这里 c 表示你要计算的权重颜色  c2 阻碍检测棋子的颜色
//也可以对c2进行优化 去掉这个参数 使用判断 只要不是默认颜色和你的颜色  一切视为阻碍
private int ishas(int x,int y,int xx,int yy,int size ,char c,char c2){
		/**
		  这里对   size>3 ? size+2:size-1
		  进行一次说明 
		  三目运算 :       条件 ?  成立的结果:不成立的结果;
		  如果条件成立 返回成立的结果 如果不成立返回不成立的结果
		  
		  这里说 如果size>3的话 就说明从这个位置开始 有4个棋子 那么就返回当前权重
		  如果 size<3 的话 代表一个棋子可以干掉,不足以重视
		**/
        if((x==0&&xx==-1)|| (x==15&&xx==1) || (y==0&&yy==-1) || (y== 15&&yy==1)){return size>3 ? size:size-1} 
        
        
        if(table[x+xx][y+yy] == c){//如果是我要的棋子 继续计算
            return ishas(x+xx,y+yy,xx,yy,size+1,c,c2); //size+1 本次计算有效 给权重+1 进行下一次计算
        }else if(table[x+xx][y+yy] == c2){ //如果是阻碍棋子 那么就意味着你要检测是棋子已经被堵了 没必要浪费时间 除非能赢
            return size>3 ? size:size-1;//这里在上面的注释上有讲解
        }
        return size;
}

Priority Detection

If the weight of detecting opponent's weight and their weight and stride as weights (say later), too

  • The largest own interests, no need to do unnecessary strife
  • In the same position coordinates, if the priority of detection and opponents of the same priority, then go directly to their own pieces
  • You can add a color variable pawn class
  • If all the grid traversal is complete, then the same priority to go their own pieces

Code

智能计算(){
		*是否第一步--> return 随机中间棋子(8,8)
		
		遍历所有格子 ->结果(x,y,当前格子颜色){
			if(当前格子颜色 ==){
				//两个都要计算并且把计算结果保存到棋子类
				计算权重(对手)-->两个方向计算权重
				计算权重(自己)-->两个方向计算权重
				
				//如果权重一样那么 对手权重-1 让自己先来
				谁的权重大--> 棋子列表.加入棋子(new 棋子(x,y,权重,你棋子的颜色))
			}
		}
		//遍历完毕
		棋子列表.按权重排序(同等权重下自己的棋子优先)
		返回棋子 = 棋子列表.get(0);
		return (返回棋子);
	}

The first chapter of the code and the code changes:

  • Whose weight is greater -> chess pieces added to the list (new pawn (x, y, weight, color of your pawn)).
    This place adds color stones
  • Pawn list. Press reordering rights (equal weights at their pieces priority)
    optimized sorting algorithm, so equally weighted when their pieces priority

Stride weight

This is focused on the need to know exactly what your favor

  • Stride right here refers to the weight
    but the most heavy weights high maximum weight is to weight the second highest right
  • According to stride weights can be calculated, in the case of equally weighted, to find out which piece in your best interest

Stepping weights to achieve:

  • Every time the right to be re-calculated to save him in an array or collection
  • Then remove the maximum weight as
  • Stride as the second largest weighting

Taking a stride weight

Taking a stride for stride weight is the weight once Optimization

  • Taking a stride algorithm to solve is more accurate calculations stride weight
  • You can set a value that is allowed across the grid is generally recommended 1--2
  • Stride value of 1 when the simulation time is 2 when the analog 2
  • See detailed illustration

Taking a stride weighting algorithm

Stepped right in front of the re-introduction is still not perfect can not reach the point of imagination

So how to solve it?

  • Let the pieces were "speculation"
  • Taking a stride to re-write a weighting algorithm
  • Each end of the calculation when the weight retention xx and yy is when the weight calculation came out at the same time let him tell Taking a stepping algorithm calculates xx and yy

Pseudo-code implementation

智能计算(){
		*是否第一步--> return 随机中间棋子(8,8)
		// 自己棋子的颜色 简称 -> 自己
		// 对手棋子的颜色 简称 -> 对手
		遍历所有格子 ->结果(x,y,当前格子颜色){
			if(当前格子颜色 ==){
				//两个都要计算并且把计算结果保存到棋子类
				计算权重(x,y,对手)-->两个方向计算权重
				计算权重(x,y,自己)-->两个方向计算权重
				
				//如果权重一样那么 对手权重-1 让自己先来
				谁的权重大--> 棋子列表.加入棋子(new 棋子(x,y,权重,自己))
			}
		}
		//遍历完毕
		棋子列表.按权重排序(同等权重下自己的棋子优先)
		返回棋子 = 棋子列表.get(0);
		return (返回棋子);
}
ishas(int x,int y,int xx,int yy,int size ,char c,char c2);//计算权重算法

计算权重(x,y,你棋子的颜色 简称->颜色){
	//这里的 -xx 和 -yy 就是对xx和yy进行相反的方向运算
	int n = ishas(x,y,xx,yy,0,颜色)+ishas(x,y,-xx,-yy,0,颜色);
	return n;
}


跨步权重(x,y,除去xx,除去yy,你棋子的颜色 简称->颜色){
	...各种方向运算...//各种 x=0 -1 1 .....
	if(xx == 除去xx && yy == 除去yy ) return 0;//这里是主权重所以跨步权重不参与
	int n = ishasKuaBu(x,y,xx,yy,0,颜色,1)+ishasKuaBu(x,y,-xx,-yy,0,颜色,1);
	return n;
}

//这里对ishas进行重新定义 增加了跨步跳转次数
ishasKuaBu(int x,int y,int xx,int yy,int size ,char c,char c2,int 跨步跳转次数){
        if((x==0&&xx==-1)|| (x==15&&xx==1) || (y==0&&yy==-1) || (y== 15&&yy==1)){return size>3 ? size:size-1} 
       
        if(table[x+xx][y+yy] == c){//如果是我要的棋子 继续计算
            return ishas(x+xx,y+yy,xx,yy,size+1,c,c2); //size+1 本次计算有效 给权重+1 进行下一次计算
        
        }else if(table[x+xx][y+yy] == c2){ //如果是阻碍棋子 那么就意味着你要检测是棋子已经被堵了 没必要浪费时间 除非能赢
            return size>3 ? size:size-1;//这里在上面的注释上有讲解
        
        }else if(table[x+xx][y+yy] ==&& 跨步跳转次数 > 0 ){//有跨步跳转机会 假装有棋子
            return ishas(x+xx,y+yy,xx,yy,size,c,c2,跨步跳转次数-1); //进行下一次计算,权重不变
        }
        return size;
}




After learning this method you can have a child who can write (novice) of the 331 procedures

Because of my chess techniques like a general, if there is a master of words, welcome to add my micro letter, explore to explore


After the update again advanced algorithms

Projection algorithm: guess if you got this move computing rival Lazi whether their detriment
global algorithm: every time you play chess -> conduct a forked tree calculation -> take the greatest chance of winning a move
tactical deployment of initial
and so on and so on ...


Previous [Java backgammon game -Ai algorithm to achieve succinctly and] -01- preliminary algorithm

Published 18 original articles · won praise 14 · views 7518

Guess you like

Origin blog.csdn.net/qq_18604209/article/details/104008083