This repository was archived by the owner on Jun 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRPSGameGUI.java
More file actions
99 lines (79 loc) · 2.75 KB
/
RPSGameGUI.java
File metadata and controls
99 lines (79 loc) · 2.75 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
import javax.swing.JOptionPane;
public class RPSGameGUI
{
public static void main (String[] args)
{
//Creating the RockPaperScissors object
RockPaperScissors rps = new RockPaperScissors (); //***Your class
// Create variables to hold data
int numGames = 0;
String userChoice = "";
String cpuChoice = "";
String winner = "";
int userWins;
int cpuWins;
String playerName = "";
int playAgain = 0;
//Show input box
playerName = JOptionPane.showInputDialog(null, "Please enter your name: ");
//Show message box
JOptionPane.showMessageDialog(null, "Welcome " + playerName);
do
{
//Set wins to 0 at beginning of loop. Will reset to 0 if loop executes again.
userWins = 0;
cpuWins = 0;
//Show input box and parse to integer
numGames = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter an odd number of games to play: "));
//Validate input
while (numGames % 2 == 0 || numGames < 1) //Even number or less than 1
{
numGames = Integer.parseInt(JOptionPane.showInputDialog(null, "Sorry, number of games must be greater than 1 and odd. Please try again:"));
}
//Play the game for the number of rounds the user entered
for (int i = 1; i <= numGames; i++)
{
//Get the user and computer choices
userChoice = rps.getUserChoice(); //***Your method
cpuChoice = rps.getCPUChoice(); //***Your method
JOptionPane.showMessageDialog(null, "Computer chooses " + cpuChoice);
//Pick winner
winner = rps.pickWinner(userChoice, cpuChoice); //***Your method
if (winner.equalsIgnoreCase("Tie"))
{
JOptionPane.showMessageDialog(null, "It's a tie! Play again.");
numGames++;
}
else
{
if (winner.equalsIgnoreCase("User"))
{
userWins++;
}
else if (winner.equalsIgnoreCase("Computer"))
{
cpuWins++;
}
else
{
JOptionPane.showMessageDialog(null,"Error in picking winner");
}
JOptionPane.showMessageDialog(null, winner + " wins!");
}
} //end for
//Print results
JOptionPane.showMessageDialog(null, playerName + " won " + userWins + " time(s).");
JOptionPane.showMessageDialog(null, "Computer won " + cpuWins + " time(s).");
if (userWins > cpuWins)
{
JOptionPane.showMessageDialog(null, playerName + " won!");
}
if (cpuWins > userWins)
{
JOptionPane.showMessageDialog(null, "The computer won!");
}
//Show confirmation box
playAgain = JOptionPane.showConfirmDialog(null, "\nThank you for playing! \n Would you like to play again?");
} while(playAgain == JOptionPane.YES_OPTION);
} //end main
} //end class