我正在尝试创建一个连接四子棋的AI,并发现了一篇文章,该文章使用位棋盘来优化移动和检查胜利。我从一个GitHub的自述文件中借鉴了几种方法,这些方法应该可以在位棋盘上进行移动和撤销移动,但不幸的是,这些方法似乎无法正常工作,因为它在位字符串的末尾放置了1,而不是每隔7位放置一个。我认为这可能是Java的一些问题导致这些方法无法正常工作,我在下面发布了一个示例程序,我认为它准确地展示了这个问题。例如,如果我设置一个长整型变量,使其在第5行有一排四个1,并显示它。它会正确显示,没有开头的零,但另一方面,如果我在第一列添加三个棋子,然后在第三列添加三个棋子。它显示为111和111。当它应该显示为
000000000000000000000000011100000000000000000000000000000000000000000000000000000111
并且没有前导零,所以
11100000000000000111
然后,如果我运行另一个测试,使用这些列的下落顺序1,3,1,3,2,4,应该会得到这个棋盘状态。
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| X | | O | | | | |
| X | X | O | O | | | |
-----------------------------
它显示为10, 10,而应该显示为
000000000000000000001000001100000000000000000000000000000000000000000000000010000011
或者
100000110000000000000010000011
这里有一些测试代码,展示了第二种情况。此时我感到困惑,因为这些方法的操作非常优雅和复杂,尽管它们只有三行代码,如果有人能告诉我我做错了什么,我将不胜感激。谢谢!
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-1]++ + ((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;}