-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathgrid.ts
More file actions
160 lines (140 loc) · 3.42 KB
/
Copy pathgrid.ts
File metadata and controls
160 lines (140 loc) · 3.42 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
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
import { Tile } from './tile.js'
import { GameState, Position } from './types.js'
/**
* Grid
*
* Singleton class that manages the game grid.
*/
export class Grid {
/** Size */
static size: number = 4
/** Cells */
static cells: (Tile | null)[][]
constructor(size: number, previousState?: GameState['grid']['cells']) {
Grid.size = size
if (previousState) Grid.fromState(previousState)
else Grid.empty()
}
/**
* Empties the grid of tiles.
*/
static empty(): void {
Grid.cells = []
for (let x = 0; x < Grid.size; x++) {
Grid.cells[x] = []
for (let y = 0; y < Grid.size; y++) Grid.cells[x].push(null)
}
}
/**
* Sets the grid from the saved state.
*
* @param state State
*/
static fromState(state: GameState['grid']['cells']): void {
Grid.cells = []
for (let x = 0; x < Grid.size; x++) {
Grid.cells[x] = []
for (let y = 0; y < Grid.size; y++) {
const tile = state[x][y]
Grid.cells[x].push(tile ? new Tile(tile.position, tile.value) : null)
}
}
}
/**
* Finds the first available random position.
*
* @returns Random Available Position
*/
static randomAvailableCell(): Position | null {
const cells = Grid.availableCells()
return cells.length > 0
? cells[Math.floor(Math.random() * cells.length)]
: null
}
/**
* Gets the available cells as positions.
*
* @returns Available Cells as Positions
*/
static availableCells(): Position[] {
const cells: Position[] = []
Grid.eachCell(function (position: Position, tile: Tile | null) {
if (tile === null) cells.push(position)
})
return cells
}
/**
* Calls a callback function for every cell.
*
* @param callback Callback function
*/
static eachCell(callback: any): void {
for (let x = 0; x < Grid.size; x++)
for (let y = 0; y < Grid.size; y++) callback({ x, y }, Grid.cells[x][y])
}
/**
* Checks if there are any available positions.
*
* @returns Available Position Status
*/
static cellsAvailable(): boolean {
return Grid.availableCells().length > 0
}
/**
* Checks if the specified cell is available.
*
* @param cell Cell
* @returns Available Cell Status
*/
static cellAvailable(cell: Position): boolean {
return !Grid.cellOccupied(cell)
}
/**
* Checks if the specified cell is occupied.
*
* @param cell Cell
* @returns Occupied Cell Status
*/
static cellOccupied(cell: Position): boolean {
return !!Grid.cellContent(cell)
}
/**
* Gets the content of the specified cell.
*
* @param cell Cell
* @returns Cell Content
*/
static cellContent(cell: Position): Tile | null {
return Grid.withinBounds(cell) ? Grid.cells[cell.x][cell.y] : null
}
/**
* Inserts a tile at a specific position.
*
* @param tile Tile
*/
static insertTile(tile: Tile): void {
Grid.cells[tile.position.x][tile.position.y] = tile
}
/**
* Removes a tile from the grid.
*
* @param tile Tile
*/
static removeTile(tile: Tile): void {
Grid.cells[tile.position.x][tile.position.y] = null
}
/**
* Checks if the specified position is within the grid.
*
* @param position Position
* @returns Position Status
*/
static withinBounds(position: Position): boolean {
return (
position.x >= 0 &&
position.x < Grid.size &&
position.y >= 0 &&
position.y < Grid.size
)
}
}