Exemplo 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");
  }
Exemplo n.º 2
0
 public void startMatch() {
   if (!params.getSilent()) {
     visualization.setVisible(true);
     // visualization.main_frame.setVisible(true);
   }
   performMatch();
 }
Exemplo n.º 3
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;
        }
      }
    }
  }
Exemplo n.º 4
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;
  }