-
Notifications
You must be signed in to change notification settings - Fork 0
/
RPSMethods
145 lines (112 loc) · 4.43 KB
/
RPSMethods
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
/*
Mario Goins II
CSC 210
*/
package rpsmethods;
import java.util.Scanner;
import java.util.Random;
public class RPSMethods {
// shows message reading: "Lets play Rock, Paper, Scissors!".
public static void displayOpeningMessage() {
System.out.println("Lets play Rock, Paper, Scissors!");
}
/*
Method that prompts user to enter an integer corresponding to their
choice of move; either rock, paper or scissors. Then create a scanner to store
their selection. In the case the user enters an integer outside of the acceptable range,
or if they input a selection of another data type, we will create a loop to
continuously prompt the user to enter in an appropriate input; integer values
between 0 and 2. We will then output a statement with the user's coresponding
move.
*/
public static int getUserMove() {
Scanner input = new Scanner(System.in);
int UserChoice = -1;
do {
System.out.println("Select 0 for scissors, 1 for rock or 2 for paper!");
if (input.hasNextInt()) {
UserChoice = input.nextInt();
if (UserChoice < 0 || UserChoice > 2) {
System.out.println("Invalid input. Please enter a number between 0 and 2.");
}
} else {
System.out.println("Invalid input. Please enter a number between 0 and 2.");
input.next();
}
} while (UserChoice < 0 || UserChoice > 2);
if (UserChoice == 0) {
System.out.println("You selected scissors!");
} else if (UserChoice == 1) {
System.out.println("You selected rock!");
} else {
System.out.println("You selected paper!");
}
return UserChoice;
}
/*
Method that creates a Random object, which generates random integers between
0 and 2. An output statement with the CPU's move will then be displayed
coresponding to whichever number is generated.
*/
public static int getCPUMove() {
Random rnd = new Random();
int CPUChoice = rnd.nextInt(3);
if (CPUChoice == 0) {
System.out.println("The computer selected scissors.");
} else if (CPUChoice == 1) {
System.out.println("The computer selected rock.");
} else {
System.out.println("The computer selected paper.");
}
return CPUChoice;
}
// Method that passes through the integers associated with the return statements
// from the 2 methods immediately above. Method will determine the winner
// of the game through a switch statement. Output messages will then be printed
public static int determineWinner(int UserChoice, int CPUChoice) {
int Result = 0;
switch (UserChoice) {
case 0:
if (CPUChoice == 0) {
System.out.println("It's a draw!");
} else if (CPUChoice == 1) {
System.out.println("Rock beats scissors. You lose!");
Result+=2;
} else {
System.out.println("Scissors beats paper. You win!");
Result++;
}
break;
case 1:
if (CPUChoice == 0) {
System.out.println("Rock beats scissors. You win!");
Result++;
} else if (CPUChoice == 1) {
System.out.println("It's a draw!");
} else {
System.out.println("Paper beats rock. You lose!");
Result+=2;
}
break;
case 2:
if (CPUChoice == 0) {
System.out.println("Scissors beats paper. You lose!");
Result+=2;
} else if (CPUChoice == 1) {
System.out.println("Paper beats rock. You win!");
Result++;
} else {
System.out.println("It's a draw!");
}
break;
}
return Result;
}
//Main method. Calls the methods to perform certain areas of code
public static void main(String[] args) {
displayOpeningMessage();
int User = getUserMove();
int CPU = getCPUMove();
determineWinner(User, CPU);
}
}