forked from gabrielecirulli/2048
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2048-solver.js
159 lines (127 loc) · 3.2 KB
/
2048-solver.js
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
/**
* Settings
*/
const initOnLoad = true;
const maxInitAttempts = 100;
const delayBetweenMoves = 150; // ms
/**
* Variables and constants
*/
let initAttempts = 0;
const keyMappings = {
ArrowLeft: 37,
ArrowUp: 38,
ArrowRight: 39,
ArrowDown: 40
};
/**
* Functions
*/
const init = () => {
const board = readBoard();
if (!board) {
initAttempts++;
if (initAttempts < maxInitAttempts) {
setTimeout(init, 0);
} else {
console.log("Could not initialize");
}
} else {
play(board);
}
};
const play = board => {
const nextMove = getNextMove(board);
if (!nextMove) {
console.log("Game over!");
} else {
simulate(keyMappings[nextMove]);
setTimeout(() => play(readBoard()), delayBetweenMoves);
}
};
const readBoard = () => {
const tiles = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]];
const tileNodes = Array.from(document.querySelectorAll(".tile"));
if (tileNodes.length === 0) {
return null;
}
tileNodes.forEach(node => {
const position = Array.from(node.classList)
.find(className => className.indexOf("tile-position-") === 0)
.replace("tile-position-", "")
.split("-")
.map(str => parseInt(str, 10) - 1);
tiles[position[1]][position[0]] = parseInt(node.textContent, 10);
});
return tiles;
};
const getNextMove = board => {
const possibleMoves = getPossibleMoves(board);
return possibleMoves[Math.floor(Math.random() * possibleMoves.length)];
};
const findLastIndex = (arr, callback) => {
const reverseIndex = arr
.slice()
.reverse()
.findIndex(callback);
if (reverseIndex === -1) {
return -1;
}
return (arr.length - reverseIndex - 1) % arr.length;
};
const getColumns = board => {
const columns = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]];
board.forEach((row, rowIndex) =>
row.forEach((num, colIndex) => {
columns[colIndex][rowIndex] = num;
})
);
return columns;
};
const isArrowLeftPossible = board =>
board.some(row => {
const firstEmptyTileIndex = row.findIndex(num => num === 0);
const lastNonEmptyTileIndex = findLastIndex(row, num => num > 0);
return (
firstEmptyTileIndex > -1 && firstEmptyTileIndex < lastNonEmptyTileIndex
);
});
const isArrowRightPossible = board =>
board.some(row => {
const firstNonEmptyTileIndex = row.findIndex(num => num > 0);
const lastEmptyTileIndex = findLastIndex(row, num => num === 0);
return (
firstNonEmptyTileIndex > -1 && firstNonEmptyTileIndex < lastEmptyTileIndex
);
});
const getPossibleMoves = board => {
const possibleMoves = [];
if (isArrowLeftPossible(board)) {
possibleMoves.push("ArrowLeft");
}
if (isArrowRightPossible(board)) {
possibleMoves.push("ArrowRight");
}
const columns = getColumns(board);
if (isArrowLeftPossible(columns)) {
possibleMoves.push("ArrowUp");
}
if (isArrowRightPossible(columns)) {
possibleMoves.push("ArrowDown");
}
return possibleMoves;
};
const simulate = keyCode => {
document.body.dispatchEvent(
new KeyboardEvent("keydown", {
bubbles: true,
keyCode
})
);
};
/**
* Initialize
*/
if (initOnLoad) {
document.addEventListener("DOMContentLoaded", init);
}