Last active
August 12, 2020 22:23
-
-
Save rahuldamodar94/1d5d6356cf3356ea5af551042e93a073 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var prompt = require("prompt"); | |
var array = [ | |
[0, 0, 0, 0, 0], | |
[0, 16, 0, 0, 0], | |
[0, 4, 0, 0, 0], | |
[16, 32, 0, 0, 0], | |
[8, 16, 0, 0, 0], | |
[4, 8, 4, 0, 0], | |
]; | |
function insert(i, j, value) { | |
array[i][j] = value; | |
} | |
function display() { | |
console.log(array); | |
} | |
function checkPossibilities(i, j) { | |
let left = false; | |
let right = false; | |
let bottom = false; | |
if (j > 0) { | |
if (array[i][j] === array[i][j - 1]) { | |
left = true; | |
} | |
} | |
if (j < 4) { | |
if (array[i][j] === array[i][j + 1]) { | |
right = true; | |
} | |
} | |
if (i < 5) { | |
if (array[i][j] === array[i + 1][j]) { | |
bottom = true; | |
} | |
} | |
return { left, right, bottom }; | |
} | |
function mergeLeft(i, j) { | |
array[i][j] = array[i][j] * 2; | |
array[i][j - 1] = 0; | |
} | |
function mergeRight(i, j) { | |
array[i][j] = array[i][j] * 2; | |
array[i][j + 1] = 0; | |
} | |
function mergeBottom(i, j) { | |
array[i][j] = array[i][j] * 2; | |
array[i + 1][j] = 0; | |
} | |
function merge(i, j, left, right, bottom) { | |
if (left) { | |
mergeLeft(i, j); | |
} | |
if (right) { | |
mergeRight(i, j); | |
} | |
if (bottom) { | |
mergeBottom(i, j); | |
} | |
} | |
function finalShift(i, j) { | |
let x = i; | |
if (i < 5) { | |
if (array[i + 1][j] === 0) { | |
array[i + 1][j] = array[i][j]; | |
array[i][j] = 0; | |
x = x + 1; | |
} | |
} | |
return x; | |
} | |
function shiftDown(x, y) { | |
i = x; | |
j = y; | |
let temp; | |
for (i = x; i > 0; i--) { | |
if (array[i - 1][j] !== 0) { | |
array[i][j] = array[i - 1][j]; | |
array[i - 1][j] = 0; | |
} | |
} | |
} | |
// display(); | |
// let { left, right, bottom } = checkPossibilities(4, 2); | |
// merge(4, 2, left, right, bottom); | |
// finalShift(4, 2); | |
// shiftDown(5, 1); | |
// display(); | |
async function main() { | |
display(); | |
// This json object is used to configure what data will be retrieved from command line. | |
var prompt_attributes = [ | |
{ | |
name: "i", | |
}, | |
{ | |
name: "j", | |
}, | |
{ | |
name: "value", | |
}, | |
]; | |
let game_over = false; | |
let lock; | |
while (!game_over && !lock) { | |
prompt.start(); | |
// Start the prompt to read user input. | |
lock = true; | |
// Prompt and get user input then display those data in console. | |
let next = async function () { | |
return new Promise((resolve, reject) => { | |
prompt.get(prompt_attributes, function (err, result) { | |
if (err) { | |
reject(err); | |
} else { | |
console.log("Command-line:"); | |
var i = parseInt(result.i); | |
var j = parseInt(result.j); | |
var value = parseInt(result.value); | |
insert(i, j, value); | |
let exit = false; | |
while (!exit) { | |
let { left, right, bottom } = checkPossibilities(i, j); | |
merge(i, j, left, right, bottom); | |
if (left) { | |
shiftDown(i, j - 1); | |
} | |
if (right) { | |
shiftDown(i, j + 1); | |
} | |
i = finalShift(i, j); | |
if (!left && !right && !bottom) { | |
exit = true; | |
} | |
} | |
resolve(false); | |
} | |
}); | |
}); | |
}; | |
lock = await next(); | |
display(); | |
} | |
} | |
main() | |
.then(() => { | |
console.log("...."); | |
}) | |
.catch((err) => { | |
console.log(err); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment