-
Notifications
You must be signed in to change notification settings - Fork 0
/
RPS.java
125 lines (105 loc) · 3.75 KB
/
RPS.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import java.util.Random;
import java.util.Scanner;
/**
* This class simulates Rock Paper Scissors
* @author cgsze
*
*/
public class RPS {
public static void main(String[] args) {
// generate computer's choice
String computer = ""; // computer's RPS choice
Random rng = new Random(); // randomly generate number 0-2
int gen = 5;
boolean guaranteeWin = false;
boolean lizardSpock = false;
// start the game
Scanner in = new Scanner(System.in); // prompt user for input
System.out.println("Configure your settings:");
System.out.println("(1) Regular Game / (2) RPSLS (under construction)");
String user = in.nextLine();
System.out.println("(3) Regular Odds / (4) Guaranteed Win");
user = in.nextLine();
if (user.equals("4")) { guaranteeWin = true; }
System.out.println("Settings Done. Game Begin\n");
while (!user.toUpperCase().equals("EXIT")) {
System.out.println("Make your choice:\nROCK / PAPER / SCISSORS / EXIT");
gen = rng.nextInt(3);
// Each randomly generated number corresponds to an RPS choice
if (gen == 0) {
computer = "ROCK";
} else if (gen == 1) {
computer = "SCISSORS";
} else if (gen == 2) {
computer = "PAPER";
}
user = in.nextLine();
if (user.toUpperCase().equals("EXIT")) { break; }
if (guaranteeWin) {
computer = guaranteedWin(user);
}
System.out.println("Computer says: " + computer);
playGame(user, computer);
System.out.println();
gen = rng.nextInt(3);
}//end of while loop
}//end of main method
/**
* Helper method to return a choice that guarantees
* the user will win in a game of RPS
* @param choice: user's input choice
* @return losing choice
*/
protected static String guaranteedWin(String choice) {
String lose = "";//the computer returns a losing choice so user wins
if (choice.toUpperCase().equals("ROCK")) {
lose = "SCISSORS";
} else if (choice.toUpperCase().equals("PAPER")) {
lose = "ROCK";
} else if (choice.toUpperCase().equals("SCISSORS")) {
lose = "PAPER";
}
return lose;
}//end of helper method
/**
* This helper method returns whether the user has won the game or not
* @param choice: user's input choice
* @param computer: computer's choice
*/
protected static void playGame(String choice, String computer) {
if (choice.toUpperCase().equals(computer.toUpperCase())) {
System.out.println("Tie");
}
else {
boolean win = determineWin(choice, computer);
if (win) { System.out.println("You win!"); }
else if (!win) { System.out.println("You lost!"); }
}
}//end of helper method
/**
* This helper method lets us know if the user's choice beats
* the computer's choice, assuming that a tie is not possible
* @param choice: user's input
* @param computer: computer's choice
* @return true if user beats computer, else false
*/
public static boolean determineWin(String choice, String computer) {
boolean won = false;
if (choice.toUpperCase().equals("ROCK")) {
if (computer.toUpperCase().equals("SCISSORS")) {
won = true;
}
} else if (choice.toUpperCase().equals("PAPER")) {
if (computer.toUpperCase().equals("ROCK")) {
won = true;
}
} else if (choice.toUpperCase().equals("SCISSORS")) {
if (computer.toUpperCase().equals("PAPER")) {
won = true;
}
} else {
System.out.println("Invalid input");
}
return won;
}//end of helper method
}//end of class