# 六子棋 **Repository Path**: Airsku/connect6 ## Basic Information - **Project Name**: 六子棋 - **Description**: 人工智能六子棋实验。 - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2022-10-25 - **Last Updated**: 2025-11-25 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # G08+六子棋 ## 快速上手 #### 环境 ``` win11 idea2019.2 jdk1.8 / jdk11 maven3.8.4 //maven配置见maven文件夹下的pdf文件,maven包自行下载。 ``` 环境配完应该就能直接跑了。 ## 实验过程及结果 #### 预实验 ###### 代码实现 主要实现了AI类子类G08AI中的findMove方法。其中分别实现了全盘随机落子、中心随机落子、邻接随机落子三种随机落子的方法。通过findMove方法中的step选择使用哪一种落子方法。 ```java public class G08AI extends AI { @Override public String name() { return "G08-AI"; } Board board=new Board(); String s="ABCDEFGHIJKLMNOPQRS"; int[] getRandomPosition(int begin,int end)//在区域[0-18]内获取随机位置 { int[] arr=new int[2]; Random rd=new Random(); arr[0]=rd.nextInt(end-begin+1)+begin; arr[1]=rd.nextInt(end-begin+1)+begin; return arr; } Integer[] getAdjacentPosition(int[] pos) { Random rd=new Random(); int[][] offset={{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}}; ArrayList arr=new ArrayList<>(); for(int i=0;i<8;++i) { Integer[] temp={pos[0]+offset[i][0],pos[1]+offset[i][1]}; if(temp[0]>=0&&temp[0]<=18&&temp[1]>=0&&temp[1]<=18) { if(temp[0]==pos[0]&&temp[1]==pos[1])continue; arr.add(temp); } } if(arr.size()==0) { return null; } else { int r=rd.nextInt(arr.size()); return arr.get(r); } } @Override public Move findMove(Move opponentMove) throws Exception { if (opponentMove == null) { Move move = this.firstMove(); this.board.makeMove(move); return move; } else { this.board.makeMove(opponentMove); int step=3;//TODO:指定落子方式 int[] pos1=null;//pos[0]=col,pos[1]=row int[] pos2=null; int begin=0,end=0; if(step==1)//全盘范围随机落子 { end=18; do { do { pos1=getRandomPosition(begin,end); pos2=getRandomPosition(begin,end); } while(pos1[0]==pos2[0]&&pos1[1]==pos2[1]); } while(this.board.get(s.charAt(pos1[0]),s.charAt(pos1[1])) != PieceColor.EMPTY || this.board.get(s.charAt(pos2[0]),s.charAt(pos2[1])) != PieceColor.EMPTY); } else if(step==2)//相邻位置随机落子 { end=18; do { pos1=getRandomPosition(begin,end); Integer[] t=getAdjacentPosition(pos1); //System.out.println("cal done"); if(t==null) { pos2=getRandomPosition(begin,end); } else{ pos2=new int[2]; pos2[0]=t[0]; pos2[1]=t[1]; } } while(this.board.get(s.charAt(pos1[0]),s.charAt(pos1[1])) != PieceColor.EMPTY || this.board.get(s.charAt(pos2[0]),s.charAt(pos2[1])) != PieceColor.EMPTY); } else{//中心区域随机落子13*13 //产生随机数 col,raw in range 3-15 begin=3; end=15; int cnt=0; do { if(cnt>=10){//如果10次冲突则使用全盘范围 begin=0; end=18; }; do { pos1=getRandomPosition(begin,end); pos2=getRandomPosition(begin,end); } while(pos1[0]==pos2[0]&&pos1[1]==pos2[1]); ++cnt; } while(this.board.get(s.charAt(pos1[0]),s.charAt(pos1[1])) != PieceColor.EMPTY || this.board.get(s.charAt(pos2[0]),s.charAt(pos2[1])) != PieceColor.EMPTY); } Move move = new Move(s.charAt(pos1[0]),s.charAt(pos1[1]),s.charAt(pos2[0]),s.charAt(pos2[1])); this.board.makeMove(move); return move; } } } ``` #### 正式实验 具体看代码。 ## 参考 [1] [极大极小值算法α-β剪枝算法的理解](https://blog.csdn.net/bryant_xw/article/details/88857258?ops_request_misc=&request_id=&biz_id=102&utm_term=极大极小法&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-1-88857258.142^v59^js_top,201^v3^add_ask&spm=1018.2226.3001.4187) [2] [ 博弈基础——极大极小搜索](https://blog.csdn.net/u013351484/article/details/50789521#commentBox) [3] [十分钟讲解博弈树五子棋AI极大极小搜索Alpha-Beta](https://www.bilibili.com/video/BV1nU4y1V788/?spm_id_from=333.337.search-card.all.click&vd_source=3a25118ada7968aab76d84c68bdd988e)