-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
118 lines (104 loc) · 3.41 KB
/
Main.java
File metadata and controls
118 lines (104 loc) · 3.41 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
import java.util.Scanner;
import java.util.Random;
enum Move {
ROCK,
PAPER,
SCISSORS
}
enum Result {
PLAYER1,
PLAYER2,
TIE,
}
class Game {
private static final Random random = new Random();
public static boolean isInteger(String str) {
try {
Integer.parseInt(str);
return true;
} catch (NumberFormatException nfe) {
return false;
}
}
private static String stringFromMove(Move move) {
return switch (move) {
case ROCK -> "Rock";
case PAPER -> "Paper";
case SCISSORS -> "Scissors";
};
}
private static Move moveFromNumber(int num) {
return switch (num) {
case 1 -> Move.ROCK;
case 2 -> Move.PAPER;
case 3 -> Move.SCISSORS;
default -> throw new IllegalArgumentException(
String.format("Num must be number from 1-3 (got %s)", num));
};
}
private static Move getCPUMove() {
return moveFromNumber((random.nextInt(3)) + 1);
}
private static Move getPlayerMoveOrExit() {
String input;
boolean repeat = false;
do {
if (repeat) {
System.out.println("Invalid input. Enter a number from 1 to 3, or type \"exit\".");
}
System.out.println("Rock (1), paper (2), or scissors (3)?" +
"\t-----\t Type \"exit\" to quit");
Scanner scanner = new Scanner(System.in);
input = scanner.nextLine().trim();
if (input.equalsIgnoreCase("exit")) {
System.out.print("Bye!");
System.exit(0);
}
repeat = true;
} while (!(isInteger(input) &&
Integer.parseInt(input) >= 1 && Integer.parseInt(input) <= 3));
return moveFromNumber(Integer.parseInt(input));
}
private static Result getResult(Move move1, Move move2) {
if (move1 == Move.ROCK && move2 == Move.SCISSORS ||
move1 == Move.PAPER && move2 == Move.ROCK ||
move1 == Move.SCISSORS && move2 == Move.PAPER) {
return Result.PLAYER1;
} else if (move2 == Move.ROCK && move1 == Move.SCISSORS ||
move2 == Move.PAPER && move1 == Move.ROCK ||
move2 == Move.SCISSORS && move1 == Move.PAPER) {
return Result.PLAYER2;
} else {
return Result.TIE;
}
}
private void PrintResult(Move playerMove, Move CPUMove) {
System.out.printf("You chose %s. CPU Chose %s.%n",
stringFromMove(playerMove), stringFromMove(CPUMove));
switch (getResult(playerMove, CPUMove)) {
case PLAYER1:
System.out.println("You win! Wanna go again?");
break;
case PLAYER2:
System.out.println("You lost :( Try again!");
break;
case TIE:
System.out.println("It's a tie! Play again!");
break;
}
}
public void gameLoop() {
Move playerMove = getPlayerMoveOrExit();
Move CPUMove = getCPUMove();
PrintResult(playerMove, CPUMove);
}
}
class Program {
public static void main(String[] args) {
Game game = new Game();
System.out.println("Welcome to rock paper scissors!");
while (true) {
game.gameLoop();
}
}
}