void cheatedFinish(int player, int reason, TextGameBoard board) { if (player == GameBoard.GREEN) { // BONUS for winner! int redStones = Math.max(board.countStones(GameBoard.RED), 32); state.cheatedFinish(GameBoard.GREEN, reason, redStones, 0); } else { // BONUS for winner! int greenStones = Math.max(board.countStones(GameBoard.GREEN), 32); state.cheatedFinish(GameBoard.RED, reason, 0, greenStones); } // state.aborted = false; System.out.println("CHEATED : " + state); }
void computePossibleMoves(TextGameBoard board, int player, int[][] possibleMoves) { for (int row = 0; row < BOARD_LENGTH; row++) { for (int col = 0; col < BOARD_LENGTH; col++) { Coordinates coord = new Coordinates(row + 1, col + 1); if (board.checkMove(player, coord)) { possibleMoves[row][col] = 1; // System.out.println( "Possible move: " + coord ); } else { possibleMoves[row][col] = 0; } } } }
/** Perform a match between two players. */ void performMatch() { TextGameBoard board = new TextGameBoard(); Coordinates move_red = new Coordinates(0, 0); Coordinates move_green = new Coordinates(0, 0); if (!params.getSilent()) { visualization.update(board); } boolean irregularFinish = false; statusText = ""; while (!board.isFull()) { // -------------------------------------------------------------- // RED makes move // -------------------------------------------------------------- try { move_red = performMove(GameBoard.RED, board); } catch (IllegalMoveException e) { irregularFinish = true; break; } catch (TimeExceededException e) { irregularFinish = true; break; } catch (InterruptedException e) { // this is fatal! System.out.println("Player was interrupted!"); return; } if (!params.getSilent()) { visualization.update(board); } // ------------------------------------------------------------- // GREEN makes move // ------------------------------------------------------------- try { move_green = performMove(GameBoard.GREEN, board); } catch (IllegalMoveException e) { irregularFinish = true; break; } catch (TimeExceededException e) { irregularFinish = true; break; } catch (InterruptedException e) { // this is fatal! System.out.println("Player was interrupted!"); return; } if (!params.getSilent()) { visualization.update(board); } // ------------------------------------------------------------- // game over? // ------------------------------------------------------------- if (move_red == null && move_green == null) { System.out.println("Vorzeitiges Ende"); // TODO: Store this in GameState // TODO: Test this writeToLog("no moves left"); statusText = "Finished, no moves left."; break; } } if (!irregularFinish) { if (board.isFull()) { statusText = "Finished."; } writeToLog( "finished reds=" + board.countStones(GameBoard.RED) + " greens=" + board.countStones(GameBoard.GREEN)); int redStones = board.countStones(GameBoard.RED); int greenStones = board.countStones(GameBoard.GREEN); regularFinish(redStones, greenStones); } if (!params.getSilent()) { if (state.getResult() == GameState.RESULT_DRAW_GAME) { visualization.setStatusLine( statusText + " (DRAW " + state.getRedStones() + ":" + state.getGreenStones() + ")"); } else if (state.getResult() == GameState.RESULT_GREEN_WINS) { visualization.setStatusLine( statusText + " (GREEN " + name_green + " WON " + state.getGreenStones() + ":" + state.getRedStones() + ")"); } else if (state.getResult() == GameState.RESULT_RED_WINS) { visualization.setStatusLine( statusText + " (RED " + name_red + " WON " + state.getRedStones() + ":" + state.getGreenStones() + ")"); } } // ----------------------------------------------------------------- // Quit program after some time // ----------------------------------------------------------------- if (timeBeforeExit > 0) { try { Thread.sleep(timeBeforeExit); } catch (InterruptedException e) { } // System.exit(0); } else { // wait forever while (true) { try { Thread.sleep(500); } catch (InterruptedException e) { break; } } } }
Coordinates performMove(int player, TextGameBoard board) throws InterruptedException, IllegalMoveException, TimeExceededException { Coordinates currentMove = null; int[][] toFlip = new int[BOARD_LENGTH][BOARD_LENGTH]; boolean legalMove; statusText = ""; if (player != GameBoard.RED && player != GameBoard.GREEN) { throw new IllegalArgumentException("Player has to be RED or GREEN!"); } String playerName; String playerColor; ReversiPlayer reversiPlayer; if (player == GameBoard.RED) { playerColor = "Red"; playerName = name_red; reversiPlayer = player_red; } else { playerColor = "Green"; playerName = name_green; reversiPlayer = player_green; } if (!params.getSilent() && params.getAnimations()) { // show possible moves of the current player (if any) visualization.showPossibleMoves(board, player); visualization.setStatusLine(playerName + " thinking..."); } // only for animation reasons try { Thread.sleep(params.getDelay()); } catch (InterruptedException e) { } try { currentMove = makeMove(reversiPlayer, board); } catch (TimeExceededException e) { writeToLog(playerColor + " exceeds time limit"); cheatedFinish(player, GameState.CHEATED_TIME_EXCEEDED, board); statusText = playerColor + " exceeds time limit."; throw new TimeExceededException(); } // the InterruptedException is passed to the caller writeToLog( playerColor + "move=" + (currentMove == null ? "null" : currentMove.getRow() + "," + currentMove.getCol())); if (currentMove == null) { System.out.println(playerColor + " passes."); } legalMove = board.checkMove(player, currentMove); if (!legalMove) { System.out.println(playerColor + " makes illegal move: " + currentMove); writeToLog(playerColor + " makes illegal move"); if (verbose) { System.out.println(board.toString()); } cheatedFinish(player, GameState.CHEATED_ILLEGAL_MOVE, board); statusText = playerColor + " makes illegal move."; throw new IllegalMoveException( "Illegal move by player " + playerColor + "(" + currentMove + ")", currentMove); } if (currentMove != null) { computeTokensToFlip(board, currentMove, player, toFlip); if (!params.getSilent() && params.getAnimations()) { visualization.animateMove(board, currentMove, player, toFlip); } } board.makeMove(player, currentMove); if (!params.getSilent()) { visualization.setInfoLine2( name_red + " (red) vs " + name_green + " (green): " + board.countStones(GameBoard.RED) + ":" + board.countStones(GameBoard.GREEN)); } return currentMove; }