-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
330 lines (291 loc) · 9.49 KB
/
script.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
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
// Left shift 2 2 2 0, 2 0 0 2, 0 0 2 2
// Left move (merge) 2 2 2 2, 2 0 2 2, 2 2 0 2, 0 0 2 2
// same right
// Let's first create the board. We have a grid container and we need to render 16 small block in that grid
// with an initial value of 0 and give a function name
// createBoard
function createBoard(){
let grid = document.querySelector('.grid')
for (let i = 0; i < 16; i++){
let div = document.createElement('div')
div.setAttribute("id",`id_${i}`)
div.textContent = 0
grid.appendChild(div)
}
}
createBoard()
// Now in all zeros, let's randomly pop-up one number at a random zero location with a 2 or 4 with
// p(2) = 0.9 and p(4) = 0.1
// generate
//
function generate(){
let optionArray = [2, 2, 2, 2, 2, 2, 2, 2, 2, 4]
let allBlocks = document.querySelectorAll('.grid > div') // NodeList, can't directly apply filter
// convert to a valid array which has filter available, ways to do it
// [...allBlocks]
// Array.from(allBlocks)
let emptyBlocks = Array.from(allBlocks).filter((a) => a.textContent == 0)
// What if there are not empty blocks, change this code to generate only when empty blocks
if (emptyBlocks.length == 0){
return
}
let randomNumber = optionArray[Math.floor((Math.random() * 10))] // a value between 0 and 9
let randomBlock = emptyBlocks[Math.floor((Math.random() * emptyBlocks.length))] // select a random empty block
randomBlock.textContent = randomNumber
}
// alert(Math.random()) gives a number between 0 and 1
// generate()
// generate()
// generate()
// generate()
let totalScore = 0
// Write a function to move all non-zero elements to the left without merging, just shift to left
// shiftLeft
function shiftArrayLeft(valueArray){
let finalArray = valueArray.filter((a) => a != 0)
let index = finalArray.length
while (index < 4){
finalArray.push(0)
index++
}
return finalArray
}
// console.log(shiftArrayLeft([0, 2, 4, 0]))
// console.log(shiftArrayLeft([0, 0, 0, 4]))
// combine method
function combineArrayLeft(valueArray){
let index = 1
while (index < 4){
if (valueArray[index] == valueArray[index - 1]){
valueArray[index - 1] = valueArray[index] * 2
totalScore += (valueArray[index] * 2)
document.querySelector("#score").textContent = totalScore
valueArray[index] = 0
}
index++
}
return valueArray
}
// console.log(combineArrayLeft([2,2,2,2]))
// direct merge method -- homework
// function mergeArrayLeft(valueArray){
// }
// Write a function to move all non-zero elements to the right without merging, just shift to left
// shiftLeft
function shiftArrayRight(valueArray){
let finalArray = valueArray.filter((a) => a != 0)
let index = finalArray.length
while (index < 4){
finalArray.unshift(0)
index++
}
return finalArray
}
// console.log(shiftArrayRight([0, 2, 4, 0]))
// console.log(shiftArrayRight([0, 0, 0, 4]))
// combine method
function combineArrayRight(valueArray){
let index = valueArray.length - 1
while (index > 0){
if (valueArray[index] == valueArray[index - 1]){
totalScore += (valueArray[index] * 2)
document.querySelector("#score").textContent = totalScore
valueArray[index] = valueArray[index] * 2
valueArray[index - 1] = 0
}
index--
}
return valueArray
}
// console.log(combineArrayRight([2,2,2,2]))
function getRowValues(rowNumber){
let rowValues = []
for (let i = 4 * (rowNumber - 1); i < 4 * rowNumber; i++){
rowValues.push(Number(document.querySelector(`#id_${i}`).textContent))
}
return rowValues
}
// Shift a given row's elements to given direction Left or Right
function moveRow(rowNumber, direction){
let rowValues = getRowValues(rowNumber)
if (direction == 'L'){
// Method 1
rowValues = shiftArrayLeft(rowValues)
rowValues = combineArrayLeft(rowValues)
rowValues = shiftArrayLeft(rowValues)
// Method 2
// Do it as homework
}
if (direction == 'R'){
rowValues = shiftArrayRight(rowValues)
rowValues = combineArrayRight(rowValues)
rowValues = shiftArrayRight(rowValues)
}
for (let i = 4 * (rowNumber - 1); i < 4 * rowNumber; i++){
document.querySelector(`#id_${i}`).textContent = rowValues[i % 4]
}
}
// shiftRow(1, 'L')
// shiftRow(2, 'L')
// shiftRow(3, 'L')
// shiftRow(4, 'L')
// Shift all rows to left
function moveLeft(){
for (let i of [1, 2, 3, 4]){
moveRow(i, 'L')
}
}
// Shift all rows to right
function moveRight(){
for (let i of [1, 2, 3, 4]){
moveRow(i, 'R')
}
}
// Shift a given column's elements to given direction Left or Right
// a column can be thought of as a row transpose, so same left right function will work
// for a column as well to move them up and down
function getColumnValues(colNumber){
let colValues = []
for (let i = colNumber - 1; i < 16; i+= 4){
colValues.push(Number(document.querySelector(`#id_${i}`).textContent))
}
return colValues
}
function moveColumn(colNumber, direction){
let colValues = getColumnValues(colNumber)
if (direction == 'U'){
colValues = shiftArrayLeft(colValues)
colValues = combineArrayLeft(colValues)
colValues = shiftArrayLeft(colValues)
}
if (direction == 'D'){
colValues = shiftArrayRight(colValues)
colValues = combineArrayRight(colValues)
colValues = shiftArrayRight(colValues)
}
let counter = 0
for (let i = colNumber - 1; i < 16; i+= 4){
document.querySelector(`#id_${i}`).textContent = colValues[counter]
counter++
}
}
// Shift all columns to up
function moveUp(){
for (let i of [1, 2, 3, 4]){
moveColumn(i, 'U')
}
}
// Shift all rows to right
function moveDown(){
for (let i of [1, 2, 3, 4]){
moveColumn(i, 'D')
}
}
// shiftLeft()
// shiftRight()
// shiftUp()
// shiftDown()
// Now this will work for us till question 6 without any changes at all.
// Now let's make some wholesale changes to our code to implement the later portions
// of the questions
// Now we need to write logic to mergeLeft and mergeRight and also change the names
// shift functions to move functions. Move will also combine merge
// Now there could be two ways to move
// 1. shift combine shift -- the already made function can be used
// 2. Direct merge -- preferred and better way
// moveLeft()
// moveUp()
// moveRight()
// moveDown()
// Add event listener to move these things with arrow keys
// we gather an event and from the event keyCode, we decide what key was pressed and
// we take action accordingly
// 37 Left arrow
// 38 Up arrow
// 39 Right arrow
// 40 Down arrow
function keyupHandler(event){
switch(event.keyCode){
case 37:
moveLeft()
break;
case 38:
moveUp()
break;
case 39:
moveRight()
break;
case 40:
moveDown()
break;
}
generate()
if (checkForWin()){
document.body.removeEventListener('keyup', keyupHandler);
document.querySelector('#result').textContent = "YOU WIN!"
}
else if (isGameOver()){
document.body.removeEventListener('keyup', keyupHandler);
document.querySelector('#result').innerText = "Game Over"
}
}
document.body.addEventListener('keyup', keyupHandler);
// Till this Q no. 7 + our main logic for the questions is done
// Let's add logic for
// 1. Score Update --> changes in code above
// 2. Game Over -- when no more possible moves
// 3. Game Win -- when any one of the blocks is equal to 2048
// 4. Game Re-start -- reset the game Board
function checkAdjacent(values){
let index = values.length - 1
while (index > 0) {
if (values[index] === values[index - 1]) {
return true
}
index--;
}
return false
}
function isGameOver(){
let allBlocks = document.querySelectorAll('.grid > div')
let emptyBlocks = Array.from(allBlocks).filter((a) => a.textContent == 0)
if (emptyBlocks.length != 0){
return false
}
for (let rowNumber of [1, 2, 3, 4]){
let rowValues = []
for (let i = 4 * (rowNumber - 1); i < 4 * rowNumber; i++){
rowValues.push(Number(document.querySelector(`#id_${i}`).textContent))
if (rowValues.length > 1 && (rowValues[i % 4] == rowValues[i % 4 - 1])){
return false
}
}
}
for (let colNumber of [1, 2, 3, 4]){
let colValues = []
let count = 0
for (let i = (colNumber - 1); i < 16; i+=4){
colValues.push(Number(document.querySelector(`#id_${i}`).textContent))
if (colValues.length > 1 && (colValues[count] == colValues[count - 1])){
return false
}
count++
}
}
return true
}
function checkForWin(){
let allBlocks = document.querySelectorAll('.grid > div')
let winningBlocks = Array.from(allBlocks).filter((a) => a.textContent == 2048)
return winningBlocks.length != 0
}
document.querySelector('#restart-button').addEventListener('click', () => {
document.querySelector('.grid').innerHTML = ''
totalScore = 0
document.querySelector("#score").textContent = totalScore
document.querySelector('#result').innerText = "Join the numbers and get to the 2048 tile!"
createBoard()
generate()
generate()
document.body.addEventListener('keyup', keyupHandler);
})