Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package learning;
- public class Game // November 24 and 26, 2015
- {
- ArrayList<Player> players;
- ArrayList<Property> properties;
- Random random;
- Player currentPlayer;
- int currentPlayerPosition;
- public Game()
- {
- makeProperties();
- makePlayers();
- currentPlayer = players.get(0);
- currentPlayerPosition = 0;
- random = new Random(); // we have an object we want to last the lifetime of the game, and we initialize it in the constructor
- for (int i =0; i <players.size(); i ++) {
- Player player = players.get(i);
- player.addMoney(1500);
- player.setLocation(Boardwalk);
- }
- }
- public void makeProperties()
- {
- Property parkPlace = new Property(30, 5);
- Property boardwalk = new Property(40, 10);
- }
- public void makePlayers()
- {
- Player player = new Player("thimble");
- Player owner = new Player("dog");
- }
- public void playGame(player)
- {
- //use a while loop rather than a for loop because we don't know how many turns this game will take
- while(true) {
- Player winner = doTurn(currentPlayer); // doTurn should return a player if there is a winner
- if (winner != null) { // if there is a winner, return the winner
- return winner;
- }
- advanceCurrentplayer();
- }
- }
- public void doTurn(Player player)
- {
- System.out.println("Started turn: " + player.getToken());
- int die1 = random.nextInt(6) + 1;
- int die2 = random.nextInt(6) + 1;
- int diceRoll = die1 + die2;
- System.out.println("Rolled: " + die1 + " and " + die2 + " => " + diceRoll);
- Property landed = advancePlayer(player, diceRoll);
- System.out.println(player.getToken() + " landed on " + landed.getName());
- if (landed.getOwner() == null) // we're ignoring if (landed.getOwner() == player)
- {
- player.buyProperty(landed, landed.getPrice());
- System.out.println(player.getToken() + " has bought " + landed.getName() + " for $" + landed.getPrice());
- }
- else if (landed.getOwner() != player) // else if rather than else because we don't want to subtract the money if current player owns this space
- {
- player.subtractMoney(landed, landed.getRent());
- landed.getOwner().addMoney(landed.getRent());
- System.out.println(player.getToken() + " has paid " + landed.getOwner.getToken() + "$" + landed.getRent() + " for landing on " + landed.getName());
- /* landed is a Property
- landed.getOwner() <- Player object that owns property
- landed.getOwner().addMoney <- call a function on the Player object that owns the property
- then you pass to that landed.getRent() <- rent value of the property
- */
- }
- if (player.getBalance() == 0)
- {
- players.remove(player);
- System.out.println("Player " + player.getToken() + "has lost.");
- if (players.size() == 1) {
- return players.get(0);
- System.out.println(player.getToken() + "has won.");
- // first we remove the player who lost. now there's one player in the list
- // check if that player is still alive. if they are, they win
- // this would support a game with multiple players
- }
- public void advanceCurrentPlayer()
- {
- currentPlayerPosition++;
- currentPlayer = players.get(currentPlayerPosition);
- if (currentPlayerPosition >= players.size())
- {
- currentPlayerPosition = 0; // so that we don't increment past the last player in the list
- // this checks if we are at the end and, if yes, go back to the first player
- int index = -1;
- if (currentPlayer.getLocation().equals(properties.get(i))
- {
- break;
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement