Beispiel #1
0
  /**
   * Makes a guess.
   *
   * @param player the player making the guess.
   * @param guess the guess to make.
   * @return the next step in the current game.
   * @throws GameException if the guess cannot be made.
   */
  public GameStep guess(Player player, Number guess) throws GameException {
    GameStep gs = new GameStep(GameStep.Type.GUESS, player);
    this.checkStep(gs, this.gameStep);

    Round r = this.getLastRound();
    if (r != null) {
      Map<Player, Number> guesses = r.getGuesses();
      if (r.getGuesses().containsKey(player)) {
        if (this.players.size() == guesses.size()) {
          r = new Round();
          this.rounds.add(r);
        } else {
          throw new GameException(String.format("%s has already made a guess this round", player));
        }
      }
    } else {
      r = new Round();
      this.rounds.add(r);
    }

    int playerIndex = this.players.indexOf(player);
    int opponentIndex = playerIndex + 1;
    if (opponentIndex == this.players.size()) {
      opponentIndex = 0;
    }
    Player opponent = this.players.get(opponentIndex);

    r.addGuess(player, opponent, guess);

    if (this.isGameOver()) {
      this.gameStep = new GameStep(GameStep.Type.GAME_OVER);
    } else {
      Player nextPlayer = null;
      if (playerIndex == this.players.size() - 1) {
        nextPlayer = this.players.get(0);
      } else {
        nextPlayer = this.players.get(playerIndex + 1);
      }
      this.gameStep = new GameStep(GameStep.Type.GUESS, nextPlayer);
    }

    return this.gameStep;
  }