Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. package com.fluidcoding;
  2. // Brian Sonnie
  3. import java.util.Random;
  4. import java.util.Scanner;
  5.  
  6. public class Main {
  7. private static final String ROCK = "rock";
  8. private static final String PAPER = "paper";
  9. private static final String SCISSORS = "scissors";
  10. private static final String DONE = "done";
  11.  
  12. private static final String WIN_NONE = "0";
  13. private static final String WIN_PLAYER = "1";
  14. private static final String WIN_CPU = "2";
  15.  
  16. private static String firstName;
  17. public static void main(String[] args) {
  18. String playerMove;
  19. String cpuMove;
  20. String winner;
  21. int playerWins = 0;
  22. int cpuWins = 0;
  23. int ties = 0;
  24. int gamesPlayed = 0;
  25.  
  26. firstName = getPlayerName();
  27. welcomeMessage(firstName);
  28. Scanner in = new Scanner(System.in);
  29. playerMove = getPlayerMove();
  30. while(!playerMove.equals(DONE)){
  31. gamesPlayed++;
  32. cpuMove = getCpuMove();
  33. System.out.println("Computer chooses: " + cpuMove);
  34. winner = getWinner(cpuMove, playerMove);
  35. switch(winner){
  36. case WIN_PLAYER:
  37. System.out.println("You Win!!! :)");
  38. playerWins++;
  39. break;
  40. case WIN_CPU:
  41. System.out.println("You Lose!!! :(");
  42. cpuWins++;
  43. break;
  44. default:
  45. System.out.println("Tie...");
  46. ties++;
  47. break;
  48. }
  49. playerMove = getPlayerMove();
  50. }
  51. showScores(playerWins, cpuWins, gamesPlayed, ties);
  52. }
  53.  
  54. public static void welcomeMessage(String firstName){
  55. System.out.println("Hello " + firstName + " welcome to the Rock, Paper, Scissors game.");
  56. }
  57.  
  58. public static void showScores(int playerWins, int cpuWins, int gamesPlayed, int ties){
  59. System.out.println("Game Over!!!\n"
  60. + "Games Played: " + gamesPlayed
  61. + "\n" + firstName + ": " + playerWins
  62. + "\nComputer: " + cpuWins
  63. + "\nTies: " + ties
  64. );
  65. if(playerWins==cpuWins)
  66. System.out.println("TIE...");
  67. else if(playerWins>cpuWins)
  68. System.out.println("YOU WIN!!!");
  69. else
  70. System.out.println("YOU LOSE!!!");
  71. }
  72.  
  73. private static String getWinner(String cpuMove, String playerMove){
  74.  
  75. if(cpuMove.equals(playerMove))
  76. return WIN_NONE;
  77.  
  78. if((cpuMove.equals(ROCK) && playerMove.equals(PAPER))
  79. || (cpuMove.equals(SCISSORS) && playerMove.equals(PAPER))
  80. || (cpuMove.equals(PAPER) && playerMove.equals(ROCK)))
  81. return WIN_CPU;
  82.  
  83. return WIN_PLAYER;
  84.  
  85. }
  86.  
  87. public static String getPlayerMove(){
  88. String move = "";
  89. Scanner in = new Scanner(System.in);
  90. while(move.isEmpty() || !(move.equals(ROCK)
  91. || move.equals(PAPER)
  92. || move.equals(SCISSORS)
  93. || move.equals(DONE))){
  94. System.out.println("What's your move? ("+ROCK+"|"+PAPER+"|"+SCISSORS+") -- ("+DONE+") to quit");
  95. move = in.nextLine().toLowerCase();
  96. }
  97. return move;
  98. }
  99.  
  100. public static String getCpuMove(){
  101. Random rnd = new Random();
  102. int m = rnd.nextInt(3);
  103. switch(m){
  104. case 0: return PAPER;
  105. case 1: return ROCK;
  106. default: return SCISSORS;
  107. }
  108. }
  109.  
  110. public static String getPlayerName(){
  111. System.out.println("Hello what is your name?");
  112. Scanner in = new Scanner(System.in);
  113. String name = "";
  114. while (name.equals(""))
  115. name = in.nextLine();
  116. return name;
  117. }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement