-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRockPaperScissorsGame.java
More file actions
69 lines (64 loc) · 2.16 KB
/
RockPaperScissorsGame.java
File metadata and controls
69 lines (64 loc) · 2.16 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
import java.util.ArrayList;
import java.util.List;
public class RockPaperScissorsGame {
private void printReady() {
System.out.print("Ready?");
try {
Thread.sleep(50);
for (int i = 1; i <= 3; i++) {
System.out.print(i);
Thread.sleep(50);
for (int k = 0; k < 5; k++) {
System.out.print(".");
Thread.sleep(50);
}
}
System.out.println("GO!");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private int getResult(Action hAction, Action cAction){
if (hAction.equals(cAction)){
return 0;
}
if (hAction.equals(Action.ROCK) && cAction.equals(Action.SCISSORS)||hAction.equals(Action.PAPER) && cAction.equals(Action.ROCK)||hAction.equals(Action.SCISSORS) && cAction.equals(Action.PAPER)){
System.out.println("Human wins");
return 1;
} else {
System.out.println("CPU wins");
return -1;
}
}
public void play() {
List<Action> history = new ArrayList<>();
int consecutiveWin = 0;
String res;
Human human = new Human();
Strategy tempStrategy = new LastStrategy(history);
CPU cpu = new CPU(new CycleStrategy(), history);
do {
printReady();
// get result
Action humanHand = human.play();
Action cpuHand = cpu.play(consecutiveWin, history);
history.add(humanHand);
System.out.println("human: " + humanHand.toString() + " cpu: " + cpuHand.toString());
int result = getResult(humanHand, cpuHand);
if (result == 1){
consecutiveWin++;
} else {
consecutiveWin = 0;
}
//same as below: if results == 1, consecutive++ else 0
//consecutiveWin = result == 1 ? consecutiveWin + 1 : 0;
System.out.println("Do you want to play again?");
res = Utils.scanner.next();
while (!res.equals("yes") && !res.equals("no")) {
System.out.println("Wrong input! please type either \"yes\" or \"no\"");
res = Utils.scanner.next();
}
} while (res.equals("yes"));
Utils.scanner.close();
}
}