• 大小: 588KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-05-10
  • 语言: Java
  • 标签: AI  五子棋  JAVA  JPanel  

资源简介

压缩包内含代码与可执行jar包,直接idea打开就可以运行;
1.在jdk1.8环境下工作,用IDEA开发的JPanel窗口程序
2.功能主要为人机对战,支持悔棋,支持电脑先行(电脑先行一般很难赢它),支持重新开始
3.该五子棋AI使用了五元组的五子棋算法,以五个一组将棋盘分为一个个横竖斜的数组,计算各个组的权重,并不是用的极大极小值剪枝算法,算是一种新的思路,但是这个AI很强!大家都知道黑棋先手必胜,所以黑棋先手的情况下只要认真还是能赢的,但是让AI先行,就不好赢了,我水平很低,下了几把目前没赢过。

资源截图

代码片段和文件信息

package fiveBord;

public class Chessman extends DrawChessBoard{
    private int color;      //1-white,0-black
    private boolean placed = false;
    private int stepNum = 0;
    private int computerPieceX = 0;
    private int computerPieceY = 0;
    private int score;          //对该位置的打的分数

    public Chessman(int colorboolean placedint stepNumint computerPieceXint computerPieceY int score){
        this.color=color;
        this.placed=placed;
        this.stepNum=stepNum;
        this.computerPieceX=computerPieceX;
        this.computerPieceY=computerPieceY;
        this.score=score;
    }

    public boolean getPlaced() {
        return placed;
    }

    public void setPlaced(boolean placed) {
        this.placed = placed;
    }

    public int getColor() {
        return color;
    }

    public void setColor(int color) {
        this.color = color;
    }

    public int getStepNum() {
        return stepNum;
    }

    public void setStepNum(int stepNum) {
        this.stepNum = stepNum;
    }

    public int getComputerPieceX() {
        return computerPieceX;
    }

    public void setComputerPieceX(int computerPieceX) {
        this.computerPieceX = computerPieceX;
    }

    public int getComputerPieceY() {
        return computerPieceY;
    }

    public void setComputerPieceY(int computerPieceY) {
        this.computerPieceY = computerPieceY;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    //电脑落子
    public Chessman computerPiece(int computerColorChessman[][] cm){
        //每次都初始化下score评分数组
        int[][] chessboard = new int[ROWS][ROWS];   //与界面棋盘对应,0代表空,-1代表机器,1代表人类
        int[][] score = new int[ROWS][ROWS];        //每个位置得分

        for(int i = 0; i  < ROWS; i++){
            for(int j = 0; j < ROWS; j++){
                chessboard[i][j] = 0;
                if(cm[i][j]!=null && cm[i][j].getColor()==BLACK){
                    chessboard[i][j] = 1;
                }
                if(cm[i][j]!=null && cm[i][j].getColor()==WHITE){
                    chessboard[i][j] = -1;
                }
                score[i][j] = 0;
            }
        }

        //每次机器找寻落子位置,评分都重新算一遍(虽然算了很多多余的,因为上次落子时候算的大多都没变)
        //先定义一些变量
        int humanChessmanNum = 0;   //五元组中的黑棋数量
        int machineChessmanNum = 0; //五元组中的白棋数量
        int tupleScoreTmp = 0;      //五元组得分临时变量

        int goalX = -1;     //目标位置x坐标
        int goalY = -1;     //目标位置y坐标
        int maxScore = -1;  //最大分数

        //1.扫描横向的15个行
        for(int i = 0; i < 15; i++){
            for(int j = 0; j < 11; j++){
                int k = j;
                while(k < j + 5){

                    if(chessboard[i][k] == -1) machineChessmanNum++;
                    else if(chessboard[i][k] == 1)humanChessmanNum++;

                    k++;
                }
              

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2019-02-19 19:06  FiveBord\
     目录           0  2019-02-19 19:06  FiveBord\.idea\
     文件         433  2019-02-19 16:53  FiveBord\.idea\FiveBord.iml
     目录           0  2019-02-19 19:02  FiveBord\.idea\artifacts\
     文件         437  2019-02-19 19:02  FiveBord\.idea\artifacts\FiveBord_jar.xml
     文件         384  2019-02-19 16:52  FiveBord\.idea\misc.xml
     文件         275  2019-02-19 16:52  FiveBord\.idea\modules.xml
     文件        8915  2019-02-14 14:23  FiveBord\.idea\uiDesigner.xml
     文件       22056  2019-02-19 19:06  FiveBord\.idea\workspace.xml
     文件         433  2019-02-14 14:09  FiveBord\FiveBord.iml
     目录           0  2019-02-19 19:06  FiveBord\out\
     目录           0  2019-02-19 19:06  FiveBord\out\artifacts\
     目录           0  2019-02-19 19:06  FiveBord\out\artifacts\FiveBord_jar\
     文件      199062  2019-02-19 19:06  FiveBord\out\artifacts\FiveBord_jar\FiveBord.jar
     目录           0  2019-02-19 18:28  FiveBord\out\production\
     目录           0  2019-02-19 18:29  FiveBord\out\production\FiveBord\
     目录           0  2019-02-19 18:29  FiveBord\out\production\FiveBord\meta-INF\
     文件          52  2019-02-19 18:29  FiveBord\out\production\FiveBord\meta-INF\MANIFEST.MF
     目录           0  2019-02-19 19:05  FiveBord\out\production\FiveBord\fiveBord\
     文件        4854  2019-02-19 18:29  FiveBord\out\production\FiveBord\fiveBord\Chessman.class
     文件        1248  2019-02-19 18:59  FiveBord\out\production\FiveBord\fiveBord\DrawChessBoard$1.class
     文件        1404  2019-02-19 18:59  FiveBord\out\production\FiveBord\fiveBord\DrawChessBoard$2.class
     文件        1268  2019-02-19 18:59  FiveBord\out\production\FiveBord\fiveBord\DrawChessBoard$3.class
     文件        9143  2019-02-19 18:59  FiveBord\out\production\FiveBord\fiveBord\DrawChessBoard.class
     文件         898  2019-02-19 19:05  FiveBord\out\production\FiveBord\fiveBord\Main.class
     目录           0  2019-02-19 18:29  FiveBord\out\production\FiveBord\image\
     文件      204549  2019-02-19 18:29  FiveBord\out\production\FiveBord\image\chessboard.jpg
     目录           0  2019-02-19 18:28  FiveBord\src\
     目录           0  2019-02-19 18:28  FiveBord\src\meta-INF\
     文件          52  2019-02-15 10:28  FiveBord\src\meta-INF\MANIFEST.MF
     目录           0  2019-02-19 19:05  FiveBord\src\fiveBord\
............此处省略5个文件信息

评论

共有 条评论