public static void testGetPossibleMoves() { Board a = new Board(); fillColumn(a, Board.Player.RED, 0); // fill column 0 Move[] moves2 = a.getPossibleMoves(Board.Player.RED); if (moves2.length != Board.NUM_COLS - 1) { System.out.println( "Error in getPossibleMoves with 1 col filled. array size is wrong: " + moves2.length); } if (moves2[0].getColumn() != 1) { System.out.println("First col is filled, second isn't but moves[0] is " + moves2[0]); } Board b = new Board(); b.makeMove(new Move(Board.Player.RED, 4)); b.makeMove(new Move(Board.Player.YELLOW, 3)); b.makeMove(new Move(Board.Player.RED, 5)); fillColumn(b, Board.Player.RED, 0); // fill column 0 Move[] moves1 = b.getPossibleMoves(Board.Player.RED); for (Move m : moves1) { System.out.println(m); } }
@Test public void playGame() { Board b = new Board(false); b.toString(); // just need to know, that it works // wb // bw assertEquals("Black", b.getState(3, 4), STATE.BLACK); assertEquals("Black", b.getState(4, 3), STATE.BLACK); assertEquals("White", b.getState(3, 3), STATE.WHITE); assertEquals("White", b.getState(4, 4), STATE.WHITE); b.markNextMoves(); // mark available moves assertEquals("Black", b.getState(3, 4), STATE.BLACK); assertEquals("Black", b.getState(4, 3), STATE.BLACK); assertEquals("White", b.getState(3, 3), STATE.WHITE); assertEquals("White", b.getState(4, 4), STATE.WHITE); // new possible moves all Black of Course assertEquals("Selectable", b.getState(2, 3), STATE.SELECTABLE); assertEquals("Selectable", b.getState(4, 5), STATE.SELECTABLE); assertEquals("Selectable", b.getState(3, 2), STATE.SELECTABLE); assertEquals("Selectable", b.getState(5, 4), STATE.SELECTABLE); assertEquals(true, b.isNextPlayerBlack()); // make Illegal Move assertFalse(b.makeMove(1, 1)); // nothing has changed assertEquals("Black", b.getState(3, 4), STATE.BLACK); assertEquals("Black", b.getState(4, 3), STATE.BLACK); assertEquals("White", b.getState(3, 3), STATE.WHITE); assertEquals("White", b.getState(4, 4), STATE.WHITE); // new possible moves all Black of Course assertEquals("Selectable", b.getState(2, 3), STATE.SELECTABLE); assertEquals("Selectable", b.getState(4, 5), STATE.SELECTABLE); assertEquals("Selectable", b.getState(3, 2), STATE.SELECTABLE); assertEquals("Selectable", b.getState(5, 4), STATE.SELECTABLE); // make Legal Move assertTrue(b.makeMove(2, 3)); assertEquals(1, b.getWhiteStones()); assertEquals(4, b.getBlackStones()); assertEquals(false, b.isNextPlayerBlack()); // white has next move // nothing has changed assertEquals("Black", b.getState(2, 3), STATE.BLACK); // move we made assertEquals("Black", b.getState(3, 4), STATE.BLACK); assertEquals("Black", b.getState(4, 3), STATE.BLACK); assertEquals("White", b.getState(3, 3), STATE.BLACK); assertEquals("White", b.getState(4, 4), STATE.WHITE); b.markNextMoves(); b.toString(); // just need to know, that it works // try this move again assertFalse(b.makeMove(2, 3)); }
/** * This specifically gets the jumps, and recursive jumps. * * @param move is the piece to move * @return the final jump states * @throws Exception */ private ArrayList<Board> childrenHelper(Board b, Piece move) { ArrayList<Board> result = new ArrayList<Board>(); ArrayList<Piece> move_list = b.validJumps(move); for (Piece move_to : move_list) { Board temp = new Board(b); boolean remain; try { remain = temp.makeMove(move, move_to); if (remain == true) { ArrayList<Board> future_list = childrenHelper( temp, temp.getPiece(move_to.getPosition()[0], move_to.getPosition()[1])); for (Board future : future_list) { result.add(future); } if (future_list.size() == 0) result.add(temp); } else { result.add(temp); } } catch (Exception e) { System.out.println("Error: " + e.getMessage()); } } return result; }
/** * Creates all possible moves from the current board and their corresponding Node object with the * current position as the parent * * @param piece specifies player that can make the move * @throws Exception */ public void createChildren(char color) { ArrayList<Piece> moveable = current.allJumps(color); if (moveable.size() != 0) // if it's a jump, take it. find the final states and return it. { for (Piece move : moveable) { ArrayList<Board> temp = childrenHelper(current, move); for (Board t : temp) { children.add(new Node(this, t)); } } } else // else, make the moves and return it. { moveable = current.allMoves(color); for (Piece move : moveable) { ArrayList<Piece> move_list = current.validMoves(move); for (Piece move_to : move_list) { Board temp = new Board(current); try { temp.makeMove(move, move_to); children.add(new Node(this, temp)); } catch (Exception e) { System.out.println("Move not made: " + move + "-->" + move_to); } } } } }
public void select2(Board b) { for (int i = 0; i < raiz.b.getValidMoves().length; i++) { Board bClone = b.clone(); bClone.makeMove(raiz.b.getValidMoves()[i]); this.lista.add(new NodoMCTS(raiz, bClone)); this.lista.get(i).mov = raiz.b.getValidMoves()[i]; // System.out.println(raiz.b.getValidMoves()[i]); } NodoMCTS evaluado = null; boolean seleccion = true; int aux = 0; while (seleccion && aux < 1000) { profundidad = 0; evaluado = this.expand2(); if (this.movimiento == null) { this.movimiento = evaluado; this.bestoverallmove = this.movimiento.mov; } else if (this.movimiento.mov != null && evaluado.mov != null) { // System.out.println("intentos: "+evaluado.intentos); // System.out.println("aciertos: "+evaluado.aciertos); // System.out.println("Fallos: "+evaluado.fallos); // System.out.println("intentos: "+movimiento.intentos); // System.out.println("aciertos: "+movimiento.aciertos); // System.out.println("Fallos: "+movimiento.fallos); // System.out.println("----"); if (evaluado.aciertos / evaluado.intentos > this.movimiento.aciertos / this.movimiento.intentos) { this.movimiento = evaluado; this.bestoverallmove = this.movimiento.mov; } } aux += 1; } }
/** Return an int that represents how good a MOVE is in the current board. */ int eval(Move move) { Board board = new Board(getBoard()); int val; board.makeMove(move); if (board.piecesContiguous(board.turn())) { val = Integer.MAX_VALUE; } else { int humanMove = piecesDist(board.arrayofCoordinates(board.turn())); int machineMove = piecesDist(board.arrayofCoordinates(board.turn().opposite())); val = humanMove - machineMove; } return val; }
/** * Run the game until finished. If GUI is not initialized, the output will be sent to the console. */ public void runGame() { while (!isGameOver()) { // Checking to see that the move can be made (not overflowing a column) boolean moveIsSafe = false; Move nextMove = null; while (!moveIsSafe) { Move[] bestMoves = activePlayer.getMoves(board); if (bestMoves.length == 0) { gui.setMsg("Game cannot continue until a Move is produced."); continue; } else { nextMove = bestMoves[0]; } if (board.getTile(0, nextMove.getColumn()) == null) { moveIsSafe = true; } else { gui.setMsg("Illegal Move: Cannot place disc in full column. Try again."); } } board.makeMove(nextMove); if (gui == null) { System.out.println(nextMove); System.out.println(board); } else { gui.updateGUI(board, nextMove); } activePlayer = (activePlayer == player1 ? player2 : player1); // The following code causes a delay so that you can easily view the plays // being made by the AIs try { Thread.sleep(SLEEP_INTERVAL); } catch (InterruptedException e) { e.printStackTrace(); } } if (gui == null) { if (winner == null) { System.out.println("Tie game!"); } else { System.out.println(winner + " won the game!!!"); } } else { gui.notifyGameOver(winner); } }
/** @param args */ public static void main(String[] args) { Board cb = new BoardArray(); List<Moveable> moves; Scanner scanner = new Scanner(System.in); int from, to; while (true) { moves = cb.generateLegalMoves(); System.out.println("Generated " + moves.size() + " moves"); for (Moveable move : moves) { System.out.printf( "uk.ac.gla.chessmantis.Move: %d %d\n", move.getFromPosition(), move.getToPosition()); } System.out.print("Enter your move: "); from = scanner.nextInt(); to = scanner.nextInt(); cb.makeMove(new Move(from, to)); } }
/** * Collects the principal variation starting from the position on the board * * @param board The position to collect pv from * @param current_depth How deep the pv goes (avoids situations where keys point to each other * infinitely) * @return collectString The moves in a string */ public int[] collectPV(Board board, int current_depth) { int[] arrayPV = new int[128]; int move = getMove(board.zobristKey); // int i = current_depth; int i = 20; int index = 0; while (i > 0) { if (move == 0 || !board.validateHashMove(move)) break; arrayPV[index] = move; board.makeMove(move); move = getMove(board.zobristKey); i--; index++; } // Unmake the moves for (i = index - 1; i >= 0; i--) { board.unmakeMove(arrayPV[i]); } return arrayPV; } // END collectPV()
public void simulate(NodoMCTS padre) { if (profundidad > maxProfundidad) { padre.fallos += 1; update(padre.padre, padre); return; } if (turn == 0 && padre.b.isCheckMate()) { padre.aciertos += 1; update(padre.padre, padre); profundidad += 1; return; } else if (turn == 1 && padre.b.isCheckMate()) { padre.fallos += 1; update(padre.padre, padre); profundidad += 1; return; } else if (padre.b.isStalemate()) { padre.fallos += 1; update(padre.padre, padre); profundidad += 1; return; } else { if (turn == 0) turn = 1; else turn = 0; // System.out.println(padre.b.getValidMoves().length); int a = r.nextInt(padre.b.getValidMoves().length); Board bClone = padre.b.clone(); bClone.makeMove(padre.b.getValidMoves()[a]); NodoMCTS hijo = new NodoMCTS(padre, bClone); hijo.mov = padre.b.getValidMoves()[a]; profundidad += 1; simulate(hijo); update(padre.padre, padre); return; } }
/** * Fill column c of board b, starting with player p. Precondition: p is not null and 0 <= c < * NUM_COLS */ public static void fillColumn(Board b, Board.Player p, int c) { while (b.getPlayer(0, c) == null) { b.makeMove(new Move(p, c)); p = p.opponent(); } }
@Test public void playGameNoChecks() { Board b = new Board(true); // wb // bw assertEquals("Black", b.getState(3, 4), STATE.BLACK); assertEquals("Black", b.getState(4, 3), STATE.BLACK); assertEquals("White", b.getState(3, 3), STATE.WHITE); assertEquals("White", b.getState(4, 4), STATE.WHITE); assertTrue(b.markNextMoves()); // mark available moves assertTrue(b.markNextMoves()); // mark available moves, must work twice assertEquals("Black", b.getState(3, 4), STATE.BLACK); assertEquals("Black", b.getState(4, 3), STATE.BLACK); assertEquals("White", b.getState(3, 3), STATE.WHITE); assertEquals("White", b.getState(4, 4), STATE.WHITE); // new possible moves all Black of Course assertEquals("Selectable", b.getState(2, 3), STATE.SELECTABLE); assertEquals("Selectable", b.getState(4, 5), STATE.SELECTABLE); assertEquals("Selectable", b.getState(3, 2), STATE.SELECTABLE); assertEquals("Selectable", b.getState(5, 4), STATE.SELECTABLE); assertEquals(true, b.isNextPlayerBlack()); // make Illegal Move and expect an Exception assertFalse(b.makeMove(1, 1)); // nothing has changed assertEquals("Black", b.getState(3, 4), STATE.BLACK); assertEquals("Black", b.getState(4, 3), STATE.BLACK); assertEquals("White", b.getState(3, 3), STATE.WHITE); assertEquals("White", b.getState(4, 4), STATE.WHITE); // new possible moves all Black of Course assertEquals("Selectable", b.getState(2, 3), STATE.SELECTABLE); assertEquals("Selectable", b.getState(4, 5), STATE.SELECTABLE); assertEquals("Selectable", b.getState(3, 2), STATE.SELECTABLE); assertEquals("Selectable", b.getState(5, 4), STATE.SELECTABLE); // make Legal Move assertTrue(b.makeMove(2, 3)); // nothing has changed assertEquals("Black", b.getState(2, 3), STATE.BLACK); assertEquals("Black", b.getState(3, 4), STATE.BLACK); assertEquals("Black", b.getState(4, 3), STATE.BLACK); assertEquals("White", b.getState(3, 3), STATE.BLACK); assertEquals("White", b.getState(4, 4), STATE.WHITE); b.markNextMoves(); // mark available moves // all marked fields have to be playable for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (b.getBoolboard()[i * 8 + j] == STATE.SELECTABLE) { assertTrue(i + "/" + j + " is not legal but was marked as legal", b.isLegalMove(i, j)); } } } // try this move again, should fail assertFalse(b.makeMove(2, 3)); }