-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
191 lines (163 loc) · 4.25 KB
/
index.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
type FieldIndex = {
row: number,
cell: number,
};
const GAME_SIZE = 4;
class Game {
public field: number[][] = [];
public testMode: boolean = false;
private _score: number = 0;
public get score() {
return this._score;
}
private set score(newValue: number) {
this._score = newValue;
}
constructor () {
this.newGame();
}
newGame(): void {
this.score = 0;
this.field = [...Array(GAME_SIZE)].map(() =>
[...Array(GAME_SIZE)].map(() => 0));
this.createCell();
}
/**
* create random value in random empty field
* @returns
*/
private createCell(): void {
if (this.testMode) {
return;
}
const randomValue = Math.random() < 0.9 ? 2 : 4;
const index = this.getRandomIndex();
this.field[index.row][index.cell] = randomValue;
}
/**
* Get index of random empty cell
* @returns {FieldIndex}
*/
private getRandomIndex(): FieldIndex {
const emptyIndeces = this.field
.flat()
.map((a, i) => a === 0 ? i : -1)
.filter(a => a > -1);
const random = emptyIndeces[Math.floor(Math.random() * emptyIndeces.length)];
const index = {
row: Math.floor(random / GAME_SIZE),
cell: random % GAME_SIZE
};
return index;
}
testGetRandomIndex(): FieldIndex {
return this.getRandomIndex();
}
/**
* Move all values to left of field
* @returns {boolean} True if is was a valid move
*/
onSwipeLeft(): boolean {
let validMove = false;
this.field.forEach((row, i) => {
let previousIndex = -1;
row.forEach((cell, j) => {
if (cell !== 0 && j > 0) {
let newIndex = this.findFurthestCell(row, j, previousIndex);
if (typeof newIndex === 'undefined') {
newIndex = j - 1;
}
// move value to the next empty cell
if (row[newIndex] === 0) {
row[newIndex] = cell;
row[j] = 0;
validMove = true;
// move value to the next cell, that has same value
} else if(row[newIndex] === cell && newIndex > previousIndex) {
previousIndex = newIndex;
row[newIndex] += cell;
row[j] = 0;
this.score += row[newIndex];
validMove = true;
}
}
});
});
if (validMove) {
this.createCell();
}
return validMove;
}
/**
* Move all values to right of field
*/
onSwipeRight(): void {
this.field.forEach(row => {
row.reverse();
});
this.onSwipeLeft();
this.field.forEach(row => {
row.reverse();
});
}
/**
* Move all values to top of field
*/
onSwipeTop(): void {
this.field = this.rotateArrayCW(this.field);
this.onSwipeLeft();
this.field = this.rotateArrayCW(this.field);
}
/**
* Move all values to bottom of field
*/
onSwipeBottom(): void {
this.field = this.rotateArrayCW(this.field);
this.onSwipeRight();
this.field = this.rotateArrayCW(this.field);
}
/**
* Rotate a 2D array clock wise
* @see https://stackoverflow.com/questions/17428587/transposing-a-2d-array-in-javascript
* @param array
* @returns
*/
rotateArrayCW(array: number[][]): number[][] {
return array[0].map((_, colIndex) => array.map(row => [...row][colIndex]));
}
/**
* Rotate a 2D array counter clock wise
* @see https://stackoverflow.com/questions/17428587/transposing-a-2d-array-in-javascript
* @param array
* @returns
*/
rotateArrayCCW(array: number[][]): number[][] {
// const arr: number[][] = array.map(row =>
// [...row].reverse()
// );
return array[0].map((_, colIndex) => array.map(row => [...row].reverse()[colIndex]));
}
/**
* Find cell with same number as far to the left as possible
* @param row
* @param cellIndex
* @returns
*/
findFurthestCell(row: number[], cellIndex: number, previousIndex: number = -1): number | undefined {
let emptyCellIndex;
for(let i = cellIndex - 1; i > previousIndex; i--) {
const cell = row[i];
if (cell === row[cellIndex]) {
return i;
}
if (cell !== row[cellIndex] && cell !== 0) {
return emptyCellIndex;
}
if (cell === 0) {
emptyCellIndex = i;
}
}
return emptyCellIndex;
}
}
export default new Game();