forked from dntf0llow/openHalma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.java
179 lines (157 loc) · 5.84 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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import java.util.LinkedList;
import java.io.*;
public class Game implements Serializable
{
public final StarBoard board;
private LinkedList<Move> moves = new LinkedList<>();
private LinkedList<Move> cachedMoves = new LinkedList<>(); //additional list for 'undo'/'redo' functionality
private LinkedList<Player> players = new LinkedList<>();
private final Interface iface;
private Position hoverPosition = new Position(-1,-1);
private Position selectedPosition = new Position(-1,-1);
private LinkedList<Position> possibleJumps = new LinkedList<>();
private LinkedList<Player> winners = new LinkedList<>();
public Game(StarBoard board, LinkedList<Player> players) {
this.board = board;
this.players = players;
this.iface = new Graphical(this);
}
public void start() {
Logger.log(LOGGER_LEVEL.GAMEINFO, "Game started");
if (players.size() < 2 && players.get(0) instanceof LocalPlayer) {
//why not one player mode make as few moves as possible...?
Logger.log(LOGGER_LEVEL.GAMEINFO, "Only one player, have fun...");
Logger.log(LOGGER_LEVEL.GAMEINFO, "Do you know why there is an AI implemented in this software?");
}
Logger.log(LOGGER_LEVEL.TEMP_DEBUG, "got this far");
if (getNextPlayer() instanceof ComputerPlayer) {
requestMove();
}
}
public void finished() {
String s = "Game finished\n";
for (int i = 0; i < winners.size(); ++i) {
s += i + ". " + winners.get(i).toString();
}
Logger.log(LOGGER_LEVEL.GAMEINFO, s);
}
public void requestMove() {
Move move;
Player p = getNextPlayer();
do {
Logger.log(LOGGER_LEVEL.TEMP_DEBUG, "requesting from " + p.toString());
move = p.requestMove(board, new LinkedList<>(players), p); //wait til valid turn
} while ((!board.isValidMove(move)) && (getNextPlayer() == p));
tryMove(move);
}
public void tryMove(Move move) {
Player p = getNextPlayer();
if (board.isValidMove(move)) {
makeMove(move);
if (p.isFinished(board)) {
winners.push(p);
players.remove(p);
Logger.log(LOGGER_LEVEL.GAMEINFO, p.toString() + " finished the game as " + winners.size() + ".");
if (isFinished()) {
finished();
return;
}
}
Logger.log(LOGGER_LEVEL.GAMEINFO, "Next player: " + getNextPlayer().toString());
if (getNextPlayer() instanceof ComputerPlayer) {
requestMove();
} else {
}
}
}
private void makeMove(Move move) {
Logger.log(LOGGER_LEVEL.GAMEINFO, move.toString());
board.applyMoveUnchecked(move);
iface.repaint();
moves.add(move);
cachedMoves.clear();
players.addLast(players.removeFirst());
}
public boolean undoMove() {
if (undoable()) {
Move m = moves.getLast();
board.applyMoveUnchecked(new Move(m.player, m.end, m.start));
iface.repaint();
players.addFirst(players.removeLast());
cachedMoves.addLast(moves.removeLast());
return true;
} else {
return false;
}
}
public boolean redoMove() {
if (redoable()) {
board.applyMoveUnchecked(cachedMoves.getLast());
iface.repaint();
players.addLast(players.removeFirst());
moves.addLast(cachedMoves.removeLast());
return true;
} else {
return false;
}
}
public boolean undoable() {
return moves.peekFirst() != null;
}
public boolean redoable() {
return cachedMoves.peekFirst() != null;
}
public Player getNextPlayer() {
return players.peekFirst();
}
public boolean isFinished() {
for (Player p : players) {
if (!(winners.contains(p))) return false;
}
return true;
}
private boolean validHover() {
return (!hoverPosition.equals(Position.InvalidPosition));
}
public void hoverPosition(Position pos) {
hoverPosition = pos;
boolean selected = !selectedPosition.equals(Position.InvalidPosition);
if (!selected && (validHover())) {
possibleJumps = board.getJumpPositions(hoverPosition);
} else if (selected) {
possibleJumps = board.getJumpPositions(selectedPosition);
} else {
selectedPosition = Position.InvalidPosition;
possibleJumps.clear();
}
iface.repaint();
}
public void click() {
if (getNextPlayer() instanceof LocalPlayer) {
if (validHover()) {
if (hoverPosition.equals(selectedPosition)) {
selectedPosition = Position.InvalidPosition;
} else {
Move move = new Move(getNextPlayer(), selectedPosition, hoverPosition);
if (board.isValidMove(move)) {
tryMove(move);
selectedPosition = Position.InvalidPosition;
} else if ((board.getPosition(hoverPosition).getVal() >= 0) &&
(getNextPlayer().equals(board.getPosition(hoverPosition).getPlayer()))) {
selectedPosition = hoverPosition;
}
}
}
} else {
selectedPosition = Position.InvalidPosition;
}
Logger.log(LOGGER_LEVEL.DEBUG, "Clicked " + hoverPosition.toString());
hoverPosition(hoverPosition);
}
public Position getSelectedPosition() {
return selectedPosition;
}
public LinkedList<Position> getPossibleJumps() {
return possibleJumps;
}
}