当前位置: 移动技术网 > IT编程>开发语言>Java > Java实现五子棋AI算法

Java实现五子棋AI算法

2019年07月19日  | 移动技术网IT编程  | 我要评论
五子棋ai算法 也算是一个典型的游戏ai算法,一些棋类的ai算法都可以参考实现,下面是java实现代码 棋盘抽象接口 import java.util.li

五子棋ai算法 也算是一个典型的游戏ai算法,一些棋类的ai算法都可以参考实现,下面是java实现代码

棋盘抽象接口

import java.util.list; 
 
 
public interface ichessboard { 
  //取得棋盘最大横坐标 
  public int getmaxx(); 
  //最大纵坐标 
  public int getmaxy(); 
  //取得当前所有空白点,这些点才可以下棋 
  public list<point> getfreepoints(); 
} 

棋子类实现

//棋子类 
public class point { 
  // 这了性能,设成公有 
  public int x; 
  public int y; 
   
 
  public int getx() { 
    return x; 
  } 
 
  public point setx(int x) { 
    this.x = x; 
    return this; 
  } 
 
  public int gety() { 
    return y; 
  } 
 
  public point sety(int y) { 
    this.y = y; 
    return this; 
  } 
 
  public point(int x, int y) { 
    this.x = x; 
    this.y = y; 
  } 
 
 
  @override 
  public int hashcode() { 
    return x + y; 
  } 
 
  @override 
  public boolean equals(object obj) { 
    if (this == obj) 
      return true; 
    point other = (point) obj; 
    if (x != other.x) 
      return false; 
    if (y != other.y) 
      return false; 
    return true; 
  } 
 
} 

玩家抽象接口

import java.util.list; 
 
public interface iplayer { 
  //下一步棋子,传入对手已经下的棋子集合 
  public void run(list<point> enemypoints, point point); 
 
  public boolean haswin(); 
   
  public void setchessboard(ichessboard chessboard); 
   
  public list<point> getmypoints(); 
} 

玩家基础抽象类

import java.util.arraylist; 
import java.util.list; 
 
public abstract class baseplayer implements iplayer { 
  //我已下的棋子 
  protected list<point> mypoints = new arraylist<point>(200); 
  //棋盘 
  protected ichessboard chessboard; 
  //棋盘最大横坐标和纵标, 
  protected int maxx; 
  protected int maxy; 
   
  //所有空白棋子 
  protected list<point> allfreepoints; 
 
  @override 
  public final list<point> getmypoints() { 
    return mypoints; 
  } 
 
  @override 
  public void setchessboard(ichessboard chessboard) { 
    this.chessboard = chessboard; 
    allfreepoints = chessboard.getfreepoints(); 
    maxx = chessboard.getmaxx(); 
    maxy = chessboard.getmaxy(); 
    mypoints.clear(); 
  } 
   
  private final point temp = new point(0, 0); 
  //我是否是否赢了 
  public final boolean haswin(){ 
    if(mypoints.size()<5){ 
      return false; 
    } 
    point point = mypoints.get(mypoints.size()-1); 
    int count = 1; 
    int x=point.getx(),y=point.gety(); 
    //横向-- 
    temp.setx(x).sety(y); 
    while (mypoints.contains(temp.setx(temp.getx()-1)) && temp.getx()>=0 && count<5) { 
      count ++; 
    } 
    if(count>=5){ 
      return true; 
    } 
    temp.setx(x).sety(y); 
    while (mypoints.contains(temp.setx(temp.getx()+1)) && temp.getx()<maxx && count<5) { 
      count ++; 
    } 
    if(count>=5){ 
      return true; 
    } 
    //纵向| 
    count = 1; 
    temp.setx(x).sety(y); 
    while (mypoints.contains(temp.sety(temp.gety()-1)) && temp.gety()>=0) { 
      count ++; 
    } 
    if(count>=5){ 
      return true; 
    } 
    temp.setx(x).sety(y); 
    while (mypoints.contains(temp.sety(temp.gety()+1)) && temp.gety()<maxy && count<5) { 
      count ++; 
    } 
    if(count>=5){ 
      return true; 
    } 
    //正斜向 / 
    count =1; 
    temp.setx(x).sety(y); 
    while (mypoints.contains(temp.setx(temp.getx()-1).sety(temp.gety()+1)) && temp.getx()>=0 && temp.gety()<maxy) { 
      count ++; 
    } 
    if(count>=5){ 
      return true; 
    } 
    temp.setx(x).sety(y); 
    while (mypoints.contains(temp.setx(temp.getx()+1).sety(temp.gety()-1)) && temp.getx()<maxx && temp.gety()>=0 && count<6) { 
      count ++; 
    } 
    if(count>=5){ 
      return true; 
    } 
    //反斜 \ 
    count = 1; 
    temp.setx(x).sety(y); 
    while (mypoints.contains(temp.setx(temp.getx()-1).sety(temp.gety()-1)) && temp.getx()>=0 && temp.gety()>=0) { 
      count ++; 
    } 
    if(count>=5){ 
      return true; 
    } 
    temp.setx(x).sety(y); 
    while (mypoints.contains(temp.setx(temp.getx()+1).sety(temp.gety()+1)) && temp.getx()<maxx && temp.gety()<maxy && count<5) { 
      count ++; 
    } 
    if(count>=5){ 
      return true; 
    } 
    return false; 
  } 
} 

电脑ai类实现

import java.util.arraylist; 
import java.util.collections; 
import java.util.hashmap; 
import java.util.list; 
import java.util.map; 
 
//算法核心类,算法的主体思想分三个步骤, 
//第一步:根据双方的当前的形势循环地假设性的分别给自己和对方下一子(在某个范围内下子),并判断此棋子能带来的形势上的变化,如能不能冲4,能不能形成我方或敌方双3等, 
//第二步:根据上一步结果,组合每一步棋子所带来的所有结果(如某一步棋子可能形成我方1个活3,1个冲4(我叫它半活4)等),包括敌方和我方的。 
//第三步:根据用户给的规则对上一步结果进行排序,并选子(有进攻形、防守形规则) 
public class basecomputerai extends baseplayer { 
 
  // 四个方向,横- 、纵| 、正斜/ 、反斜\ 
  private static final int heng = 0; 
  private static final int zhong = 1; 
  private static final int zheng_xie = 2; 
  private static final int fan_xie = 3; 
  //往前往后 
  private static final boolean forward = true; 
  private static final boolean backward = false; 
   
   
   
  //标示分析结果当前点位是两头通(alive)还是只有一头通(half_alive),封死的棋子分析过程自动屏蔽,不作为待选棋子 
  private static final int alive = 1; 
  private static final int half_alive = 0; 
  //private static final int dead = -1; 
   
  //计算范围,太大的范围会有性能问题 
  private class calcuterange{ 
    int xstart,ystart,xstop,ystop; 
    private calcuterange(int xstart, int ystart, int xstop, int ystop) { 
      this.xstart = xstart; 
      this.ystart = ystart; 
      this.xstop = xstop; 
      this.ystop = ystop; 
    } 
  } 
   
   
  //限定电脑计算范围,如果整个棋盘计算,性能太差,目前是根据所有已下的棋子的边界值加range_step值形成,目前为1 
  private static final int range_step = 1; 
  calcuterange currentrange = new calcuterange(0, 0, 0, 0); 
  private void initrange(list<point> comuters, list<point> humans){ 
    currentrange.xstart = humans.get(0).getx()-range_step; 
    currentrange.ystart = humans.get(0).gety()-range_step; 
    currentrange.xstop = humans.get(0).getx()+range_step; 
    currentrange.ystop = humans.get(0).gety()+range_step; 
    for (point point : humans) { 
      if(point.getx()-range_step<currentrange.xstart){ 
        currentrange.xstart = point.getx()-range_step; 
      }else if(point.getx()+range_step>currentrange.xstop){ 
        currentrange.xstop = point.getx()+range_step; 
      } 
      if(point.gety()-range_step<currentrange.ystart){ 
        currentrange.ystart = point.gety()-range_step; 
      }else if(point.gety()+range_step>currentrange.ystop){ 
        currentrange.ystop = point.gety()+range_step; 
      } 
    } 
    for (point point : comuters) { 
      if(point.getx()-range_step<currentrange.xstart){ 
        currentrange.xstart = point.getx()-range_step; 
      }else if(point.getx()+range_step>currentrange.xstop){ 
        currentrange.xstop = point.getx()+range_step; 
      } 
      if(point.gety()-range_step<currentrange.ystart){ 
        currentrange.ystart = point.gety()-range_step; 
      }else if(point.gety()+range_step>currentrange.ystop){ 
        currentrange.ystop = point.gety()+range_step; 
      } 
    } 
     
    //如果范围扩大后超过了棋盘,则等于棋盘 
    currentrange.xstart=currentrange.xstart<0?0:currentrange.xstart; 
    currentrange.ystart=currentrange.ystart<0?0:currentrange.ystart; 
    currentrange.xstop=currentrange.xstop>=maxx?maxx-1:currentrange.xstop; 
    currentrange.ystop=currentrange.ystop>=maxy?maxy-1:currentrange.ystop; 
  } 
 
