-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.java
More file actions
95 lines (82 loc) · 1.97 KB
/
Game.java
File metadata and controls
95 lines (82 loc) · 1.97 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
import javax.swing.JPanel;
import java.util.ArrayList;
import java.util.List;
import java.awt.Dimension;
import java.awt.GridLayout;
public class Game extends JPanel {
private int size;
private List<Cell> cells;
private Board board;
private Display display;
public Game(int sz, Display d) {
size = sz;
cells = new ArrayList<>();
board = new Board(size);
display = d;
for (int i = 0; i < size * size; i++) {
Cell cell = new Cell(size);
cells.add(cell);
add(cell);
}
setPreferredSize(new Dimension(2000 / size, 2000 / size));
setLayout(new GridLayout(size, size));
setVisible(true);
update();
}
public void aiMove() {
AI ai = new AI(size, new ArrayList<>(board.getBoard()));
int direction = ai.playerMove();
if (direction == 1) {
up();
} else if (direction == 2) {
down();
} else if (direction == 3) {
right();
} else if (direction == 4) {
left();
}
}
public void up() {
board.verticalCollapse(true, true);
update();
}
public void down() {
board.verticalCollapse(false, true);
update();
}
public void right() {
board.horizontalCollapse(false, true);
update();
}
public void left() {
board.horizontalCollapse(true, true);
update();
}
public void undo() {
board.undo();
update();
}
public void redo() {
board.redo();
update();
}
private void update() {
for (int i = 0; i < size * size; i++) {
String val = board.getStringBoard().get(i);
if (val.equals("0")) {
cells.get(i).update("");
} else {
cells.get(i).update(val);
}
}
if (board.gameover) {
gameover();
display.gameover();
}
}
private void gameover() {
for (int i = 0; i < size * size; i++) {
cells.get(i).gameover();
}
}
}