-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.java
89 lines (82 loc) · 2.85 KB
/
Game.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import java.util.concurrent.*;
public class Game {
private static GLib gui;
private static boolean computerFirst;
private static Computer cpu;
// private static Player player1, player2;
private static Boolean player1first = false;
private static int sticks;
private static int player;
public Game() {
}
public void play() {
// player1 = new Player();
// player2 = new Player();
cpu = new Computer();
gui = new GLib(cpu);
sticks = Board.getSticks();
}
public static void runMove(int move) {
if (move > sticks / 2) {
gui.displayMsg("You can't take that many sticks, only up to half of the sticks left not over!");
} else if ((sticks - move) == 1) {
if (cpu.isActive()) {
if (player == 1 && computerFirst) {
gui.displayMsg("CPU has won!");
} else if (player == 2 && !computerFirst) {
gui.displayMsg("CPU has won!");
} else {
gui.displayMsg("Player has won!");
}
} else {
gui.displayMsg("Player " + player + " Has won!");
}
ScheduledExecutorService ses = Executors.newScheduledThreadPool(1);
Runnable task2 = () -> startGame();
ses.schedule(task2, 2, TimeUnit.SECONDS);
ses.shutdown();
} else {
sticks -= move;
gui.updateSticks(sticks);
player = (player == 1) ? 2 : 1;
if (cpu.isActive()) {
gui.displayMsg("Player go.");
if (player == 1 && computerFirst) {
cpu.turn(1, sticks);
} else if (player == 2 && !computerFirst) {
cpu.turn(2, sticks);
}
} else {
gui.displayMsg("Player " + player + " go.");
}
}
}
public static void startGame() {
player = 1;
if (cpu.isActive()) {
if (Math.round(Math.random() * 1.1) == 0) {
computerFirst = false;
gui.displayMsg("CPU goes second");
System.out.println("CPU SECOND");
} else {
computerFirst = true;
gui.displayMsg("CPU goes first");
System.out.println("CPU FIRST");
}
} else if (!cpu.isActive()) {
if (Math.round(Math.random() * 1.1) == 0) {
player1first = true;
gui.displayMsg("Player 1 goes first.");
} else {
player1first = false;
gui.displayMsg("Player 2 goes first");
}
}
Board.populate();
sticks = Board.getSticks();
gui.setupGame();
}
public static boolean isFirstPlayerFirst() {
return player1first;
}
}