0

本质上,我试图创建一个连接四个 Ai,我遇到了一篇文章,它使用位板来优化移动和检查胜利。基本上我从 git hub自述文件中获取了一些方法这应该在你的位板上进行移动并撤消移动,不幸的是,这些方法似乎无法正常工作,因为它将它们放在位串的末尾而不是将它们间隔 7。我认为这可能是一些 java阻止这些正常工作的事情,我已经发布了一个示例程序,我认为它可以准确地说明问题。例如,如果我设置一个长变量,使其在第 5 行有一行四个 1 并显示它。它正确显示而没有开头的零,但另一方面,如果我在第一列中添加三个标记。然后是第三列的三个标记。它显示为 111 和 111。它应该是什么时候

000000000000000000000000011100000000000000
000000000000000000000000000000000000000111

并且没有前导零,所以

11100000000000000
111

然后,如果我用这些列下降 1、3、1、3、2、4 运行另一个测试,这应该会导致这种板状态。

|   |   |   |   |   |   |   |
|   |   |   |   |   |   |   |
|   |   |   |   |   |   |   |
|   |   |   |   |   |   |   |
| X |   | O |   |   |   |   |
| X | X | O | O |   |   |   |
-----------------------------

它应该显示 10、10

000000000000000000001000001100000000000000
000000000000000000000000000000000010000011

或者

1000001100000000000000
10000011

这是一些演示第二种情况的测试代码。在这一点上,我不知所措,因为这些方法的操作非常优雅和复杂,尽管它们只有 3 行代码,如果有人能告诉我我在做什么有什么问题,我将不胜感激。干杯!

public class EConnectFour {
    private static int[] height = {0, 0, 0, 0, 0, 0, 0};
    private static int counter = 0;
    private static int[] moves = new int[42];
    private static long[] bitBoard = new long[2];



    public static void main(String[] args) {
          long TOP = 0b0000000001000000100000010000001000000000000000000L;
          System.out.println(Long.toBinaryString(TOP));
          makeMove(1);
          makeMove(3);
          makeMove(1);
          makeMove(3);
          makeMove(2);
          makeMove(4);
          System.out.println(Long.toBinaryString(bitBoard[0]));
          System.out.println(Long.toBinaryString(bitBoard[1]));
    }

    private static boolean isWin(long board) {
        int[] directions = {1, 7, 6, 8};
        long bb;
        for(int direction : directions) {
            bb = board & (board >> direction);
            if ((bb & (bb >> (2 * direction))) != 0) return true;
        }
        return false;
    }

    private static void makeMove(int column) {
        long move = 1L << height[column]++;
        bitBoard[counter & 1] ^= move; 
        moves[counter++] = column;  
    }

    private static void undoMove() {
        int column = moves[--counter];
        long move = 1L << --height[column];
        bitBoard[counter & 1] ^= move;
    }
}
4

1 回答 1

0

您无意中不断将令牌放入同一个第一个插槽中。

代替:

// long move = 1L << height[column]++;
long move = 1L << (height[column]++ + ((column-1) * height.length));

您对的引用column减一(Java 中的数组索引为 0)。跑步结束时,高度如下所示:

height = [0, 2, 1, 2, 1, 0, 0]

您可以通过以下方式解决此问题:

long move = 1L << (height[column-1]++ + ((column-1) * height.length));

makeMove 的最终版本:

private static void makeMove(int column) {
    long move = 1L << (height[column-1]++ + ((column-1) * height.length));
    bitBoard[counter & 1] ^= move; 
    moves[counter++] = column;  
}

undoMove 的版本:

private static void undoMove() {
    int column = moves[--counter];
    moves[counter] = 0;  
    long move = 1L << (--height[column-1] + ((column-1) * height.length));
    bitBoard[counter & 1] ^= move;
}
于 2020-01-24T18:37:26.283 回答