Esempio n. 1
0
  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;
  }