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

Intelligent chess backgammon how to achieve? Revealed it to


Source connection: the Java backgammon games (console Ai pure algorithm)


Basic weight calculation

Now talk about backgammon is how to determine the odds of
here right when it comes to weight if your weight is chess position 4 then you will be able to win the game

Here that position 1> 2> 3> 4> O right weight
O maximum weight of 4 weight if he is playing chess so he can get the game victory at this location

O here represent chess positions

Here Insert Picture Description
Some people say that if the strange chess position in the middle of it just to collect five pieces
have to explain it here

Reverse calculate the weight

In fact, as in this case the query may be reversed and it adds weight
, for example, FIG. 1> 5> O direction and there are two pieces if
so the O location will get the game victory
and we take the inverse front weight weight weight 2 is behind the weight is 2 add up to exactly 4 so get the game victory

Detailed later in advanced algorithms

Weights implementation code

Here I put myself before hair java code came buckle

/*
           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;
    }

Calculate the weight pseudo code - if looking at the above hard look at this

char table [16][16] ;//棋盘
   /*
   * x 和 y  代表坐标
   * xx :x方向需要增加的值
   * yy :y方向需要增加的值
   *
   * 例如xx = -1 yy= -1
   * 代表需要获取(x-1,y-1)的棋子
   *
   *  xx = 1 yy= 1
   * 代表需要获取(x+1,y+1)的棋子
   *
   */
计算权重( x, y,  xx,  yy ,初始权重 简称 -> 权重 , 你棋子的颜色 简称 -> 颜色){
    	//边缘检测防止超出棋盘位置
        if((x==0&&xx==-1)|| (x==15&&xx==1) || (y==0&&yy==-1) || (y== 15&&yy==1)) 
        return 权重; //不能计算墙以外的 所以直接返回权重
		/*上面这句话的意思是
			就拿 第一个 (x==0&&xx==-1) 来说:
			如果他的横坐标为x 那么 就不能再进行x-1操作了 因为棋盘就这么大
		*/
		
		//这里假设 xx 和 yy = 1
		//如果棋盘里的指定的格子(x+1,y+1)的颜色和你的棋子颜色一样 那么就对那个棋子在进行一次计算权重
        if(table[x+xx][y+yy] == 颜色){
            return 计算权重( x+xx,y+yy ,      xx , yy ,      权重+1,     颜色);//递归
            //             x坐标  y坐标    下一个棋子方向   本次计算权重+1   颜色不变
        }
        //如果不等直接把权重的值返回
        return 权重;
    }

Game pseudo-code operation

Here pseudo-code to write it

char table [16][16]; //棋盘

Player p1 = new Player("P1","黑");

//这里就不弄类了 懂啥意思就行
Player.下棋(){
	//里面定义的方法
	控制台输入 <- (4,6)
	table[4][6] = this.棋子颜色
	判断胜负(this.棋子颜色);
}


判断胜负(x,y,棋子颜色){

	...各种方向判断...

	这里就拿 xx = 1 ,yy = 1 来说  就是依次判断右下角是不是跟你下的棋子颜色一样
	
	//       这里判断右下角权重       这里是反方向判断左上角权重
	int n = ishas(x,y,xx,yy,0,c) + ishas(x,y,-xx,-yy,0,c);
	
	如果他们的权重之和等于4 那么
    if(n>=4)
    游戏结束



}

while(游戏结束条件){
	Player p = 获取下棋玩家(轮到谁返回谁);
	p.下棋()
	打印棋盘();
}

A very simple program written, this is the game running process
alone take this code creates two players, people who can play chess


Ai is how to play chess

PK methods between people who already know the following to say Ai chess algorithm ideas
or take weight chartHere Insert Picture Description

Primary Algorithm

  • I have found that the threat to grid I want to quickly fill pit
  • I'm not a threat to me go on my own

Pseudo-code implementation

char table [16][16]; //棋盘
List<棋子> 棋子列表;

// Ai 玩家 也是玩家 所以 他有玩家的功能 只不过下棋方式 是自己定义的
class AiPlayer extends Player{

	@重写
	下棋(){
		//里面定义的方法
		控制台输入 <- 智能计算(4,6)
		table[4][6] = this.棋子颜色
		判断胜负(this.棋子颜色);
	}
	
	智能计算(){
		*是否第一步--> return 随机中间棋子(8,8)
		// 自己棋子的颜色 简称 -> 自己
		// 对手棋子的颜色 简称 -> 对手
		遍历所有格子 ->结果(x,y,当前格子颜色){
			if(当前格子颜色 ==){
				//两个都要计算并且把计算结果保存到棋子类
				计算权重(x,y,对手)-->两个方向计算权重
				计算权重(x,y,自己)-->两个方向计算权重
				
				//如果权重一样那么 对手权重-1 让自己先来
				谁的权重大--> 棋子列表.加入棋子(new 棋子(x,y,权重))
			}
		}
		//遍历完毕
		棋子列表.按权重排序()
		返回棋子 = 棋子列表.get(0);
		return (返回棋子);
	}
}

After the update again advanced algorithms

  • Reverse calculate the weight algorithm
  • Stepping algorithm
  • Taking a stride algorithm
  • Projection algorithm: guess if you got this move computing rival Lazi whether their detriment
  • Global algorithms: every time you play chess -> conduct a forked tree calculation -> take the greatest chance of winning a move
  • Preliminary matrix method
  • wait wait wait…

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


Source connection: the Java backgammon games (console Ai pure algorithm)


Published 18 original articles · won praise 14 · views 7515

Guess you like

Origin blog.csdn.net/qq_18604209/article/details/104003789
Recommended