Esempio n. 1
0
  /**
   * @param params
   * @param vis
   */
  public Arena(ProgramParameters params, Visualization vis) {
    backlog = new StringBuffer();
    logStreams = new Vector();
    server_thread = new ServerThread();
    server_thread.init(5454, this);
    server_thread.start();

    this.params = params;

    this.logFile = params.getLogfile();
    this.matchId = params.getGameId();

    String pr = params.getRedName();
    String pg = params.getGreenName();
    long timeOut = params.getTimeout();
    // String logFile = params.getLogfile();
    // delay = (int) params.getDelay();

    timeBeforeExit = (int) params.getTimeBeforeExit();

    player_red = instantiatePlayer(pr);
    player_green = instantiatePlayer(pg);

    writeToLog("new game");
    writeToLog("red=" + pr + " green=" + pg);

    visualization = vis;

    if (!params.getSilent()) {
      visualization.init(this);
    }
    this.timeOut = timeOut;

    // initialize both players
    player_red.initialize(GameBoard.RED, timeOut);
    player_green.initialize(GameBoard.GREEN, timeOut);

    name_red = pr.substring(pr.lastIndexOf('.') + 1);
    name_green = pg.substring(pg.lastIndexOf('.') + 1);

    if (!params.getSilent()) {

      visualization.setInfoLine("delay=" + params.getDelay());
      visualization.setInfoLine2(name_red + " (red) vs " + name_green + " (green)");
    }

    writeToLog("initialized");
  }
Esempio n. 2
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;
  }