Repository navigation
Expand file tree
/
Copy pathRPS.java
More file actions
155 lines (119 loc) · 3.99 KB
/
Copy pathRPS.java
File metadata and controls
155 lines (119 loc) · 3.99 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
import java.util.Scanner;
import java.util.Random;
public class RPS {
public static void main(String[] args) {
// User can play Rock, Paper, Scissors against program
Scanner sc = new Scanner(System.in);
Random rand = new Random();
// variables
int replay = 0, user, comp, userWins, compWins, numGames, min = 1, max = 3;
String userWord, compWord;
boolean replayBool = true;
// do while for replayability
do {
// reset wins
userWins = 0;
compWins = 0;
numGames = 0;
// prompt for # of games
System.out.println("Do you want to");
System.out.println("1 - Play a Single Game");
System.out.println("2 - Play a Series of Games");
System.out.println("3 - Leave the Game");
user = sc.nextInt();
// switch for # of games
switch (user) {
case 1:
numGames = 1;
break;
case 2:
System.out.print("Enter an odd number of games to play: ");
user = sc.nextInt();
if (user > 0) {
if (user % 2 != 0) {
numGames = (user / 2) + 1;
} else {
System.out.println("Invalid number of games entered.");
replayBool = false;
}
} else {
System.out.println("Invalid number of games entered.");
replayBool = false;
}
break;
case 3:
replayBool = false;
break;
default:
System.out.println("Invalid input... Ending game");
System.out.println("Thanks for playing!");
}
// while loop for game
while (numGames != compWins && numGames != userWins) {
// randomize computer choice
comp = rand.nextInt(max - min + 1) + min;
// user choice
System.out.println("Select:");
System.out.println("1 - Rock");
System.out.println("2 - Paper");
System.out.println("3 - Scissors");
user = sc.nextInt();
// converting numbers to strings
if (comp == 1) {
compWord = "Rock";
} else if (comp == 2) {
compWord = "Paper";
} else {
compWord = "Scissors";
}
if (user == 1) {
userWord = "Rock";
} else if (user == 2) {
userWord = "Paper";
} else {
userWord = "Scissors";
}
// output
System.out.println("---------------------");
System.out.println("User: " + userWord);
System.out.println("Computer: " + compWord);
System.out.println("---------------------");
if ((user == 2 && comp == 1) || (user == 1 && comp == 3) || (user == 3 && comp == 2)) {
System.out.println("User wins!");
System.out.println("---------------------");
userWins++;
} else if ((user == 1 && comp == 2) || (user == 3 && comp == 1) || (user == 2 && comp == 3)) {
System.out.println("Computer wins!");
System.out.println("---------------------");
compWins++;
} else if ((user == 1 && comp == 1) || (user == 3 && comp == 3) || (user == 2 && comp == 2)) {
System.out.println("It's a draw!");
System.out.println("---------------------");
} else {
System.out.println("");
}
System.out.println("User Wins: " + userWins);
System.out.println("Computer Wins: " + compWins);
System.out.println("---------------------");
}
// if else for win message
if (numGames == userWins && replayBool == true) {
System.out.println("Congrats! You beat the computer.");
System.out.println("Would you like to play again?");
System.out.println("1 - Yes");
System.out.println("2 - No");
replay = sc.nextInt();
} else if (numGames == compWins && replayBool == true) {
System.out.println("Sorry! The computer beat you :(");
System.out.println("Would you like to try again?");
System.out.println("1 - Yes");
System.out.println("2 - No");
replay = sc.nextInt();
} else {
replay = 2;
}
} while (replay == 1);
System.out.println("Thanks for playing!");
sc.close();
}
}