I am making an AI following this article. The basic idea is using bitboards to make a extremely fast protocol for making modifications to the board and permutating through possible game outcomes.
But the article is written in python and I had trouble following the initial bitboard setup. I went to the github readme they reference in hopes to develop the framework for my AI and then worry about alpha-beta pruning and the machine learning later. Essentially I got past the methods for checkWin
, makeMove
, and undoMove
and estabilished the required fields for the height of columns, moves, the count of moves, and the array of long variables representing the bitboards for the players.
But I am currently stuck at the generate moves method because it tries to initialize all the values at the top of the bitboard outside of the columns. But in java I can't seem to do this: it just gives me the error this literal type is out of range for type long. Additionally seeing as moves is a int[]
I'm not sure how to use moves.push(col)
. Because that gives the error cannot invoke push(int)
. Below you will find the broken listMoves
method and my current code and I would be very appreciative if anyone could tell me some things I might be doing wrong in my code and or how to fix the listMoves
method. Cheers!
private int[] listMoves() {
int[] moves;
long TOP = 1000000_1000000_1000000_1000000_1000000_1000000_1000000L;
for(int col = 0; col <= 6; col++) {
if ((TOP & (1L << height[col])) == 0) moves.push(col);
}
return moves;
}
My code
public class EConnectFour {
private int[] height = {0, 0, 0, 0, 0, 0, 0};
private int[] moves = new int[42]; // add size
private int counter = 0;
private long[] bitBoard = new long[2];
public static void main(String[] args) {
}
private char[][] getGameState() {
// add code to get current gamestate
return new char[0][0];
}
private boolean isWin(long bitBoard) {
int[] directions = {1, 7, 6, 8};
long bb;
for(int direction : directions) {
bb = bitBoard & (bitBoard >> direction);
if ((bb & (bb >> (2 * direction))) != 0) return true;
}
return false;
}
private void makeMove(int column) {
long move = 1L << height[column]++;
bitBoard[counter & 1] ^= move;
moves[counter++] = column;
}
private void undoMove() {
int column = moves[--counter];
long move = 1L << --height[column];
bitBoard[counter & 1] ^= move;
}
}