Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. import java.util.Random;
  2. import java.util.Scanner;
  3.  
  4.  
  5. public class RockPaperScissors {
  6. // Random r = new Random();
  7. private static final long WAITTIME = 25; // Konstpaus
  8. public static void main(String[] arg){
  9. while(true){
  10. try{
  11. Thread.sleep(WAITTIME*50);
  12. }catch(InterruptedException ie){
  13. System.err.print(ie);
  14. }
  15. System.out.printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n" +
  16. "%s is the winner!" +
  17. "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n\n",decideOutcome(getAction(),getCpMove()));
  18. }
  19. }
  20.  
  21. public static int getCpMove(){
  22. int cpMove = (new Random()).nextInt(2)+1;
  23. String r;
  24. switch(cpMove){
  25. case 1: r = "Computer picked rock."; break;
  26. case 2: r = "Computer picked paper."; break;
  27. case 3: r = "Computer picked scissors."; break;
  28. default: r = "Hey, hold on. That's not a valid number!";
  29. }
  30.  
  31. try{
  32. Thread.sleep(WAITTIME);
  33. }catch(InterruptedException ie){
  34. System.err.print(ie);
  35. }
  36.  
  37. System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n" +
  38. r + "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n");
  39. return cpMove;
  40. }
  41.  
  42. public static int getAction(){
  43. String a = "";
  44. int x;
  45. System.out.println("~~~~~~ Make your move ~~~~~~");
  46. System.out.println("1) Rock.\n" +
  47. "2) Paper.\n" +
  48. "3) Scissors.\n" +
  49. "~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
  50. do{
  51. x = (new Scanner(System.in)).nextInt();
  52. switch(x){
  53. case 1: a = "You picked rock."; break;
  54. case 2: a = "You picked paper."; break;
  55. case 3: a = "You picked scissors."; break;
  56. default: a = "Hey, hold on. That's not a valid number!";
  57. }
  58. System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n" +
  59. a + "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  60. }while(x < 1 && x > 3);
  61. return x;
  62. }
  63.  
  64. public static String decideOutcome(int player, int cp){
  65. String winner = "";
  66. int p = player;
  67. int c = cp;
  68.  
  69. try{
  70. Thread.sleep(WAITTIME);
  71. }catch(InterruptedException ie){
  72. System.err.print(ie);
  73. }
  74.  
  75. System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  76. if(cp == player){
  77. System.out.println("It's a tie!");
  78. winner = "No one";
  79. }
  80. else if((cp == 1 && player == 2) || (cp == 2 && player == 1)){
  81. System.out.println("Paper beats rock!");
  82. winner = (cp != 1) ? "Computer" : "Player";
  83. }
  84. else if((cp == 1 && player == 3) || (cp == 3 && player == 1)){
  85. System.out.println("Rock beats scissors!");
  86. winner = (cp == 1) ? "Computer" : "Player";
  87. }
  88. else if ((cp == 2 && player == 3) || (cp == 3 && player == 2)){
  89. System.out.println("Scissors beat paper!");
  90. winner = (cp != 2) ? "Computer" : "Player";
  91. }
  92. else{
  93. System.out.println("I missed an outcome, please notify me of this.");
  94. winner = "Murphy's law";
  95. }
  96. return winner;
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement