コード例 #1
0
  private void jailOptions() {
    int choice = this.displayJailMenu();
    while (choice != 0) {
      switch (choice) {

          // player decided to roll dice
        case 1:
          PairDice dice = PairDice.getInstance(Game.getDebugDice());
          UI.diceDisplay(dice);
          if (dice.same) {
            StdOut.println("Double you are free, next turn you will be able to move");
            Game.getCurrentPlayer().setJailTime(0);
          } else {

            // this condition just protects if you mess to much with debug
            // options
            if (Game.getCurrentPlayer().getJailTime() > 0) {
              Game.getCurrentPlayer().setJailTime(Game.getCurrentPlayer().getJailTime() - 1);
            }

            if (Game.getCurrentPlayer().getJailTime() > 0) {
              StdOut.println(
                  "No luck. You will be stuck in jail for "
                      + Game.getCurrentPlayer().getJailTime()
                      + " more turn(s).");
            } else {
              StdOut.println("You are free. You will be able to move in next turn.");
            }
          }
          choice = 0;
          UI.pause();
          break;

          // buy the card
        case 2:
          ArrayList<Player> players = new ArrayList<Player>();

          for (Player player : Game.getOtherPlayers()) {
            if (player.getCardsSize() > 0) {
              players.add(player);
            }
          }

          if (players.size() > 0) {
            Pair<Player, Long> winner =
                Game.getBank().auction("\"Get out\" card auction", players, false);

            // first hold Player object and second holds price
            if (winner.first != null) {
              if (winner.second <= Game.getCurrentPlayer().getCash()) {
                // get ACK from current player if he is willing pay the price
                if (UI.askNgetBoolean(
                    winner.first
                        + " is asking "
                        + Bank.moneyEuro(winner.second)
                        + ", do you accept?")) {
                  // pay for the card
                  Game.getCurrentPlayer().payToPlayer(winner.first, winner.second);

                  // get the card itself
                  winner.first.transferCardJail(Game.getCurrentPlayer());
                }
                // in case you don't accept nothing will happen
              } else {
                StdOut.println("Sorry winner was asking more than you can afford to pay.");
              }
            } else {
              StdOut.println("Nobody was willing to sell you the card.");
            }
          } else {
            StdOut.println("There are no players who have the \"Get out\" card.");
          }

          UI.pause();
          break;

          // use the card
        case 3:
          if (Game.getCurrentPlayer().getCardsSize() > 0) {
            Game.getCurrentPlayer().setJailTime(0);
            Game.getCurrentPlayer().useCard();

            StdOut.println("You will be able to move next round.");

            // leave the menu
            choice = 0;
          } else {
            StdOut.println("You can't use a card, because you have none!");
          }
          UI.pause();
          break;

          // give up on this game
        case 4:
          if (UI.askNgetBoolean("Are you sure to give up?")) Game.getCurrentPlayer().giveUp();
          choice = 0;
          break;

          // save game status and exit the application
        case 5:
          if (UI.askNgetBoolean("Are you sure to suspend the game?")) {
            Game.save();
            Game.stopGame();
            choice = 0;
          }
          break;

        case 6:
          debugOptions();
          break;

        case 0:
        default:
          break;
      }
      if (choice != 0) choice = this.displayJailMenu();
    }
  }
コード例 #2
0
  private void playerOptions() {
    int choice = this.displayPlayerMenu();
    while (choice != 0) {
      switch (choice) {

          // player decided to roll dice
        case 1:
          PairDice dice = PairDice.getInstance(Game.getDebugDice());
          UI.diceDisplay(dice);
          Game.getCurrentPlayer().step(dice.total);
          if (dice.same) {
            StdOut.println("\nYou threw a DOUBLE, you are rolling for the second time.");
            UI.pause();

            dice = PairDice.getInstance(Game.getDebugDice());
            UI.diceDisplay(dice);
            Game.getCurrentPlayer().step(dice.total);
            if (dice.same) {
              StdOut.println(
                  "\nYou threw a DOUBLE for the second time, you are rolling the third time.");
              StdOut.println("Note: be careful or you are going to jail.");
              UI.pause();

              dice = PairDice.getInstance(Game.getDebugDice());
              UI.diceDisplay(dice);
              if (dice.same) {
                StdOut.println(
                    "\nYou threw a DOUBLE for the third time, you are going straight to jail.");
                // Game.getCurrentPlayer().jumpTo(Game.getBoard().getJailPosition());
                // will change possition as well
                Game.getCurrentPlayer().setJailTime(3);
              } else {
                Game.getCurrentPlayer().step(dice.total);
              }
            }
          }
          // step is already called in tileActions
          // Game.getBoard().tileActions();
          choice = 0;
          UI.pause();
          break;

          // to see what tiles he owns
        case 2:
          UI.displayMyProperties();
          UI.pause();
          break;

          // to see stats of all players together
        case 3:
          UI.displayChoiceItems(
              "Players", UI.playersToItems(Game.getAllPlayingPlayers()), false, false);
          UI.pause();
          break;

          // Sell properties to other player
        case 4:
          // TODO test it more
          ArrayList<BoardTile> tiles = new ArrayList<BoardTile>();

          for (BoardTile tile :
              BoardSearch.all().filterByOwner(Game.getCurrentPlayer()).getTiles()) {
            if (Game.getBoard()
                .getTile(Game.getCurrentPlayer().getPosition())
                .ownAllofThisColorAreNotUpgraded()) {
              if (tile.getUpgradeLevel() == 0) {
                tiles.add(tile);
              }
            }
          }

          if (tiles.size() > 0) {
            int forSale =
                UI.displayMenuNGetChoice(
                    "Choose tile to sell", BoardSearch.all(tiles).toMenuItems(), false, true);
            //            BoardTile tileForSale = tiles

            Pair<Player, Long> winner =
                Game.getBank().auction("Auction for " + tiles.get(forSale), Game.getOtherPlayers());

            if (winner.first != null) {
              tiles.get(forSale).setOwned(true);
              tiles.get(forSale).setOwner(winner.first);
              winner.first.payToPlayer(Game.getCurrentPlayer(), winner.second);

            } else {
              StdOut.println("Nobody was interested to buy your property.");
            }

          } else {
            StdOut.println(
                "You don't own any properties avaiable for selling. Can't be upgraded and:");
            StdOut.println("To sell property all properties of this colour can't be upgraded!");
          }
          UI.pause();
          break;

          // Downgrade tiles
        case 5:
          // TODO test it more
          UI.displayMyProperties();

          ArrayList<BoardTile> forDowngrade = new ArrayList<BoardTile>();

          for (BoardTile tile :
              BoardSearch.all().filterByOwner(Game.getCurrentPlayer()).getTiles()) {
            if (tile.getUpgradeLevel() > 0) {
              forDowngrade.add(tile);
            }
          }

          if (forDowngrade.size() > 0) {
            if (UI.askNgetBoolean("Are you sure to downgrade some properties?")) {
              int downgradeIndex =
                  UI.displayMenuNGetChoice(
                      "Choose property to downgrade",
                      BoardSearch.all(forDowngrade).toMenuItems(),
                      false,
                      true);

              forDowngrade.get(downgradeIndex).decUpgradeLevel();
            }
          } else {
            StdOut.println("No properties for downgrading");
          }
          UI.pause();
          break;

          // Mortages
        case 6:
          // TODO test it more
          UI.displayMyProperties();

          ArrayList<BoardTile> mortaged =
              BoardSearch.all()
                  .filterByOwner(Game.getCurrentPlayer())
                  .filterByMortaged(true)
                  .getTiles();

          if (mortaged.size() > 0) {
            UI.displayChoiceItems(
                "Mortage properties", BoardSearch.all(mortaged).toMenuItems(), false, true);

            if (UI.askNgetBoolean("Do you want buy some of them back?")) {

              int buyBack =
                  UI.displayMenuNGetChoice(
                      "Mortage properties", BoardSearch.all(mortaged).toMenuItems(), false, true);

              if (mortaged.get(buyBack).mortageBuyOfPrice() <= Game.getCurrentPlayer().getCash()) {

                // you can afford it but still lets confirm it from you
                if (UI.askNgetBoolean(
                    "It will cost "
                        + Bank.roundMoney(mortaged.get(buyBack).mortageBuyOfPrice())
                        + ", do you want it back?")) {
                  mortaged.get(buyBack).mortageFromBank();
                }

              } else {
                StdOut.println(
                    "You cannot affort to pay: "
                        + Bank.roundMoney(mortaged.get(buyBack).mortageBuyOfPrice()));
              }
            }

          } else {
            StdOut.println("You didn't mortaged any properties.");
          }
          UI.pause();

          break;

          // give up on this game
        case 7:
          if (UI.askNgetBoolean("Are you sure to give up?")) Game.getCurrentPlayer().giveUp();
          choice = 0;
          break;

          // save game status and exit the application
        case 8:
          if (UI.askNgetBoolean("Are you sure to suspend the game?")) {
            Game.save();
            Game.stopGame();
            choice = 0;
          }
          break;

        case 9:
          boolean allAgree = true;
          Long max = 0L;

          UI.displayChoiceItems(
              "Players", UI.playersToItems(Game.getAllPlayingPlayers()), false, false);

          // at least empty player, just in case so there will be no NULL
          // pointer issues later
          Player winner = new Player();

          for (Player player : Game.getAllPlayingPlayers()) {
            if (!UI.askNgetBoolean(player + " do you agree with stopping this game?")) {
              allAgree = false;
            }
            // while traversing all players we can also find who is the richest
            // one
            if (player.totalNetWorth() > max) {
              max = player.totalNetWorth();
              winner = player;
            }
          }
          if (allAgree) {
            // we will leave the menu
            choice = 0;

            UI.anounceWinner(winner);
            Game.stopGame();
          } else {
            StdOut.println("Not all players agreed with this request.");
          }
          break;

        case 10:
          debugOptions();
          break;

        case 0:
        default:
          break;
      }
      if (choice != 0) choice = this.displayPlayerMenu();
    }
  }