  // 分析当前形式的入口方法,分析总共分三个步骤,第三步骤可由子类干预以作难度控制 
  private point doanalysis(list<point> comuters, list<point> humans) { 
    if(humans.size()==1){//第一步 
      return getfirstpoint(humans); 
    } 
     
    //初始化计算范围 
    initrange(comuters, humans); 
     
    //清除以前的结果 
    initanalysisresults(); 
    // 开始分析,扫描所有空白点,形成第一次分析结果 
    point bestpoint = dofirstanalysis(comuters, humans); 
    if(bestpoint!=null){ 
      //system.out.println("这个棋子最重要,只能下这个棋子"); 
      return bestpoint; 
    } 
    // 分析第一次结果,找到自己的最佳点位 
    bestpoint = docomputersencondanalysis(computerfirstresults,computersencodresults); 
    if(bestpoint!=null){ 
      //system.out.println("快要赢了,就下这个棋子"); 
      return bestpoint; 
    } 
    computerfirstresults.clear(); 
    system.gc(); 
    // 分析第一次结果,找到敌人的最佳点位 
    bestpoint = dohumansencondanalysis(humanfirstresults,humansencodresults); 
    if(bestpoint!=null){ 
      //system.out.println("再不下这个棋子就输了"); 
      return bestpoint; 
    } 
    humanfirstresults.clear(); 
    system.gc(); 
    //没找到绝杀点,第三次结果分析 
    return dothirdanalysis(); 
  } 
   
 
  //下第一步棋子,不需要复杂的计算,根据人类第一步棋子x值减1完成 
  private point getfirstpoint(list<point> humans) { 
    point point = humans.get(0); 
    if(point.getx()==0 || point.gety()==0 || point.getx()==maxx && point.gety()==maxy) 
      return new point(maxx/2, maxy/2); 
    else{ 
      return new point(point.getx()-1,point.gety()); 
    } 
  } 
 
// private int debugx,debugy;//用于debug 
 
  // 开始分析,扫描所有空白点,形成第一次分析结果 
  private point dofirstanalysis(list<point> comuters, list<point> humans){ 
    int size = allfreepoints.size(); 
    point computerpoint = null; 
    point humanpoint = null; 
    int x,y; 
    firstanalysisresult firstanalysisresult; 
    for (int i = 0; i < size; i++) { 
      computerpoint = allfreepoints.get(i); 
      //先把x、y坐标记下来,因为在分析过程中会改变原来的对象 
      x = computerpoint.getx(); 
      y = computerpoint.gety(); 
      if(x<currentrange.xstart || x>currentrange.xstop || y<currentrange.ystart || y>currentrange.ystop){ 
        continue; 
      } 
       
//     if(x==debugx && y==debugy){ 
//       system.out.println("sssssssssssss"); 
//     } 
       
      //尝试在此位置上下一个棋子,并分析在“横向”这个方向上我方可形成的状态,如活4,活3,半活4,活2等所有状态 
      firstanalysisresult = tryandcountresult(comuters,humans, computerpoint, heng); 
      computerpoint.setx(x).sety(y);//回复点位的原值,以供下次分析 
      if(firstanalysisresult!=null){//无返回结果此方向上不可能达到五个棋子, 
        if(firstanalysisresult.count==5)//等于5表示在此点上下棋子即可连成5个,胜利了,不再往下进行分析 
          return computerpoint; 
        //记录第一次分析结果 
        addtofirstanalysisresult(firstanalysisresult,computerfirstresults); 
      } 
       
      //在“纵向”这个方向上重复上面的步骤 
      firstanalysisresult = tryandcountresult(comuters,humans, computerpoint, zhong); 
      computerpoint.setx(x).sety(y); 
      if(firstanalysisresult!=null){//死棋,不下 
        if(firstanalysisresult.count==5) 
          return computerpoint; 
         
        addtofirstanalysisresult(firstanalysisresult,computerfirstresults); 
      } 
       
      //正斜向 
      firstanalysisresult = tryandcountresult(comuters,humans, computerpoint, zheng_xie); 
      computerpoint.setx(x).sety(y); 
      if(firstanalysisresult!=null){//死棋,不下 
        if(firstanalysisresult.count==5) 
          return computerpoint; 
         
        addtofirstanalysisresult(firstanalysisresult,computerfirstresults); 
      } 
       
      //反斜向 
      firstanalysisresult = tryandcountresult(comuters,humans, computerpoint, fan_xie); 
      computerpoint.setx(x).sety(y); 
      if(firstanalysisresult!=null){//死棋,不下 
        if(firstanalysisresult.count==5) 
          return computerpoint; 
         
        addtofirstanalysisresult(firstanalysisresult,computerfirstresults); 
      } 
       
      //在“横向”上分析此棋子可在敌方形成如何状态,如敌方的活3、半活4等 
      firstanalysisresult = tryandcountresult(humans,comuters, computerpoint, heng); 
      computerpoint.setx(x).sety(y); 
      if(firstanalysisresult!=null){//死棋,不下 
        if(firstanalysisresult.count==5) 
          humanpoint = computerpoint; 
         
        addtofirstanalysisresult(firstanalysisresult,humanfirstresults); 
      } 
       
      //“纵向” 
      firstanalysisresult = tryandcountresult(humans,comuters, computerpoint, zhong); 
      computerpoint.setx(x).sety(y); 
      if(firstanalysisresult!=null){//死棋,不下 
        if(firstanalysisresult.count==5) 
          humanpoint = computerpoint; 
         
        addtofirstanalysisresult(firstanalysisresult,humanfirstresults); 
      } 
       
      //“正斜” 
      firstanalysisresult = tryandcountresult(humans,comuters, computerpoint, zheng_xie); 
      computerpoint.setx(x).sety(y); 
      if(firstanalysisresult!=null){//死棋,不下 
        if(firstanalysisresult.count==5) 
          humanpoint = computerpoint; 
         
        addtofirstanalysisresult(firstanalysisresult,humanfirstresults); 
      } 
       
      //“反斜” 
      firstanalysisresult = tryandcountresult(humans,comuters, computerpoint, fan_xie); 
      computerpoint.setx(x).sety(y); 
      if(firstanalysisresult!=null){//死棋,不下 
        if(firstanalysisresult.count==5) 
          humanpoint = computerpoint; 
         
        addtofirstanalysisresult(firstanalysisresult,humanfirstresults); 
      } 
    } 
    //如果没有绝杀棋子,第一次分析不需要返回结果 
    return humanpoint; 
  } 
   
  //第二次分析,分析第一次形成的结果,第一次分析结果会把一步棋在四个方向上可形成的结果生成最多四个firstanalysisresult对象(敌我各四) 
  //这里要把这四个对象组合成一个sencondanalysisresult对象, 
  private point docomputersencondanalysis(map<point,list<firstanalysisresult>> firstresults,list<sencondanalysisresult> sencodresults) { 
    list<firstanalysisresult> list = null; 
    sencondanalysisresult sr = null; 
    for (point p : firstresults.keyset()) { 
      sr = new sencondanalysisresult(p); 
      list = firstresults.get(p); 
      for (firstanalysisresult result : list) { 
        if(result.count==4){ 
          if(result.alivestate==alive){//经过前面的过滤,双方都排除了绝杀棋,有活4就下这一步了,再下一步就赢了 
            return result.point;//如果有绝杀,第一轮已返回,在此轮活4已经是好的棋子,直接返回,不再往下分析 
          }else{ 
            sr.halfalive4 ++; 
            computer4halfalives.add(sr); 
          } 
        }else if(result.count==3){ 
          if(result.alivestate==alive){ 
            sr.alive3++; 
            if(sr.alive3==1){ 
              computer3alives.add(sr); 
            }else{ 
              computerdouble3alives.add(sr); 
            } 
          }else{ 
            sr.halfalive3++; 
            computer3halfalives.add(sr); 
          } 
        }else{//半活2在第一阶段已被排除,不再处理 
          sr.alive2++; 
          if(sr.alive2==1){ 
            computer2alives.add(sr); 
          }else{ 
            computerdouble2alives.add(sr); 
          } 
        } 
      } 
      sencodresults.add(sr); 
    } 
    //没有找到活4 
    return null; 
  } 
   
  //这个方法和上面的基本一样,但为了性能,少作几次判断,将人类和电脑的分开了 
  private point dohumansencondanalysis(map<point,list<firstanalysisresult>> firstresults,list<sencondanalysisresult> sencodresults) { 
    list<firstanalysisresult> list = null; 
    sencondanalysisresult sr = null; 
    for (point p : firstresults.keyset()) { 
      sr = new sencondanalysisresult(p); 
      list = firstresults.get(p); 
      for (firstanalysisresult result : list) { 
        if(result.count==4){ 
          if(result.alivestate==alive){ 
            human4alives.add(sr); 
          }else{ 
            sr.halfalive4 ++; 
            human4halfalives.add(sr); 
          } 
        }else if(result.count==3){ 
          if(result.alivestate==alive){ 
            sr.alive3++; 
            if(sr.alive3==1){ 
              human3alives.add(sr); 
            }else{ 
              humandouble3alives.add(sr); 
            } 
          }else{ 
            sr.halfalive3++; 
            human3halfalives.add(sr); 
          } 
        }else{ 
          sr.alive2++; 
          if(sr.alive2==1){ 
            human2alives.add(sr); 
          }else{ 
            humandouble2alives.add(sr); 
          } 
        } 
      } 
      sencodresults.add(sr); 
    } 
    //没有找到活4 
    return null; 
  } 
   
  private void sleep(int minisecond){ 
    try { 
      thread.sleep(minisecond); 
    } catch (interruptedexception e) { 
    } 
  } 
   
   
  //第三次分析,双方都不可以制造活4,找双活3棋子,不行就找半活4,再不行就找单活3,双活2 
  private point dothirdanalysis() { 
    if(!computer4halfalives.isempty()){ 
      return computer4halfalives.get(0).point; 
    } 
    system.gc(); 
    sleep(300); 
    collections.sort(computersencodresults); 
    system.gc(); 
     
    //即将单活4,且我没有半活4以上的,只能堵 
    point mostbest = getbestpoint(human4alives, computersencodresults); 
    if(mostbest!=null) 
      return mostbest; 
     
    collections.sort(humansencodresults); 
    system.gc(); 
     
    mostbest = getbestpoint(); 
    if(mostbest!=null) 
      return mostbest; 
     
    //拿出各自排第一的,谁好就下谁 
    return computersencodresults.get(0).point; 
  } 
   
  //子类实现这个方法,并改变其顺序可以实现防守为主还是猛攻 
  protected point getbestpoint(){ 
    //即将单活4,且我没有半活4以上的,只能堵 
    point mostbest = getbestpoint(computerdouble3alives, humansencodresults); 
    if(mostbest!=null) 
      return mostbest; 
     
    mostbest = getbestpoint(computer3alives, humansencodresults); 
    if(mostbest!=null) 
      return mostbest; 
     
    mostbest = getbestpoint(humandouble3alives, computersencodresults); 
    if(mostbest!=null) 
      return mostbest; 
     
    mostbest = getbestpoint(human3alives, computersencodresults); 
    if(mostbest!=null) 
      return mostbest; 
 
    mostbest = getbestpoint(computerdouble2alives, humansencodresults); 
    if(mostbest!=null) 
      return mostbest; 
     
    mostbest = getbestpoint(computer2alives, humansencodresults); 
    if(mostbest!=null) 
      return mostbest; 
     
    mostbest = getbestpoint(computer3halfalives, humansencodresults); 
    if(mostbest!=null) 
      return mostbest; 
     
    mostbest = getbestpoint(human4halfalives, computersencodresults); 
    if(mostbest!=null) 
      return mostbest; 
     
    mostbest = getbestpoint(humandouble2alives, computersencodresults); 
    if(mostbest!=null) 
      return mostbest; 
     
    mostbest = getbestpoint(human2alives, computersencodresults); 
    if(mostbest!=null) 
      return mostbest; 
     
    mostbest = getbestpoint(human3halfalives, computersencodresults); 
    return mostbest; 
  } 
   
   
  //第三次分析的最后一步,第二次结果已经过排序,在此可以从前面选出最好的棋子 
  protected point getbestpoint(list<sencondanalysisresult> mybest,list<sencondanalysisresult> yoursencodresults){ 
    if(!mybest.isempty()){ 
      if(mybest.size()>1){ 
        for (sencondanalysisresult your : yoursencodresults) { 
          if(mybest.contains(your)){ 
            return your.point; 
          } 
        } 
        return mybest.get(0).point; 
      }else{ 
        return mybest.get(0).point; 
      } 
    } 
    return null; 
  } 
   
   
  //第一次分析结果 
  private final map<point,list<firstanalysisresult>> computerfirstresults = new hashmap<point,list<firstanalysisresult>>(); 
  private final map<point,list<firstanalysisresult>> humanfirstresults = new hashmap<point,list<firstanalysisresult>>(); 
  //第二次总结果 
  protected final list<sencondanalysisresult> computersencodresults = new arraylist<sencondanalysisresult>(); 
  protected final list<sencondanalysisresult> humansencodresults = new arraylist<sencondanalysisresult>(); 
  //第二次分结果,电脑 
  protected final list<sencondanalysisresult> computer4halfalives = new arraylist<sencondanalysisresult>(2); 
  protected final list<sencondanalysisresult> computerdouble3alives = new arraylist<sencondanalysisresult>(4); 
  protected final list<sencondanalysisresult> computer3alives = new arraylist<sencondanalysisresult>(5); 
  protected final list<sencondanalysisresult> computerdouble2alives = new arraylist<sencondanalysisresult>(); 
  protected final list<sencondanalysisresult> computer2alives = new arraylist<sencondanalysisresult>(); 
  protected final list<sencondanalysisresult> computer3halfalives = new arraylist<sencondanalysisresult>(); 
   
  //第二次分结果,人类 
  protected final list<sencondanalysisresult> human4alives = new arraylist<sencondanalysisresult>(2); 
  protected final list<sencondanalysisresult> human4halfalives = new arraylist<sencondanalysisresult>(5); 
  protected final list<sencondanalysisresult> humandouble3alives = new arraylist<sencondanalysisresult>(2); 
  protected final list<sencondanalysisresult> human3alives = new arraylist<sencondanalysisresult>(10); 
  protected final list<sencondanalysisresult> humandouble2alives = new arraylist<sencondanalysisresult>(3); 
  protected final list<sencondanalysisresult> human2alives = new arraylist<sencondanalysisresult>(); 
  protected final list<sencondanalysisresult> human3halfalives = new arraylist<sencondanalysisresult>(); 
   
  //第一次分析前清空上一步棋子的分析结果 
  private void initanalysisresults(){ 
    computerfirstresults.clear(); 
    humanfirstresults.clear(); 
    //第二次总结果 
    computersencodresults.clear(); 
    humansencodresults.clear(); 
    //第二次分结果 
    computer4halfalives.clear(); 
    computerdouble3alives.clear(); 
    computer3alives.clear(); 
    computerdouble2alives.clear(); 
    computer2alives.clear(); 
    computer3halfalives.clear(); 
     
    //第二次分结果,人类 
    human4alives.clear(); 
    human4halfalives.clear(); 
    humandouble3alives.clear(); 
    human3alives.clear(); 
    humandouble2alives.clear(); 
    human2alives.clear(); 
    human3halfalives.clear(); 
    system.gc(); 
  } 
   
  //加入到第一次分析结果中 
  private void addtofirstanalysisresult(firstanalysisresult result,map<point,list<firstanalysisresult>> dest){ 
    if(dest.containskey(result.point)){ 
      dest.get(result.point).add(result); 
    }else{ 
      list<firstanalysisresult> list = new arraylist<firstanalysisresult>(1); 
      list.add(result); 
      dest.put(result.point, list); 
    } 
  } 
   
   
  //第一次分析结果类 
  private class firstanalysisresult{ 
    //连续数 
    int count; 
    //点位 
    point point; 
    //方向 
    int direction; 
    //状态 
    int alivestate; 
    private firstanalysisresult(int count, point point, int direction) { 
      this(count, point, direction, alive); 
    } 
     
    private firstanalysisresult(int count, point point, int direction,int alivestate) { 
      this.count = count; 
      this.point = point; 
      this.direction = direction; 
      this.alivestate = alivestate; 
    } 
     
 
     
    private firstanalysisresult init(point point,int direction,int alivestate){ 
      this.count = 1; 
      this.point = point; 
      this.direction = direction; 
      this.alivestate = alivestate; 
      return this; 
    } 
     
    private firstanalysisresult cloneme(){ 
      return new firstanalysisresult(count, point, direction,alivestate); 
    } 
     
  } 
   
  //第二次分析结果类 
  class sencondanalysisresult implements comparable<sencondanalysisresult>{ 
    int alive4 = 0; 
    //活3数量 
    int alive3 = 0; 
    //半活4,一头封的 
    int halfalive4 = 0; 
    //半活3,一头封的 
    int halfalive3 = 0; 
    //活2数量 
    int alive2 = 0; 
    //点位 
    point point; 
     
    @override 
    public int hashcode() { 
      final int prime = 31; 
      int result = 1; 
      result = prime * result + ((point == null) ? 0 : point.hashcode()); 
      return result; 
    } 
    @override 
    public boolean equals(object obj) { 
      sencondanalysisresult other = (sencondanalysisresult) obj; 
      if (point == null) { 
        if (other.point != null) 
          return false; 
      } else if (!point.equals(other.point)) 
        return false; 
      return true; 
    } 
 
    private sencondanalysisresult(point point) { 
      this.point = point; 
    } 
     
     
    //第三次分析时,对第二次分析结果进行排序,此为排序回调函数 
    @override 
    public int compareto(sencondanalysisresult another) { 
      return comparetowresult(this, another); 
    } 
       
  } 
   
  //返加-1则第一个参数优先,1则第二个参数优先,0则按原来顺序 
  private int comparetowresult(sencondanalysisresult oneresult,sencondanalysisresult another){ 
    if(oneresult.alive4>another.alive4){ 
      return -1; 
    } 
    if(oneresult.alive4<another.alive4){ 
      return 1; 
    } 
    if(oneresult.halfalive4>another.halfalive4){ 
      return -1; 
    } 
    if(oneresult.halfalive4<another.halfalive4){ 
      return 1; 
    } 
    if(oneresult.alive3>another.alive3){ 
      return -1; 
    } 
    if(oneresult.alive3<another.alive3){ 
      return 1; 
    } 
    if(oneresult.alive2>another.alive2){ 
      return -1; 
    } 
    if(oneresult.alive2<another.alive2){ 
      return 1; 
    } 
    if(oneresult.halfalive3>another.halfalive3){ 
      return -1; 
    } 
    if(oneresult.halfalive3>another.halfalive3){ 
      return 1; 
    } 
    return 0; 
  } 
   
   
  //一个临时对象,供第一次分析时临时存放分析结果使用,如果分析出有活1以上(不含)的结果,则调用其cloneme方法获得结果,否则抛弃此结果 
  private final firstanalysisresult far = new firstanalysisresult(1, null, heng); 
  // 分析如果在当前位下一子,会形成某个方向上多少个子,参数:当前己方已下的所有点,当前要假设的点,需要判断的方向 
  private firstanalysisresult tryandcountresult(list<point> mypoints,list<point> enemypoints, point point,int direction) { 
    int x = point.getx(); 
    int y = point.gety(); 
    firstanalysisresult fr = null; 
     
    int maxcountonthisdirection = maxcountonthisdirection(point, enemypoints, direction, 1); 
    if(maxcountonthisdirection<5){ 
      //无意义的棋子 
      return null;//此方向不足五个空位,已排除己方已下的棋子 
    }else if(maxcountonthisdirection==5){ 
      //半死状态,当是一头通 
      fr = far.init(point, direction,half_alive); 
    }else{ 
      //两头皆通 
      fr = far.init(point, direction,alive); 
    } 
     
    //在前和后的方向上计算一次 
    countpoint(mypoints,enemypoints,point.setx(x).sety(y),fr,direction,forward); 
    countpoint(mypoints,enemypoints,point.setx(x).sety(y),fr,direction,backward); 
     
     
    if(fr.count<=1 || (fr.count==2 && fr.alivestate==half_alive)){//活1,半活2及其以下结果,抛弃 
      return null; 
    } 
    //返回复制的结果 
    return fr.cloneme(); 
  } 
   
  //棋子出了墙 
  private boolean isoutsideofwall(point point,int direction){ 
    if(direction==heng){ 
      return point.getx()<0 || point.getx()>=maxx;//最大的x和y值均在墙外所以用等号 
    }else if(direction==zhong){ 
      return point.gety()<0 || point.gety()>=maxy; 
    }else{//这里可能有问题 
      return point.getx()<0 || point.gety()<0 || point.getx()>=maxx || point.gety()>=maxy; 
    } 
  } 
   
