-
Notifications
You must be signed in to change notification settings - Fork 0
/
RockPaperScissors.java
70 lines (57 loc) · 2.02 KB
/
RockPaperScissors.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
/*
Rock paper scissors
Rock beats scissors beats paper beats rock
*/
package Project1;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class RockPaperScissors {
public static void main(String[] args) {
String[] rps = {"r","p","s"};
String computerMove = rps[new Random().nextInt(rps.length)];
Scanner s = new Scanner(System.in);
while (true) {
String playerMove;
while (true) {
System.out.println("Please enter your move (r, p, s)");
playerMove = s.nextLine();
if (playerMove.equals("r") || playerMove.equals("p") || playerMove.equals("s")){
break;
} else
System.out.println(playerMove + " is not a valid move");
}
System.out.println("Computer played: " + computerMove );
if(playerMove.equals(computerMove)){
System.out.println("The game was a tie");
}
else if (playerMove.equals("r")){
if (computerMove.equals("p")){
System.out.println("You lose!");
} else if(computerMove.equals("s")){
System.out.println("You win!");
}
}
else if (playerMove.equals("p")){
if (computerMove.equals("s")){
System.out.println("You lose!");
} else if(computerMove.equals("r")){
System.out.println("You win!");
}
}
else {
if (computerMove.equals("r")){
System.out.println("You lose!");
} else if(computerMove.equals("p")){
System.out.println("You win!");
}
}
System.out.println("Play again? (y/n)");
String playAgain = s.nextLine();
if (!playAgain.equals("y")) {
break;
}
}
s.close();
}
}