-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRockPaperScissors.java
More file actions
226 lines (172 loc) · 6.42 KB
/
RockPaperScissors.java
File metadata and controls
226 lines (172 loc) · 6.42 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import java.util.Random;
import java.util.Scanner;
public class RockPaperScissors {
/**
* This method checks that the parameter 'turn' represents a valid turn. A valid turn is
* in the range 1 to 3 inclusive. A value of 1 means ROCK; a value of 2 means PAPER and a value of
* 3 means SCISSORS. Each turn value is, therefore, represented as an integer. Any value of
* 'turn' outside of this range is invalid.
* @param turn The turn to check.
* @return true if the turn is valid, false if is not.
*/
public boolean isValidTurn(int turn) {
if (turn == 1 || turn == 2 || turn == 3)
return true;
else
return false;
}
/**
* This method effectively converts the keyboard input into a valid turn. The keyboard
* input is in the form of a String and will represent the key pressed by the player.
* The player presses 'r' for rock, 'p' for paper and 's' for scissors. This method converts
* that input into a valid turn, represented as an integer in the range 1-3. If the parameter
* 'input' does not contain 'r' or 'p' or 's' this method should return the value -1.
* @param input The turn selected by the player. Options described above.
* @return The turn as an integer. 1 means rock, 2 means paper, 3 means scissors. A return
* value of -1 means that the parameter was invalid.
*/
public int getPlayerTurn(String input) {
// int turn = -1;
if (input.equals("r"))
return 1;
else
if (input.equals("p"))
return 2;
else
if (input.equals("s"))
return 3;
return -1;
}
/**
* This method decides which of the two players - the player or the computer - has won the round.
* The winner is decided as follows:
* - rock beats scissors
* - paper beats rock
* - scissors beats paper
* - if the the players choose the same object the round is a draw.
* @param playerChoice The player's turn.
* @param computerChoice The computer's turn.
* @return The winner, according to the rules above. If the player wins the method should
* return 'PLAYER', if the computer wins the method should return 'COMPUTER', and if it's a
* draw the method should return 'DRAW'.
*/
public String getWinner(int playerChoice, int computerChoice) {
if (playerChoice == 1 && (computerChoice == 1 || computerChoice == 2 || computerChoice == 3))
return PLAYER;
if (playerChoice == 2 && (computerChoice == 3 ))
return PLAYER;
if (computerChoice == 1 && (playerChoice == 1 || playerChoice == 2 || playerChoice == 3))
return COMPUTER;
if (computerChoice == 2 && playerChoice == 3 )
return COMPUTER;
if (playerChoice == computerChoice)
return DRAW;
return null;
}
/**
* This method effectively converts a turn represented as an integer to a String.
* If the parameter has the value 1 the method should return 'ROCK'.
* If the parameter has the value 2 the method should return 'PAPER'.
* If the parameter has the value 3 the method should return 'SCISSORS'.
* @param turn The turn to convert to a String.
* @return The turn as a String.
*/
public String getTurnAsString(int turn) {
if ( turn == 1)
return ROCK;
if ( turn == 2)
return PAPER;
if ( turn == 3)
return SCISSORS;
return null;
}
private final static int IROCK = 1;
private final static int IPAPER = 2;
private final static int ISCISSORS = 3;
private final static String ROCK_INPUT = "r";
private final static String PAPER_INPUT = "p";
private final static String SCISSORS_INPUT = "s";
private final static String ROCK = "Rock";
private final static String PAPER = "Paper";
private final static String SCISSORS = "Scissors";
private final static String PLAYER = "PLAYER";
private final static String COMPUTER = "COMPUTER";
private final static String DRAW = "DRAW";
private Random rand = new Random(System.currentTimeMillis());
private int playerWins;
private int computerWins;
private void run() {
System.out.println("Choose rock ('r' key), paper ('p' key) or scissors ('s' key). Press 'x' to quit.");
Scanner sc = new Scanner(System.in);
String splayerTurn = sc.nextLine();
while (!splayerTurn.equals("x")) {
int playerTurn = getPlayerTurn(splayerTurn);
if (!isValidTurn(playerTurn)) {
System.out.println("Invalid input.");
}
else {
int computerTurn = getRandomTurn();
System.out.println("The player chose " + getTurnAsString(playerTurn) + " and the computer chose " + getTurnAsString(computerTurn));
String result = getWinner(playerTurn, computerTurn);
recordWin(result);
if (result.equals(DRAW)) {
System.out.println("The game is a draw.");
}
else {
System.out.println("The winner is the " + result + ".");
}
}
splayerTurn = sc.nextLine();
}
sc.close();
System.out.println("Quitting.");
System.out.println("The player won " + getPlayerWins() + " time(s).");
System.out.println("The computer won " + getComputerWins() + " time(s).");
}
private int getRandomTurn() {
return rand.nextInt(3)+1;
}
/**
* This method records a win for either the player or the computer based on the parameter 'result'.
* If the value of the parameter is 'PLAYER' the method increments the number of player wins.
* If the value of the parameter is 'COMPUTER' the method increments the number of computer wins.
* @param result the player who has won. Either 'PLAYER' or 'COMPUTER'.
*/
private void recordWin(String result) {
if (result.equals(PLAYER)) {
addPlayerWin();
}
if (result.equals(COMPUTER)) {
addComputerWin();
}
}
/**
* This method returns the number of times the player has won.
* @return The number of times the player has won.
*/
private int getPlayerWins() {
return playerWins;
}
/**
* This method returns the number of times the computer has won.
* @return The number of times the computer has won.
*/
private int getComputerWins() {
return computerWins;
}
/**
* This method adds 1 to the number of player wins.
*/
private void addPlayerWin() {
++playerWins;
}
/**
* This method adds 1 to the number of computer wins.
*/
private void addComputerWin() {
++computerWins;
}
public static void main(String[] args) {
new RockPaperScissors().run();
}
}