zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Java 自动下棋

2023-09-11 14:18:49 时间

学习 Java 写的代码片段增加熟练度。

第一步定义一个棋盘(这里用二维数组)和棋盘大小

  //初始化棋盘
    public void initBoard() {
        board = new String[BOARD_SIZE][BOARD_SIZE];
        for (int i = 0; i < board.length; i++) {
//            String[] itemArg = board[i];
            for (int j = 0; j < board.length; j++) {
                board[i][j] = "┼";
            }
        }
    }
    //打印棋盘
    public void printData() {
        System.out.println("------start-----");
        for (int i = 0; i < board.length; i++) {
            for (String item :
                    board[i]) {
                System.out.print(item);
            }
            System.out.println();//一行结束换行打印
        }
        System.out.print("-------end-------");
        System.out.print("\n\n\n\n");
    }

随机获取棋子的位置 小棋盘大小用到Math.random(), do{}while(boolean);

Math.random() * 5  随机出来的结果是从 0 - 5 的浮点类型

do{}while(boolean); 先执行代码块,在判断条件 ture 继续循环 false 结束循环。

//随机获取出棋子位置(x,y)
private int[] getPosition(ArrayList<int[]> historyPosition) {
    int[] p = new int[2];
    do {
        p[0] = (int) (Math.random() * BOARD_SIZE);//[0,5)
        p[1] = (int) (Math.random() * BOARD_SIZE);
    } while (dataRepeat(p, historyPosition));//判断当前位置是否已有棋子
    return p;
}

判断当前位置是否有棋子用到 for each{}

//当前位置已有棋子重新获取
private boolean dataRepeat(int[] positionBoard, ArrayList<int[]> hpArg) {
    boolean repeat = false;
    for (int[] hp:
         hpArg) {
        repeat = Arrays.equals(positionBoard, hp);
        if(repeat)
            return repeat;
    }
    return repeat;
}

全部代码

class Gobang {
    //定义棋盘大小
    private static int BOARD_SIZE = 5;
    //定义一个二维数组
    private static String[][] board;
    //初始化棋盘
    public void initBoard() {
        board = new String[BOARD_SIZE][BOARD_SIZE];
        for (int i = 0; i < board.length; i++) {
//            String[] itemArg = board[i];
            for (int j = 0; j < board.length; j++) {
                board[i][j] = "┼";
            }
        }
    }
    //打印棋盘
    public void printData() {
        System.out.println("------start-----");
        for (int i = 0; i < board.length; i++) {
            for (String item :
                    board[i]) {
                System.out.print(item);
            }
            System.out.println();//一行结束换行打印
        }
        System.out.print("-------end-------");
        System.out.print("\n\n\n\n");
    }

    public static void main(String[] arg) {
        Gobang gobang = new Gobang();
        gobang.initBoard();
        gobang.printData();
        gobang.startRandomPosition();

    }
    //开始下棋
    public void startRandomPosition() {
        int label = 0;
        //记录已经有棋子的位置
        ArrayList<int[]> historyPosition = new ArrayList<>();
        String[] boardChild = {"●", "○"};//黑白棋子
        int[] positionBlack;
        do {
            positionBlack = getPosition(historyPosition);
            board[positionBlack[0]][positionBlack[1]] = boardChild[label % 2];//获取棋子黑白交替获取
            System.out.println(Arrays.toString(positionBlack) + boardChild[label % 2] + (label % 2 == 0 ? "黑子" : "白子"));
            printData();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            historyPosition.add(positionBlack);//记录位置
            label++; //交换棋子
        } while (historyPosition.size() < BOARD_SIZE * BOARD_SIZE);

    }

    //当前位置已有棋子重新获取
    private boolean dataRepeat(int[] positionBoard, ArrayList<int[]> hpArg) {
        boolean repeat = false;
        for (int[] hp:
             hpArg) {
            repeat = Arrays.equals(positionBoard, hp);
            if(repeat)
                return repeat;
        }
        return repeat;
    }
    //随机获取出棋子位置(x,y)
    private int[] getPosition(ArrayList<int[]> historyPosition) {
        int[] p = new int[2];
        do {
            p[0] = (int) (Math.random() * BOARD_SIZE);
            p[1] = (int) (Math.random() * BOARD_SIZE);
        } while (dataRepeat(p, historyPosition));
        return p;
    }
}