From c80c1b978288b46c81fc3d351d4843d7b10e46e5 Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Mon, 1 Feb 2016 16:20:56 -0800 Subject: [PATCH 01/52] function working for collision logic in both positive and negative directions --- javascripts/collision.js | 127 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 javascripts/collision.js diff --git a/javascripts/collision.js b/javascripts/collision.js new file mode 100644 index 0000000..364aa0a --- /dev/null +++ b/javascripts/collision.js @@ -0,0 +1,127 @@ +// var tileCollision = function (array, direction) { +// // clone array +// newArray = array.slice(); +// // remove 0's from array +// for(var i = newArray.length - 1; i >= 0; i--) { +// if(newArray[i] === 0) { +// newArray.splice(i, 1); +// } +// } +// // depending on direction, start checking one way or another +// // determine if the newArray still has more than one thing in it +// if (newArray.length > 2) { +// if (newArray[0] === newArray[1]) { +// newArray[0] = newArray[0] * 2; +// newArray[1] = 0; +// if (array[2] === array[3]) { +// newArray[1] = newArray[2] * 2; +// newArray[3] = 0; +// newArray[2] = 0; +// } +// } else if (newArray[1] === newArray[2]) { +// newArray[1] = newArray[1] * 2; +// newArray[2] = 0; +// } else if (newArray [2] === newArray[3]) { +// newArray[2] = newArray[2] * 2; +// newArray[3] = 0; +// } +// } +// if (newArray.length < 4) { +// while (newArray.length < 4) { +// newArray.push(0); +// } +// } +// return newArray; +// }; + +var removeZeros = function(array) { + // for(var i = array.length - 1; i >= 0; i--) { + // if(array[i] === 0) { + // array.splice(i, 1); + // } + // } + return array.filter(function (val) { + return val !== 0; + }); +}; + + +var cleanCollision = function(array, direction) { + // clone array + newArray = array.slice(); + // remove 0's from array + newArray = removeZeros(newArray); + + // combine anything that needs combining + if (newArray.length > 1) { + if (direction === 'positive') { + for ( a = newArray.length, j = 0; a > 0; j++, a--) { + if (newArray[j] === newArray[(j+1)]) { + newArray[j] = newArray[j] * 2; + newArray[(j+1)] = 0; + } + } + } else if (direction === 'negative') { + for ( a = newArray.length; a > 0; a--) { + if (newArray[a] === newArray[(a-1)]) { + newArray[a] = newArray[a] * 2; + newArray[(a-1)] = 0; + } + } + } + } + // remove all zeros again + newArray = removeZeros(newArray); + + // add in zeros until array reaches four spots + if (newArray.length < 4) { + if (direction === 'positive') { + while (newArray.length < 4) { + newArray.push(0); + } + } else if (direction === 'negative') { + while (newArray.length < 4) { + newArray.unshift(0); + } + } + } + return newArray; +}; + +var collision = function(array, direction) { + // clone array + newArray = array.slice(); + // remove 0's from array + newArray = removeZeros(newArray); + // reverse array if direction is negative + if (direction === 'negative') { + newArray.reverse(); + } + // combine anything that needs combining + if (newArray.length > 1) { + for ( a = newArray.length, j = 0; a > 0; j++, a--) { + if (newArray[j] === newArray[(j+1)]) { + newArray[j] = newArray[j] * 2; + newArray[(j+1)] = 0; + // this.score += newArray[j] + } + } + } + // remove all zeros again + newArray = removeZeros(newArray); + if (newArray.length < 4) { + while (newArray.length < 4) { + newArray.push(0); + } + } + + // reverse again if direction was negative + return direction === 'negative' ? newArray.reverse() : newArray; +}; + +var testOne = [0, 2, 0, 0]; +var testTwo = [4, 2, 4, 4]; +var testThree = [0, 0, 0, 16]; +var testFour = [0, 0, 8, 16]; + +tileCollision(testTwo); \ No newline at end of file From ac5c1fbd6aeb480911d65b17110bed3209cbb77a Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Tue, 2 Feb 2016 09:44:12 -0800 Subject: [PATCH 02/52] some gameOver logic that isn't quite working yet --- javascripts/gameOver.js | 62 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 javascripts/gameOver.js diff --git a/javascripts/gameOver.js b/javascripts/gameOver.js new file mode 100644 index 0000000..075792b --- /dev/null +++ b/javascripts/gameOver.js @@ -0,0 +1,62 @@ +var removeZeros = function(array) { + return array.filter(function (val) { + return val !== 0; + }); +}; + +var collision = function(array, direction) { + // clone array + newArray = array.slice(); + // remove 0's from array + newArray = removeZeros(newArray); + // reverse array if direction is negative + if (direction === 'negative') { + newArray.reverse(); + } + // combine anything that needs combining + if (newArray.length > 1) { + for ( a = newArray.length, j = 0; a > 0; j++, a--) { + if (newArray[j] === newArray[(j+1)]) { + newArray[j] = newArray[j] * 2; + newArray[(j+1)] = 0; + // this.score += newArray[j] + } + } + } + // remove all zeros again + newArray = removeZeros(newArray); + if (newArray.length < 4) { + while (newArray.length < 4) { + newArray.push(0); + } + } + + // reverse again if direction was negative + return direction === 'negative' ? newArray.reverse() : newArray; +}; + +var gameOver = function (gameArray) { + var over = true; + while (over === false) { + + } + for (i = gameArray.length-1; i >= 0; i--) { + if (collision(gameArray[i]) !== gameArray[i]) { + over = false; + break; + } + } + return over; +}; + +var test =[[2, 4, 2, 4], [4, 2, 4, 2], [2, 4, 2, 4], [4, 2, 4, 2]]; +var testOne = [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 8, 9], [10, 11, 12, 13]]; +var testThree = [[1, 2, 9, 10], [3, 4, 11, 12], [5, 6, 13, 14], [7, 8, 15, 16]]; + + +console.log("expect: true"); +console.log(gameOver(test)); +console.log("expect: false"); +console.log(gameOver(testOne)); +console.log("expect: true"); +console.log(gameOver(testThree)); \ No newline at end of file From d3c3208af7d7e052d77b2463d2f5611933c5c028 Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Tue, 2 Feb 2016 09:45:24 -0800 Subject: [PATCH 03/52] removed extraneous code --- javascripts/collision.js | 79 ---------------------------------------- 1 file changed, 79 deletions(-) diff --git a/javascripts/collision.js b/javascripts/collision.js index 364aa0a..88c9dc5 100644 --- a/javascripts/collision.js +++ b/javascripts/collision.js @@ -1,39 +1,3 @@ -// var tileCollision = function (array, direction) { -// // clone array -// newArray = array.slice(); -// // remove 0's from array -// for(var i = newArray.length - 1; i >= 0; i--) { -// if(newArray[i] === 0) { -// newArray.splice(i, 1); -// } -// } -// // depending on direction, start checking one way or another -// // determine if the newArray still has more than one thing in it -// if (newArray.length > 2) { -// if (newArray[0] === newArray[1]) { -// newArray[0] = newArray[0] * 2; -// newArray[1] = 0; -// if (array[2] === array[3]) { -// newArray[1] = newArray[2] * 2; -// newArray[3] = 0; -// newArray[2] = 0; -// } -// } else if (newArray[1] === newArray[2]) { -// newArray[1] = newArray[1] * 2; -// newArray[2] = 0; -// } else if (newArray [2] === newArray[3]) { -// newArray[2] = newArray[2] * 2; -// newArray[3] = 0; -// } -// } -// if (newArray.length < 4) { -// while (newArray.length < 4) { -// newArray.push(0); -// } -// } -// return newArray; -// }; - var removeZeros = function(array) { // for(var i = array.length - 1; i >= 0; i--) { // if(array[i] === 0) { @@ -45,49 +9,6 @@ var removeZeros = function(array) { }); }; - -var cleanCollision = function(array, direction) { - // clone array - newArray = array.slice(); - // remove 0's from array - newArray = removeZeros(newArray); - - // combine anything that needs combining - if (newArray.length > 1) { - if (direction === 'positive') { - for ( a = newArray.length, j = 0; a > 0; j++, a--) { - if (newArray[j] === newArray[(j+1)]) { - newArray[j] = newArray[j] * 2; - newArray[(j+1)] = 0; - } - } - } else if (direction === 'negative') { - for ( a = newArray.length; a > 0; a--) { - if (newArray[a] === newArray[(a-1)]) { - newArray[a] = newArray[a] * 2; - newArray[(a-1)] = 0; - } - } - } - } - // remove all zeros again - newArray = removeZeros(newArray); - - // add in zeros until array reaches four spots - if (newArray.length < 4) { - if (direction === 'positive') { - while (newArray.length < 4) { - newArray.push(0); - } - } else if (direction === 'negative') { - while (newArray.length < 4) { - newArray.unshift(0); - } - } - } - return newArray; -}; - var collision = function(array, direction) { // clone array newArray = array.slice(); From 7de23b559220280a57b38373f090a4849c9eaec9 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Tue, 2 Feb 2016 09:52:04 -0800 Subject: [PATCH 04/52] add gameover function --- javascripts/2048.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/javascripts/2048.js b/javascripts/2048.js index 4b2746c..43a9984 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -1,5 +1,7 @@ var Game = function() { // Game logic and initialization here + this.gameOver = false; + this.board = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]; }; Game.prototype.moveTile = function(tile, direction) { @@ -20,6 +22,35 @@ Game.prototype.moveTile = function(tile, direction) { } }; +Game.prototype.updateGameOver = function(){ + // has free spaces + var noZeroes = true, + pairFound = false; + this.board.forEach(function(row) { + noZeroes = noZeroes && row.every(function(val) { + return val !== 0; + }); + }); + // if there are no zeroes, the game is not over + if (!noZeroes) { + return; + } else { + var i = 0; + while (!pairFound && i < 4) { + var j = 0; + while (!pairFound && j < 4) { + if (this.board[i][j] == this.board[i][j+1] || + (this.board[i+1] && this.board[i][j] == this.board[i+1][j])) { + pairFound = true; + } + j++; + } + i++; + } + if (!pairFound) { this.gameOver = true; } + } +}; + $(document).ready(function() { console.log("ready to go!"); // Any interactive jQuery functionality From 6118a1aed541807da452065159c2a11dc0f16e97 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Tue, 2 Feb 2016 10:22:21 -0800 Subject: [PATCH 05/52] create addTile method for adding and displaying new tile --- index.html | 2 +- javascripts/2048.js | 21 ++++++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/index.html b/index.html index 4740408..8461cd9 100644 --- a/index.html +++ b/index.html @@ -26,7 +26,7 @@
-
2
+ diff --git a/javascripts/2048.js b/javascripts/2048.js index 43a9984..9da2554 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -4,6 +4,21 @@ var Game = function() { this.board = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]; }; +Game.prototype.addTile = function() { + var tilePlaced = false; + while (!tilePlaced) { + var col = Math.floor(Math.random() * (4 - 0)), + row = Math.floor(Math.random() * (4 - 0)); + if (this.board[row][col] === 0) { + tilePlaced = true; + this.board[row][col] = 2; + // Update data-val when we add random 2 or 4 + var $tileHTML = $('
2
'); + $('#gameboard').append($tileHTML); + } + } +}; + Game.prototype.moveTile = function(tile, direction) { // Game method here switch(direction) { @@ -55,13 +70,17 @@ $(document).ready(function() { console.log("ready to go!"); // Any interactive jQuery functionality var game = new Game(); - + game.addTile(); + game.addTile(); $('body').keydown(function(event){ var arrows = [37, 38, 39, 40]; if (arrows.indexOf(event.which) > -1) { var tile = $('.tile'); + // console.log(tile); + // $(tile).attr('data-row', 'r0'); game.moveTile(tile, event.which); } + }); }); From 6ed7318dd0b5158bc05d319f5885b3f760dff6e7 Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Tue, 2 Feb 2016 11:35:47 -0800 Subject: [PATCH 06/52] revamped for tile objects, addTiles working but not displaying --- javascripts/2048.js | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/javascripts/2048.js b/javascripts/2048.js index 9da2554..dde6687 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -1,24 +1,38 @@ var Game = function() { // Game logic and initialization here this.gameOver = false; - this.board = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]; + this.board = []; }; -Game.prototype.addTile = function() { +var tileCount = 0; + +var Tile = function (row, col) { + // make new Tiles + this.row = row; + this.col = col; + // update so it can also be 4 + this.val = 2; + this.tileId = tileCount++; +}; + + +Game.prototype.addTile = function () { var tilePlaced = false; + var currentTiles = []; + this.board.forEach(function (tile) { + currentTiles.push([tile.row, tile.col]); + }); while (!tilePlaced) { - var col = Math.floor(Math.random() * (4 - 0)), - row = Math.floor(Math.random() * (4 - 0)); - if (this.board[row][col] === 0) { - tilePlaced = true; - this.board[row][col] = 2; - // Update data-val when we add random 2 or 4 - var $tileHTML = $('
2
'); - $('#gameboard').append($tileHTML); - } + var col = Math.floor(Math.random() * (4 - 0)), + row = Math.floor(Math.random() * (4 - 0)); + if (!currentTiles.includes([row, col])) { + tilePlaced = true; + this.board.push(new Tile(row, col)); + } } }; + Game.prototype.moveTile = function(tile, direction) { // Game method here switch(direction) { @@ -72,6 +86,7 @@ $(document).ready(function() { var game = new Game(); game.addTile(); game.addTile(); + console.dir(game.board); $('body').keydown(function(event){ var arrows = [37, 38, 39, 40]; if (arrows.indexOf(event.which) > -1) { From 138b6b2e25e7f0792efa3d7ed71ba8327dfc7c9f Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Tue, 2 Feb 2016 11:36:35 -0800 Subject: [PATCH 07/52] comment w next step --- javascripts/2048.js | 1 + 1 file changed, 1 insertion(+) diff --git a/javascripts/2048.js b/javascripts/2048.js index dde6687..a51bb30 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -28,6 +28,7 @@ Game.prototype.addTile = function () { if (!currentTiles.includes([row, col])) { tilePlaced = true; this.board.push(new Tile(row, col)); + // make tile display on front end? } } }; From 62944b6252bbf8640dc942dad908c776ca06b9ae Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Tue, 2 Feb 2016 11:44:00 -0800 Subject: [PATCH 08/52] addTile properly displaying initial tiles, like a boss --- javascripts/2048.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/javascripts/2048.js b/javascripts/2048.js index a51bb30..4ee94cc 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -28,7 +28,8 @@ Game.prototype.addTile = function () { if (!currentTiles.includes([row, col])) { tilePlaced = true; this.board.push(new Tile(row, col)); - // make tile display on front end? + var $tileHTML = $('
2
'); + $('#gameboard').append($tileHTML); } } }; From 71c03830321931de558ec9cf5777a039ac9c2458 Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Tue, 2 Feb 2016 11:46:41 -0800 Subject: [PATCH 09/52] new Tile html includes tileId in class --- javascripts/2048.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/javascripts/2048.js b/javascripts/2048.js index 4ee94cc..1f74bc7 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -27,8 +27,9 @@ Game.prototype.addTile = function () { row = Math.floor(Math.random() * (4 - 0)); if (!currentTiles.includes([row, col])) { tilePlaced = true; - this.board.push(new Tile(row, col)); - var $tileHTML = $('
2
'); + newTile = new Tile(row, col); + this.board.push(newTile); + var $tileHTML = $('
2
'); $('#gameboard').append($tileHTML); } } From 65e69ecd81566078cb850ff47145f667d1448007 Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Tue, 2 Feb 2016 11:57:47 -0800 Subject: [PATCH 10/52] hard coded moving tiles working --- javascripts/2048.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/javascripts/2048.js b/javascripts/2048.js index 1f74bc7..bad2a3e 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -12,7 +12,7 @@ var Tile = function (row, col) { this.col = col; // update so it can also be 4 this.val = 2; - this.tileId = tileCount++; + this.tileId = String(tileCount++); }; @@ -41,6 +41,11 @@ Game.prototype.moveTile = function(tile, direction) { switch(direction) { case 38: //up console.log('up'); + this.board.forEach(function (tile) { + tile.row = 0; + $("." + tile.tileId).attr("data-row", "r " + tile.row); + }); + console.log(this.board); break; case 40: //down console.log('down'); From ff298d57df88bc2364f379b1df9294aac96f8bc3 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Tue, 2 Feb 2016 15:02:36 -0800 Subject: [PATCH 11/52] back end tile collision function for pressing up --- index.html | 1 + javascripts/2048.js | 45 +++++++++++++++++++++++++++-------- javascripts/underscore-min.js | 6 +++++ 3 files changed, 42 insertions(+), 10 deletions(-) create mode 100644 javascripts/underscore-min.js diff --git a/index.html b/index.html index 8461cd9..7bce92d 100644 --- a/index.html +++ b/index.html @@ -4,6 +4,7 @@ 2048 + diff --git a/javascripts/2048.js b/javascripts/2048.js index bad2a3e..65611ba 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -35,17 +35,32 @@ Game.prototype.addTile = function () { } }; - Game.prototype.moveTile = function(tile, direction) { // Game method here switch(direction) { case 38: //up - console.log('up'); - this.board.forEach(function (tile) { - tile.row = 0; - $("." + tile.tileId).attr("data-row", "r " + tile.row); + var groupedTiles = _.groupBy(this.board, function(tile) { + return tile.col; + }); + console.log(groupedTiles, Object.keys(groupedTiles)); + // iterate through each column + Object.keys(groupedTiles).forEach(function(key){ + var colArray = groupedTiles[key]; + for (var row = 0; row < colArray.length; row++) { + // if combining + if (colArray[row].val === colArray[row+1].val) { + colArray[row+1].val *= 2; + colArray[row+1].row = row; + // delete current value + colArray.splice(row, 1); + // if not combining + } else { + colArray[row].row = row; + } + } + console.log(colArray); }); - console.log(this.board); + break; case 40: //down console.log('down'); @@ -89,12 +104,22 @@ Game.prototype.updateGameOver = function(){ }; $(document).ready(function() { - console.log("ready to go!"); // Any interactive jQuery functionality var game = new Game(); - game.addTile(); - game.addTile(); - console.dir(game.board); + var t1 = new Tile(1, 0), + t2 = new Tile(3, 0), + t3 = new Tile(2, 0), + t4 = new Tile(4, 0); + game.board = [t1, t2, t3, t4]; + var $t1HTML = $('
2
'); + $('#gameboard').append($t1HTML); + var $t2HTML = $('
2
'); + $('#gameboard').append($t2HTML); + var $t3HTML = $('
2
'); + $('#gameboard').append($t3HTML); + var $t4HTML = $('
2
'); + $('#gameboard').append($t4HTML); + $('body').keydown(function(event){ var arrows = [37, 38, 39, 40]; if (arrows.indexOf(event.which) > -1) { diff --git a/javascripts/underscore-min.js b/javascripts/underscore-min.js new file mode 100644 index 0000000..f01025b --- /dev/null +++ b/javascripts/underscore-min.js @@ -0,0 +1,6 @@ +// Underscore.js 1.8.3 +// http://underscorejs.org +// (c) 2009-2015 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors +// Underscore may be freely distributed under the MIT license. +(function(){function n(n){function t(t,r,e,u,i,o){for(;i>=0&&o>i;i+=n){var a=u?u[i]:i;e=r(e,t[a],a,t)}return e}return function(r,e,u,i){e=b(e,i,4);var o=!k(r)&&m.keys(r),a=(o||r).length,c=n>0?0:a-1;return arguments.length<3&&(u=r[o?o[c]:c],c+=n),t(r,e,u,o,c,a)}}function t(n){return function(t,r,e){r=x(r,e);for(var u=O(t),i=n>0?0:u-1;i>=0&&u>i;i+=n)if(r(t[i],i,t))return i;return-1}}function r(n,t,r){return function(e,u,i){var o=0,a=O(e);if("number"==typeof i)n>0?o=i>=0?i:Math.max(i+a,o):a=i>=0?Math.min(i+1,a):i+a+1;else if(r&&i&&a)return i=r(e,u),e[i]===u?i:-1;if(u!==u)return i=t(l.call(e,o,a),m.isNaN),i>=0?i+o:-1;for(i=n>0?o:a-1;i>=0&&a>i;i+=n)if(e[i]===u)return i;return-1}}function e(n,t){var r=I.length,e=n.constructor,u=m.isFunction(e)&&e.prototype||a,i="constructor";for(m.has(n,i)&&!m.contains(t,i)&&t.push(i);r--;)i=I[r],i in n&&n[i]!==u[i]&&!m.contains(t,i)&&t.push(i)}var u=this,i=u._,o=Array.prototype,a=Object.prototype,c=Function.prototype,f=o.push,l=o.slice,s=a.toString,p=a.hasOwnProperty,h=Array.isArray,v=Object.keys,g=c.bind,y=Object.create,d=function(){},m=function(n){return n instanceof m?n:this instanceof m?void(this._wrapped=n):new m(n)};"undefined"!=typeof exports?("undefined"!=typeof module&&module.exports&&(exports=module.exports=m),exports._=m):u._=m,m.VERSION="1.8.3";var b=function(n,t,r){if(t===void 0)return n;switch(null==r?3:r){case 1:return function(r){return n.call(t,r)};case 2:return function(r,e){return n.call(t,r,e)};case 3:return function(r,e,u){return n.call(t,r,e,u)};case 4:return function(r,e,u,i){return n.call(t,r,e,u,i)}}return function(){return n.apply(t,arguments)}},x=function(n,t,r){return null==n?m.identity:m.isFunction(n)?b(n,t,r):m.isObject(n)?m.matcher(n):m.property(n)};m.iteratee=function(n,t){return x(n,t,1/0)};var _=function(n,t){return function(r){var e=arguments.length;if(2>e||null==r)return r;for(var u=1;e>u;u++)for(var i=arguments[u],o=n(i),a=o.length,c=0;a>c;c++){var f=o[c];t&&r[f]!==void 0||(r[f]=i[f])}return r}},j=function(n){if(!m.isObject(n))return{};if(y)return y(n);d.prototype=n;var t=new d;return d.prototype=null,t},w=function(n){return function(t){return null==t?void 0:t[n]}},A=Math.pow(2,53)-1,O=w("length"),k=function(n){var t=O(n);return"number"==typeof t&&t>=0&&A>=t};m.each=m.forEach=function(n,t,r){t=b(t,r);var e,u;if(k(n))for(e=0,u=n.length;u>e;e++)t(n[e],e,n);else{var i=m.keys(n);for(e=0,u=i.length;u>e;e++)t(n[i[e]],i[e],n)}return n},m.map=m.collect=function(n,t,r){t=x(t,r);for(var e=!k(n)&&m.keys(n),u=(e||n).length,i=Array(u),o=0;u>o;o++){var a=e?e[o]:o;i[o]=t(n[a],a,n)}return i},m.reduce=m.foldl=m.inject=n(1),m.reduceRight=m.foldr=n(-1),m.find=m.detect=function(n,t,r){var e;return e=k(n)?m.findIndex(n,t,r):m.findKey(n,t,r),e!==void 0&&e!==-1?n[e]:void 0},m.filter=m.select=function(n,t,r){var e=[];return t=x(t,r),m.each(n,function(n,r,u){t(n,r,u)&&e.push(n)}),e},m.reject=function(n,t,r){return m.filter(n,m.negate(x(t)),r)},m.every=m.all=function(n,t,r){t=x(t,r);for(var e=!k(n)&&m.keys(n),u=(e||n).length,i=0;u>i;i++){var o=e?e[i]:i;if(!t(n[o],o,n))return!1}return!0},m.some=m.any=function(n,t,r){t=x(t,r);for(var e=!k(n)&&m.keys(n),u=(e||n).length,i=0;u>i;i++){var o=e?e[i]:i;if(t(n[o],o,n))return!0}return!1},m.contains=m.includes=m.include=function(n,t,r,e){return k(n)||(n=m.values(n)),("number"!=typeof r||e)&&(r=0),m.indexOf(n,t,r)>=0},m.invoke=function(n,t){var r=l.call(arguments,2),e=m.isFunction(t);return m.map(n,function(n){var u=e?t:n[t];return null==u?u:u.apply(n,r)})},m.pluck=function(n,t){return m.map(n,m.property(t))},m.where=function(n,t){return m.filter(n,m.matcher(t))},m.findWhere=function(n,t){return m.find(n,m.matcher(t))},m.max=function(n,t,r){var e,u,i=-1/0,o=-1/0;if(null==t&&null!=n){n=k(n)?n:m.values(n);for(var a=0,c=n.length;c>a;a++)e=n[a],e>i&&(i=e)}else t=x(t,r),m.each(n,function(n,r,e){u=t(n,r,e),(u>o||u===-1/0&&i===-1/0)&&(i=n,o=u)});return i},m.min=function(n,t,r){var e,u,i=1/0,o=1/0;if(null==t&&null!=n){n=k(n)?n:m.values(n);for(var a=0,c=n.length;c>a;a++)e=n[a],i>e&&(i=e)}else t=x(t,r),m.each(n,function(n,r,e){u=t(n,r,e),(o>u||1/0===u&&1/0===i)&&(i=n,o=u)});return i},m.shuffle=function(n){for(var t,r=k(n)?n:m.values(n),e=r.length,u=Array(e),i=0;e>i;i++)t=m.random(0,i),t!==i&&(u[i]=u[t]),u[t]=r[i];return u},m.sample=function(n,t,r){return null==t||r?(k(n)||(n=m.values(n)),n[m.random(n.length-1)]):m.shuffle(n).slice(0,Math.max(0,t))},m.sortBy=function(n,t,r){return t=x(t,r),m.pluck(m.map(n,function(n,r,e){return{value:n,index:r,criteria:t(n,r,e)}}).sort(function(n,t){var r=n.criteria,e=t.criteria;if(r!==e){if(r>e||r===void 0)return 1;if(e>r||e===void 0)return-1}return n.index-t.index}),"value")};var F=function(n){return function(t,r,e){var u={};return r=x(r,e),m.each(t,function(e,i){var o=r(e,i,t);n(u,e,o)}),u}};m.groupBy=F(function(n,t,r){m.has(n,r)?n[r].push(t):n[r]=[t]}),m.indexBy=F(function(n,t,r){n[r]=t}),m.countBy=F(function(n,t,r){m.has(n,r)?n[r]++:n[r]=1}),m.toArray=function(n){return n?m.isArray(n)?l.call(n):k(n)?m.map(n,m.identity):m.values(n):[]},m.size=function(n){return null==n?0:k(n)?n.length:m.keys(n).length},m.partition=function(n,t,r){t=x(t,r);var e=[],u=[];return m.each(n,function(n,r,i){(t(n,r,i)?e:u).push(n)}),[e,u]},m.first=m.head=m.take=function(n,t,r){return null==n?void 0:null==t||r?n[0]:m.initial(n,n.length-t)},m.initial=function(n,t,r){return l.call(n,0,Math.max(0,n.length-(null==t||r?1:t)))},m.last=function(n,t,r){return null==n?void 0:null==t||r?n[n.length-1]:m.rest(n,Math.max(0,n.length-t))},m.rest=m.tail=m.drop=function(n,t,r){return l.call(n,null==t||r?1:t)},m.compact=function(n){return m.filter(n,m.identity)};var S=function(n,t,r,e){for(var u=[],i=0,o=e||0,a=O(n);a>o;o++){var c=n[o];if(k(c)&&(m.isArray(c)||m.isArguments(c))){t||(c=S(c,t,r));var f=0,l=c.length;for(u.length+=l;l>f;)u[i++]=c[f++]}else r||(u[i++]=c)}return u};m.flatten=function(n,t){return S(n,t,!1)},m.without=function(n){return m.difference(n,l.call(arguments,1))},m.uniq=m.unique=function(n,t,r,e){m.isBoolean(t)||(e=r,r=t,t=!1),null!=r&&(r=x(r,e));for(var u=[],i=[],o=0,a=O(n);a>o;o++){var c=n[o],f=r?r(c,o,n):c;t?(o&&i===f||u.push(c),i=f):r?m.contains(i,f)||(i.push(f),u.push(c)):m.contains(u,c)||u.push(c)}return u},m.union=function(){return m.uniq(S(arguments,!0,!0))},m.intersection=function(n){for(var t=[],r=arguments.length,e=0,u=O(n);u>e;e++){var i=n[e];if(!m.contains(t,i)){for(var o=1;r>o&&m.contains(arguments[o],i);o++);o===r&&t.push(i)}}return t},m.difference=function(n){var t=S(arguments,!0,!0,1);return m.filter(n,function(n){return!m.contains(t,n)})},m.zip=function(){return m.unzip(arguments)},m.unzip=function(n){for(var t=n&&m.max(n,O).length||0,r=Array(t),e=0;t>e;e++)r[e]=m.pluck(n,e);return r},m.object=function(n,t){for(var r={},e=0,u=O(n);u>e;e++)t?r[n[e]]=t[e]:r[n[e][0]]=n[e][1];return r},m.findIndex=t(1),m.findLastIndex=t(-1),m.sortedIndex=function(n,t,r,e){r=x(r,e,1);for(var u=r(t),i=0,o=O(n);o>i;){var a=Math.floor((i+o)/2);r(n[a])i;i++,n+=r)u[i]=n;return u};var E=function(n,t,r,e,u){if(!(e instanceof t))return n.apply(r,u);var i=j(n.prototype),o=n.apply(i,u);return m.isObject(o)?o:i};m.bind=function(n,t){if(g&&n.bind===g)return g.apply(n,l.call(arguments,1));if(!m.isFunction(n))throw new TypeError("Bind must be called on a function");var r=l.call(arguments,2),e=function(){return E(n,e,t,this,r.concat(l.call(arguments)))};return e},m.partial=function(n){var t=l.call(arguments,1),r=function(){for(var e=0,u=t.length,i=Array(u),o=0;u>o;o++)i[o]=t[o]===m?arguments[e++]:t[o];for(;e=e)throw new Error("bindAll must be passed function names");for(t=1;e>t;t++)r=arguments[t],n[r]=m.bind(n[r],n);return n},m.memoize=function(n,t){var r=function(e){var u=r.cache,i=""+(t?t.apply(this,arguments):e);return m.has(u,i)||(u[i]=n.apply(this,arguments)),u[i]};return r.cache={},r},m.delay=function(n,t){var r=l.call(arguments,2);return setTimeout(function(){return n.apply(null,r)},t)},m.defer=m.partial(m.delay,m,1),m.throttle=function(n,t,r){var e,u,i,o=null,a=0;r||(r={});var c=function(){a=r.leading===!1?0:m.now(),o=null,i=n.apply(e,u),o||(e=u=null)};return function(){var f=m.now();a||r.leading!==!1||(a=f);var l=t-(f-a);return e=this,u=arguments,0>=l||l>t?(o&&(clearTimeout(o),o=null),a=f,i=n.apply(e,u),o||(e=u=null)):o||r.trailing===!1||(o=setTimeout(c,l)),i}},m.debounce=function(n,t,r){var e,u,i,o,a,c=function(){var f=m.now()-o;t>f&&f>=0?e=setTimeout(c,t-f):(e=null,r||(a=n.apply(i,u),e||(i=u=null)))};return function(){i=this,u=arguments,o=m.now();var f=r&&!e;return e||(e=setTimeout(c,t)),f&&(a=n.apply(i,u),i=u=null),a}},m.wrap=function(n,t){return m.partial(t,n)},m.negate=function(n){return function(){return!n.apply(this,arguments)}},m.compose=function(){var n=arguments,t=n.length-1;return function(){for(var r=t,e=n[t].apply(this,arguments);r--;)e=n[r].call(this,e);return e}},m.after=function(n,t){return function(){return--n<1?t.apply(this,arguments):void 0}},m.before=function(n,t){var r;return function(){return--n>0&&(r=t.apply(this,arguments)),1>=n&&(t=null),r}},m.once=m.partial(m.before,2);var M=!{toString:null}.propertyIsEnumerable("toString"),I=["valueOf","isPrototypeOf","toString","propertyIsEnumerable","hasOwnProperty","toLocaleString"];m.keys=function(n){if(!m.isObject(n))return[];if(v)return v(n);var t=[];for(var r in n)m.has(n,r)&&t.push(r);return M&&e(n,t),t},m.allKeys=function(n){if(!m.isObject(n))return[];var t=[];for(var r in n)t.push(r);return M&&e(n,t),t},m.values=function(n){for(var t=m.keys(n),r=t.length,e=Array(r),u=0;r>u;u++)e[u]=n[t[u]];return e},m.mapObject=function(n,t,r){t=x(t,r);for(var e,u=m.keys(n),i=u.length,o={},a=0;i>a;a++)e=u[a],o[e]=t(n[e],e,n);return o},m.pairs=function(n){for(var t=m.keys(n),r=t.length,e=Array(r),u=0;r>u;u++)e[u]=[t[u],n[t[u]]];return e},m.invert=function(n){for(var t={},r=m.keys(n),e=0,u=r.length;u>e;e++)t[n[r[e]]]=r[e];return t},m.functions=m.methods=function(n){var t=[];for(var r in n)m.isFunction(n[r])&&t.push(r);return t.sort()},m.extend=_(m.allKeys),m.extendOwn=m.assign=_(m.keys),m.findKey=function(n,t,r){t=x(t,r);for(var e,u=m.keys(n),i=0,o=u.length;o>i;i++)if(e=u[i],t(n[e],e,n))return e},m.pick=function(n,t,r){var e,u,i={},o=n;if(null==o)return i;m.isFunction(t)?(u=m.allKeys(o),e=b(t,r)):(u=S(arguments,!1,!1,1),e=function(n,t,r){return t in r},o=Object(o));for(var a=0,c=u.length;c>a;a++){var f=u[a],l=o[f];e(l,f,o)&&(i[f]=l)}return i},m.omit=function(n,t,r){if(m.isFunction(t))t=m.negate(t);else{var e=m.map(S(arguments,!1,!1,1),String);t=function(n,t){return!m.contains(e,t)}}return m.pick(n,t,r)},m.defaults=_(m.allKeys,!0),m.create=function(n,t){var r=j(n);return t&&m.extendOwn(r,t),r},m.clone=function(n){return m.isObject(n)?m.isArray(n)?n.slice():m.extend({},n):n},m.tap=function(n,t){return t(n),n},m.isMatch=function(n,t){var r=m.keys(t),e=r.length;if(null==n)return!e;for(var u=Object(n),i=0;e>i;i++){var o=r[i];if(t[o]!==u[o]||!(o in u))return!1}return!0};var N=function(n,t,r,e){if(n===t)return 0!==n||1/n===1/t;if(null==n||null==t)return n===t;n instanceof m&&(n=n._wrapped),t instanceof m&&(t=t._wrapped);var u=s.call(n);if(u!==s.call(t))return!1;switch(u){case"[object RegExp]":case"[object String]":return""+n==""+t;case"[object Number]":return+n!==+n?+t!==+t:0===+n?1/+n===1/t:+n===+t;case"[object Date]":case"[object Boolean]":return+n===+t}var i="[object Array]"===u;if(!i){if("object"!=typeof n||"object"!=typeof t)return!1;var o=n.constructor,a=t.constructor;if(o!==a&&!(m.isFunction(o)&&o instanceof o&&m.isFunction(a)&&a instanceof a)&&"constructor"in n&&"constructor"in t)return!1}r=r||[],e=e||[];for(var c=r.length;c--;)if(r[c]===n)return e[c]===t;if(r.push(n),e.push(t),i){if(c=n.length,c!==t.length)return!1;for(;c--;)if(!N(n[c],t[c],r,e))return!1}else{var f,l=m.keys(n);if(c=l.length,m.keys(t).length!==c)return!1;for(;c--;)if(f=l[c],!m.has(t,f)||!N(n[f],t[f],r,e))return!1}return r.pop(),e.pop(),!0};m.isEqual=function(n,t){return N(n,t)},m.isEmpty=function(n){return null==n?!0:k(n)&&(m.isArray(n)||m.isString(n)||m.isArguments(n))?0===n.length:0===m.keys(n).length},m.isElement=function(n){return!(!n||1!==n.nodeType)},m.isArray=h||function(n){return"[object Array]"===s.call(n)},m.isObject=function(n){var t=typeof n;return"function"===t||"object"===t&&!!n},m.each(["Arguments","Function","String","Number","Date","RegExp","Error"],function(n){m["is"+n]=function(t){return s.call(t)==="[object "+n+"]"}}),m.isArguments(arguments)||(m.isArguments=function(n){return m.has(n,"callee")}),"function"!=typeof/./&&"object"!=typeof Int8Array&&(m.isFunction=function(n){return"function"==typeof n||!1}),m.isFinite=function(n){return isFinite(n)&&!isNaN(parseFloat(n))},m.isNaN=function(n){return m.isNumber(n)&&n!==+n},m.isBoolean=function(n){return n===!0||n===!1||"[object Boolean]"===s.call(n)},m.isNull=function(n){return null===n},m.isUndefined=function(n){return n===void 0},m.has=function(n,t){return null!=n&&p.call(n,t)},m.noConflict=function(){return u._=i,this},m.identity=function(n){return n},m.constant=function(n){return function(){return n}},m.noop=function(){},m.property=w,m.propertyOf=function(n){return null==n?function(){}:function(t){return n[t]}},m.matcher=m.matches=function(n){return n=m.extendOwn({},n),function(t){return m.isMatch(t,n)}},m.times=function(n,t,r){var e=Array(Math.max(0,n));t=b(t,r,1);for(var u=0;n>u;u++)e[u]=t(u);return e},m.random=function(n,t){return null==t&&(t=n,n=0),n+Math.floor(Math.random()*(t-n+1))},m.now=Date.now||function(){return(new Date).getTime()};var B={"&":"&","<":"<",">":">",'"':""","'":"'","`":"`"},T=m.invert(B),R=function(n){var t=function(t){return n[t]},r="(?:"+m.keys(n).join("|")+")",e=RegExp(r),u=RegExp(r,"g");return function(n){return n=null==n?"":""+n,e.test(n)?n.replace(u,t):n}};m.escape=R(B),m.unescape=R(T),m.result=function(n,t,r){var e=null==n?void 0:n[t];return e===void 0&&(e=r),m.isFunction(e)?e.call(n):e};var q=0;m.uniqueId=function(n){var t=++q+"";return n?n+t:t},m.templateSettings={evaluate:/<%([\s\S]+?)%>/g,interpolate:/<%=([\s\S]+?)%>/g,escape:/<%-([\s\S]+?)%>/g};var K=/(.)^/,z={"'":"'","\\":"\\","\r":"r","\n":"n","\u2028":"u2028","\u2029":"u2029"},D=/\\|'|\r|\n|\u2028|\u2029/g,L=function(n){return"\\"+z[n]};m.template=function(n,t,r){!t&&r&&(t=r),t=m.defaults({},t,m.templateSettings);var e=RegExp([(t.escape||K).source,(t.interpolate||K).source,(t.evaluate||K).source].join("|")+"|$","g"),u=0,i="__p+='";n.replace(e,function(t,r,e,o,a){return i+=n.slice(u,a).replace(D,L),u=a+t.length,r?i+="'+\n((__t=("+r+"))==null?'':_.escape(__t))+\n'":e?i+="'+\n((__t=("+e+"))==null?'':__t)+\n'":o&&(i+="';\n"+o+"\n__p+='"),t}),i+="';\n",t.variable||(i="with(obj||{}){\n"+i+"}\n"),i="var __t,__p='',__j=Array.prototype.join,"+"print=function(){__p+=__j.call(arguments,'');};\n"+i+"return __p;\n";try{var o=new Function(t.variable||"obj","_",i)}catch(a){throw a.source=i,a}var c=function(n){return o.call(this,n,m)},f=t.variable||"obj";return c.source="function("+f+"){\n"+i+"}",c},m.chain=function(n){var t=m(n);return t._chain=!0,t};var P=function(n,t){return n._chain?m(t).chain():t};m.mixin=function(n){m.each(m.functions(n),function(t){var r=m[t]=n[t];m.prototype[t]=function(){var n=[this._wrapped];return f.apply(n,arguments),P(this,r.apply(m,n))}})},m.mixin(m),m.each(["pop","push","reverse","shift","sort","splice","unshift"],function(n){var t=o[n];m.prototype[n]=function(){var r=this._wrapped;return t.apply(r,arguments),"shift"!==n&&"splice"!==n||0!==r.length||delete r[0],P(this,r)}}),m.each(["concat","join","slice"],function(n){var t=o[n];m.prototype[n]=function(){return P(this,t.apply(this._wrapped,arguments))}}),m.prototype.value=function(){return this._wrapped},m.prototype.valueOf=m.prototype.toJSON=m.prototype.value,m.prototype.toString=function(){return""+this._wrapped},"function"==typeof define&&define.amd&&define("underscore",[],function(){return m})}).call(this); +//# sourceMappingURL=underscore-min.map \ No newline at end of file From 95c3bfee1372ec4e32425c43aff50036dfa7bfd6 Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Tue, 2 Feb 2016 15:32:29 -0800 Subject: [PATCH 12/52] removed extraneous files --- javascripts/collision.js | 48 ------------------------------- javascripts/gameOver.js | 62 ---------------------------------------- 2 files changed, 110 deletions(-) delete mode 100644 javascripts/collision.js delete mode 100644 javascripts/gameOver.js diff --git a/javascripts/collision.js b/javascripts/collision.js deleted file mode 100644 index 88c9dc5..0000000 --- a/javascripts/collision.js +++ /dev/null @@ -1,48 +0,0 @@ -var removeZeros = function(array) { - // for(var i = array.length - 1; i >= 0; i--) { - // if(array[i] === 0) { - // array.splice(i, 1); - // } - // } - return array.filter(function (val) { - return val !== 0; - }); -}; - -var collision = function(array, direction) { - // clone array - newArray = array.slice(); - // remove 0's from array - newArray = removeZeros(newArray); - // reverse array if direction is negative - if (direction === 'negative') { - newArray.reverse(); - } - // combine anything that needs combining - if (newArray.length > 1) { - for ( a = newArray.length, j = 0; a > 0; j++, a--) { - if (newArray[j] === newArray[(j+1)]) { - newArray[j] = newArray[j] * 2; - newArray[(j+1)] = 0; - // this.score += newArray[j] - } - } - } - // remove all zeros again - newArray = removeZeros(newArray); - if (newArray.length < 4) { - while (newArray.length < 4) { - newArray.push(0); - } - } - - // reverse again if direction was negative - return direction === 'negative' ? newArray.reverse() : newArray; -}; - -var testOne = [0, 2, 0, 0]; -var testTwo = [4, 2, 4, 4]; -var testThree = [0, 0, 0, 16]; -var testFour = [0, 0, 8, 16]; - -tileCollision(testTwo); \ No newline at end of file diff --git a/javascripts/gameOver.js b/javascripts/gameOver.js deleted file mode 100644 index 075792b..0000000 --- a/javascripts/gameOver.js +++ /dev/null @@ -1,62 +0,0 @@ -var removeZeros = function(array) { - return array.filter(function (val) { - return val !== 0; - }); -}; - -var collision = function(array, direction) { - // clone array - newArray = array.slice(); - // remove 0's from array - newArray = removeZeros(newArray); - // reverse array if direction is negative - if (direction === 'negative') { - newArray.reverse(); - } - // combine anything that needs combining - if (newArray.length > 1) { - for ( a = newArray.length, j = 0; a > 0; j++, a--) { - if (newArray[j] === newArray[(j+1)]) { - newArray[j] = newArray[j] * 2; - newArray[(j+1)] = 0; - // this.score += newArray[j] - } - } - } - // remove all zeros again - newArray = removeZeros(newArray); - if (newArray.length < 4) { - while (newArray.length < 4) { - newArray.push(0); - } - } - - // reverse again if direction was negative - return direction === 'negative' ? newArray.reverse() : newArray; -}; - -var gameOver = function (gameArray) { - var over = true; - while (over === false) { - - } - for (i = gameArray.length-1; i >= 0; i--) { - if (collision(gameArray[i]) !== gameArray[i]) { - over = false; - break; - } - } - return over; -}; - -var test =[[2, 4, 2, 4], [4, 2, 4, 2], [2, 4, 2, 4], [4, 2, 4, 2]]; -var testOne = [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 8, 9], [10, 11, 12, 13]]; -var testThree = [[1, 2, 9, 10], [3, 4, 11, 12], [5, 6, 13, 14], [7, 8, 15, 16]]; - - -console.log("expect: true"); -console.log(gameOver(test)); -console.log("expect: false"); -console.log(gameOver(testOne)); -console.log("expect: true"); -console.log(gameOver(testThree)); \ No newline at end of file From 77ee506f1a6d9fa533895235078e63c9ac6c9fb1 Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Tue, 2 Feb 2016 15:56:28 -0800 Subject: [PATCH 13/52] html displaying and combining properly --- javascripts/2048.js | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/javascripts/2048.js b/javascripts/2048.js index 65611ba..1229ca1 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -29,7 +29,7 @@ Game.prototype.addTile = function () { tilePlaced = true; newTile = new Tile(row, col); this.board.push(newTile); - var $tileHTML = $('
2
'); + var $tileHTML = $('
2
'); $('#gameboard').append($tileHTML); } } @@ -51,7 +51,13 @@ Game.prototype.moveTile = function(tile, direction) { if (colArray[row].val === colArray[row+1].val) { colArray[row+1].val *= 2; colArray[row+1].row = row; - // delete current value + // change HTML of tile + $("#" + colArray[row+1].tileId).attr("data-row", "r" + row); + $("#" + colArray[row+1].tileId).attr("data-val", colArray[row+1].val); + $("#" + colArray[row+1].tileId).html(colArray[row+1].val); + // delete current html object + $("#" + colArray[row].tileId).remove(); + // delete current value (tile object) colArray.splice(row, 1); // if not combining } else { @@ -106,18 +112,18 @@ Game.prototype.updateGameOver = function(){ $(document).ready(function() { // Any interactive jQuery functionality var game = new Game(); - var t1 = new Tile(1, 0), - t2 = new Tile(3, 0), + var t1 = new Tile(0, 0), + t2 = new Tile(1, 0), t3 = new Tile(2, 0), - t4 = new Tile(4, 0); + t4 = new Tile(3, 0); game.board = [t1, t2, t3, t4]; - var $t1HTML = $('
2
'); + var $t1HTML = $('
2
'); $('#gameboard').append($t1HTML); - var $t2HTML = $('
2
'); + var $t2HTML = $('
2
'); $('#gameboard').append($t2HTML); - var $t3HTML = $('
2
'); + var $t3HTML = $('
2
'); $('#gameboard').append($t3HTML); - var $t4HTML = $('
2
'); + var $t4HTML = $('
2
'); $('#gameboard').append($t4HTML); $('body').keydown(function(event){ From 55b22057507ed306823b6420187fa3a50030a24f Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Tue, 2 Feb 2016 16:32:07 -0800 Subject: [PATCH 14/52] bugfix remove deleted tiles from this.board --- javascripts/2048.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/javascripts/2048.js b/javascripts/2048.js index 1229ca1..826a354 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -42,9 +42,10 @@ Game.prototype.moveTile = function(tile, direction) { var groupedTiles = _.groupBy(this.board, function(tile) { return tile.col; }); - console.log(groupedTiles, Object.keys(groupedTiles)); + console.log(groupedTiles, Object.keys(groupedTiles), this.board); // iterate through each column - Object.keys(groupedTiles).forEach(function(key){ + + var func = function(key){ var colArray = groupedTiles[key]; for (var row = 0; row < colArray.length; row++) { // if combining @@ -55,6 +56,9 @@ Game.prototype.moveTile = function(tile, direction) { $("#" + colArray[row+1].tileId).attr("data-row", "r" + row); $("#" + colArray[row+1].tileId).attr("data-val", colArray[row+1].val); $("#" + colArray[row+1].tileId).html(colArray[row+1].val); + // delete from board + var deleteTileIndex = _.indexOf(this.board, colArray[row]); + this.board.splice(deleteTileIndex, 1); // delete current html object $("#" + colArray[row].tileId).remove(); // delete current value (tile object) @@ -64,8 +68,10 @@ Game.prototype.moveTile = function(tile, direction) { colArray[row].row = row; } } - console.log(colArray); - }); + console.log(this.board); + }; + func = _.bind(func, this); + Object.keys(groupedTiles).forEach(function(key) { return func(key); }); break; case 40: //down From 863cfd323bdbf9b2d66841b3811c0234db79c14a Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Tue, 2 Feb 2016 16:47:17 -0800 Subject: [PATCH 15/52] Big fixed: addTile won't double up on tile spaces --- javascripts/2048.js | 50 ++++++++++++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 14 deletions(-) diff --git a/javascripts/2048.js b/javascripts/2048.js index 826a354..297d81d 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -22,10 +22,13 @@ Game.prototype.addTile = function () { this.board.forEach(function (tile) { currentTiles.push([tile.row, tile.col]); }); + console.log(currentTiles); while (!tilePlaced) { var col = Math.floor(Math.random() * (4 - 0)), row = Math.floor(Math.random() * (4 - 0)); - if (!currentTiles.includes([row, col])) { + var sameTile = _.find(this.board, function(tile) { return tile.col === col && tile.row === row; }); + console.log(sameTile); + if (!sameTile) { tilePlaced = true; newTile = new Tile(row, col); this.board.push(newTile); @@ -118,19 +121,38 @@ Game.prototype.updateGameOver = function(){ $(document).ready(function() { // Any interactive jQuery functionality var game = new Game(); - var t1 = new Tile(0, 0), - t2 = new Tile(1, 0), - t3 = new Tile(2, 0), - t4 = new Tile(3, 0); - game.board = [t1, t2, t3, t4]; - var $t1HTML = $('
2
'); - $('#gameboard').append($t1HTML); - var $t2HTML = $('
2
'); - $('#gameboard').append($t2HTML); - var $t3HTML = $('
2
'); - $('#gameboard').append($t3HTML); - var $t4HTML = $('
2
'); - $('#gameboard').append($t4HTML); + + game.addTile(); + game.addTile(); + game.addTile(); + game.addTile(); + game.addTile(); + game.addTile(); + game.addTile(); + game.addTile(); + game.addTile(); + game.addTile(); + game.addTile(); + game.addTile(); + game.addTile(); + game.addTile(); + game.addTile(); + + // hard coding for moveTile tests + // + // var t1 = new Tile(0, 0), + // t2 = new Tile(1, 0), + // t3 = new Tile(2, 0), + // t4 = new Tile(3, 0); + // game.board = [t1, t2, t3, t4]; + // var $t1HTML = $('
2
'); + // $('#gameboard').append($t1HTML); + // var $t2HTML = $('
2
'); + // $('#gameboard').append($t2HTML); + // var $t3HTML = $('
2
'); + // $('#gameboard').append($t3HTML); + // var $t4HTML = $('
2
'); + // $('#gameboard').append($t4HTML); $('body').keydown(function(event){ var arrows = [37, 38, 39, 40]; From dde79c58b35b3cb64556291d1f9fc70245bd37b6 Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Tue, 2 Feb 2016 16:52:55 -0800 Subject: [PATCH 16/52] added catch for if a tile doesn't have pair --- javascripts/2048.js | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/javascripts/2048.js b/javascripts/2048.js index 297d81d..1481ce9 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -52,23 +52,25 @@ Game.prototype.moveTile = function(tile, direction) { var colArray = groupedTiles[key]; for (var row = 0; row < colArray.length; row++) { // if combining - if (colArray[row].val === colArray[row+1].val) { - colArray[row+1].val *= 2; - colArray[row+1].row = row; - // change HTML of tile - $("#" + colArray[row+1].tileId).attr("data-row", "r" + row); - $("#" + colArray[row+1].tileId).attr("data-val", colArray[row+1].val); - $("#" + colArray[row+1].tileId).html(colArray[row+1].val); - // delete from board - var deleteTileIndex = _.indexOf(this.board, colArray[row]); - this.board.splice(deleteTileIndex, 1); - // delete current html object - $("#" + colArray[row].tileId).remove(); - // delete current value (tile object) - colArray.splice(row, 1); - // if not combining - } else { - colArray[row].row = row; + if (colArray[row] && colArray[row+1]) { + if (colArray[row].val === colArray[row+1].val) { + colArray[row+1].val *= 2; + colArray[row+1].row = row; + // change HTML of tile + $("#" + colArray[row+1].tileId).attr("data-row", "r" + row); + $("#" + colArray[row+1].tileId).attr("data-val", colArray[row+1].val); + $("#" + colArray[row+1].tileId).html(colArray[row+1].val); + // delete from board + var deleteTileIndex = _.indexOf(this.board, colArray[row]); + this.board.splice(deleteTileIndex, 1); + // delete current html object + $("#" + colArray[row].tileId).remove(); + // delete current value (tile object) + colArray.splice(row, 1); + // if not combining + } else { + colArray[row].row = row; + } } } console.log(this.board); From aac978893f757d89708ee4a3f83d78fe9979689a Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Tue, 2 Feb 2016 16:55:57 -0800 Subject: [PATCH 17/52] DRYed bug fix, still not working entirely --- javascripts/2048.js | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/javascripts/2048.js b/javascripts/2048.js index 1481ce9..0e4de40 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -52,25 +52,23 @@ Game.prototype.moveTile = function(tile, direction) { var colArray = groupedTiles[key]; for (var row = 0; row < colArray.length; row++) { // if combining - if (colArray[row] && colArray[row+1]) { - if (colArray[row].val === colArray[row+1].val) { - colArray[row+1].val *= 2; - colArray[row+1].row = row; - // change HTML of tile - $("#" + colArray[row+1].tileId).attr("data-row", "r" + row); - $("#" + colArray[row+1].tileId).attr("data-val", colArray[row+1].val); - $("#" + colArray[row+1].tileId).html(colArray[row+1].val); - // delete from board - var deleteTileIndex = _.indexOf(this.board, colArray[row]); - this.board.splice(deleteTileIndex, 1); - // delete current html object - $("#" + colArray[row].tileId).remove(); - // delete current value (tile object) - colArray.splice(row, 1); - // if not combining - } else { - colArray[row].row = row; - } + if (colArray[row] && colArray[row+1] && colArray[row].val === colArray[row+1].val) { + colArray[row+1].val *= 2; + colArray[row+1].row = row; + // change HTML of tile + $("#" + colArray[row+1].tileId).attr("data-row", "r" + row); + $("#" + colArray[row+1].tileId).attr("data-val", colArray[row+1].val); + $("#" + colArray[row+1].tileId).html(colArray[row+1].val); + // delete from board + var deleteTileIndex = _.indexOf(this.board, colArray[row]); + this.board.splice(deleteTileIndex, 1); + // delete current html object + $("#" + colArray[row].tileId).remove(); + // delete current value (tile object) + colArray.splice(row, 1); + // if not combining + } else { + colArray[row].row = row; } } console.log(this.board); From adbaf5cf9a060f7851a79d6e9b9ff822b5facc78 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Tue, 2 Feb 2016 16:57:51 -0800 Subject: [PATCH 18/52] delete extraneous comments and consoles --- javascripts/2048.js | 34 ---------------------------------- 1 file changed, 34 deletions(-) diff --git a/javascripts/2048.js b/javascripts/2048.js index 0e4de40..5f0cd57 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -22,12 +22,10 @@ Game.prototype.addTile = function () { this.board.forEach(function (tile) { currentTiles.push([tile.row, tile.col]); }); - console.log(currentTiles); while (!tilePlaced) { var col = Math.floor(Math.random() * (4 - 0)), row = Math.floor(Math.random() * (4 - 0)); var sameTile = _.find(this.board, function(tile) { return tile.col === col && tile.row === row; }); - console.log(sameTile); if (!sameTile) { tilePlaced = true; newTile = new Tile(row, col); @@ -45,9 +43,7 @@ Game.prototype.moveTile = function(tile, direction) { var groupedTiles = _.groupBy(this.board, function(tile) { return tile.col; }); - console.log(groupedTiles, Object.keys(groupedTiles), this.board); // iterate through each column - var func = function(key){ var colArray = groupedTiles[key]; for (var row = 0; row < colArray.length; row++) { @@ -71,7 +67,6 @@ Game.prototype.moveTile = function(tile, direction) { colArray[row].row = row; } } - console.log(this.board); }; func = _.bind(func, this); Object.keys(groupedTiles).forEach(function(key) { return func(key); }); @@ -90,32 +85,6 @@ Game.prototype.moveTile = function(tile, direction) { }; Game.prototype.updateGameOver = function(){ - // has free spaces - var noZeroes = true, - pairFound = false; - this.board.forEach(function(row) { - noZeroes = noZeroes && row.every(function(val) { - return val !== 0; - }); - }); - // if there are no zeroes, the game is not over - if (!noZeroes) { - return; - } else { - var i = 0; - while (!pairFound && i < 4) { - var j = 0; - while (!pairFound && j < 4) { - if (this.board[i][j] == this.board[i][j+1] || - (this.board[i+1] && this.board[i][j] == this.board[i+1][j])) { - pairFound = true; - } - j++; - } - i++; - } - if (!pairFound) { this.gameOver = true; } - } }; $(document).ready(function() { @@ -158,9 +127,6 @@ $(document).ready(function() { var arrows = [37, 38, 39, 40]; if (arrows.indexOf(event.which) > -1) { var tile = $('.tile'); - // console.log(tile); - // $(tile).attr('data-row', 'r0'); - game.moveTile(tile, event.which); } From 144644a3f969faf659ae3d173997c408ec91ee2a Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Tue, 2 Feb 2016 17:05:58 -0800 Subject: [PATCH 19/52] bugfix moveTile switching around --- javascripts/2048.js | 1 + 1 file changed, 1 insertion(+) diff --git a/javascripts/2048.js b/javascripts/2048.js index 5f0cd57..dc6fccf 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -46,6 +46,7 @@ Game.prototype.moveTile = function(tile, direction) { // iterate through each column var func = function(key){ var colArray = groupedTiles[key]; + colArray = _.sortBy(colArray, function(tile){ return tile.row; }); for (var row = 0; row < colArray.length; row++) { // if combining if (colArray[row] && colArray[row+1] && colArray[row].val === colArray[row+1].val) { From 9bcaa8c9d871e14b998396f2197ae0a4a859c537 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Tue, 2 Feb 2016 17:15:45 -0800 Subject: [PATCH 20/52] bugfix update data-row attr for not combined tiles in moveTile --- javascripts/2048.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/javascripts/2048.js b/javascripts/2048.js index dc6fccf..9291f34 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -66,6 +66,7 @@ Game.prototype.moveTile = function(tile, direction) { // if not combining } else { colArray[row].row = row; + $("#" + colArray[row].tileId).attr("data-row", "r" + row); } } }; @@ -106,7 +107,7 @@ $(document).ready(function() { game.addTile(); game.addTile(); game.addTile(); - game.addTile(); + // game.addTile(); // hard coding for moveTile tests // From df3f5fd123787c13f28e0994cfec50aead6af0b4 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Wed, 3 Feb 2016 10:50:02 -0800 Subject: [PATCH 21/52] complete down arrow functionality in makeMove --- javascripts/2048.js | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/javascripts/2048.js b/javascripts/2048.js index 9291f34..ba5e1af 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -75,7 +75,40 @@ Game.prototype.moveTile = function(tile, direction) { break; case 40: //down - console.log('down'); + groupedTiles = _.groupBy(this.board, function(tile) { + return tile.col; + }); + // iterate through each column + func = function(key){ + var colArray = groupedTiles[key]; + colArray = _.sortBy(colArray, function(tile){ return tile.row; }); + for (var i = 0; i < colArray.length; i++) { + // if combining + var arrayIndex = colArray.length - i - 1, + row = 3 - i; + if (colArray[arrayIndex] && colArray[arrayIndex-1] && colArray[arrayIndex].val === colArray[arrayIndex-1].val) { + colArray[arrayIndex-1].val *= 2; + colArray[arrayIndex-1].row = row; + // change HTML of tile + $("#" + colArray[arrayIndex-1].tileId).attr("data-row", "r" + row); + $("#" + colArray[arrayIndex-1].tileId).attr("data-val", colArray[arrayIndex-1].val); + $("#" + colArray[arrayIndex-1].tileId).html(colArray[arrayIndex-1].val); + // delete from board + var deleteTileIndex = _.indexOf(this.board, colArray[arrayIndex]); + this.board.splice(deleteTileIndex, 1); + // delete current html object + $("#" + colArray[arrayIndex].tileId).remove(); + // delete current value (tile object) + colArray.splice(arrayIndex, 1); + // if not combining + } else { + colArray[arrayIndex].row = row; + $("#" + colArray[arrayIndex].tileId).attr("data-row", "r" + row); + } + } + }; + func = _.bind(func, this); + Object.keys(groupedTiles).forEach(function(key) { return func(key); }); break; case 37: //left console.log('left'); From c322cd542928b300538f9c35ae7d45aa609925cf Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Wed, 3 Feb 2016 10:58:26 -0800 Subject: [PATCH 22/52] left arrow works --- javascripts/2048.js | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/javascripts/2048.js b/javascripts/2048.js index ba5e1af..ebc2ac2 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -110,9 +110,43 @@ Game.prototype.moveTile = function(tile, direction) { func = _.bind(func, this); Object.keys(groupedTiles).forEach(function(key) { return func(key); }); break; + case 37: //left - console.log('left'); + // console.log('left'); + var groupedTiles = _.groupBy(this.board, function(tile) { + return tile.row; + }); + // iterate through each row + var func = function(key){ + var rowArray = groupedTiles[key]; + rowArray = _.sortBy(rowArray, function(tile){ return tile.col; }); + for (var col = 0; col < rowArray.length; col++) { + // if combining + if (rowArray[col] && rowArray[col+1] && rowArray[col].val === rowArray[col+1].val) { + rowArray[col+1].val *= 2; + rowArray[col+1].col = col; + // change HTML of tile + $("#" + rowArray[col+1].tileId).attr("data-col", "c" + col); + $("#" + rowArray[col+1].tileId).attr("data-val", rowArray[col+1].val); + $("#" + rowArray[col+1].tileId).html(rowArray[col+1].val); + // delete from board + var deleteTileIndex = _.indexOf(this.board, rowArray[col]); + this.board.splice(deleteTileIndex, 1); + // delete current html object + $("#" + rowArray[col].tileId).remove(); + // delete current tile object) + rowArray.splice(col, 1); + // if not combining + } else { + rowArray[col].col = col; + $("#" + rowArray[col].tileId).attr("data-col", "c" + col); + } + } + }; + func = _.bind(func, this); + Object.keys(groupedTiles).forEach(function(key) { return func(key); }); break; + case 39: //right console.log('right'); break; From 68613c8df4e26268055ef15d2d9985f63ed760e1 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Wed, 3 Feb 2016 11:03:07 -0800 Subject: [PATCH 23/52] add right functionality to makeMove function --- javascripts/2048.js | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/javascripts/2048.js b/javascripts/2048.js index ebc2ac2..38fe7bd 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -148,7 +148,40 @@ Game.prototype.moveTile = function(tile, direction) { break; case 39: //right - console.log('right'); + groupedTiles = _.groupBy(this.board, function(tile) { + return tile.row; + }); + // iterate through each column + func = function(key){ + var rowArray = groupedTiles[key]; + rowArray = _.sortBy(rowArray, function(tile){ return tile.col; }); + for (var i = 0; i < rowArray.length; i++) { + // if combining + var arrayIndex = rowArray.length - i - 1, + col = 3 - i; + if (rowArray[arrayIndex] && rowArray[arrayIndex-1] && rowArray[arrayIndex].val === rowArray[arrayIndex-1].val) { + rowArray[arrayIndex-1].val *= 2; + rowArray[arrayIndex-1].col = col; + // change HTML of tile + $("#" + rowArray[arrayIndex-1].tileId).attr("data-col", "c" + col); + $("#" + rowArray[arrayIndex-1].tileId).attr("data-val", rowArray[arrayIndex-1].val); + $("#" + rowArray[arrayIndex-1].tileId).html(rowArray[arrayIndex-1].val); + // delete from board + var deleteTileIndex = _.indexOf(this.board, rowArray[arrayIndex]); + this.board.splice(deleteTileIndex, 1); + // delete current html object + $("#" + rowArray[arrayIndex].tileId).remove(); + // delete current value (tile object) + rowArray.splice(arrayIndex, 1); + // if not combining + } else { + rowArray[arrayIndex].col = col; + $("#" + rowArray[arrayIndex].tileId).attr("data-col", "c" + col); + } + } + }; + func = _.bind(func, this); + Object.keys(groupedTiles).forEach(function(key) { return func(key); }); break; } }; From 3afa0cb8633a345581aefad66b4a4b79def2772d Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Wed, 3 Feb 2016 11:26:08 -0800 Subject: [PATCH 24/52] only add tile when move is made for up arrow --- javascripts/2048.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/javascripts/2048.js b/javascripts/2048.js index 38fe7bd..3dd8416 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -12,6 +12,7 @@ var Tile = function (row, col) { this.col = col; // update so it can also be 4 this.val = 2; + this.moveCount = 0; this.tileId = String(tileCount++); }; @@ -42,7 +43,8 @@ Game.prototype.moveTile = function(tile, direction) { case 38: //up var groupedTiles = _.groupBy(this.board, function(tile) { return tile.col; - }); + }), + initMoves = this.board.map(function(tile) {return tile.moveCount;}); // iterate through each column var func = function(key){ var colArray = groupedTiles[key]; @@ -52,6 +54,7 @@ Game.prototype.moveTile = function(tile, direction) { if (colArray[row] && colArray[row+1] && colArray[row].val === colArray[row+1].val) { colArray[row+1].val *= 2; colArray[row+1].row = row; + colArray[row+1].moveCount++; // change HTML of tile $("#" + colArray[row+1].tileId).attr("data-row", "r" + row); $("#" + colArray[row+1].tileId).attr("data-val", colArray[row+1].val); @@ -64,14 +67,19 @@ Game.prototype.moveTile = function(tile, direction) { // delete current value (tile object) colArray.splice(row, 1); // if not combining - } else { + } else if (colArray[row].row !== row){ colArray[row].row = row; $("#" + colArray[row].tileId).attr("data-row", "r" + row); + colArray[row].moveCount++; } } }; func = _.bind(func, this); Object.keys(groupedTiles).forEach(function(key) { return func(key); }); + var afterMoves = this.board.map(function(tile) {return tile.moveCount;}), + matching = afterMoves.every(function(element, index) { return initMoves[index] === element; }); + console.log(matching, initMoves, afterMoves); + if (!matching) { this.addTile(); } break; case 40: //down @@ -113,11 +121,11 @@ Game.prototype.moveTile = function(tile, direction) { case 37: //left // console.log('left'); - var groupedTiles = _.groupBy(this.board, function(tile) { + groupedTiles = _.groupBy(this.board, function(tile) { return tile.row; }); // iterate through each row - var func = function(key){ + func = function(key){ var rowArray = groupedTiles[key]; rowArray = _.sortBy(rowArray, function(tile){ return tile.col; }); for (var col = 0; col < rowArray.length; col++) { From 7c1fe6ec94524261f13f1c232a5aef5256d88fcc Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Wed, 3 Feb 2016 13:40:44 -0800 Subject: [PATCH 25/52] removed extraneous code --- javascripts/2048.js | 25 ------------------------- 1 file changed, 25 deletions(-) diff --git a/javascripts/2048.js b/javascripts/2048.js index 3dd8416..addc197 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -207,31 +207,6 @@ $(document).ready(function() { game.addTile(); game.addTile(); game.addTile(); - game.addTile(); - game.addTile(); - game.addTile(); - game.addTile(); - game.addTile(); - game.addTile(); - game.addTile(); - game.addTile(); - // game.addTile(); - - // hard coding for moveTile tests - // - // var t1 = new Tile(0, 0), - // t2 = new Tile(1, 0), - // t3 = new Tile(2, 0), - // t4 = new Tile(3, 0); - // game.board = [t1, t2, t3, t4]; - // var $t1HTML = $('
2
'); - // $('#gameboard').append($t1HTML); - // var $t2HTML = $('
2
'); - // $('#gameboard').append($t2HTML); - // var $t3HTML = $('
2
'); - // $('#gameboard').append($t3HTML); - // var $t4HTML = $('
2
'); - // $('#gameboard').append($t4HTML); $('body').keydown(function(event){ var arrows = [37, 38, 39, 40]; From bc4c88746839aa146cf8b2a3c4a78f3cbf4bcaac Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Wed, 3 Feb 2016 14:00:47 -0800 Subject: [PATCH 26/52] added moveCount functionality to right, left, and down --- javascripts/2048.js | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/javascripts/2048.js b/javascripts/2048.js index addc197..43e53a0 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -79,13 +79,18 @@ Game.prototype.moveTile = function(tile, direction) { var afterMoves = this.board.map(function(tile) {return tile.moveCount;}), matching = afterMoves.every(function(element, index) { return initMoves[index] === element; }); console.log(matching, initMoves, afterMoves); - if (!matching) { this.addTile(); } + + if (!matching) { + game.addTile(); + } break; + case 40: //down groupedTiles = _.groupBy(this.board, function(tile) { return tile.col; }); + initMoves = this.board.map(function(tile) {return tile.moveCount;}); // iterate through each column func = function(key){ var colArray = groupedTiles[key]; @@ -97,6 +102,7 @@ Game.prototype.moveTile = function(tile, direction) { if (colArray[arrayIndex] && colArray[arrayIndex-1] && colArray[arrayIndex].val === colArray[arrayIndex-1].val) { colArray[arrayIndex-1].val *= 2; colArray[arrayIndex-1].row = row; + colArray[arrayIndex-1].moveCount++; // change HTML of tile $("#" + colArray[arrayIndex-1].tileId).attr("data-row", "r" + row); $("#" + colArray[arrayIndex-1].tileId).attr("data-val", colArray[arrayIndex-1].val); @@ -111,12 +117,19 @@ Game.prototype.moveTile = function(tile, direction) { // if not combining } else { colArray[arrayIndex].row = row; + colArray[arrayIndex].moveCount++; $("#" + colArray[arrayIndex].tileId).attr("data-row", "r" + row); } } }; + func = _.bind(func, this); Object.keys(groupedTiles).forEach(function(key) { return func(key); }); + afterMoves = this.board.map(function(tile) {return tile.moveCount;}); + matching = afterMoves.every(function(element, index) { return initMoves[index] === element; }); + console.log(matching, initMoves, afterMoves); + if (!matching) { this.addTile(); } + break; case 37: //left @@ -124,6 +137,7 @@ Game.prototype.moveTile = function(tile, direction) { groupedTiles = _.groupBy(this.board, function(tile) { return tile.row; }); + initMoves = this.board.map(function(tile) {return tile.moveCount;}); // iterate through each row func = function(key){ var rowArray = groupedTiles[key]; @@ -133,6 +147,7 @@ Game.prototype.moveTile = function(tile, direction) { if (rowArray[col] && rowArray[col+1] && rowArray[col].val === rowArray[col+1].val) { rowArray[col+1].val *= 2; rowArray[col+1].col = col; + rowArray[col+1].moveCount++; // change HTML of tile $("#" + rowArray[col+1].tileId).attr("data-col", "c" + col); $("#" + rowArray[col+1].tileId).attr("data-val", rowArray[col+1].val); @@ -147,18 +162,24 @@ Game.prototype.moveTile = function(tile, direction) { // if not combining } else { rowArray[col].col = col; + rowArray[col].moveCount++; $("#" + rowArray[col].tileId).attr("data-col", "c" + col); } } }; func = _.bind(func, this); Object.keys(groupedTiles).forEach(function(key) { return func(key); }); + afterMoves = this.board.map(function(tile) {return tile.moveCount;}); + matching = afterMoves.every(function(element, index) { return initMoves[index] === element; }); + console.log(matching, initMoves, afterMoves); + if (!matching) { this.addTile(); } break; case 39: //right groupedTiles = _.groupBy(this.board, function(tile) { return tile.row; }); + initMoves = this.board.map(function(tile) {return tile.moveCount;}); // iterate through each column func = function(key){ var rowArray = groupedTiles[key]; @@ -170,6 +191,7 @@ Game.prototype.moveTile = function(tile, direction) { if (rowArray[arrayIndex] && rowArray[arrayIndex-1] && rowArray[arrayIndex].val === rowArray[arrayIndex-1].val) { rowArray[arrayIndex-1].val *= 2; rowArray[arrayIndex-1].col = col; + rowArray[arrayIndex-1].moveCount++; // change HTML of tile $("#" + rowArray[arrayIndex-1].tileId).attr("data-col", "c" + col); $("#" + rowArray[arrayIndex-1].tileId).attr("data-val", rowArray[arrayIndex-1].val); @@ -184,12 +206,19 @@ Game.prototype.moveTile = function(tile, direction) { // if not combining } else { rowArray[arrayIndex].col = col; + rowArray[arrayIndex].moveCount++; $("#" + rowArray[arrayIndex].tileId).attr("data-col", "c" + col); } } }; + func = _.bind(func, this); Object.keys(groupedTiles).forEach(function(key) { return func(key); }); + afterMoves = this.board.map(function(tile) {return tile.moveCount;}); + matching = afterMoves.every(function(element, index) { return initMoves[index] === element; }); + console.log(matching, initMoves, afterMoves); + if (!matching) { this.addTile(); } + break; } }; From 39f31c801f5ed967f4de5b9dc140573b0f2ec6a8 Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Wed, 3 Feb 2016 14:13:38 -0800 Subject: [PATCH 27/52] delayed addTiles for each direction --- javascripts/2048.js | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/javascripts/2048.js b/javascripts/2048.js index 43e53a0..19aeff8 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -38,6 +38,7 @@ Game.prototype.addTile = function () { }; Game.prototype.moveTile = function(tile, direction) { + var addTileCallback = function () { this.addTile(); }.bind(this) ; // Game method here switch(direction) { case 38: //up @@ -79,11 +80,11 @@ Game.prototype.moveTile = function(tile, direction) { var afterMoves = this.board.map(function(tile) {return tile.moveCount;}), matching = afterMoves.every(function(element, index) { return initMoves[index] === element; }); console.log(matching, initMoves, afterMoves); - + if (!matching) { - game.addTile(); + setTimeout(addTileCallback, 200); } - + break; case 40: //down @@ -128,8 +129,11 @@ Game.prototype.moveTile = function(tile, direction) { afterMoves = this.board.map(function(tile) {return tile.moveCount;}); matching = afterMoves.every(function(element, index) { return initMoves[index] === element; }); console.log(matching, initMoves, afterMoves); - if (!matching) { this.addTile(); } + if (!matching) { + setTimeout(addTileCallback, 200); + } + break; case 37: //left @@ -172,8 +176,10 @@ Game.prototype.moveTile = function(tile, direction) { afterMoves = this.board.map(function(tile) {return tile.moveCount;}); matching = afterMoves.every(function(element, index) { return initMoves[index] === element; }); console.log(matching, initMoves, afterMoves); - if (!matching) { this.addTile(); } - break; + + if (!matching) { + setTimeout(addTileCallback, 200); + } break; case 39: //right groupedTiles = _.groupBy(this.board, function(tile) { @@ -217,8 +223,10 @@ Game.prototype.moveTile = function(tile, direction) { afterMoves = this.board.map(function(tile) {return tile.moveCount;}); matching = afterMoves.every(function(element, index) { return initMoves[index] === element; }); console.log(matching, initMoves, afterMoves); - if (!matching) { this.addTile(); } + if (!matching) { + setTimeout(addTileCallback, 200); + } break; } }; From 265ec5cb2630c64e49ea2d6bceebc4726d28defd Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Wed, 3 Feb 2016 14:15:45 -0800 Subject: [PATCH 28/52] remove extra console logs --- javascripts/2048.js | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/javascripts/2048.js b/javascripts/2048.js index 19aeff8..1cc5fe7 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -79,12 +79,9 @@ Game.prototype.moveTile = function(tile, direction) { Object.keys(groupedTiles).forEach(function(key) { return func(key); }); var afterMoves = this.board.map(function(tile) {return tile.moveCount;}), matching = afterMoves.every(function(element, index) { return initMoves[index] === element; }); - console.log(matching, initMoves, afterMoves); - if (!matching) { setTimeout(addTileCallback, 200); } - break; case 40: //down @@ -123,21 +120,16 @@ Game.prototype.moveTile = function(tile, direction) { } } }; - func = _.bind(func, this); Object.keys(groupedTiles).forEach(function(key) { return func(key); }); afterMoves = this.board.map(function(tile) {return tile.moveCount;}); matching = afterMoves.every(function(element, index) { return initMoves[index] === element; }); - console.log(matching, initMoves, afterMoves); - if (!matching) { setTimeout(addTileCallback, 200); } - break; case 37: //left - // console.log('left'); groupedTiles = _.groupBy(this.board, function(tile) { return tile.row; }); @@ -175,8 +167,6 @@ Game.prototype.moveTile = function(tile, direction) { Object.keys(groupedTiles).forEach(function(key) { return func(key); }); afterMoves = this.board.map(function(tile) {return tile.moveCount;}); matching = afterMoves.every(function(element, index) { return initMoves[index] === element; }); - console.log(matching, initMoves, afterMoves); - if (!matching) { setTimeout(addTileCallback, 200); } break; @@ -222,8 +212,6 @@ Game.prototype.moveTile = function(tile, direction) { Object.keys(groupedTiles).forEach(function(key) { return func(key); }); afterMoves = this.board.map(function(tile) {return tile.moveCount;}); matching = afterMoves.every(function(element, index) { return initMoves[index] === element; }); - console.log(matching, initMoves, afterMoves); - if (!matching) { setTimeout(addTileCallback, 200); } From fd0e5825220031b8ea47523a9c086d9378b1477a Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Wed, 3 Feb 2016 14:24:27 -0800 Subject: [PATCH 29/52] refactored moveTile for DRYer code --- javascripts/2048.js | 45 +++++++++++---------------------------------- 1 file changed, 11 insertions(+), 34 deletions(-) diff --git a/javascripts/2048.js b/javascripts/2048.js index 1cc5fe7..bf37cab 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -40,12 +40,12 @@ Game.prototype.addTile = function () { Game.prototype.moveTile = function(tile, direction) { var addTileCallback = function () { this.addTile(); }.bind(this) ; // Game method here + var initMoves = this.board.map(function(tile) {return tile.moveCount;}); switch(direction) { case 38: //up var groupedTiles = _.groupBy(this.board, function(tile) { return tile.col; - }), - initMoves = this.board.map(function(tile) {return tile.moveCount;}); + }); // iterate through each column var func = function(key){ var colArray = groupedTiles[key]; @@ -75,20 +75,12 @@ Game.prototype.moveTile = function(tile, direction) { } } }; - func = _.bind(func, this); - Object.keys(groupedTiles).forEach(function(key) { return func(key); }); - var afterMoves = this.board.map(function(tile) {return tile.moveCount;}), - matching = afterMoves.every(function(element, index) { return initMoves[index] === element; }); - if (!matching) { - setTimeout(addTileCallback, 200); - } break; case 40: //down groupedTiles = _.groupBy(this.board, function(tile) { return tile.col; }); - initMoves = this.board.map(function(tile) {return tile.moveCount;}); // iterate through each column func = function(key){ var colArray = groupedTiles[key]; @@ -120,20 +112,12 @@ Game.prototype.moveTile = function(tile, direction) { } } }; - func = _.bind(func, this); - Object.keys(groupedTiles).forEach(function(key) { return func(key); }); - afterMoves = this.board.map(function(tile) {return tile.moveCount;}); - matching = afterMoves.every(function(element, index) { return initMoves[index] === element; }); - if (!matching) { - setTimeout(addTileCallback, 200); - } break; case 37: //left groupedTiles = _.groupBy(this.board, function(tile) { return tile.row; }); - initMoves = this.board.map(function(tile) {return tile.moveCount;}); // iterate through each row func = function(key){ var rowArray = groupedTiles[key]; @@ -163,19 +147,12 @@ Game.prototype.moveTile = function(tile, direction) { } } }; - func = _.bind(func, this); - Object.keys(groupedTiles).forEach(function(key) { return func(key); }); - afterMoves = this.board.map(function(tile) {return tile.moveCount;}); - matching = afterMoves.every(function(element, index) { return initMoves[index] === element; }); - if (!matching) { - setTimeout(addTileCallback, 200); - } break; + break; case 39: //right groupedTiles = _.groupBy(this.board, function(tile) { return tile.row; }); - initMoves = this.board.map(function(tile) {return tile.moveCount;}); // iterate through each column func = function(key){ var rowArray = groupedTiles[key]; @@ -207,16 +184,16 @@ Game.prototype.moveTile = function(tile, direction) { } } }; - - func = _.bind(func, this); - Object.keys(groupedTiles).forEach(function(key) { return func(key); }); - afterMoves = this.board.map(function(tile) {return tile.moveCount;}); - matching = afterMoves.every(function(element, index) { return initMoves[index] === element; }); - if (!matching) { - setTimeout(addTileCallback, 200); - } break; } + + func = _.bind(func, this); + Object.keys(groupedTiles).forEach(function(key) { return func(key); }); + afterMoves = this.board.map(function(tile) {return tile.moveCount;}); + matching = afterMoves.every(function(element, index) { return initMoves[index] === element; }); + if (!matching) { + setTimeout(addTileCallback, 200); + } }; Game.prototype.updateGameOver = function(){ From 7a85cb53844c6302ea3669e2cbeb0d44c92b9cf7 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Wed, 3 Feb 2016 14:33:30 -0800 Subject: [PATCH 30/52] add score functionality --- index.html | 1 + javascripts/2048.js | 12 ++++++------ 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/index.html b/index.html index 7bce92d..e8e2e2a 100644 --- a/index.html +++ b/index.html @@ -8,6 +8,7 @@ +

Score: 0

diff --git a/javascripts/2048.js b/javascripts/2048.js index bf37cab..c0cb773 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -2,6 +2,7 @@ var Game = function() { // Game logic and initialization here this.gameOver = false; this.board = []; + this.score = 0; }; var tileCount = 0; @@ -54,6 +55,7 @@ Game.prototype.moveTile = function(tile, direction) { // if combining if (colArray[row] && colArray[row+1] && colArray[row].val === colArray[row+1].val) { colArray[row+1].val *= 2; + this.score += colArray[row+1].val; colArray[row+1].row = row; colArray[row+1].moveCount++; // change HTML of tile @@ -91,6 +93,7 @@ Game.prototype.moveTile = function(tile, direction) { row = 3 - i; if (colArray[arrayIndex] && colArray[arrayIndex-1] && colArray[arrayIndex].val === colArray[arrayIndex-1].val) { colArray[arrayIndex-1].val *= 2; + this.score += colArray[arrayIndex-1].val; colArray[arrayIndex-1].row = row; colArray[arrayIndex-1].moveCount++; // change HTML of tile @@ -126,6 +129,7 @@ Game.prototype.moveTile = function(tile, direction) { // if combining if (rowArray[col] && rowArray[col+1] && rowArray[col].val === rowArray[col+1].val) { rowArray[col+1].val *= 2; + this.score += rowArray[col+1].val; rowArray[col+1].col = col; rowArray[col+1].moveCount++; // change HTML of tile @@ -163,6 +167,7 @@ Game.prototype.moveTile = function(tile, direction) { col = 3 - i; if (rowArray[arrayIndex] && rowArray[arrayIndex-1] && rowArray[arrayIndex].val === rowArray[arrayIndex-1].val) { rowArray[arrayIndex-1].val *= 2; + this.score += rowArray[arrayIndex-1].val; rowArray[arrayIndex-1].col = col; rowArray[arrayIndex-1].moveCount++; // change HTML of tile @@ -202,19 +207,14 @@ Game.prototype.updateGameOver = function(){ $(document).ready(function() { // Any interactive jQuery functionality var game = new Game(); - - game.addTile(); - game.addTile(); game.addTile(); game.addTile(); - game.addTile(); - game.addTile(); - $('body').keydown(function(event){ var arrows = [37, 38, 39, 40]; if (arrows.indexOf(event.which) > -1) { var tile = $('.tile'); game.moveTile(tile, event.which); + $('#score').html('Score: ' + game.score); } }); From 8293274f961a1375bd570525c7c36f3eb0fb3121 Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Wed, 3 Feb 2016 14:47:09 -0800 Subject: [PATCH 31/52] game reset functional, yussss --- index.html | 3 ++- javascripts/2048.js | 22 +++++++++++++++++----- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/index.html b/index.html index e8e2e2a..d279f29 100644 --- a/index.html +++ b/index.html @@ -8,7 +8,8 @@ -

Score: 0

+

+
diff --git a/javascripts/2048.js b/javascripts/2048.js index c0cb773..fc1fd3a 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -1,8 +1,6 @@ var Game = function() { // Game logic and initialization here - this.gameOver = false; - this.board = []; - this.score = 0; + this.highScore = 0; }; var tileCount = 0; @@ -17,6 +15,16 @@ var Tile = function (row, col) { this.tileId = String(tileCount++); }; +Game.prototype.startGame = function () { + $('.tile').remove(); + this.gameOver = false; + this.board = []; + this.score = 0; + $('#score').html('Score: '+ this.score); + this.addTile(); + this.addTile(); +}; + Game.prototype.addTile = function () { var tilePlaced = false; @@ -207,8 +215,8 @@ Game.prototype.updateGameOver = function(){ $(document).ready(function() { // Any interactive jQuery functionality var game = new Game(); - game.addTile(); - game.addTile(); + game.startGame(); + $('body').keydown(function(event){ var arrows = [37, 38, 39, 40]; if (arrows.indexOf(event.which) > -1) { @@ -217,5 +225,9 @@ $(document).ready(function() { $('#score').html('Score: ' + game.score); } + $('#reset').click(function () { + game.startGame(); + }); + }); }); From ae99748f5103c1c8169382cc4f368d20421d8b07 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Wed, 3 Feb 2016 15:27:19 -0800 Subject: [PATCH 32/52] add game won functionality --- javascripts/2048.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/javascripts/2048.js b/javascripts/2048.js index fc1fd3a..7bc88a9 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -18,6 +18,7 @@ var Tile = function (row, col) { Game.prototype.startGame = function () { $('.tile').remove(); this.gameOver = false; + this.gameWon = false; this.board = []; this.score = 0; $('#score').html('Score: '+ this.score); @@ -25,6 +26,11 @@ Game.prototype.startGame = function () { this.addTile(); }; +Game.prototype.updateGameWon = function(){ + var values = this.board.map(function(tile){ return tile.val; }); + if (values.includes(2048)) { this.gameWon = true; } +}; + Game.prototype.addTile = function () { var tilePlaced = false; @@ -207,6 +213,10 @@ Game.prototype.moveTile = function(tile, direction) { if (!matching) { setTimeout(addTileCallback, 200); } + if (this.gameWon === false) { + this.updateGameWon(); + alert('Game won!'); + } }; Game.prototype.updateGameOver = function(){ @@ -216,7 +226,6 @@ $(document).ready(function() { // Any interactive jQuery functionality var game = new Game(); game.startGame(); - $('body').keydown(function(event){ var arrows = [37, 38, 39, 40]; if (arrows.indexOf(event.which) > -1) { From 1597ec81faf14f1324a97070d3f07efc2806797e Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Thu, 4 Feb 2016 11:12:24 -0800 Subject: [PATCH 33/52] gameOver code in progress --- javascripts/2048.js | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/javascripts/2048.js b/javascripts/2048.js index 7bc88a9..57bbe86 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -31,6 +31,41 @@ Game.prototype.updateGameWon = function(){ if (values.includes(2048)) { this.gameWon = true; } }; +Game.prototype.checkGameOver = function () { + if (this.board.length !== 16) { + return; + } else { + var groupedTiles = _.groupBy(this.board, function(tile) { + return tile.val; + }); + for (var val in groupedTiles) { + var tiles = groupedTiles[val]; + for (var i=0; i < tiles.length; i++) { + var oddCol = tiles[i].col % 2 === 1; + var colMod; + if (oddCol) { + colMod = 1; + } else { + colMod = 0; + } + var oddRow = tiles[i].row % 2 === 1; + var rowMod; + if (oddRow) { + rowMod = 1; + } else { + rowMod = 0; + } + var match = _.find(tiles, function(tile) { + return (tile.row === tiles[i].row && tile.col % 2 === colMod) || (tile.col === tiles[i].col && tile.row % 2 === rowMod); + }); + if (match) { return; } + } + } + this.gameOver = true; + console.log(this.gameOver); + } +}; + Game.prototype.addTile = function () { var tilePlaced = false; @@ -214,8 +249,10 @@ Game.prototype.moveTile = function(tile, direction) { setTimeout(addTileCallback, 200); } if (this.gameWon === false) { - this.updateGameWon(); - alert('Game won!'); + this.updateGameWon(); + if (this.gameWon) { + alert('Game won!'); + } } }; From df6c3390566a60a41c02ac5056e402e5376045be Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Thu, 4 Feb 2016 11:41:11 -0800 Subject: [PATCH 34/52] fix checkGameOver logic --- index.html | 4 ++-- javascripts/2048.js | 51 +++++++++++++++++++-------------------------- 2 files changed, 24 insertions(+), 31 deletions(-) diff --git a/index.html b/index.html index d279f29..5771853 100644 --- a/index.html +++ b/index.html @@ -8,8 +8,8 @@ -

- + +
diff --git a/javascripts/2048.js b/javascripts/2048.js index 57bbe86..e2535b9 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -41,22 +41,11 @@ Game.prototype.checkGameOver = function () { for (var val in groupedTiles) { var tiles = groupedTiles[val]; for (var i=0; i < tiles.length; i++) { - var oddCol = tiles[i].col % 2 === 1; - var colMod; - if (oddCol) { - colMod = 1; - } else { - colMod = 0; - } - var oddRow = tiles[i].row % 2 === 1; - var rowMod; - if (oddRow) { - rowMod = 1; - } else { - rowMod = 0; - } var match = _.find(tiles, function(tile) { - return (tile.row === tiles[i].row && tile.col % 2 === colMod) || (tile.col === tiles[i].col && tile.row % 2 === rowMod); + return (tiles[i].row === tile.row && + (tiles[i].col == tile.col + 1 || tiles[i].col == tile.col - 1)) || + (tiles[i].col === tile.col && + (tiles[i].row == tile.row + 1 || tiles[i].row == tile.row - 1)); }); if (match) { return; } } @@ -68,12 +57,13 @@ Game.prototype.checkGameOver = function () { Game.prototype.addTile = function () { - var tilePlaced = false; - var currentTiles = []; - this.board.forEach(function (tile) { - currentTiles.push([tile.row, tile.col]); - }); - while (!tilePlaced) { + if (this.board.length < 16) { + var tilePlaced = false; + var currentTiles = []; + this.board.forEach(function (tile) { + currentTiles.push([tile.row, tile.col]); + }); + while (!tilePlaced) { var col = Math.floor(Math.random() * (4 - 0)), row = Math.floor(Math.random() * (4 - 0)); var sameTile = _.find(this.board, function(tile) { return tile.col === col && tile.row === row; }); @@ -84,6 +74,7 @@ Game.prototype.addTile = function () { var $tileHTML = $('
2
'); $('#gameboard').append($tileHTML); } + } } }; @@ -254,6 +245,7 @@ Game.prototype.moveTile = function(tile, direction) { alert('Game won!'); } } + this.checkGameOver(); }; Game.prototype.updateGameOver = function(){ @@ -264,16 +256,17 @@ $(document).ready(function() { var game = new Game(); game.startGame(); $('body').keydown(function(event){ - var arrows = [37, 38, 39, 40]; - if (arrows.indexOf(event.which) > -1) { - var tile = $('.tile'); - game.moveTile(tile, event.which); - $('#score').html('Score: ' + game.score); - } - + if (!game.gameOver) { + var arrows = [37, 38, 39, 40]; + if (arrows.indexOf(event.which) > -1) { + var tile = $('.tile'); + game.moveTile(tile, event.which); + $('#score').html('Score: ' + game.score); + } + } + }); $('#reset').click(function () { game.startGame(); }); - }); }); From 4e288d516cf275f18767aaeb39c932f135ee067c Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Thu, 4 Feb 2016 11:58:25 -0800 Subject: [PATCH 35/52] add 4 value to new tiles --- index.html | 2 +- javascripts/2048.js | 15 ++++++++------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/index.html b/index.html index 5771853..17c364c 100644 --- a/index.html +++ b/index.html @@ -9,7 +9,7 @@ - +
diff --git a/javascripts/2048.js b/javascripts/2048.js index e2535b9..75cb927 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -5,12 +5,11 @@ var Game = function() { var tileCount = 0; -var Tile = function (row, col) { +var Tile = function (row, col, val) { // make new Tiles this.row = row; this.col = col; - // update so it can also be 4 - this.val = 2; + this.val = val; this.moveCount = 0; this.tileId = String(tileCount++); }; @@ -57,9 +56,11 @@ Game.prototype.checkGameOver = function () { Game.prototype.addTile = function () { + var valArray = [2, 2, 2, 2, 2, 2, 2, 2, 2, 4]; if (this.board.length < 16) { - var tilePlaced = false; - var currentTiles = []; + var tilePlaced = false, + val = valArray[Math.floor(Math.random() * 10)], + currentTiles = []; this.board.forEach(function (tile) { currentTiles.push([tile.row, tile.col]); }); @@ -69,9 +70,9 @@ Game.prototype.addTile = function () { var sameTile = _.find(this.board, function(tile) { return tile.col === col && tile.row === row; }); if (!sameTile) { tilePlaced = true; - newTile = new Tile(row, col); + newTile = new Tile(row, col, val); this.board.push(newTile); - var $tileHTML = $('
2
'); + var $tileHTML = $('
'+val+'
'); $('#gameboard').append($tileHTML); } } From 079d383d2ae2a42b2478acc380445a3fbd67d0c5 Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Thu, 4 Feb 2016 12:02:14 -0800 Subject: [PATCH 36/52] awsd keys work to play game as well as arrows --- javascripts/2048.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/javascripts/2048.js b/javascripts/2048.js index 57bbe86..d82a98e 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -92,6 +92,7 @@ Game.prototype.moveTile = function(tile, direction) { // Game method here var initMoves = this.board.map(function(tile) {return tile.moveCount;}); switch(direction) { + case 87: case 38: //up var groupedTiles = _.groupBy(this.board, function(tile) { return tile.col; @@ -128,6 +129,7 @@ Game.prototype.moveTile = function(tile, direction) { }; break; + case 83: case 40: //down groupedTiles = _.groupBy(this.board, function(tile) { return tile.col; @@ -166,6 +168,7 @@ Game.prototype.moveTile = function(tile, direction) { }; break; + case 65: case 37: //left groupedTiles = _.groupBy(this.board, function(tile) { return tile.row; @@ -202,6 +205,7 @@ Game.prototype.moveTile = function(tile, direction) { }; break; + case 68: case 39: //right groupedTiles = _.groupBy(this.board, function(tile) { return tile.row; @@ -264,8 +268,8 @@ $(document).ready(function() { var game = new Game(); game.startGame(); $('body').keydown(function(event){ - var arrows = [37, 38, 39, 40]; - if (arrows.indexOf(event.which) > -1) { + var directions = [37, 38, 39, 40, 87, 65, 83, 68]; + if (directions.indexOf(event.which) > -1) { var tile = $('.tile'); game.moveTile(tile, event.which); $('#score').html('Score: ' + game.score); From 4059409bfbd90fde3d9a6041233ce452d2bb4c9f Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Thu, 4 Feb 2016 13:13:08 -0800 Subject: [PATCH 37/52] working on css --- index.html | 11 ++++++++--- stylesheets/2048.css | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/index.html b/index.html index d279f29..aa44ac2 100644 --- a/index.html +++ b/index.html @@ -8,8 +8,14 @@ -

- +
+

2048

+
+

SCORE

+

+
+
+ New Game
@@ -29,7 +35,6 @@

-
diff --git a/stylesheets/2048.css b/stylesheets/2048.css index 2a46a94..afc0ff6 100644 --- a/stylesheets/2048.css +++ b/stylesheets/2048.css @@ -2,6 +2,14 @@ html { font: normal normal 30px/1 "Clear Sans", "Helvetica Neue", Arial, sans-serif; } +body { + background-color: #FAF8EF; +} + +h1 { + color: #776e65; +} + #gameboard { background: #bbada0; border-radius: 0.5rem; @@ -11,6 +19,36 @@ html { width: 18.5rem; } +#reset { + color: #F9F6F2; +} + +.header { + display: block; + margin: 0 auto; + width: 300px; +} + +.score-container { + background-color: #BBADA0; + width: 125px; + padding: 5px; + font-weight: bold; + color: #EEE4DA; +} + +.score-header { + font-size: 14px; + text-align: center; + padding: 0; + margin: 0; +} + +.score { + padding: 5px; + margin-top: 10px; +} + .cell { background: rgba(238, 228, 218, 0.35); border-radius: 0.5rem; From 64055fe7bf82d173a1a463321480ca22793de39b Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Thu, 4 Feb 2016 13:40:36 -0800 Subject: [PATCH 38/52] stupid merge error fixed --- javascripts/2048.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/javascripts/2048.js b/javascripts/2048.js index 5df7ce4..a9d672e 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -261,15 +261,12 @@ $(document).ready(function() { var game = new Game(); game.startGame(); $('body').keydown(function(event){ -<<<<<<< HEAD var directions = [37, 38, 39, 40, 87, 65, 83, 68]; if (directions.indexOf(event.which) > -1) { var tile = $('.tile'); game.moveTile(tile, event.which); $('#score').html('Score: ' + game.score); } - -======= if (!game.gameOver) { var arrows = [37, 38, 39, 40]; if (arrows.indexOf(event.which) > -1) { @@ -279,7 +276,6 @@ $(document).ready(function() { } } }); ->>>>>>> 4e288d516cf275f18767aaeb39c932f135ee067c $('#reset').click(function () { game.startGame(); }); From 153895ac45af2eb7da2ae7fb82b7c9e47432ddf8 Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Thu, 4 Feb 2016 13:47:02 -0800 Subject: [PATCH 39/52] score looking a little better --- index.html | 2 +- javascripts/2048.js | 4 ++-- stylesheets/2048.css | 6 ++++-- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/index.html b/index.html index aa44ac2..5232595 100644 --- a/index.html +++ b/index.html @@ -12,7 +12,7 @@

2048

SCORE

-

+
New Game diff --git a/javascripts/2048.js b/javascripts/2048.js index a9d672e..19c7335 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -20,7 +20,7 @@ Game.prototype.startGame = function () { this.gameWon = false; this.board = []; this.score = 0; - $('#score').html('Score: '+ this.score); + $('#score').html(this.score); this.addTile(); this.addTile(); }; @@ -272,7 +272,7 @@ $(document).ready(function() { if (arrows.indexOf(event.which) > -1) { var tile = $('.tile'); game.moveTile(tile, event.which); - $('#score').html('Score: ' + game.score); + $('#score').html(game.score); } } }); diff --git a/stylesheets/2048.css b/stylesheets/2048.css index afc0ff6..497956e 100644 --- a/stylesheets/2048.css +++ b/stylesheets/2048.css @@ -35,18 +35,20 @@ h1 { padding: 5px; font-weight: bold; color: #EEE4DA; + text-align: center; } .score-header { font-size: 14px; text-align: center; padding: 0; - margin: 0; + margin: 0 auto; } .score { padding: 5px; - margin-top: 10px; + margin-top: 25px; + color: #; } .cell { From 925b5bd2be5bfae281478347e1eea7ff92dc24ca Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Thu, 4 Feb 2016 13:51:16 -0800 Subject: [PATCH 40/52] add high score --- index.html | 2 ++ javascripts/2048.js | 19 +++++++++---------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/index.html b/index.html index aa44ac2..2ca1acd 100644 --- a/index.html +++ b/index.html @@ -13,6 +13,8 @@

2048

SCORE

+

HIGH SCORE

+

New Game diff --git a/javascripts/2048.js b/javascripts/2048.js index a9d672e..8737872 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -251,6 +251,9 @@ Game.prototype.moveTile = function(tile, direction) { } } this.checkGameOver(); + if (this.score > this.highScore) { + this.highScore = this.score; + } }; Game.prototype.updateGameOver = function(){ @@ -258,21 +261,17 @@ Game.prototype.updateGameOver = function(){ $(document).ready(function() { // Any interactive jQuery functionality - var game = new Game(); + var tile, + game = new Game(); game.startGame(); $('body').keydown(function(event){ - var directions = [37, 38, 39, 40, 87, 65, 83, 68]; - if (directions.indexOf(event.which) > -1) { - var tile = $('.tile'); - game.moveTile(tile, event.which); - $('#score').html('Score: ' + game.score); - } if (!game.gameOver) { - var arrows = [37, 38, 39, 40]; - if (arrows.indexOf(event.which) > -1) { - var tile = $('.tile'); + var directions = [37, 38, 39, 40, 87, 65, 83, 68]; + if (directions.indexOf(event.which) > -1) { + tile = $('.tile'); game.moveTile(tile, event.which); $('#score').html('Score: ' + game.score); + $('#high_score').html('Score: ' + game.highScore); } } }); From 7b9031def60a3a5c7b8ba3e573b5525026ae1bde Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Thu, 4 Feb 2016 13:52:26 -0800 Subject: [PATCH 41/52] New Game button visible --- stylesheets/2048.css | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/stylesheets/2048.css b/stylesheets/2048.css index 497956e..ef78f39 100644 --- a/stylesheets/2048.css +++ b/stylesheets/2048.css @@ -20,7 +20,10 @@ h1 { } #reset { - color: #F9F6F2; + color: #FFFFFF; + background-color: #8F7A66; + width: 250px; + padding: 10px; } .header { @@ -31,7 +34,7 @@ h1 { .score-container { background-color: #BBADA0; - width: 125px; + width: 80px; padding: 5px; font-weight: bold; color: #EEE4DA; @@ -48,7 +51,7 @@ h1 { .score { padding: 5px; margin-top: 25px; - color: #; + color: #FFFFFF; } .cell { From a0899318f8cdbea8bdeec8eefa3c6e312a6eb19e Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Thu, 4 Feb 2016 13:55:11 -0800 Subject: [PATCH 42/52] display high score at beginning of game --- javascripts/2048.js | 1 + 1 file changed, 1 insertion(+) diff --git a/javascripts/2048.js b/javascripts/2048.js index 96d816f..aebfbb8 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -21,6 +21,7 @@ Game.prototype.startGame = function () { this.board = []; this.score = 0; $('#score').html(this.score); + $('#high_score').html(this.highScore); this.addTile(); this.addTile(); }; From dab597b8f8317fa0c68d7c8550224809dbaf7ee4 Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Thu, 4 Feb 2016 14:49:11 -0800 Subject: [PATCH 43/52] some CSS is okay i guess ughh --- index.html | 62 +++++++++++++++++++++++--------------------- stylesheets/2048.css | 40 +++++++++++++++++++++++----- 2 files changed, 66 insertions(+), 36 deletions(-) diff --git a/index.html b/index.html index feae6f1..887d311 100644 --- a/index.html +++ b/index.html @@ -8,35 +8,39 @@ -
-

2048

-
-

SCORE

-
-

HIGH SCORE

-
-
+
+
+

2048

+
+

SCORE

+
+
+
+

BEST

+
+
+ New Game +
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
- New Game -
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
diff --git a/stylesheets/2048.css b/stylesheets/2048.css index ef78f39..bd1e8c8 100644 --- a/stylesheets/2048.css +++ b/stylesheets/2048.css @@ -6,8 +6,17 @@ body { background-color: #FAF8EF; } +#container { + width: 600px; + margin: 0 auto; +} + h1 { color: #776e65; + position: relative; + display: inline; + top: 25px; + font-size: 104px; } #gameboard { @@ -20,27 +29,44 @@ h1 { } #reset { + position: relative; color: #FFFFFF; background-color: #8F7A66; width: 250px; padding: 10px; + font-weight: bold; + bottom: 50px; } .header { - display: block; - margin: 0 auto; - width: 300px; -} +/* display: block; +*/ margin: 0 auto; + width: 500px; + margin-bottom: 0px; +/* height: 200px; +*/} -.score-container { +.score-container, .high-score-container { + position: relative; + left: 300px; background-color: #BBADA0; width: 80px; + height: 70px; padding: 5px; font-weight: bold; color: #EEE4DA; text-align: center; } +.score-container { + bottom: 65px; +} + +.high-score-container { + left: 400px; + bottom: 145px; +} + .score-header { font-size: 14px; text-align: center; @@ -48,9 +74,9 @@ h1 { margin: 0 auto; } -.score { +#score, #high_score { padding: 5px; - margin-top: 25px; + margin-top: 10px; color: #FFFFFF; } From 11f8e86be394755ba836708530cd77642a36cc96 Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Thu, 4 Feb 2016 14:51:37 -0800 Subject: [PATCH 44/52] front board looks pretty okay i guess --- stylesheets/2048.css | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/stylesheets/2048.css b/stylesheets/2048.css index bd1e8c8..f03361d 100644 --- a/stylesheets/2048.css +++ b/stylesheets/2048.css @@ -35,7 +35,7 @@ h1 { width: 250px; padding: 10px; font-weight: bold; - bottom: 50px; + bottom: 100px; } .header { @@ -43,8 +43,8 @@ h1 { */ margin: 0 auto; width: 500px; margin-bottom: 0px; -/* height: 200px; -*/} + height: 200px; +} .score-container, .high-score-container { position: relative; From 59f0af56780769472508f6329dc8fe1888a492d6 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Thu, 4 Feb 2016 15:05:51 -0800 Subject: [PATCH 45/52] change css to display game over message --- index.html | 4 ++++ javascripts/2048.js | 6 +++++- stylesheets/2048.css | 47 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 1 deletion(-) diff --git a/index.html b/index.html index feae6f1..5f8761f 100644 --- a/index.html +++ b/index.html @@ -19,6 +19,10 @@

HIGH SCORE

New Game
+
+

Game over!

+ +
diff --git a/javascripts/2048.js b/javascripts/2048.js index aebfbb8..0c6bc01 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -15,6 +15,7 @@ var Tile = function (row, col, val) { }; Game.prototype.startGame = function () { + $('#game-message').fadeOut(); $('.tile').remove(); this.gameOver = false; this.gameWon = false; @@ -51,7 +52,7 @@ Game.prototype.checkGameOver = function () { } } this.gameOver = true; - console.log(this.gameOver); + $('#game-message').fadeIn(); } }; @@ -279,5 +280,8 @@ $(document).ready(function() { $('#reset').click(function () { game.startGame(); }); + $('button').click(function () { + game.startGame(); + }); }); diff --git a/stylesheets/2048.css b/stylesheets/2048.css index ef78f39..e461d17 100644 --- a/stylesheets/2048.css +++ b/stylesheets/2048.css @@ -10,6 +10,38 @@ h1 { color: #776e65; } +.gameover { + color: #fdd785; + -webkit-text-stroke-width: 1px; + -webkit-text-stroke-color: #776e65; + text-align: center; + vertical-align: bottom; + height: 300px; + font-size: 2.5em; +} + +.btn { + display: block; + margin: auto; + -webkit-border-radius: 28; + -moz-border-radius: 28; + border-radius: 28px; + -webkit-box-shadow: 3px 3px 3px #666666; + -moz-box-shadow: 3px 3px 3px #666666; + box-shadow: 3px 3px 3px #666666; + font-family: Arial; + color: #423d42; + font-size: 25px; + background: #fdd785; + padding: 10px 20px 10px 20px; + text-decoration: none; +} + +.btn:hover { + background: #fae9c3; + text-decoration: none; +} + #gameboard { background: #bbada0; border-radius: 0.5rem; @@ -19,6 +51,21 @@ h1 { width: 18.5rem; } +#game-message{ + display: none; + background: #bbada0; + opacity: 0.7; + filter: alpha(opacity=70); /* For IE8 and earlier */ + border-radius: 0.5rem; + height: 18.5rem; + position: absolute; + width: 18.5rem; + z-index: 1000; + -webkit-animation: fade-in 800ms ease 1200ms; + -moz-animation: fade-in 800ms ease 1200ms; + animation: fade-in 800ms ease 1200ms; +} + #reset { color: #FFFFFF; background-color: #8F7A66; From 628947c2a60d5f8940dcf807dacbab4454a0ef5e Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Thu, 4 Feb 2016 15:39:49 -0800 Subject: [PATCH 46/52] updated css for gameover screen --- stylesheets/2048.css | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/stylesheets/2048.css b/stylesheets/2048.css index 25fe109..d511f9d 100644 --- a/stylesheets/2048.css +++ b/stylesheets/2048.css @@ -20,13 +20,15 @@ h1 { } .gameover { - color: #fdd785; + color: #776e65; -webkit-text-stroke-width: 1px; -webkit-text-stroke-color: #776e65; text-align: center; vertical-align: bottom; height: 300px; font-size: 2.5em; + font-family: "Clear Sans", "Helvetica Neue", Arial, sans-serif; + font-weight: bold; } .btn { @@ -35,13 +37,13 @@ h1 { -webkit-border-radius: 28; -moz-border-radius: 28; border-radius: 28px; - -webkit-box-shadow: 3px 3px 3px #666666; - -moz-box-shadow: 3px 3px 3px #666666; - box-shadow: 3px 3px 3px #666666; + -webkit-box-shadow: 3px 3px 3px #776e65; + -moz-box-shadow: 3px 3px 3px #776e65; + box-shadow: 3px 3px 3px #776e65; font-family: Arial; - color: #423d42; + color: #FFFFFF; font-size: 25px; - background: #fdd785; + background: #776e65; padding: 10px 20px 10px 20px; text-decoration: none; } @@ -63,8 +65,8 @@ h1 { #game-message{ display: none; background: #bbada0; - opacity: 0.7; - filter: alpha(opacity=70); /* For IE8 and earlier */ + opacity: 0.8; + filter: alpha(opacity=80); /* For IE8 and earlier */ border-radius: 0.5rem; height: 18.5rem; position: absolute; From d789e6f2ce8dafe6f9c2a64514e26f744facb4d2 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Fri, 5 Feb 2016 13:50:53 -0800 Subject: [PATCH 47/52] add game won message --- index.html | 11 ++++++++--- javascripts/2048.js | 10 ++++++---- stylesheets/2048.css | 5 +++-- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/index.html b/index.html index 65e7063..bf46685 100644 --- a/index.html +++ b/index.html @@ -19,12 +19,17 @@

SCORE

BEST

- New Game + New Game
-

Game over!

- +

Game over!

+ +
+
+

Game won!

+ +
diff --git a/javascripts/2048.js b/javascripts/2048.js index 0c6bc01..f282de2 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -249,7 +249,7 @@ Game.prototype.moveTile = function(tile, direction) { if (this.gameWon === false) { this.updateGameWon(); if (this.gameWon) { - alert('Game won!'); + $('#game-won').fadeIn(); } } this.checkGameOver(); @@ -277,11 +277,13 @@ $(document).ready(function() { } } }); - $('#reset').click(function () { + $('.reset').click(function () { game.startGame(); + $('#game-won').fadeOut(); }); - $('button').click(function () { - game.startGame(); + + $('.resume').click(function() { + $('#game-won').fadeOut(); }); }); diff --git a/stylesheets/2048.css b/stylesheets/2048.css index d511f9d..48ff070 100644 --- a/stylesheets/2048.css +++ b/stylesheets/2048.css @@ -19,7 +19,7 @@ h1 { font-size: 104px; } -.gameover { +.game-change { color: #776e65; -webkit-text-stroke-width: 1px; -webkit-text-stroke-color: #776e65; @@ -62,7 +62,8 @@ h1 { width: 18.5rem; } -#game-message{ +#game-message, +#game-won { display: none; background: #bbada0; opacity: 0.8; From b8e9720d7943d68ad1205d2ef5ea0316ffa9cf34 Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Fri, 5 Feb 2016 13:52:38 -0800 Subject: [PATCH 48/52] slightly changed header size --- stylesheets/2048.css | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stylesheets/2048.css b/stylesheets/2048.css index 48ff070..6e54e6e 100644 --- a/stylesheets/2048.css +++ b/stylesheets/2048.css @@ -93,7 +93,7 @@ h1 { */ margin: 0 auto; width: 500px; margin-bottom: 0px; - height: 200px; + height: 190px; } .score-container, .high-score-container { From cdad9e512e6ce69f204b16108767cbc536435cad Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Fri, 5 Feb 2016 14:21:49 -0800 Subject: [PATCH 49/52] added a delay, so two tiles combining over a long distance looks less wonky --- javascripts/2048.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/javascripts/2048.js b/javascripts/2048.js index f282de2..919fd1b 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -107,10 +107,11 @@ Game.prototype.moveTile = function(tile, direction) { $("#" + colArray[row+1].tileId).attr("data-val", colArray[row+1].val); $("#" + colArray[row+1].tileId).html(colArray[row+1].val); // delete from board + var deleteTileIndex = _.indexOf(this.board, colArray[row]); this.board.splice(deleteTileIndex, 1); // delete current html object - $("#" + colArray[row].tileId).remove(); + $("#" + colArray[row].tileId).delay(100).fadeOut(50, function(){$(this).remove();}); // delete current value (tile object) colArray.splice(row, 1); // if not combining @@ -149,7 +150,7 @@ Game.prototype.moveTile = function(tile, direction) { var deleteTileIndex = _.indexOf(this.board, colArray[arrayIndex]); this.board.splice(deleteTileIndex, 1); // delete current html object - $("#" + colArray[arrayIndex].tileId).remove(); + $("#" + colArray[arrayIndex].tileId).delay(100).fadeOut(50, function(){$(this).remove();}); // delete current value (tile object) colArray.splice(arrayIndex, 1); // if not combining @@ -186,7 +187,7 @@ Game.prototype.moveTile = function(tile, direction) { var deleteTileIndex = _.indexOf(this.board, rowArray[col]); this.board.splice(deleteTileIndex, 1); // delete current html object - $("#" + rowArray[col].tileId).remove(); + $("#" + rowArray[col].tileId).delay(100).fadeOut(50, function(){$(this).remove();}); // delete current tile object) rowArray.splice(col, 1); // if not combining @@ -220,12 +221,12 @@ Game.prototype.moveTile = function(tile, direction) { // change HTML of tile $("#" + rowArray[arrayIndex-1].tileId).attr("data-col", "c" + col); $("#" + rowArray[arrayIndex-1].tileId).attr("data-val", rowArray[arrayIndex-1].val); - $("#" + rowArray[arrayIndex-1].tileId).html(rowArray[arrayIndex-1].val); + $("#" + rowArray[arrayIndex-1].tileId).html(rowArray[arrayIndex-1].val).delay(1000); // delete from board var deleteTileIndex = _.indexOf(this.board, rowArray[arrayIndex]); this.board.splice(deleteTileIndex, 1); // delete current html object - $("#" + rowArray[arrayIndex].tileId).remove(); + $("#" + rowArray[arrayIndex].tileId).delay(100).fadeOut(50, function(){$(this).remove();}); // delete current value (tile object) rowArray.splice(arrayIndex, 1); // if not combining @@ -243,6 +244,7 @@ Game.prototype.moveTile = function(tile, direction) { Object.keys(groupedTiles).forEach(function(key) { return func(key); }); afterMoves = this.board.map(function(tile) {return tile.moveCount;}); matching = afterMoves.every(function(element, index) { return initMoves[index] === element; }); + if (!matching) { setTimeout(addTileCallback, 200); } From 3efc9453558af0b84290bd3537d17a4120e49419 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Fri, 5 Feb 2016 14:23:25 -0800 Subject: [PATCH 50/52] format score boxes --- stylesheets/2048.css | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stylesheets/2048.css b/stylesheets/2048.css index 48ff070..648fec7 100644 --- a/stylesheets/2048.css +++ b/stylesheets/2048.css @@ -100,6 +100,7 @@ h1 { position: relative; left: 300px; background-color: #BBADA0; + border-radius: 0.5rem; width: 80px; height: 70px; padding: 5px; @@ -118,14 +119,13 @@ h1 { } .score-header { - font-size: 14px; + font-size: 20px; text-align: center; padding: 0; margin: 0 auto; } #score, #high_score { - padding: 5px; margin-top: 10px; color: #FFFFFF; } From d642eed2da4af85ce97deee51a731f7688b4e9ac Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Fri, 5 Feb 2016 14:33:01 -0800 Subject: [PATCH 51/52] change animation timings for collisions --- javascripts/2048.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/javascripts/2048.js b/javascripts/2048.js index 919fd1b..c3af8aa 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -111,7 +111,7 @@ Game.prototype.moveTile = function(tile, direction) { var deleteTileIndex = _.indexOf(this.board, colArray[row]); this.board.splice(deleteTileIndex, 1); // delete current html object - $("#" + colArray[row].tileId).delay(100).fadeOut(50, function(){$(this).remove();}); + $("#" + colArray[row].tileId).delay(75).fadeOut(50, function(){$(this).remove();}); // delete current value (tile object) colArray.splice(row, 1); // if not combining @@ -150,7 +150,7 @@ Game.prototype.moveTile = function(tile, direction) { var deleteTileIndex = _.indexOf(this.board, colArray[arrayIndex]); this.board.splice(deleteTileIndex, 1); // delete current html object - $("#" + colArray[arrayIndex].tileId).delay(100).fadeOut(50, function(){$(this).remove();}); + $("#" + colArray[arrayIndex].tileId).delay(75).fadeOut(50, function(){$(this).remove();}); // delete current value (tile object) colArray.splice(arrayIndex, 1); // if not combining @@ -187,7 +187,7 @@ Game.prototype.moveTile = function(tile, direction) { var deleteTileIndex = _.indexOf(this.board, rowArray[col]); this.board.splice(deleteTileIndex, 1); // delete current html object - $("#" + rowArray[col].tileId).delay(100).fadeOut(50, function(){$(this).remove();}); + $("#" + rowArray[col].tileId).delay(75).fadeOut(50, function(){$(this).remove();}); // delete current tile object) rowArray.splice(col, 1); // if not combining @@ -221,12 +221,12 @@ Game.prototype.moveTile = function(tile, direction) { // change HTML of tile $("#" + rowArray[arrayIndex-1].tileId).attr("data-col", "c" + col); $("#" + rowArray[arrayIndex-1].tileId).attr("data-val", rowArray[arrayIndex-1].val); - $("#" + rowArray[arrayIndex-1].tileId).html(rowArray[arrayIndex-1].val).delay(1000); + $("#" + rowArray[arrayIndex-1].tileId).html(rowArray[arrayIndex-1].val); // delete from board var deleteTileIndex = _.indexOf(this.board, rowArray[arrayIndex]); this.board.splice(deleteTileIndex, 1); // delete current html object - $("#" + rowArray[arrayIndex].tileId).delay(100).fadeOut(50, function(){$(this).remove();}); + $("#" + rowArray[arrayIndex].tileId).delay(75).fadeOut(50, function(){$(this).remove();}); // delete current value (tile object) rowArray.splice(arrayIndex, 1); // if not combining From 14f5870d7b1514f233e79242612d738f999d33e1 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Fri, 5 Feb 2016 14:39:32 -0800 Subject: [PATCH 52/52] style new game button --- stylesheets/2048.css | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/stylesheets/2048.css b/stylesheets/2048.css index 51399e8..ccb58d6 100644 --- a/stylesheets/2048.css +++ b/stylesheets/2048.css @@ -82,10 +82,14 @@ h1 { position: relative; color: #FFFFFF; background-color: #8F7A66; - width: 250px; + width: 200px; + text-align: center; + display: block; + margin: auto; + border-radius: 0.5rem; padding: 10px; font-weight: bold; - bottom: 100px; + bottom: 110px; } .header {