-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.ts
367 lines (331 loc) · 9.64 KB
/
game.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
import { iterate } from 'iterare';
import { cloneDeep } from 'lodash-es';
import { GuardedMap } from '../app/map';
import {
addPoint,
arePointsEqual,
arraysUnorderedEqual,
assert,
decodePoint,
DeepReadonly,
encodePoint,
Point,
subtractPoint,
t,
} from '../app/types';
import { MessageId } from '../app/intl';
import { PlayerIndex } from './player';
export enum GameStatus {
Starting = 'starting',
Configuring = 'configuring',
Playing = 'playing',
Finished = 'finished',
}
export interface Turn {
/**
* Cell shots during turn in ascending order for each {@link PlayerIndex}.
*/
cells: [Point[], Point[]];
}
/**
* Turn history sorted in ascending order.
*/
export type TurnHistory = Turn[];
export interface PlayerTurn {
/**
* Cell shots during turn in ascending order.
*/
cells: Point[];
}
/**
* Turn history sorted in ascending order.
*/
export type PlayerTurnHistory = PlayerTurn[];
export function getPlayerTurns<H extends DeepReadonly<TurnHistory>>(
turnHistory: H,
index: PlayerIndex
) {
return iterate(turnHistory)
.map((t) => ({ cells: t.cells[index] }))
.toArray();
}
export enum BoardCellStatus {
Untouched = 'untouched',
Hit = 'hit',
/**
* Marks the cell near sunk ship.
* @type {BoardCellStatus.NoShip}
*/
NoShip = 'no-ship',
}
export interface HitBoardCell {
shipId: number;
status: BoardCellStatus.Hit;
}
export interface NotHitBoardCell {
shipId: null;
status: Exclude<BoardCellStatus, BoardCellStatus.Hit>;
}
export type BoardCell = HitBoardCell | NotHitBoardCell;
export function createBoardCell(): BoardCell {
return {
shipId: null,
status: BoardCellStatus.Untouched,
};
}
export function areBoardCellsEqual(cell1: DeepReadonly<BoardCell>, cell2: DeepReadonly<BoardCell>) {
return cell1.shipId === cell2.shipId && cell1.status === cell2.status;
}
export const defaultBoardSize: DeepReadonly<Point> = { x: 10, y: 10 };
/**
* Board is accessed as `board[x][y]`, X axis is →, Y axis is ↓.
*/
export type Board = BoardCell[][];
export function getBoardSize(board: DeepReadonly<Board>): Point {
return board.length > 0 ? { x: board.length, y: board[0].length } : { x: 0, y: 0 };
}
export function createBoard(size = defaultBoardSize): Board {
return Array(size.x)
.fill(null)
.map(() =>
Array(size.y)
.fill(null)
.map(() => createBoardCell())
);
}
export function areBoardsEqual(board1: DeepReadonly<Board>, board2: DeepReadonly<Board>) {
let boardsEqual = false;
for (let x = 0; x < board1.length; x += 1) {
boardsEqual ||= board1[x].every((cell, y) => areBoardCellsEqual(board1[x][y], board2[x][y]));
if (!boardsEqual) {
break;
}
}
return boardsEqual;
}
export enum Direction {
Right = 'right',
Bottom = 'bottom',
Left = 'left',
Top = 'top',
}
export const directionOrder: ReadonlyArray<Direction> = Object.values(Direction);
export const defaultDirectionIndex = 0;
export const defaultDirection = directionOrder[defaultDirectionIndex];
export function tryPushFromEdges(points: DeepReadonly<Point[]>, boardSize: DeepReadonly<Point>) {
const pushOffsets = new GuardedMap<Direction, number>(
iterate(directionOrder).map((d) => t(d, 0))
);
for (const point of points) {
if (point.x < 0) {
pushOffsets.set(Direction.Left, Math.max(pushOffsets.get(Direction.Left), -point.x));
} else if (point.x >= boardSize.x) {
pushOffsets.set(
Direction.Right,
Math.min(pushOffsets.get(Direction.Right), -point.x + boardSize.x - 1)
);
}
if (point.y < 0) {
pushOffsets.set(Direction.Top, Math.max(pushOffsets.get(Direction.Top), -point.y));
} else if (point.y >= boardSize.y) {
pushOffsets.set(
Direction.Bottom,
Math.min(pushOffsets.get(Direction.Bottom), -point.y + boardSize.y - 1)
);
}
}
const offsetPoint: Point = { x: 0, y: 0 };
for (const [direction, offset] of pushOffsets) {
switch (direction) {
case Direction.Left:
case Direction.Right: {
if (offset !== 0) {
assert(offsetPoint.x === 0, 'The points do not fit horizontally!');
offsetPoint.x = offset;
}
break;
}
case Direction.Top:
case Direction.Bottom: {
if (offset !== 0) {
assert(offsetPoint.y === 0, 'The points do not fit vertically!');
offsetPoint.y = offset;
}
break;
}
}
}
return applyOffset(offsetPoint, points, false);
}
/**
* https://youtu.be/o3uJCCa5w2A
* @param {DeepReadonly<Point[]>} points
* @param {Direction} from
* @param {Direction} to
* @returns {Point[]}
*/
export function rotatePoints(
points: DeepReadonly<Point[]>,
from: Direction,
to: Direction
): Point[] {
const newPoints = cloneDeep(points) as Point[];
const fromI = directionOrder.indexOf(from);
const toI = directionOrder.indexOf(to);
const steps = toI - fromI;
if (steps > 0) {
for (let i = 0; i < steps; i += 1) {
for (const p of newPoints) {
[p.x, p.y] = [-p.y, p.x];
}
}
} else if (steps < 0) {
for (let i = steps; i < 0; i += 1) {
for (const p of newPoints) {
[p.x, p.y] = [p.y, -p.x];
}
}
}
return newPoints;
}
export function getSurroundingCells(
points: DeepReadonly<Point[]>,
size: DeepReadonly<Point>
): Point[] {
const pointSet = iterate(points).map(encodePoint).toSet();
const surroundingSet = new Set<string>();
for (const point of points) {
for (
let x = Math.max(point.x - 1, 0), stopX = Math.min(point.x + 2, size.x);
x < stopX;
x += 1
) {
for (
let y = Math.max(point.y - 1, 0), stopY = Math.min(point.y + 2, size.y);
y < stopY;
y += 1
) {
const hashed = encodePoint({ x, y });
if (pointSet.has(hashed)) {
continue;
}
surroundingSet.add(hashed);
}
}
}
return iterate(surroundingSet).map(decodePoint).toArray();
}
export interface PointsBoundingRectangle {
topLeft: Point;
bottomRight: Point;
}
export function getBoundingRectangle(points: DeepReadonly<Point[]>): PointsBoundingRectangle {
assert(points.length > 0, 'No points to calculate bounding rectangle!');
const rectangle: PointsBoundingRectangle = {
topLeft: { x: Number.MAX_SAFE_INTEGER, y: Number.MAX_SAFE_INTEGER },
bottomRight: { x: Number.MIN_SAFE_INTEGER, y: Number.MIN_SAFE_INTEGER },
};
for (const p of points) {
rectangle.topLeft.x = Math.min(p.x, rectangle.topLeft.x);
rectangle.topLeft.y = Math.min(p.y, rectangle.topLeft.y);
rectangle.bottomRight.x = Math.max(p.x, rectangle.bottomRight.x);
rectangle.bottomRight.y = Math.max(p.y, rectangle.bottomRight.y);
}
return rectangle;
}
export function normalizeBoundingRectangle(
rectangle: DeepReadonly<PointsBoundingRectangle>
): Point {
return addPoint(subtractPoint(rectangle.bottomRight, rectangle.topLeft), { x: 1, y: 1 });
}
export interface ShipType {
shipTypeId: number;
name: MessageId;
/**
* Offsets of ship cells, except the first one (it's always `{ x: 0, y: 0 }`). One-cell chips will have it empty.
*
* For example, 2-cell straight ship in {@link Direction.Left} direction would have it as `[{ x: 1, y: 0 }]`.
*/
cellOffsets1: Point[];
/**
* Number of ships per player per game.
*/
shipCount: number;
}
export function areShipTypesEqual(type1: DeepReadonly<ShipType>, type2: DeepReadonly<ShipType>) {
const offsets1 = iterate(type1.cellOffsets1).map(encodePoint).toSet();
const offsets2 = iterate(type2.cellOffsets1).map(encodePoint).toSet();
return (
type1.shipTypeId === type2.shipTypeId &&
type1.name === type2.name &&
iterate(offsets1).every((p) => offsets2.has(p)) &&
type1.shipCount === type2.shipCount
);
}
export function applyOffset(
offset: DeepReadonly<Point>,
points: DeepReadonly<Point[]>,
includeInitial = true
): Point[] {
const newPoints = includeInitial ? [cloneDeep(offset) as Point] : [];
for (const point of points) {
newPoints.push(addPoint(offset, point));
}
return newPoints;
}
export function isOutOfBound(point: DeepReadonly<Point>, size: DeepReadonly<Point>): boolean {
return point.x < 0 || point.x >= size.x || point.y < 0 || point.y >= size.y;
}
export enum ShipStatus {
Afloat = 'afloat',
Sunk = 'sunk',
}
export interface Ship {
shipId: number;
// playerIndex: PlayerIndex;
status: ShipStatus;
shipTypeId: number;
direction: Direction;
/**
* All ship cells absolute coordinates in the board. Its size is larger than {@link ShipType.cellOffsets1} by 1.
*
* For example, 2-cell straight ship at `{ x: 3, y: 4 }` with {@link Direction.Left} direction would have it as `[{ x: 3, y: 4 }, { x: 4, y: 4 }]`.
*/
shipCells: Point[];
}
export function createShips(shipTypes: DeepReadonly<ShipType[]>): Ship[] {
const ships = [];
let id = 0;
for (const type of shipTypes) {
for (let i = 0; i < type.shipCount; i += 1) {
ships.push(createShip(type, defaultDirection, id));
id += 1;
}
}
return ships;
}
export function createShip(
shipType: DeepReadonly<ShipType>,
direction = defaultDirection,
id = 0
): Ship {
return {
shipId: id,
status: ShipStatus.Afloat,
shipTypeId: shipType.shipTypeId,
direction,
shipCells: [],
};
}
export function areShipsEqual(ship1: DeepReadonly<Ship>, ship2: DeepReadonly<Ship>) {
return (
ship1.shipId === ship2.shipId &&
ship1.status === ship2.status &&
ship1.shipTypeId === ship2.shipTypeId &&
ship1.direction === ship2.direction &&
arraysUnorderedEqual(ship1.shipCells, ship2.shipCells, arePointsEqual)
);
}
export function cloneShip(ship: DeepReadonly<Ship>): Ship {
return { ...ship, shipCells: ship.shipCells.slice() };
}