Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.51 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.io.*;
  3. import java.util.Random;
  4.  
  5. public class RPS
  6. { // Start of Class
  7.  
  8.  
  9. static String getPlayerName()
  10. { // Start of getPlayerName method
  11. Scanner inPut = new Scanner(System.in);
  12. System.out.print("Enter your first name: ");
  13. String playerName = inPut.nextLine();
  14. while (playerName.length() == 0)
  15. { // Start of If
  16. System.out.println("No name entered.");
  17. System.out.print("Please enter your name: ");
  18. playerName = inPut.nextLine();
  19. } // End of If
  20. return playerName ;
  21. } // End of getPlayerName method
  22.  
  23. static void welcomeMessage(String playerName)
  24. {
  25. System.out.println("Hello "+playerName+". Welcome to the Rock, Paper, Scissors game.");
  26. }
  27.  
  28. static String playerChoiceM()
  29. {
  30. Scanner inPut = new Scanner(System.in);
  31. System.out.print("Enter Rock, Paper, Scissors, or Done: ");
  32. String playerChoice = inPut.nextLine();
  33. while (!playerChoice.equalsIgnoreCase("Rock") && !playerChoice.equalsIgnoreCase("Scissors") && !playerChoice.equalsIgnoreCase("Paper") && !playerChoice.equalsIgnoreCase("Done"))
  34. {
  35. System.out.print("Enter Rock, Paper, Scissors, or Done: ");
  36. playerChoice=inPut.nextLine();
  37. }
  38. System.out.println("You selected "+playerChoice);
  39. return playerChoice;
  40. }
  41.  
  42. static String computerChoiceM()
  43. {
  44. Random rand = new Random();
  45. String[] computerChoiceArray ={"Rock","Paper","Scissors"};
  46. int randomIndex = rand.nextInt(3);
  47. String computerChoice = computerChoiceArray[randomIndex];
  48. System.out.println("The computer chose "+computerChoice);
  49. return computerChoice;
  50. }
  51.  
  52. static int[] determineWinner(String playerChoice , String computerChoice)
  53. {
  54. int[] scores;
  55. scores = new int[3];
  56. if (playerChoice.equalsIgnoreCase("Rock") && computerChoice.equalsIgnoreCase("Paper"))
  57. { scores[1] +=1;
  58. System.out.println("You lose!"); }
  59. else if (playerChoice.equalsIgnoreCase("Rock") && computerChoice.equalsIgnoreCase("Scissors"))
  60. { scores[0] +=1;
  61. System.out.println("You win!");}
  62. else if (playerChoice.equalsIgnoreCase("Rock") && computerChoice.equalsIgnoreCase("Rock"))
  63. {scores[2] +=1;
  64. System.out.println("You tie!"); }
  65. else if (playerChoice.equalsIgnoreCase("Paper") && computerChoice.equalsIgnoreCase("Rock"))
  66. {scores[0] +=1;
  67. System.out.println("You win!");}
  68. else if (playerChoice.equalsIgnoreCase("Paper") && computerChoice.equalsIgnoreCase("Paper"))
  69. {scores[2] +=1;
  70. System.out.println("You tie!");}
  71. else if (playerChoice.equalsIgnoreCase("Paper") && computerChoice.equalsIgnoreCase("Scissors"))
  72. {scores[1] +=1;
  73. System.out.println("You lose!");}
  74. else if (playerChoice.equalsIgnoreCase("Scissors") && computerChoice.equalsIgnoreCase("Scissors"))
  75. {scores[2] +=1;
  76. System.out.println("You tie!");}
  77. else if (playerChoice.equalsIgnoreCase("Scissors") && computerChoice.equalsIgnoreCase("Paper"))
  78. {scores[0] +=1;
  79. System.out.println("You win!");}
  80. else if (playerChoice.equalsIgnoreCase("Scissors") && computerChoice.equalsIgnoreCase("Rock"))
  81. {scores[1] +=1;
  82. System.out.println("You lose!");}
  83. return scores;
  84. }
  85.  
  86. static void winnerBoard(int playerWins , int computerWins , int gameCounter , int ties)
  87. {
  88. System.out.println();
  89. System.out.println("Thank you for playing! Here are the results:");
  90. System.out.println("You won "+playerWins+" time(s)!");
  91. System.out.println("Computer won "+computerWins+" time(s)!");
  92. System.out.println("It was a tie "+ties+" time(s)!");
  93. System.out.println("You played "+gameCounter+" game(s)!");
  94. }
  95.  
  96. public static void main(String[] args)
  97. {
  98. Scanner inPut = new Scanner(System.in);
  99. String playerName = getPlayerName();
  100. welcomeMessage(playerName);
  101. System.out.println();
  102. String anotherGame = "yes";
  103. int[] scores={0,0,0};
  104. int playerWins =0, computerWins=0 , ties=0 , gameCounter=1;
  105. while (anotherGame.equalsIgnoreCase("Yes"))
  106. {
  107. String playerChoice=playerChoiceM();
  108. if (playerChoice.equalsIgnoreCase("Done"))
  109. {
  110. winnerBoard(playerWins,computerWins,ties,gameCounter);
  111. System.exit(0);
  112. }
  113. String computerChoice=computerChoiceM();
  114. scores = determineWinner(playerChoice , computerChoice);
  115. playerWins += scores[0];
  116. computerWins += scores[1];
  117. ties += scores[2];
  118.  
  119. System.out.println();
  120. System.out.print("Would you like to play another game? yes or no: ");
  121. anotherGame = inPut.nextLine();
  122. System.out.println();
  123. if (anotherGame.equalsIgnoreCase("yes"))
  124. {
  125. ++gameCounter;
  126. }
  127. else if (!anotherGame.equalsIgnoreCase("yes") || !anotherGame.equalsIgnoreCase("no"))
  128. {
  129. while (!anotherGame.equalsIgnoreCase("no") && !anotherGame.equalsIgnoreCase("yes"))
  130. {
  131. System.out.print("Would you like to play another game? yes or no: ");
  132. anotherGame = inPut.nextLine();
  133. if (anotherGame.equalsIgnoreCase("yes"))
  134. {
  135. ++gameCounter;
  136. }
  137. }
  138. }
  139. }
  140. winnerBoard(playerWins,computerWins,gameCounter,ties);
  141.  
  142. }
  143.  
  144.  
  145. } // End of Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement