Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.82 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3. import java.util.List;
  4. import java.util.Scanner;
  5.  
  6. public class YatzyUI {
  7.  
  8. public static void main(String[] args) {
  9. Scanner scanner = new Scanner(System.in);
  10. System.out.println("Enter the name of the first player");
  11. String firstPlayerName = scanner.next();
  12. System.out.println("Enter the name of the second player");
  13. String secondPlayerName = scanner.next();
  14. Player p1 = new Player(firstPlayerName);
  15. Player p2 = new Player(secondPlayerName);
  16. List<Player> players = new ArrayList<>();
  17. players.add(p1);
  18. players.add(p2);
  19. int score = 0;
  20. try {
  21. for (int i = 1; i <= 15; i++) {
  22. p1.firstRollDice(firstPlayerName);
  23. p1.secondAndThirdRollDice();
  24. p1.selectAndSetCategoryScore(p1.getDiceArray());
  25. }
  26. } catch (NullPointerException e) {
  27. System.out.println("Please enter a valid category ,the category chosen is invalid ");
  28. p1.selectAndSetCategoryScore(p1.getDiceArray());
  29. }
  30. try {
  31. for (int i = 1; i <= 15; i++) {
  32. p2.firstRollDice(secondPlayerName);
  33. p2.secondAndThirdRollDice();
  34. p2.selectAndSetCategoryScore(p1.getDiceArray());
  35. }
  36.  
  37. } catch (NullPointerException e) {
  38. System.out.println("Please enter a valid category ,the category chosen is invalid ");
  39. p2.selectAndSetCategoryScore(p1.getDiceArray());
  40. }
  41.  
  42. Collections.sort(players, new ScoreComparartor());
  43. Player winner = players.remove(1);
  44. System.out.println("Following is the winner of the game :" + winner.getPlayerName() + " with the score : " + winner.getPlayerScore());
  45. }
  46. }
  47.  
  48. import YatzyGameCategories.Categories;
  49.  
  50. import java.util.Map;
  51. import java.util.Scanner;
  52.  
  53. public class Player {
  54.  
  55. public String getPlayerName() {
  56. return playerName;
  57. }
  58.  
  59. public void setPlayerName(String playerName) {
  60. this.playerName = playerName;
  61. }
  62.  
  63. private String playerName;
  64. Map<Integer, Categories> remainingCategories = Categories.getCategories();
  65.  
  66. public int getPlayerScore() {
  67. return playerScore;
  68. }
  69.  
  70. public void setPlayerScore(int score) {
  71. this.playerScore = score;
  72. }
  73.  
  74. private int playerScore;
  75.  
  76. public int[] getDiceArray() {
  77. return diceArray;
  78. }
  79.  
  80. public void setDiceArray(int[] diceArray) {
  81. this.diceArray = diceArray;
  82. }
  83.  
  84. int[] diceArray;
  85.  
  86. public Player(String name)
  87. {
  88. this.playerName=name;
  89. }
  90.  
  91. public void firstRollDice() {
  92. System.out.println(playerName + "'s turn. Click 'Roll Dice' button to roll the dice.");
  93. diceArray= RandomDieGenerator.randomDieValueGenerator(5);
  94. for (int i : diceArray)
  95. System.out.println(i);
  96. }
  97.  
  98. public void secondAndThirdRollDice() {
  99. for (int i = 0; i < 2; i++) {
  100. System.out.println("Select the dice you wish to re-roll"); // 3 turns
  101. System.out.println("Enter the die position separated by ,");
  102. Scanner sc = new Scanner(System.in);
  103. String s = sc.next();
  104. String dicesToReRoll[] = s.split(",");
  105. int dicesReRolled[] = new int[dicesToReRoll.length];
  106. for (int j = 0; j < dicesToReRoll.length; j++) {
  107. dicesReRolled[j] = Integer.parseInt(String.valueOf(dicesToReRoll[j]));
  108. diceArray[dicesReRolled[j] - 1] = RandomDieGenerator.randomDieValueGenerator(dicesReRolled.length)[j];
  109. }
  110. setDiceArray(diceArray);
  111. for (int k : diceArray)
  112. System.out.println(k);
  113.  
  114. }
  115. }
  116.  
  117. public void selectAndSetCategoryScore(int ...diceArray) {
  118.  
  119. remainingCategories.forEach((key, value) -> System.out.println(key + ":" + value.getName(key)));
  120. System.out.println("Select a Category for this roll.");
  121. Scanner sc = new Scanner(System.in);
  122. int category = 0;
  123.  
  124. category = sc.nextInt();
  125. Categories cat = remainingCategories.get(category);
  126. remainingCategories.remove(category);
  127. remainingCategories.forEach((key, value) -> System.out.println(key + ":" + value.getName(key)));
  128. //throws error if user selects a category other than values present in the remaining categories
  129. playerScore = playerScore + cat.score(diceArray);
  130. setPlayerScore(playerScore);
  131.  
  132.  
  133.  
  134. }
  135. }
  136.  
  137. p2.firstRollDice(secondPlayerName);
  138.  
  139. p1.secondAndThirdRollDice();
  140.  
  141. p1.selectAndSetCategoryScore(p1.getDiceArray());
  142.  
  143. } catch (NullPointerException e) {
  144.  
  145. for (int turn = 1; turn <= 15; turn++) {
  146. for (Player: players) {
  147. // Play one turn.
  148. }
  149. }
  150.  
  151. import java.util.ArrayList;
  152. import java.util.Collections;
  153. import java.util.List;
  154. import java.util.Scanner;
  155.  
  156. public class YatzyUI {
  157.  
  158. public static void main(String[] args) {
  159. System.out.println("Enter the names of the players separated by comma,");
  160. Scanner sc = new Scanner(System.in);
  161. String names = sc.next();
  162. String[] playersName = names.split(",");
  163. List<Player> players=new ArrayList<>();
  164. for(int i=0;i<playersName.length;i++)
  165. {
  166. players.add(new Player(playersName[i]));
  167. }
  168. int score = 0;
  169. for(Player p:players) {
  170. while(p.remainingCategories.size()!=0 )
  171. {
  172. p.firstRollDice();
  173. p.secondAndThirdRollDice();
  174. p.selectAndSetCategoryScore();
  175. }
  176. }
  177. Collections.sort(players, new ScoreComparartor());
  178. Player winner = players.remove(0);
  179. System.out.println("Following is the winner of the game :" + winner.getPlayerName() + " with the score : " + winner.getPlayerScore());
  180. }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement