/** * Provides the ability to insert a new Score entity. * * @param board object representing the state of the board * @return the board including the computer's move */ @ApiMethod(name = "board.getmove", path = "getmove", httpMethod = "POST") public Board getmove( Board board, @Named("id") int id, @Named("row") int row, @Named("col") int col) { System.out.println("id: " + id); CellContainer cellContainer = CellContainer.fromJson(board.getState()); ArrayList<Cell> cells = cellContainer.cells; ArrayList<Cell> unoccupiedCells = new ArrayList<Cell>(); for (Cell cell : cells) { System.out.println("row: " + row + " | cell.x: " + cell.x); if (row == cell.x && col == cell.y && row != 100 && col != 100) { System.out.println("updated 1"); cell.val += (id + 1) * 10; CellContainer updateContainer = new CellContainer(cells); Board updated = new Board(CellContainer.toJson(updateContainer)); return updated; } if (cell.val <= 10) { unoccupiedCells.add(cell); } } if (id == 0) System.out.println("what"); Random randomG = new Random(); System.out.println("updated 2"); Cell randomfreeCell = unoccupiedCells.get(randomG.nextInt(unoccupiedCells.size())); randomfreeCell.val = randomfreeCell.val + (id + 1) * 10; CellContainer updateContainer = new CellContainer(cells); Board updated = new Board(CellContainer.toJson(updateContainer)); return updated; }
/** * Constructs a new game by parsing given array of string as an array of FEN primitives. * * <p>FEN parsing is permissive and uses best-effort approach i.e. anything, that is salvaged of * given input, is used as best as possible, and for the rest, reasonable default values are * assumed. * * @param fenArray An array of FEN primitives used to initialize a game. Only the six first array * elements are used and the are discarded if provided. */ public Chess(String[] fenArray) { turn = true; enpassant = null; halfmove = 0; fullmove = 1; if (fenArray.length >= 4) { try { enpassant = new Coord(fenArray[3]); } catch (Exception pass) { } } if (fenArray.length >= 1) { board = new Board(fenArray[0], enpassant); } else { board = new Board(); } if (fenArray.length >= 2) { if (fenArray[1].equals("b")) { turn = false; } } if (fenArray.length >= 3) { castling = new Castle(fenArray[2]); } else { castling = new Castle(); } castling.crop(board.getState()); if (fenArray.length >= 6) { try { halfmove = Integer.parseInt(fenArray[4]); fullmove = Integer.parseInt(fenArray[5]); } catch (NumberFormatException pass) { } } }
/** * Checks for a victory condition. * * @param {string} boardString Current state of the board. * @return {number} Status code for the victory state. */ @ApiMethod(name = "board.checkForVictory", path = "checkForVictory", httpMethod = "POST") public Status checkForVictory(Board board) { CellContainer cellContainer = CellContainer.fromJson(board.getState()); ArrayList<Cell> cells = cellContainer.cells; int num_free = 0; int score_player1 = 0; int score_player2 = 0; for (int i = 0; i < cells.size(); i++) { Cell cell = cells.get(i); if (0 < cell.val && cell.val <= 10) { num_free += 1; } else if (10 < cell.val && cell.val <= 20) { score_player1 += cell.val - 10; } else if (20 < cell.val && cell.val <= 30) { score_player2 += cell.val - 20; } } Status s = new Status(); System.out.println("num free:" + num_free); if (num_free > 0) { s.setStatus(0); return s; } if (score_player1 > score_player2) { s.setStatus(1); return s; } if (score_player1 < score_player2) { s.setStatus(2); return s; } s.setStatus(3); return s; };
@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)); }
@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)); }
/** * Return the state of the board. * * @return Two dimensional array of type Type containing the state of the board. */ public Type[][] getState() { return board.getState(); }