Created
December 17, 2018 17:02
Star
You must be signed in to star a gist
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
/** | |
* In the game 2048, the board consists of a 2-dimensional array of values. | |
* | |
* Every tile will always be a power of 2, starting with 2. | |
* | |
* The player can swipe left, up, right, or down. When this happens the numbers | |
* "collapse" in that direction. Duplicate values merge into a single tile and | |
* after swiping all non-zero values should have shifted in that direction. | |
* | |
* NOTE! A maximum of 2 tiles can merge at a time! So if you have a row of [4, 4, 2, 2] | |
* after collapsing left, you should now have [8, 4, 0, 0] | |
* | |
* Follow the steps below to get the tests passing so that you have valid working | |
* functions for LEFT swipe and RIGHT swipe to match the rules of the game. | |
* | |
* If you want to see the game in action, you can here: | |
* | |
* http://2048game.com/ | |
*/ | |
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
// TASK 1 | |
// | |
// This function should duplicate a 2-dimensional array of numbers. | |
// | |
// The original array(s) should not be modified! | |
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
const cloneBoard = boardValues => { | |
}; | |
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
// TASK 2 | |
// | |
// Write a function that returns new values for a board if you were | |
// to swipe / move it LEFT and RIGHT (up and down are similar). | |
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
const collapseLeft = oldBoard => { | |
let newBoard = cloneBoard(oldBoard); | |
// Write code here. | |
return newBoard; | |
}; | |
const collapseRight = oldBoard => { | |
let newBoard = cloneBoard(oldBoard); | |
// Write code here. | |
return newBoard; | |
}; | |
//--------------------------------------------- | |
// | |
// TESTS | |
// | |
//--------------------------------------------- | |
expectEquals(cloneBoard( | |
[[2, 4, 8, 16], | |
[32, 64, 128, 256], | |
[512, 1024, 2048, 4096], | |
[8192, 16384, 32768, 65536]]), | |
[[2, 4, 8, 16], | |
[32, 64, 128, 256], | |
[512, 1024, 2048, 4096], | |
[8192, 16384, 32768, 65536]], '1. cloneBoard test failed'); | |
expectEquals(collapseLeft( | |
[[0, 0, 0, 2], | |
[0, 0, 4, 0], | |
[0, 8, 0, 0], | |
[16, 0, 0, 0]]), | |
[[2, 0, 0, 0], | |
[4, 0, 0, 0], | |
[8, 0, 0, 0], | |
[16, 0, 0, 0]], '2. collapseLeft test A failed'); | |
expectEquals(collapseLeft( | |
[[2, 2, 2, 2], | |
[0, 0, 0, 0], | |
[0, 0, 0, 0], | |
[4, 4, 2, 2]]), | |
[[4, 4, 0, 0], | |
[0, 0, 0, 0], | |
[0, 0, 0, 0], | |
[8, 4, 0, 0]], '2.B collapseLeft test B failed'); | |
expectEquals(collapseRight( | |
[[0, 0, 0, 2], | |
[0, 0, 4, 0], | |
[0, 8, 0, 0], | |
[16, 0, 0, 0]]), | |
[[0, 0, 0, 2], | |
[0, 0, 0, 4], | |
[0, 0, 0, 8], | |
[0, 0, 0, 16]], '3. collapseRight test A failed'); | |
expectEquals(collapseRight( | |
[[2, 2, 2, 2], | |
[0, 0, 0, 0], | |
[0, 0, 0, 0], | |
[4, 4, 2, 2]]), | |
[[0, 0, 4, 4], | |
[0, 0, 0, 0], | |
[0, 0, 0, 0], | |
[0, 0, 8, 4]], '3.B collapseRight test B failed'); | |
function expectEquals(v1, v2, message) { | |
if (v1 && v2) { | |
let st1 = JSON.stringify(v1); | |
let st2 = JSON.stringify(v2); | |
if (st1 !== st2) { | |
console.error(message, v1.join('\n'), v2.join('\n')); | |
} | |
} else console.error(message); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment