本质上,我试图创建一个连接四个人工智能,我遇到了一个 article它使用位板来优化移动和检查胜利。本质上我从 git hub readme 获取了一些方法。应该在位板上进行移动和撤消移动,不幸的是,这些方法似乎无法正常工作,因为它将它们放置在位串的末尾,而不是按 7 间隔。我认为这可能是一些 java阻止这些正常工作的事情,我在下面发布了一个示例程序,我认为它准确地演示了该问题。例如,如果我设置一个 long 变量,使其在第五行有一行四个 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;
}
}
最佳答案
您无意中不断将代币放入同一个第一个槽中。
替换:
// long move = 1L << height[column]++;
long move = 1L << (height[column]++ + ((column-1) * height.length));
您对column
的引用减少了1(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;
}
关于Java 连接四个按位函数和位板错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59901227/