  private point pointtonext(point point,int direction,boolean forward){ 
    switch (direction) { 
      case heng: 
        if(forward) 
          point.x++; 
        else 
          point.x--; 
        break; 
      case zhong: 
        if(forward) 
          point.y++; 
        else 
          point.y--; 
        break; 
      case zheng_xie: 
        if(forward){ 
          point.x++; 
          point.y--; 
        }else{ 
          point.x--; 
          point.y++; 
        } 
        break; 
      case fan_xie: 
        if(forward){ 
          point.x++; 
          point.y++; 
        }else{ 
          point.x--; 
          point.y--; 
        } 
        break; 
    } 
    return point; 
  } 
   
  //在某个方向(八个中的一个)可下多少棋子,这个方法是第一分析中的核心方法 
  private void countpoint(list<point> mypoints, list<point> enemypoints, point point, firstanalysisresult fr,int direction,boolean forward) { 
    if(mypoints.contains(pointtonext(point,direction,forward))){ 
      fr.count ++; 
      if(mypoints.contains(pointtonext(point,direction,forward))){ 
        fr.count ++; 
        if(mypoints.contains(pointtonext(point,direction,forward))){ 
          fr.count ++; 
          if(mypoints.contains(pointtonext(point,direction,forward))){ 
            fr.count ++; 
          }else if(enemypoints.contains(point) || isoutsideofwall(point,direction)){ 
            fr.alivestate=half_alive; 
          } 
        }else if(enemypoints.contains(point) || isoutsideofwall(point,direction)){ 
          fr.alivestate=half_alive; 
        } 
      }else if(enemypoints.contains(point) || isoutsideofwall(point,direction)){ 
        fr.alivestate=half_alive; 
      } 
    }else if(enemypoints.contains(point) || isoutsideofwall(point,direction)){ 
      fr.alivestate=half_alive; 
    } 
  } 
   
   
 
  //在某个方向上是否还能下到满五个棋子 
  private int maxcountonthisdirection(point point,list<point> enemypoints,int direction,int count){ 
    int x=point.getx(),y=point.gety(); 
    switch (direction) { 
    //横向 
    case heng: 
      while (!enemypoints.contains(point.setx(point.getx()-1)) && point.getx()>=0 && count<6) { 
        count ++; 
      } 
      point.setx(x); 
      while (!enemypoints.contains(point.setx(point.getx()+1)) && point.getx()<maxx && count<6) { 
        count ++; 
      } 
      break; 
    //纵向 
    case zhong: 
      while (!enemypoints.contains(point.sety(point.gety()-1)) && point.gety()>=0) { 
        count ++; 
      } 
      point.sety(y); 
      while (!enemypoints.contains(point.sety(point.gety()+1)) && point.gety()<maxy && count<6) { 
        count ++; 
      } 
      break; 
    //正斜向 / 
    case zheng_xie: 
      while (!enemypoints.contains(point.setx(point.getx()-1).sety(point.gety()+1)) && point.getx()>=0 && point.gety()<maxy) { 
        count ++; 
      } 
      point.setx(x).sety(y); 
      while (!enemypoints.contains(point.setx(point.getx()+1).sety(point.gety()-1)) && point.getx()<maxx && point.gety()>=0 && count<6) { 
        count ++; 
      } 
      break; 
    //反斜 / 
    case fan_xie: 
      while (!enemypoints.contains(point.setx(point.getx()-1).sety(point.gety()-1)) && point.getx()>=0 && point.gety()>=0) { 
        count ++; 
      } 
      point.setx(x).sety(y); 
      while (!enemypoints.contains(point.setx(point.getx()+1).sety(point.gety()+1)) && point.getx()<maxx && point.gety()<maxy && count<6) { 
        count ++; 
      } 
      break; 
    } 
    return count; 
  } 
   
  //下棋子,对外接口 
  @override 
  public void run(list<point> humans,point p) { 
    //把人类下的最后一步棋子去除 
    allfreepoints.remove(humans.get(humans.size()-1)); 
    //电脑可以下的一步棋子 
    point result = doanalysis(mypoints, humans); 
    //去除电脑下的棋子 
    allfreepoints.remove(result); 
    //加入到电脑棋子中,下棋了 
    mypoints.add(result); 
  } 
} 

人类玩家实现起来就非常简单

import java.util.list; 
 
public class humanplayer extends baseplayer { 
 
  @override 
  public void run(list<point> enemypoints,point p) { 
    getmypoints().add(p); 
    allfreepoints.remove(p); 
  } 
} 

总结:虽然是java写的但算法已被抽象可以方便的修改成各种平台的实现。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网