Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. class Legacy {
  2.  
  3. static int humanScore = 0
  4. static int compScore = 0
  5. static String wantToPlay
  6. static String playersChoice
  7. static String computerPick
  8.  
  9. public static void main(String[] args) {
  10. wantToPlay = System.console().readLine("Would you like to play RoShamBo? Enter Yes or No: ").toLowerCase()
  11.  
  12. startGame()
  13. }
  14.  
  15. private static void startGame() {
  16. if (wantToPlay == "yes") {
  17. playersChoice = System.console().readLine("Make a selection: r for rock, p for paper, s for scissors: ").toLowerCase()
  18.  
  19. makeSelection()
  20. }
  21. else if (wantToPlay == "no") {
  22. println("Good Bye")
  23. System.exit(0)
  24. }
  25. else {
  26. wantToPlay = System.console().readLine("Please Enter Yes or No: ").toLowerCase()
  27. startGame()
  28. }
  29. }
  30.  
  31. private static void makeSelection() {
  32. if (playersChoice == "r" || playersChoice == "p" || playersChoice == "s") {
  33. computerPick = computerChoice()
  34.  
  35. println("You chose ${playersChoice} and the computer chose ${computerPick}")
  36.  
  37. int gameResult = compareToWin(playersChoice, computerPick)
  38.  
  39. switch(gameResult) {
  40. case 0:
  41. println("The result is a tie.")
  42. break
  43. case -1:
  44. println("The computer wins")
  45. compScore++;
  46. break
  47. case 1:
  48. println("You win")
  49. humanScore++
  50. break
  51. }
  52.  
  53. println("Score is You = ${humanScore} and the Machine = ${compScore}")
  54.  
  55. if (humanScore > compScore) {
  56. println("Keep beating the machine")
  57. }
  58. else if (compScore > humanScore) {
  59. println("You are getting your ass handed to you!!!")
  60. }
  61. else {
  62. println("Can someone go ahead already, geez")
  63. }
  64.  
  65. wantToPlay = System.console().readLine("Would you like to play again? ").toLowerCase()
  66.  
  67. if (wantToPlay == "yes" || wantToPlay == "no") {
  68. startGame()
  69. }
  70. else {
  71. wantToPlay = System.console().readLine("Please enter Yes or No").toLowerCase()
  72. startGame()
  73. }
  74. }
  75. else {
  76. playersChoice = System.console().readLine("Your only choices are r,p, and s. Choose again: ").toLowerCase()
  77. makeSelection()
  78. }
  79. }
  80.  
  81. private static String computerChoice() {
  82. Random rnd = new Random()
  83. int i = rnd.nextInt(3)
  84.  
  85. if (i == 0) return "r"
  86. if (i == 1) return "p"
  87.  
  88. return "s"
  89. }
  90.  
  91. private static int compareToWin(String humanChoice, String compChoice) {
  92. if (humanChoice == "r" && compChoice == "p") return -1
  93. if (humanChoice == "r" && compChoice == "s") return 1
  94. if (humanChoice == "p" && compChoice == "s") return -1
  95. if (humanChoice == "p" && compChoice == "r") return 1
  96. if (humanChoice == "s" && compChoice == "r") return -1
  97. if (humanChoice == "s" && compChoice == "p") return 1
  98.  
  99. return 0
  100. }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement