Advertisement
mellynx

Untitled

Nov 26th, 2015
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. package learning;
  2.  
  3. public class Game // November 24 and 26, 2015
  4. {
  5. ArrayList<Player> players;
  6. ArrayList<Property> properties;
  7.  
  8. Random random;
  9. Player currentPlayer;
  10. int currentPlayerPosition;
  11.  
  12. public Game()
  13. {
  14. makeProperties();
  15. makePlayers();
  16. currentPlayer = players.get(0);
  17. currentPlayerPosition = 0;
  18.  
  19. random = new Random(); // we have an object we want to last the lifetime of the game, and we initialize it in the constructor
  20.  
  21. for (int i =0; i <players.size(); i ++) {
  22. Player player = players.get(i);
  23. player.addMoney(1500);
  24. player.setLocation(Boardwalk);
  25. }
  26. }
  27. public void makeProperties()
  28. {
  29. Property parkPlace = new Property(30, 5);
  30. Property boardwalk = new Property(40, 10);
  31. }
  32. public void makePlayers()
  33. {
  34. Player player = new Player("thimble");
  35. Player owner = new Player("dog");
  36. }
  37. public void playGame(player)
  38. {
  39. //use a while loop rather than a for loop because we don't know how many turns this game will take
  40. while(true) {
  41. Player winner = doTurn(currentPlayer); // doTurn should return a player if there is a winner
  42. if (winner != null) { // if there is a winner, return the winner
  43. return winner;
  44. }
  45. advanceCurrentplayer();
  46. }
  47. }
  48. public void doTurn(Player player)
  49. {
  50. System.out.println("Started turn: " + player.getToken());
  51.  
  52. int die1 = random.nextInt(6) + 1;
  53. int die2 = random.nextInt(6) + 1;
  54. int diceRoll = die1 + die2;
  55.  
  56. System.out.println("Rolled: " + die1 + " and " + die2 + " => " + diceRoll);
  57.  
  58. Property landed = advancePlayer(player, diceRoll);
  59.  
  60. System.out.println(player.getToken() + " landed on " + landed.getName());
  61.  
  62. if (landed.getOwner() == null) // we're ignoring if (landed.getOwner() == player)
  63. {
  64. player.buyProperty(landed, landed.getPrice());
  65. System.out.println(player.getToken() + " has bought " + landed.getName() + " for $" + landed.getPrice());
  66. }
  67.  
  68. 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
  69. {
  70. player.subtractMoney(landed, landed.getRent());
  71. landed.getOwner().addMoney(landed.getRent());
  72.  
  73. System.out.println(player.getToken() + " has paid " + landed.getOwner.getToken() + "$" + landed.getRent() + " for landing on " + landed.getName());
  74.  
  75. /* landed is a Property
  76. landed.getOwner() <- Player object that owns property
  77. landed.getOwner().addMoney <- call a function on the Player object that owns the property
  78. then you pass to that landed.getRent() <- rent value of the property
  79. */
  80. }
  81. if (player.getBalance() == 0)
  82. {
  83. players.remove(player);
  84. System.out.println("Player " + player.getToken() + "has lost.");
  85.  
  86. if (players.size() == 1) {
  87. return players.get(0);
  88. System.out.println(player.getToken() + "has won.");
  89. // first we remove the player who lost. now there's one player in the list
  90. // check if that player is still alive. if they are, they win
  91. // this would support a game with multiple players
  92. }
  93. public void advanceCurrentPlayer()
  94. {
  95. currentPlayerPosition++;
  96. currentPlayer = players.get(currentPlayerPosition);
  97.  
  98. if (currentPlayerPosition >= players.size())
  99. {
  100. currentPlayerPosition = 0; // so that we don't increment past the last player in the list
  101. // this checks if we are at the end and, if yes, go back to the first player
  102. int index = -1;
  103.  
  104. if (currentPlayer.getLocation().equals(properties.get(i))
  105. {
  106. break;
  107. }
  108. }
  109. }
  110. }
  111. }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement