This repository has been archived by the owner on Mar 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBattle.java
294 lines (261 loc) · 10.7 KB
/
Battle.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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package a8;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.border.TitledBorder;
import javax.swing.JButton;
/**
* This class creates an organized JFrame where a human player can "battle"
* against the computer in the game Rock, Paper, Scissors. The player can select
* their hand and also choose how the computer comes up with it's move, either
* randomly, or based of the players last move.
*
* @author Nils Streedain
*
*/
@SuppressWarnings("serial")
public class Battle extends JFrame implements ActionListener {
private JButton rock;
private JButton paper;
private JButton scissors;
private JRadioButton random;
private JRadioButton lastChoice;
private String playerMove;
private String playerLastMove;
private String computerMoveType = "random";
private String computerMove;
private String winner;
private JLabel playerHand;
private JLabel winnerLabel;
private JLabel computerHand;
/**
* Constructs an organized JFrame User Interface with two main grid sections
* (2x1), an upper section for determining player and computer moves, and a
* lower section for showing game results and a bit of ASCII art each section
* contains a JPanel. Within the upper section there is another grid section
* (1x2), each again containing a JPanel, on the top left there is the player
* move selections, and on the top right there is the computer move calculation
* options.
*/
public Battle() {
super("Rock Paper Scissors");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
rock = new JButton("Rock");
paper = new JButton("Paper");
scissors = new JButton("Scissors");
random = new JRadioButton("Random Choice", true);
lastChoice = new JRadioButton("Player's Last Choice");
JPanel mainPanel = new JPanel();
JPanel topPanel = new JPanel();
JPanel topLeftPanel = new JPanel();
JPanel topRightPanel = new JPanel();
JPanel bottomPanel = new JPanel();
BoxLayout box = new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS);
mainPanel.setLayout(box);
mainPanel.add(topPanel);
mainPanel.add(bottomPanel);
topPanel.setLayout(new GridLayout(1, 2));
topPanel.setSize(topPanel.getWidth(), 0);
topPanel.add(topLeftPanel);
topPanel.add(topRightPanel);
topLeftPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Your Move",
TitledBorder.CENTER, TitledBorder.TOP));
topLeftPanel.add(rock);
topLeftPanel.add(paper);
topLeftPanel.add(scissors);
topRightPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Computer's Move",
TitledBorder.CENTER, TitledBorder.TOP));
topRightPanel.add(random);
topRightPanel.add(lastChoice);
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(random);
buttonGroup.add(lastChoice);
bottomPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Game Results",
TitledBorder.LEFT, TitledBorder.TOP));
bottomPanel.setLayout(new GridLayout(1, 3));
playerHand = new JLabel("<html><br><br><br><br><br><br><br><br><br></html>");
winnerLabel = new JLabel("");
computerHand = new JLabel("<html><br><br><br><br><br><br><br><br><br></html>");
bottomPanel.add(playerHand);
playerHand.setHorizontalAlignment(JLabel.CENTER);
bottomPanel.add(winnerLabel);
winnerLabel.setHorizontalAlignment(JLabel.CENTER);
bottomPanel.add(computerHand);
computerHand.setHorizontalAlignment(JLabel.CENTER);
setContentPane(mainPanel);
pack();
rock.addActionListener(this);
paper.addActionListener(this);
scissors.addActionListener(this);
random.addActionListener(this);
lastChoice.addActionListener(this);
}
// Getters and Setters for private variables
public String getPlayerMove() {
return playerMove;
}
public void setPlayerMove(String playerMove) {
this.playerMove = playerMove;
}
public String getComputerMoveType() {
return computerMoveType;
}
public void setComputerMoveType(String computerMoveType) {
this.computerMoveType = computerMoveType;
}
public String getComputerMove() {
return computerMove;
}
public void setComputerMove(String computerMove) {
this.computerMove = computerMove;
}
public String getPlayerLastMove() {
return playerLastMove;
}
public void setPlayerLastMove(String playerLastMove) {
this.playerLastMove = playerLastMove;
}
public String getWinner() {
return winner;
}
public void setWinner(String winner) {
this.winner = winner;
}
/**
* This method calculates what move the computer will make by first determining
* the move type (random or player's last move), then if random, it will pick a
* random int between 1 and 3, using that number to set the computerMove for
* "rock", "paper", or "scissors". If the lastChoice moveType is selected, the
* method will instead set the computerMove to "rock" if it's the first game, or
* to the player's last move if it's any other.
*
* @param moveType the name of the file to read
*/
private void computerMove(String moveType) {
if (moveType == "random") {
int rand = (int) (Math.random() * 3) + 1;
if (rand == 1) {
setComputerMove("rock");
} else if (rand == 2) {
setComputerMove("paper");
} else if (rand == 3) {
setComputerMove("scissors");
}
} else if (moveType == "lastChoice") {
if (getPlayerLastMove() != null) {
setComputerMove(getPlayerLastMove());
} else {
setComputerMove("rock");
}
}
}
/**
* This method will take both the player's selected move and the computer's
* selected move and calculate who won that match based of the rules of Rock,
* Paper, Scissors. It will the run updatePanel();
*/
private void pickWinner() {
if (getPlayerMove() == "rock") {
if (getComputerMove() == "rock") {
setWinner("Everyone loses.");
} else if (getComputerMove() == "paper") {
setWinner("Computer wins.");
} else if (getComputerMove() == "scissors") {
setWinner("You Win!");
}
}
if (getPlayerMove() == "paper") {
if (getComputerMove() == "rock") {
setWinner("You Win!");
} else if (getComputerMove() == "paper") {
setWinner("Everyone loses.");
} else if (getComputerMove() == "scissors") {
setWinner("Computer wins.");
}
}
if (getPlayerMove() == "scissors") {
if (getComputerMove() == "rock") {
setWinner("Computer wins.");
} else if (getComputerMove() == "paper") {
setWinner("You Win!");
} else if (getComputerMove() == "scissors") {
setWinner("Everyone loses.");
}
}
updatePanel();
}
/**
* This method will output both who the winner is and what moves each player
* made, as well as some ASCII art representing their move.
*/
private void updatePanel() {
if (playerMove == "rock") {
playerHand.setText(
"<html>Player: Rock<br><br><br> _______<br>---' ____)<br> (_____)<br> (_____)<br> (____)<br>---.__(___)</html>");
} else if (playerMove == "paper") {
playerHand.setText(
"<html>Player: Paper<br><br><br> _______<br>---' ____)____<br> ______)<br> _______)<br> _______)<br>---._________)</html>");
} else if (playerMove == "scissors") {
playerHand.setText(
"<html>Player: Scissors<br> .-. _<br> | | / )<br> | |/ /<br> _|__ /_ <br> / __)-' ) <br> \\ `(.-') <br> / ._>-'<br>/ \\/ </html>");
}
if (computerMove == "rock") {
computerHand.setText(
"<html>Computer: Rock<br><br><br><br> .——_<br> .:(` )<br>:( ))<br>`( )<br> ` ___-:’</html>");
} else if (computerMove == "paper") {
computerHand.setText(
"<html>Computer: Paper<br><br><br> _________<br> / /<br> / <i>Paper</i> /<br> / /<br> / /<br>/________/</html>");
} else if (computerMove == "scissors") {
computerHand.setText(
"<html>Computer: Scissors<br><br> _ _<br > (_) / )<br> | (_/ <br> _+/ <br> //|\\<br> // | |<br> (/ |/ </html>");
}
winnerLabel.setText(winner);
}
/**
* The main method creates a new battle UI and sets it to visible.
*/
public static void main(String[] args) {
Battle app = new Battle();
app.setVisible(true);
}
/**
* This method overrides the actionPerformed method from ActionListener. The
* goal of this method it to detect when a button is pressed and decide what to
* do based of the button pressed. If it's a player move, it will set the
* players move, calculate the computer's move, and pick and display the winner.
* It will also log the player's move for the next round. If the computer
* moveType is changed, instead it will just set the computer move type.
*/
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == rock) {
setPlayerMove("rock");
computerMove(getComputerMoveType());
pickWinner();
}
if (e.getSource() == paper) {
setPlayerMove("paper");
computerMove(getComputerMoveType());
pickWinner();
}
if (e.getSource() == scissors) {
setPlayerMove("scissors");
computerMove(getComputerMoveType());
pickWinner();
}
if (e.getSource() == random) {
setComputerMoveType("random");
}
if (e.getSource() == lastChoice) {
setComputerMoveType("lastChoice");
}
setPlayerLastMove(getPlayerMove());
}
}