Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement