This repository was archived by the owner on Sep 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHiveGameRules.ts
372 lines (317 loc) · 14.3 KB
/
HiveGameRules.ts
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
import { Board, Coordinates, Field, GameState, Move, PIECETYPE, PLAYERCOLOR, SHIFT } from './Hive'
export class GameRuleLogic {
static getDirections(c: Coordinates): Coordinates[] {
return [
new Coordinates(c.q + 1, c.r - 1, c.s + 0),
new Coordinates(c.q + 1, c.r + 0, c.s - 1),
new Coordinates(c.q + 0, c.r + 1, c.s - 1),
new Coordinates(c.q - 1, c.r + 1, c.s + 0),
new Coordinates(c.q - 1, c.r + 0, c.s + 1),
new Coordinates(c.q + 0, c.r - 1, c.s + 1),
]
}
static getNeighbours(board: Board, field: Coordinates): Field[] {
return this.getDirections(field).filter(c => board.getField(c) != null).map(c => board.getField(c))
}
static isNeighbour(a: Coordinates, b: Coordinates): boolean {
return this.getDirections(a).some(t => b.equal(t))
}
static getQueen(board: Board, color: PLAYERCOLOR): Field {
let queen = this.findPiecesOfTypeAndPlayer(board, 'BEE', color)
if (queen.length == 0) {
return null
}
return queen[0]
}
static isQueenBlocked(board: Board, color: PLAYERCOLOR): boolean {
return !this.getNeighbours(board, this.getQueen(board, color).coordinates).some(field => field.stack == null)
}
static findPiecesOfTypeAndPlayer(board: Board, type: PIECETYPE, color: PLAYERCOLOR): Field[] {
return this.getFieldsWithPiece(board).filter(e => e.stack.some(p => p.kind == type && p.color == color))
}
static fieldsOwnedByPlayer(board: Board, color: PLAYERCOLOR): Field[] {
return this.getFieldsWithPiece(board).filter(e => e.owner() == color)
}
static getFieldsWithPiece(board: Board): Field[] {
let fields = []
board.fields.forEach((row) => {
row.forEach((field) => {
if (field != null && field.stack.length > 0) {
fields.push(field)
}
})
})
return fields
}
/** Validates whether nor not the path to the neighbour via adjacent tiles is obstructed or not
*
* @param board
* @param a
* @param b
*/
static isPathToNeighbourObstructed(board: Board, a: Coordinates, b: Coordinates): boolean {
if (!this.isNeighbour(a, b)) {
console.log('Feld a ist kein Nachbar von b', a, b)
return true
}
let shared = this.sharedNeighboursOfTwoCoords(board, a, b)
// 2 benachbarte Felder müssen mindestens 1 und höchstens 2 weiteren gemeinsamen Nachbarn haben
if (shared.length > 2 || shared.length < 1) {
console.log('Unerwartete Anzahl an gemeinsamen Nachbarfeldern von a, b, shared', a, b, shared)
return true
}
// verhindere dass er sich nicht am rand des schwarms bewegt und beispielsweise "jumpt"
return !((shared.some(e => e.stack.length == 0 && !e.obstructed) || shared.length == 1) && shared.some(e => e.stack.length > 0))
}
/** Validates whether nor not the path to the neighbour via adjacent tiles is obstructed or not
*
* @param board
* @param a
* @param b
* @param except
*/
static isPathToNeighbourObstructedExcept(board: Board, a: Coordinates, b: Coordinates, except: Coordinates): boolean {
if (!this.isNeighbour(a, b)) {
console.log('Feld a ist kein Nachbar von b', a, b)
return true
}
let shared = this.sharedNeighboursOfTwoCoords(board, a, b)
// 2 benachbarte Felder müssen mindestens 1 und höchstens 2 weiteren gemeinsamen Nachbarn haben
if (shared.length > 2 || shared.length < 1) {
console.log('Unerwartete Anzahl an gemeinsamen Nachbarfeldern von a, b, shared', a, b, shared)
return true
}
// verhindere dass er sich nicht am rand des schwarms bewegt und beispielsweise "jumpt"
// Falls der einzige Schwarmnachbar except ist
if (shared.some(e => except.equal(e.coordinates)) && shared.filter(e => e.stack.length > 0).length == 1) {
return true
}
// Falls es keinen Schwarmnachbar gibt
if (!shared.some(e => e.stack.length > 0)) {
return true
}
// ist platz zum laufen?
return !((shared.some(e => e.stack.length == 0 && !e.obstructed || except.equal(e.coordinates)) || shared.length == 1))
}
static sharedNeighboursOfTwoCoords(board: Board, a: Coordinates, b: Coordinates): Field[] {
let nb = this.getNeighbours(board, b)
return this.getNeighbours(board, a).filter(tile => nb.some(e => tile.coordinates.equal(e.coordinates)))
}
/** Determines whether or not given coordinate is adjacent to the swarm
* if except is != null, the field of except will not be counted as part of the swarm
*
* @param board
* @param field
* @param except
*/
static isFieldNextToSwarm(board: Board, field: Coordinates, except: Coordinates): boolean {
return this.getFieldsNextToSwarm(board, except).some(f => field.equal(f.coordinates))
}
/** returns all fields adjacent to the swarm.
* If except is != null the field of except is not counted as the swarm and fields only adjacent to this perticular field wont be listed within the return value
*
* @param board
* @param except
*/
static getFieldsNextToSwarm(board: Board, except: Coordinates): Field[] {
let tiles: Field[] = []
for (let field of this.getFieldsWithPiece(board).filter(e => except == null || !except.equal(e.coordinates))) {
tiles = tiles.concat(this.getNeighbours(board, field.coordinates)
.filter(e => e.stack.length == 0 && !e.obstructed && (except == null || !except.equal(e.coordinates)) && !tiles.some(f => e.coordinates.equal(f.coordinates))))
}
return tiles
}
static isOnBoard(c: Coordinates): boolean {
return (Math.abs(c.q) <= SHIFT && Math.abs(c.r) <= SHIFT && Math.abs(c.s) <= SHIFT)
}
static getLineBetweenCoords(board: Board, a: Coordinates, b: Coordinates): Field[] {
if (!a.isInLineWith(b)) {
console.log('Feld a ', a, ' ist nicht mit Feld b ', b, ' in einer Reihe, kann daher nicht die Coordinaten dazwischen bekommen')
return []
}
// get diff between 2 coords
let d_q = a.q - b.q
let d_r = a.r - b.r
let d_s = a.s - b.s
let d = d_q == 0 ? Math.abs(d_r) : Math.abs(d_q)
let tmp: Coordinates[] = []
for (let i = 1; i < d; i++) {
tmp.push(new Coordinates(b.q + (d_q > 0 ? 1 : d_q < 0 ? -1 : 0) * i, b.r + (d_r > 0 ? 1 : d_r < 0 ? -1 : 0) * i, b.s + (d_s > 0 ? 1 : d_s < 0 ? -1 : 0) * i))
}
return tmp.map(e => board.getField(e))
}
static isSwarmConnected(board: Board): boolean {
if (this.getFieldsWithPiece(board).length == 0) {
return true
}
let visitedFields = [this.getFieldsWithPiece(board)[0]]
let totalPieces = board.countPieces()
let index = 0
do {
visitedFields = visitedFields.concat(this.getNeighbours(board, visitedFields[index].coordinates)
.filter(e => e.stack.length > 0 && !visitedFields.some(f => f.coordinates.equal(e.coordinates))))
if (visitedFields.reduce((prev, e) => prev + e.stack.length, 0) == totalPieces) {
return true
}
} while (++index < visitedFields.length)
return false
}
static validateMove(board: Board, from: Coordinates = null, to: Coordinates = null, state: GameState = null, move: Move = null): boolean {
if (move != null) {
if (state == null) {
console.log('%cValidate SET-Move with insufficient parameters, state required!', 'color: #f00')
return false
}
from = move.fromField
to = move.toField
board = state.board
if (move.moveType == 'SET') {
// console.log('%cValidiere SET-move: ', 'color: #f00', move.undeployedPiece, ' to: ', to)
switch (state.board.countPieces()) {
case 0:
return this.isOnBoard(to)
case 1:
return this.getFieldsNextToSwarm(state.board, null).some(e => e.coordinates.equal(to))
default:
return this.getNeighbours(state.board, to)
.some(e => e.owner() == state.currentPlayerColor) && !this.getNeighbours(state.board, to)
.some(e => e.owner() == state.getOtherPlayer().color)
}
}
} else if (from == null || to == null) {
console.log('%cValidate Move with insufficient parameters', 'color: #f00')
return false
}
// console.log('%cValidiere DRAG-move from: ', 'color: #f00', from, ' to: ', to)
if (!this.isOnBoard(from) || !this.isOnBoard(to)) {
console.log('Korrumpierte Koordinaten gegeben (out of board): ', from, to)
return false
}
if (board.getField(from).stack.length < 1) {
console.log('Keine zu ziehende Figur auf dem Feld gefunden: ', from)
return false
} else if (board.getField(from).stack.length > 1 && board.getTopPiece(from).kind != 'BEETLE') {
console.log('Beim Stack mit mehr als 1 piece auf dem Feld: ', board.getField(from), ' ist das oberste Piece kein BEETLE!!')
return false
}
// Beetle darf drauf
if (board.getTopPiece(from).kind == 'BEETLE') {
if (!this.getFieldsWithPiece(board).some(e => e.coordinates.equal(to)) && (board.getField(from).stack.length == 1 && !this.isFieldNextToSwarm(board, to, from) || board.getField(from).stack.length > 1 && !this.isFieldNextToSwarm(board, to, null))) {
console.log('Das Ziel des Beetles ist weder auf einem anderen Insekt, noch neben dem Schwarm')
return false
}
} else if (!this.isFieldNextToSwarm(board, to, from)) {
console.log('Das Feld ist nicht neben dem Schwarm: ', to)
return false
}
let clone = board.clone()
clone.getField(from).stack.pop()
if (!this.isSwarmConnected(clone)) {
console.log('Das Feld: ', from, ' ist nicht als 1 Schwarm verbunden')
return false
}
switch (board.getTopPiece(from).kind) {
case 'ANT':
// console.log('Es ist eine Ameise')
return this.validateAntMove(board, from, to)
case 'BEE':
// console.log('Es ist eine Biene')
return this.validateBeeMove(board, from, to)
case 'BEETLE':
// console.log('Es ist ein Käfer')
return this.validateBeetleMove(board, from, to)
case 'GRASSHOPPER':
// console.log('Es ist ein Grashüpfer')
return this.validateGrasshopperMove(board, from, to)
case 'SPIDER':
// console.log('Es ist eine Spider')
return this.validateSpiderMove(board, from, to)
default:
console.log('%cDa ist aber wirklich ordentlich was schief gegangen.... unbekannter typ: ', 'color: #f00')
console.log(board.getTopPiece(from).kind)
return false
}
}
static validateAntMove(board: Board, from: Coordinates, to: Coordinates): boolean {
// console.log('Ant move validation')
let swarm = this.getFieldsNextToSwarm(board, from)
let visitedFields = [board.getField(from)]
let index = 0
do {
visitedFields = visitedFields.concat(this.getNeighbours(board, visitedFields[index].coordinates)
.filter(e => !visitedFields.some(f => f.coordinates.equal(e.coordinates))
&& swarm.some(f => e.coordinates.equal(f.coordinates))
&& !this.isPathToNeighbourObstructedExcept(board, visitedFields[index].coordinates, e.coordinates, from)))
if (visitedFields.some(e => e.coordinates.equal(to))) {
return true
}
} while (++index < visitedFields.length)
return false
}
static validateBeeMove(board: Board, from: Coordinates, to: Coordinates): boolean {
return this.isNeighbour(from, to) && !this.isPathToNeighbourObstructed(board, from, to)
}
static validateBeetleMove(board: Board, from: Coordinates, to: Coordinates): boolean {
return this.isNeighbour(from, to) && (this.sharedNeighboursOfTwoCoords(board, from, to)
.some(e => e.stack.length > 0) || board.getField(to).stack.length > 0 || board.getField(from).stack.length > 1)
}
static validateGrasshopperMove(board: Board, from: Coordinates, to: Coordinates): boolean {
return from.isInLineWith(to) && !this.getLineBetweenCoords(board, from, to)
.some(e => e.stack.length == 0) && !this.isNeighbour(from, to)
}
static validateSpiderMove(board: Board, from: Coordinates, to: Coordinates): boolean {
// aber jetzt mal so richtig inperformant... :D
let swarm = this.getFieldsNextToSwarm(board, from)
// 1. Schritt
for (let depth1 of this.getNeighbours(board, from)
.filter(e => !e.obstructed && e.stack.length == 0)
.map(e => e.coordinates)
.filter(e => swarm.some(s => e.equal(s.coordinates)) && !this.isPathToNeighbourObstructedExcept(board, from, e, from))) {
// 2. Schritt
for (let depth2 of this.getNeighbours(board, depth1)
.filter(e => !e.obstructed && e.stack.length == 0)
.map(e => e.coordinates)
.filter(e => !e.equal(from) && swarm.some(s => e.equal(s.coordinates)) && !this.isPathToNeighbourObstructedExcept(board, depth1, e, from))) {
// Ist 3. Schritt = Ziel
if (this.getNeighbours(board, depth2)
.filter(e => !e.obstructed && e.stack.length == 0)
.map(e => e.coordinates)
.filter(e => !e.equal(from) && !e.equal(depth1) && swarm.some(s => e.equal(s.coordinates)) && !this.isPathToNeighbourObstructedExcept(board, depth2, e, from))
.some(e => e.equal(to))) {
return true
}
}
}
return false
}
static possibleMoves(state: GameState, field: Coordinates): Coordinates[] {
const topPiece = state.board.getTopPiece(field)
if (topPiece == null) {
console.warn('Das Startfeld ist leer: ', field, state.board.toString())
return null
}
if (this.getQueen(state.board, topPiece.color) == null) {
console.warn(topPiece.color, 'hat seine Biene noch nicht gesetzt')
return []
}
const clone = state.board.clone()
clone.getField(field).stack.pop()
if (!this.isSwarmConnected(clone)) {
console.warn('Durch einen Zug von', field, 'wäre der Schwarm nicht mehr verbunden')
return []
}
const kind = state.board.getTopPiece(field).kind
if (kind == 'BEETLE' || kind == 'BEE') {
return this.getNeighbours(state.board, field).map(e => e.coordinates).filter(e => this.validateMove(state.board, field, e))
}
const allFields = this.getFieldsNextToSwarm(state.board, field).map(e => e.coordinates)
if (kind == 'SPIDER') {
return allFields.filter(e => field.distanceTo(e) <= 3 && this.validateMove(state.board, field, e))
}
if (kind == 'GRASSHOPPER') {
return allFields.filter(e => field.isInLineWith(e) && this.validateMove(state.board, field, e))
}
// fürs erste brute-force durch
return allFields.filter(e => this.validateMove(state.board, field, e))
}
}