From f7e444e1386f8c4ea750764f8d98ebbcd9b4f247 Mon Sep 17 00:00:00 2001 From: Kelly Date: Mon, 1 Feb 2016 15:52:52 -0800 Subject: [PATCH 01/46] create addTile function --- javascripts/2048.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/javascripts/2048.js b/javascripts/2048.js index 07aef24..876dcaa 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -5,7 +5,13 @@ var Game = function() { [0, 0, 0, 0]]; }; - +Game.prototype.addTile = function() { + var row = Math.floor(Math.random() * 3); + var column = Math.floor(Math.random() * 3); + var options = [2,2,4]; + var value = options[Math.floor(Math.random() * options.length)]; + this.board[row][column] = value; +}; Game.prototype.moveTile = function(tile, direction) { From a7fcc4568f4cb13281ebd69628399799e27e9ed3 Mon Sep 17 00:00:00 2001 From: Kelly Date: Mon, 1 Feb 2016 16:03:07 -0800 Subject: [PATCH 02/46] add randomly two spots that aren't 0 but if most spots are full it is very slow --- javascripts/2048.js | 71 +++++++++++++++++++++++++-------------------- 1 file changed, 39 insertions(+), 32 deletions(-) diff --git a/javascripts/2048.js b/javascripts/2048.js index 876dcaa..5e27a44 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -3,46 +3,53 @@ var Game = function() { [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]; + this.addTile(); + this.addTile(); }; + Game.prototype.addTile = function() { var row = Math.floor(Math.random() * 3); var column = Math.floor(Math.random() * 3); + while (game.board[row][column] !== 0) { + row = Math.floor(Math.random() * 3); + column = Math.floor(Math.random() * 3); + } var options = [2,2,4]; var value = options[Math.floor(Math.random() * options.length)]; this.board[row][column] = value; }; -Game.prototype.moveTile = function(tile, direction) { - // Game method here - switch(direction) { - case 38: //up - console.log('up'); - break; - case 40: //down - console.log('down'); - break; - case 37: //left - console.log('left'); - break; - case 39: //right - console.log('right'); - break; - } -}; - -$(document).ready(function() { - console.log("ready to go!"); - // Any interactive jQuery functionality - var game = new Game(); - - $('body').keydown(function(event){ - var arrows = [37, 38, 39, 40]; - if (arrows.indexOf(event.which) > -1) { - var tile = $('.tile'); - - game.moveTile(tile, event.which); - } - }); -}); +// Game.prototype.moveTile = function(tile, direction) { +// // Game method here +// switch(direction) { +// case 38: //up +// console.log('up'); +// break; +// case 40: //down +// console.log('down'); +// break; +// case 37: //left +// console.log('left'); +// break; +// case 39: //right +// console.log('right'); +// break; +// } +// }; +// +// $(document).ready(function() { +// console.log("ready to go!"); +// // Any interactive jQuery functionality +// var game = new Game(); +// +// $('body').keydown(function(event){ +// var arrows = [37, 38, 39, 40]; +// if (arrows.indexOf(event.which) > -1) { +// var tile = $('.tile'); +// +// game.moveTile(tile, event.which); +// } +// }); +// }); From 09eb505341838186fbbc5af18b9af445fbc8968d Mon Sep 17 00:00:00 2001 From: Kelly Date: Mon, 1 Feb 2016 16:16:39 -0800 Subject: [PATCH 03/46] change random row and column generator to cap at 4 --- javascripts/2048.js | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/javascripts/2048.js b/javascripts/2048.js index 5e27a44..f247a8c 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -7,20 +7,19 @@ var Game = function() { this.addTile(); }; - +//check if board is full before calling this method (otherwise infinite loop) Game.prototype.addTile = function() { - var row = Math.floor(Math.random() * 3); - var column = Math.floor(Math.random() * 3); - while (game.board[row][column] !== 0) { - row = Math.floor(Math.random() * 3); - column = Math.floor(Math.random() * 3); + var row = Math.floor(Math.random() * 4); + var column = Math.floor(Math.random() * 4); + while (this.board[row][column] !== 0) { + row = Math.floor(Math.random() * 4); + column = Math.floor(Math.random() * 4); } var options = [2,2,4]; var value = options[Math.floor(Math.random() * options.length)]; this.board[row][column] = value; }; - // Game.prototype.moveTile = function(tile, direction) { // // Game method here // switch(direction) { From ba9faf8b4d6bd7cb5ea2ec8414a017cc371a5501 Mon Sep 17 00:00:00 2001 From: emgord Date: Mon, 1 Feb 2016 16:37:33 -0800 Subject: [PATCH 04/46] draw board works, need to eliminate zeros --- index.html | 2 +- javascripts/2048.js | 76 ++++++++++++++++++++++++++------------------- 2 files changed, 45 insertions(+), 33 deletions(-) diff --git a/index.html b/index.html index 4740408..b7ac7cf 100644 --- a/index.html +++ b/index.html @@ -26,7 +26,7 @@
-
2
+ diff --git a/javascripts/2048.js b/javascripts/2048.js index f247a8c..dbaa5d7 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -20,35 +20,47 @@ Game.prototype.addTile = function() { this.board[row][column] = value; }; -// Game.prototype.moveTile = function(tile, direction) { -// // Game method here -// switch(direction) { -// case 38: //up -// console.log('up'); -// break; -// case 40: //down -// console.log('down'); -// break; -// case 37: //left -// console.log('left'); -// break; -// case 39: //right -// console.log('right'); -// break; -// } -// }; -// -// $(document).ready(function() { -// console.log("ready to go!"); -// // Any interactive jQuery functionality -// var game = new Game(); -// -// $('body').keydown(function(event){ -// var arrows = [37, 38, 39, 40]; -// if (arrows.indexOf(event.which) > -1) { -// var tile = $('.tile'); -// -// game.moveTile(tile, event.which); -// } -// }); -// }); +Game.prototype.moveTile = function(tile, direction) { + // Game method here + switch(direction) { + case 38: //up + console.log('up'); + break; + case 40: //down + console.log('down'); + break; + case 37: //left + console.log('left'); + break; + case 39: //right + console.log('right'); + break; + } +}; + +Game.prototype.drawBoard = function(){ + for(var row = 0; row < this.board.length; row++) { + for(var col = 0; col < this.board.length; col++) + $("#gameboard").append($("
" + this.board[row][col] + "
")); + } +}; + +Game.prototype.clearBoard = function(){ + +}; + +$(document).ready(function() { + console.log("ready to go!"); + // Any interactive jQuery functionality + var game = new Game(); + game.drawBoard(); + + $('body').keydown(function(event){ + var arrows = [37, 38, 39, 40]; + if (arrows.indexOf(event.which) > -1) { + var tile = $('.tile'); + + game.moveTile(tile, event.which); + } + }); +}); From 470867be25ec847161c5a163d854e97356aa77b5 Mon Sep 17 00:00:00 2001 From: emgord Date: Mon, 1 Feb 2016 16:42:40 -0800 Subject: [PATCH 05/46] draw board does not show zeros now --- javascripts/2048.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/javascripts/2048.js b/javascripts/2048.js index dbaa5d7..2f8673f 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -41,8 +41,10 @@ Game.prototype.moveTile = function(tile, direction) { Game.prototype.drawBoard = function(){ for(var row = 0; row < this.board.length; row++) { for(var col = 0; col < this.board.length; col++) + if (this.board[row][col] !== 0) { $("#gameboard").append($("
" + this.board[row][col] + "
")); } + } }; Game.prototype.clearBoard = function(){ From 379a47f7d4626c30c6181df710375c6449bb420f Mon Sep 17 00:00:00 2001 From: emgord Date: Mon, 1 Feb 2016 16:46:06 -0800 Subject: [PATCH 06/46] refined add tile, missing { added --- index.html | 1 - javascripts/2048.js | 10 ++++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/index.html b/index.html index b7ac7cf..4a7e370 100644 --- a/index.html +++ b/index.html @@ -26,7 +26,6 @@
- diff --git a/javascripts/2048.js b/javascripts/2048.js index 2f8673f..3311577 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -40,10 +40,12 @@ Game.prototype.moveTile = function(tile, direction) { Game.prototype.drawBoard = function(){ for(var row = 0; row < this.board.length; row++) { - for(var col = 0; col < this.board.length; col++) - if (this.board[row][col] !== 0) { - $("#gameboard").append($("
" + this.board[row][col] + "
")); - } + for(var col = 0; col < this.board.length; col++) { + var value = this.board[row][col]; + if (value !== 0) { + $("#gameboard").append($("
" + value + "
")); + } + } } }; From 0c1587a94ee3bf737292d28cdad4a0628bb6b53d Mon Sep 17 00:00:00 2001 From: emgord Date: Mon, 1 Feb 2016 16:48:07 -0800 Subject: [PATCH 07/46] added clear board function --- javascripts/2048.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/javascripts/2048.js b/javascripts/2048.js index 3311577..d1359dc 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -50,7 +50,7 @@ Game.prototype.drawBoard = function(){ }; Game.prototype.clearBoard = function(){ - + $(".tile").remove(); }; $(document).ready(function() { @@ -59,6 +59,7 @@ $(document).ready(function() { var game = new Game(); game.drawBoard(); + $('body').keydown(function(event){ var arrows = [37, 38, 39, 40]; if (arrows.indexOf(event.which) > -1) { From 2032021ca6500b595f1f0877338c53100d8eed95 Mon Sep 17 00:00:00 2001 From: emgord Date: Mon, 1 Feb 2016 16:51:00 -0800 Subject: [PATCH 08/46] add new tiles with each turn, clear and print board --- javascripts/2048.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/javascripts/2048.js b/javascripts/2048.js index d1359dc..e0acf17 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -66,6 +66,9 @@ $(document).ready(function() { var tile = $('.tile'); game.moveTile(tile, event.which); + game.addTile(); + game.clearBoard(); + game.drawBoard(); } }); }); From 69156f5ad1b4dcc1d29456a5ebace4a5b7329692 Mon Sep 17 00:00:00 2001 From: Kelly Date: Tue, 2 Feb 2016 10:08:06 -0800 Subject: [PATCH 09/46] add right arrow shift function --- javascripts/2048.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/javascripts/2048.js b/javascripts/2048.js index e0acf17..4a2a131 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -20,6 +20,23 @@ Game.prototype.addTile = function() { this.board[row][column] = value; }; +// this assumes arrow key was right +Game.prototype.shiftTiles = function() { + for (row = 0; row < this.board.length; row++) { + var zeros = []; + var nonzeros = []; + for (col = 0; col < this.board.length; col++) { + if (this.board[row][col] === 0) { + zeros.push(this.board[row][col]); + } else { + nonzeros.push(this.board[row][col]); + } + } + var new_row = zeros.concat(nonzeros); + this.board[row] = new_row; + } +}; + Game.prototype.moveTile = function(tile, direction) { // Game method here switch(direction) { @@ -34,6 +51,7 @@ Game.prototype.moveTile = function(tile, direction) { break; case 39: //right console.log('right'); + this.shiftTiles(); break; } }; From dddaaa038c617923ebd3d0d0c76a72cedb610497 Mon Sep 17 00:00:00 2001 From: emgord Date: Tue, 2 Feb 2016 10:34:54 -0800 Subject: [PATCH 10/46] left shift working --- javascripts/2048.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/javascripts/2048.js b/javascripts/2048.js index 4a2a131..198fb24 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -20,6 +20,25 @@ Game.prototype.addTile = function() { this.board[row][column] = value; }; +Game.prototype.leftShifter = function() { + // var shifter = function(){ + for (row = 0; row < this.board.length; row++) { + var zeros = []; + var nonzeros = []; + for (col = 0; col < this.board.length; col++) { + if (this.board[row][col] === 0) { + zeros.push(this.board[row][col]); + } else { + nonzeros.push(this.board[row][col]); + } + } + var new_row = nonzeros.concat(zeros); + this.board[row] = new_row; + } + // }; + // return shifter; +}; + // this assumes arrow key was right Game.prototype.shiftTiles = function() { for (row = 0; row < this.board.length; row++) { @@ -48,6 +67,7 @@ Game.prototype.moveTile = function(tile, direction) { break; case 37: //left console.log('left'); + this.leftShifter(); break; case 39: //right console.log('right'); From a4c7bc1cfb25b2542503958cd1d2f56b5fd8ae78 Mon Sep 17 00:00:00 2001 From: emgord Date: Tue, 2 Feb 2016 11:06:11 -0800 Subject: [PATCH 11/46] added separate functions for shifting up down left and right, looking for a way to refractor --- javascripts/2048.js | 50 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 44 insertions(+), 6 deletions(-) diff --git a/javascripts/2048.js b/javascripts/2048.js index 198fb24..f285441 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -22,10 +22,10 @@ Game.prototype.addTile = function() { Game.prototype.leftShifter = function() { // var shifter = function(){ - for (row = 0; row < this.board.length; row++) { + for (var row = 0; row < this.board.length; row++) { var zeros = []; var nonzeros = []; - for (col = 0; col < this.board.length; col++) { + for (var col = 0; col < this.board.length; col++) { if (this.board[row][col] === 0) { zeros.push(this.board[row][col]); } else { @@ -40,11 +40,11 @@ Game.prototype.leftShifter = function() { }; // this assumes arrow key was right -Game.prototype.shiftTiles = function() { - for (row = 0; row < this.board.length; row++) { +Game.prototype.rightShifter = function() { + for (var row = 0; row < this.board.length; row++) { var zeros = []; var nonzeros = []; - for (col = 0; col < this.board.length; col++) { + for (var col = 0; col < this.board.length; col++) { if (this.board[row][col] === 0) { zeros.push(this.board[row][col]); } else { @@ -56,13 +56,51 @@ Game.prototype.shiftTiles = function() { } }; +Game.prototype.upShifter = function() { + for (var col = 0; col < this.board.length; col++) { + var zeros = []; + var nonzeros = []; + for (var row = 0; row < this.board.length; row++) { + if (this.board[row][col] === 0) { + zeros.push(this.board[row][col]); + } else { + nonzeros.push(this.board[row][col]); + } + } + var new_col = nonzeros.concat(zeros); + for (var i = 0; i < this.board.length; i++){ + this.board[i][col] = new_col[i]; + } + } +}; + +Game.prototype.downShifter = function() { + for (var col = 0; col < this.board.length; col++) { + var zeros = []; + var nonzeros = []; + for (var row = 0; row < this.board.length; row++) { + if (this.board[row][col] === 0) { + zeros.push(this.board[row][col]); + } else { + nonzeros.push(this.board[row][col]); + } + } + var new_col = zeros.concat(nonzeros); + for (var i = 0; i < this.board.length; i++){ + this.board[i][col] = new_col[i]; + } + } +}; + Game.prototype.moveTile = function(tile, direction) { // Game method here switch(direction) { case 38: //up console.log('up'); + this.upShifter(); break; case 40: //down + this.downShifter(); console.log('down'); break; case 37: //left @@ -71,7 +109,7 @@ Game.prototype.moveTile = function(tile, direction) { break; case 39: //right console.log('right'); - this.shiftTiles(); + this.rightShifter(); break; } }; From 756441df0c0577511d6cf5deb26a22db1172b108 Mon Sep 17 00:00:00 2001 From: emgord Date: Tue, 2 Feb 2016 11:34:35 -0800 Subject: [PATCH 12/46] merge function added to right shift, currently merging more than once --- javascripts/2048.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/javascripts/2048.js b/javascripts/2048.js index f285441..c635a4c 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -20,8 +20,18 @@ Game.prototype.addTile = function() { this.board[row][column] = value; }; +var merge = function(array){ + for(var i = 0; i < array.length; i++){ + if (array[i] == array[i+1]){ + var val = array[i]; + array[i] = 0; + array[i+1] = val * 2; + } + } + +}; + Game.prototype.leftShifter = function() { - // var shifter = function(){ for (var row = 0; row < this.board.length; row++) { var zeros = []; var nonzeros = []; @@ -35,8 +45,6 @@ Game.prototype.leftShifter = function() { var new_row = nonzeros.concat(zeros); this.board[row] = new_row; } - // }; - // return shifter; }; // this assumes arrow key was right @@ -52,6 +60,7 @@ Game.prototype.rightShifter = function() { } } var new_row = zeros.concat(nonzeros); + merge(new_row); this.board[row] = new_row; } }; From a07fa1713f589b1c47fc9637918ca60e264219cd Mon Sep 17 00:00:00 2001 From: Kelly Date: Tue, 2 Feb 2016 14:46:53 -0800 Subject: [PATCH 13/46] update merge function to work for right arrow --- javascripts/2048.js | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/javascripts/2048.js b/javascripts/2048.js index c635a4c..713c2aa 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -21,13 +21,28 @@ Game.prototype.addTile = function() { }; var merge = function(array){ - for(var i = 0; i < array.length; i++){ - if (array[i] == array[i+1]){ - var val = array[i]; - array[i] = 0; - array[i+1] = val * 2; + var squished_array = []; + while (array.length > 0) { + if (array.length === 1) { + squished_array.push(array[0]); + array.splice(0, 1); + } else if (array[0] === array[1]) { + squished_array.push(array[0] * 2); + array.splice(0, 2); + } else { + squished_array.push(array[0]); + array.splice(0, 1); } + } + return squished_array; + // for(var i = 0; i < array.length; i++){ + // if (array[i] == array[i+1]){ + // var val = array[i]; + // array[i] = 0; + // array[i+1] = val * 2; + // } + // } }; @@ -47,20 +62,22 @@ Game.prototype.leftShifter = function() { } }; -// this assumes arrow key was right Game.prototype.rightShifter = function() { for (var row = 0; row < this.board.length; row++) { - var zeros = []; + // var zeros = []; var nonzeros = []; for (var col = 0; col < this.board.length; col++) { - if (this.board[row][col] === 0) { - zeros.push(this.board[row][col]); - } else { + if (this.board[row][col] !== 0) { nonzeros.push(this.board[row][col]); } } + if (nonzeros.length !== 0) { + nonzeros = merge(nonzeros); + } + + var numZeros = (this.board[row].length - nonzeros.length); + var zeros = new Array(numZeros + 1).join('0').split('').map(parseFloat); var new_row = zeros.concat(nonzeros); - merge(new_row); this.board[row] = new_row; } }; From 33e61ffeafe8346e324957a66489664a64bcceca Mon Sep 17 00:00:00 2001 From: emgord Date: Tue, 2 Feb 2016 15:39:47 -0800 Subject: [PATCH 14/46] downshift and right shift working --- javascripts/2048.js | 51 +++++++++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 20 deletions(-) diff --git a/javascripts/2048.js b/javascripts/2048.js index 713c2aa..f7d1840 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -20,7 +20,7 @@ Game.prototype.addTile = function() { this.board[row][column] = value; }; -var merge = function(array){ +var mergeLeftUp = function(array){ var squished_array = []; while (array.length > 0) { if (array.length === 1) { @@ -33,17 +33,25 @@ var merge = function(array){ squished_array.push(array[0]); array.splice(0, 1); } - } return squished_array; - // for(var i = 0; i < array.length; i++){ - // if (array[i] == array[i+1]){ - // var val = array[i]; - // array[i] = 0; - // array[i+1] = val * 2; - // } - // } +}; +var mergeRightDown = function(array){ + var squished_array = []; + while (array.length > 0) { + if (array.length === 1) { + squished_array.unshift(array[0]); + array.splice(0, 1); + } else if (array[array.length-1] === array[array.length-2]) { + squished_array.unshift(array[array.length-1] * 2); + array.splice(array.length-2, 2); + } else { + squished_array.unshift(array[array.length-1]); + array.splice(array.length-1, 1); + } + } + return squished_array; }; Game.prototype.leftShifter = function() { @@ -64,7 +72,6 @@ Game.prototype.leftShifter = function() { Game.prototype.rightShifter = function() { for (var row = 0; row < this.board.length; row++) { - // var zeros = []; var nonzeros = []; for (var col = 0; col < this.board.length; col++) { if (this.board[row][col] !== 0) { @@ -72,35 +79,37 @@ Game.prototype.rightShifter = function() { } } if (nonzeros.length !== 0) { - nonzeros = merge(nonzeros); + nonzeros = mergeRightDown(nonzeros); } - var numZeros = (this.board[row].length - nonzeros.length); + var numZeros = (this.board.length - nonzeros.length); var zeros = new Array(numZeros + 1).join('0').split('').map(parseFloat); var new_row = zeros.concat(nonzeros); this.board[row] = new_row; } }; -Game.prototype.upShifter = function() { +Game.prototype.downShifter = function() { for (var col = 0; col < this.board.length; col++) { - var zeros = []; var nonzeros = []; for (var row = 0; row < this.board.length; row++) { - if (this.board[row][col] === 0) { - zeros.push(this.board[row][col]); - } else { + if (this.board[row][col] !== 0) { nonzeros.push(this.board[row][col]); } } - var new_col = nonzeros.concat(zeros); + if (nonzeros.length !== 0) { + nonzeros = mergeRightDown(nonzeros); + } + var numZeros = (this.board.length - nonzeros.length); + var zeros = new Array(numZeros + 1).join('0').split('').map(parseFloat); + var new_col = zeros.concat(nonzeros); for (var i = 0; i < this.board.length; i++){ this.board[i][col] = new_col[i]; } } }; -Game.prototype.downShifter = function() { +Game.prototype.upShifter = function() { for (var col = 0; col < this.board.length; col++) { var zeros = []; var nonzeros = []; @@ -111,13 +120,15 @@ Game.prototype.downShifter = function() { nonzeros.push(this.board[row][col]); } } - var new_col = zeros.concat(nonzeros); + var new_col = nonzeros.concat(zeros); for (var i = 0; i < this.board.length; i++){ this.board[i][col] = new_col[i]; } } }; + + Game.prototype.moveTile = function(tile, direction) { // Game method here switch(direction) { From c22febf7b7d4f8519ba00e83650af269ac2b05de Mon Sep 17 00:00:00 2001 From: Kelly Date: Tue, 2 Feb 2016 15:53:17 -0800 Subject: [PATCH 15/46] update shift methods to work for up and left --- javascripts/2048.js | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/javascripts/2048.js b/javascripts/2048.js index f7d1840..fb46afd 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -56,15 +56,18 @@ var mergeRightDown = function(array){ Game.prototype.leftShifter = function() { for (var row = 0; row < this.board.length; row++) { - var zeros = []; var nonzeros = []; for (var col = 0; col < this.board.length; col++) { - if (this.board[row][col] === 0) { - zeros.push(this.board[row][col]); - } else { + if (this.board[row][col] !== 0) { nonzeros.push(this.board[row][col]); } } + if (nonzeros.length !== 0) { + nonzeros = mergeLeftUp(nonzeros); + } + + var numZeros = (this.board.length - nonzeros.length); + var zeros = new Array(numZeros + 1).join('0').split('').map(parseFloat); var new_row = nonzeros.concat(zeros); this.board[row] = new_row; } @@ -111,19 +114,23 @@ Game.prototype.downShifter = function() { Game.prototype.upShifter = function() { for (var col = 0; col < this.board.length; col++) { - var zeros = []; var nonzeros = []; for (var row = 0; row < this.board.length; row++) { - if (this.board[row][col] === 0) { - zeros.push(this.board[row][col]); - } else { + if (this.board[row][col] !== 0) { nonzeros.push(this.board[row][col]); } } + + if (nonzeros.length !== 0) { + nonzeros = mergeLeftUp(nonzeros); + } + var numZeros = (this.board.length - nonzeros.length); + var zeros = new Array(numZeros + 1).join('0').split('').map(parseFloat); var new_col = nonzeros.concat(zeros); for (var i = 0; i < this.board.length; i++){ this.board[i][col] = new_col[i]; } + } }; From d5f79732163c01a5553fa4ce9f00d50f5750d492 Mon Sep 17 00:00:00 2001 From: Kelly Date: Tue, 2 Feb 2016 16:05:17 -0800 Subject: [PATCH 16/46] prevent infinite loop in addTile --- javascripts/2048.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/javascripts/2048.js b/javascripts/2048.js index fb46afd..dbe43bd 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -9,6 +9,11 @@ var Game = function() { //check if board is full before calling this method (otherwise infinite loop) Game.prototype.addTile = function() { + var flattenedBoard = [].concat.apply([],this.board); + if (flattenedBoard.indexOf(0) === -1) { + return; + } + var row = Math.floor(Math.random() * 4); var column = Math.floor(Math.random() * 4); while (this.board[row][column] !== 0) { From 03d4a834c4874726d9c37c47ace1377cba1985ed Mon Sep 17 00:00:00 2001 From: Kelly Date: Tue, 2 Feb 2016 16:29:51 -0800 Subject: [PATCH 17/46] we're about to make weird changes, come back here if it fails --- javascripts/2048.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/javascripts/2048.js b/javascripts/2048.js index dbe43bd..4d50aef 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -5,9 +5,16 @@ var Game = function() { [0, 0, 0, 0]]; this.addTile(); this.addTile(); + this.win = false; +}; + +Game.prototype.checkWinner = function() { + var flattenedBoard = [].concat.apply([],this.board); + if (flattenedBoard.indexOf(2048) !== -1) { + this.win = true; + } }; -//check if board is full before calling this method (otherwise infinite loop) Game.prototype.addTile = function() { var flattenedBoard = [].concat.apply([],this.board); if (flattenedBoard.indexOf(0) === -1) { From cca2e41691df152af2e9f713729a93a8960a4793 Mon Sep 17 00:00:00 2001 From: Kelly Date: Tue, 2 Feb 2016 16:37:36 -0800 Subject: [PATCH 18/46] add checkLoser function --- javascripts/2048.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/javascripts/2048.js b/javascripts/2048.js index 4d50aef..b8fd63f 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -6,8 +6,21 @@ var Game = function() { this.addTile(); this.addTile(); this.win = false; + this.lose = false; }; +Game.prototype.checkLoser = function() { + var fakeBoard = this.board.slice(0); + upShifter(fakeBoard); + leftShifter(fakeBoard); + rightShifter(fakeBoard); + downShifter(fakeBoard); + if (fakeBoard === this.board) { + this.lose = true; + } +}; + + Game.prototype.checkWinner = function() { var flattenedBoard = [].concat.apply([],this.board); if (flattenedBoard.indexOf(2048) !== -1) { From 69c11f302beebd6a1110c98ba86eaad4ebc96bbb Mon Sep 17 00:00:00 2001 From: Kelly Date: Tue, 2 Feb 2016 16:55:57 -0800 Subject: [PATCH 19/46] update shift functions to accept a board as a parameter --- javascripts/2048.js | 151 ++++++++++++++++++++++---------------------- 1 file changed, 77 insertions(+), 74 deletions(-) diff --git a/javascripts/2048.js b/javascripts/2048.js index b8fd63f..d9c0443 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -9,42 +9,6 @@ var Game = function() { this.lose = false; }; -Game.prototype.checkLoser = function() { - var fakeBoard = this.board.slice(0); - upShifter(fakeBoard); - leftShifter(fakeBoard); - rightShifter(fakeBoard); - downShifter(fakeBoard); - if (fakeBoard === this.board) { - this.lose = true; - } -}; - - -Game.prototype.checkWinner = function() { - var flattenedBoard = [].concat.apply([],this.board); - if (flattenedBoard.indexOf(2048) !== -1) { - this.win = true; - } -}; - -Game.prototype.addTile = function() { - var flattenedBoard = [].concat.apply([],this.board); - if (flattenedBoard.indexOf(0) === -1) { - return; - } - - var row = Math.floor(Math.random() * 4); - var column = Math.floor(Math.random() * 4); - while (this.board[row][column] !== 0) { - row = Math.floor(Math.random() * 4); - column = Math.floor(Math.random() * 4); - } - var options = [2,2,4]; - var value = options[Math.floor(Math.random() * options.length)]; - this.board[row][column] = value; -}; - var mergeLeftUp = function(array){ var squished_array = []; while (array.length > 0) { @@ -79,106 +43,140 @@ var mergeRightDown = function(array){ return squished_array; }; -Game.prototype.leftShifter = function() { - for (var row = 0; row < this.board.length; row++) { +var leftShifter = function(board) { + for (var row = 0; row < board.length; row++) { var nonzeros = []; - for (var col = 0; col < this.board.length; col++) { - if (this.board[row][col] !== 0) { - nonzeros.push(this.board[row][col]); + for (var col = 0; col < board.length; col++) { + if (board[row][col] !== 0) { + nonzeros.push(board[row][col]); } } if (nonzeros.length !== 0) { nonzeros = mergeLeftUp(nonzeros); } - var numZeros = (this.board.length - nonzeros.length); + var numZeros = (board.length - nonzeros.length); var zeros = new Array(numZeros + 1).join('0').split('').map(parseFloat); var new_row = nonzeros.concat(zeros); - this.board[row] = new_row; + board[row] = new_row; } }; -Game.prototype.rightShifter = function() { - for (var row = 0; row < this.board.length; row++) { +var rightShifter = function(board) { + for (var row = 0; row < board.length; row++) { var nonzeros = []; - for (var col = 0; col < this.board.length; col++) { - if (this.board[row][col] !== 0) { - nonzeros.push(this.board[row][col]); + for (var col = 0; col < board.length; col++) { + if (board[row][col] !== 0) { + nonzeros.push(board[row][col]); } } if (nonzeros.length !== 0) { nonzeros = mergeRightDown(nonzeros); } - var numZeros = (this.board.length - nonzeros.length); + var numZeros = (board.length - nonzeros.length); var zeros = new Array(numZeros + 1).join('0').split('').map(parseFloat); var new_row = zeros.concat(nonzeros); - this.board[row] = new_row; + board[row] = new_row; } }; -Game.prototype.downShifter = function() { - for (var col = 0; col < this.board.length; col++) { +var downShifter = function(board) { + for (var col = 0; col < board.length; col++) { var nonzeros = []; - for (var row = 0; row < this.board.length; row++) { - if (this.board[row][col] !== 0) { - nonzeros.push(this.board[row][col]); + for (var row = 0; row < board.length; row++) { + if (board[row][col] !== 0) { + nonzeros.push(board[row][col]); } } if (nonzeros.length !== 0) { nonzeros = mergeRightDown(nonzeros); } - var numZeros = (this.board.length - nonzeros.length); + var numZeros = (board.length - nonzeros.length); var zeros = new Array(numZeros + 1).join('0').split('').map(parseFloat); var new_col = zeros.concat(nonzeros); - for (var i = 0; i < this.board.length; i++){ - this.board[i][col] = new_col[i]; + for (var i = 0; i < board.length; i++){ + board[i][col] = new_col[i]; } } }; -Game.prototype.upShifter = function() { - for (var col = 0; col < this.board.length; col++) { +var upShifter = function(board) { + for (var col = 0; col < board.length; col++) { var nonzeros = []; - for (var row = 0; row < this.board.length; row++) { - if (this.board[row][col] !== 0) { - nonzeros.push(this.board[row][col]); + for (var row = 0; row < board.length; row++) { + if (board[row][col] !== 0) { + nonzeros.push(board[row][col]); } } if (nonzeros.length !== 0) { nonzeros = mergeLeftUp(nonzeros); } - var numZeros = (this.board.length - nonzeros.length); + var numZeros = (board.length - nonzeros.length); var zeros = new Array(numZeros + 1).join('0').split('').map(parseFloat); var new_col = nonzeros.concat(zeros); - for (var i = 0; i < this.board.length; i++){ - this.board[i][col] = new_col[i]; + for (var i = 0; i < board.length; i++){ + board[i][col] = new_col[i]; } } }; +Game.prototype.checkLoser = function() { + var fakeBoard = this.board.slice(0); + upShifter(fakeBoard); + leftShifter(fakeBoard); + rightShifter(fakeBoard); + downShifter(fakeBoard); + if (fakeBoard === this.board) { + this.lose = true; + } +}; +Game.prototype.checkWinner = function() { + var flattenedBoard = [].concat.apply([],this.board); + if (flattenedBoard.indexOf(2048) !== -1) { + this.win = true; + } +}; + +Game.prototype.addTile = function() { + var flattenedBoard = [].concat.apply([],this.board); + if (flattenedBoard.indexOf(0) === -1) { + return; + } + + var row = Math.floor(Math.random() * 4); + var column = Math.floor(Math.random() * 4); + while (this.board[row][column] !== 0) { + row = Math.floor(Math.random() * 4); + column = Math.floor(Math.random() * 4); + } + var options = [2,2,4]; + var value = options[Math.floor(Math.random() * options.length)]; + this.board[row][column] = value; +}; + Game.prototype.moveTile = function(tile, direction) { // Game method here switch(direction) { case 38: //up - console.log('up'); - this.upShifter(); + // console.log('up'); + upShifter(this.board); break; case 40: //down - this.downShifter(); - console.log('down'); + downShifter(this.board); + // console.log('down'); break; case 37: //left - console.log('left'); - this.leftShifter(); + // console.log('left'); + leftShifter(this.board); break; case 39: //right - console.log('right'); - this.rightShifter(); + // console.log('right'); + rightShifter(this.board); break; } }; @@ -214,6 +212,11 @@ $(document).ready(function() { game.addTile(); game.clearBoard(); game.drawBoard(); + game.checkWinner(); + game.checkLoser(); + if (this.win || this.lose) { + console.log("Game Over, lost: " + this.lose + ", win: " + this.win); + } } }); }); From 41d2e8f38c560377a8d37dcb40bb46c0de95b256 Mon Sep 17 00:00:00 2001 From: emgord Date: Wed, 3 Feb 2016 11:07:24 -0800 Subject: [PATCH 20/46] game win and game lose is set to true when that case is reached --- javascripts/2048.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/javascripts/2048.js b/javascripts/2048.js index d9c0443..772cd17 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -123,13 +123,17 @@ var upShifter = function(board) { } }; +function arraysEqual(a1,a2) { + return JSON.stringify(a1)==JSON.stringify(a2); +} + Game.prototype.checkLoser = function() { var fakeBoard = this.board.slice(0); upShifter(fakeBoard); leftShifter(fakeBoard); rightShifter(fakeBoard); downShifter(fakeBoard); - if (fakeBoard === this.board) { + if (arraysEqual(fakeBoard, this.board)) { this.lose = true; } }; @@ -214,8 +218,8 @@ $(document).ready(function() { game.drawBoard(); game.checkWinner(); game.checkLoser(); - if (this.win || this.lose) { - console.log("Game Over, lost: " + this.lose + ", win: " + this.win); + if (game.win || game.lose) { + console.log("Game Over, lost: " + game.lose + ", win: " + game.win); } } }); From 016554e85d83c79933cfa83fe58ca1db9c36a318 Mon Sep 17 00:00:00 2001 From: Kelly Date: Wed, 3 Feb 2016 11:13:31 -0800 Subject: [PATCH 21/46] move lose check to be only when board is full --- javascripts/2048.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/javascripts/2048.js b/javascripts/2048.js index 772cd17..69e8495 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -3,6 +3,10 @@ var Game = function() { [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]; + // this.board = [[23, 24, 25, 26], + // [27, 28, 29, 30], + // [31, 32, 33, 34], + // [35, 36, 37, 34]]; this.addTile(); this.addTile(); this.win = false; @@ -149,6 +153,7 @@ Game.prototype.checkWinner = function() { Game.prototype.addTile = function() { var flattenedBoard = [].concat.apply([],this.board); if (flattenedBoard.indexOf(0) === -1) { + this.checkLoser(); return; } @@ -217,7 +222,6 @@ $(document).ready(function() { game.clearBoard(); game.drawBoard(); game.checkWinner(); - game.checkLoser(); if (game.win || game.lose) { console.log("Game Over, lost: " + game.lose + ", win: " + game.win); } From 276e12faa2216eed47ff532fbcca54333be841de Mon Sep 17 00:00:00 2001 From: Kelly Date: Wed, 3 Feb 2016 11:32:45 -0800 Subject: [PATCH 22/46] update arrays to include 9 2s --- javascripts/2048.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/javascripts/2048.js b/javascripts/2048.js index 69e8495..6ff60b0 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -163,7 +163,7 @@ Game.prototype.addTile = function() { row = Math.floor(Math.random() * 4); column = Math.floor(Math.random() * 4); } - var options = [2,2,4]; + var options = [2,2,2,2,2,2,2,2,2,4]; var value = options[Math.floor(Math.random() * options.length)]; this.board[row][column] = value; }; From 60ff0ae3d7c70a9eea65c4041b6af9825dc895d6 Mon Sep 17 00:00:00 2001 From: emgord Date: Wed, 3 Feb 2016 15:16:40 -0800 Subject: [PATCH 23/46] score working for merge left --- javascripts/2048.js | 57 ++++++++++++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 21 deletions(-) diff --git a/javascripts/2048.js b/javascripts/2048.js index 6ff60b0..2c4e09b 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -3,31 +3,37 @@ var Game = function() { [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]; - // this.board = [[23, 24, 25, 26], - // [27, 28, 29, 30], - // [31, 32, 33, 34], - // [35, 36, 37, 34]]; + // this.board = [[2, 2, 32, 0], + // [2, 2, 32, 0], + // [4, 2, 0, 0], + // [2, 2, 2, 0]]; this.addTile(); this.addTile(); this.win = false; this.lose = false; + this.score = 0; }; var mergeLeftUp = function(array){ + var score = 0; var squished_array = []; while (array.length > 0) { if (array.length === 1) { squished_array.push(array[0]); array.splice(0, 1); } else if (array[0] === array[1]) { - squished_array.push(array[0] * 2); + var mergeVal = array[0] * 2; + squished_array.push(mergeVal); + score += mergeVal; array.splice(0, 2); } else { squished_array.push(array[0]); array.splice(0, 1); } } - return squished_array; + return { squished_array: squished_array, + score: score + }; }; var mergeRightDown = function(array){ @@ -48,22 +54,26 @@ var mergeRightDown = function(array){ }; var leftShifter = function(board) { - for (var row = 0; row < board.length; row++) { - var nonzeros = []; - for (var col = 0; col < board.length; col++) { - if (board[row][col] !== 0) { - nonzeros.push(board[row][col]); - } - } - if (nonzeros.length !== 0) { - nonzeros = mergeLeftUp(nonzeros); +var score = 0; + for (var row = 0; row < board.length; row++) { + var nonzeros = []; + for (var col = 0; col < board.length; col++) { + if (board[row][col] !== 0) { + nonzeros.push(board[row][col]); } - - var numZeros = (board.length - nonzeros.length); - var zeros = new Array(numZeros + 1).join('0').split('').map(parseFloat); - var new_row = nonzeros.concat(zeros); - board[row] = new_row; } + if (nonzeros.length !== 0) { + var mergeReturn = mergeLeftUp(nonzeros); + nonzeros = mergeReturn.squished_array; + score += mergeReturn.score; + } + + var numZeros = (board.length - nonzeros.length); + var zeros = new Array(numZeros + 1).join('0').split('').map(parseFloat); + var new_row = nonzeros.concat(zeros); + board[row] = new_row; + } + return score; }; var rightShifter = function(board) { @@ -131,6 +141,10 @@ function arraysEqual(a1,a2) { return JSON.stringify(a1)==JSON.stringify(a2); } +Game.prototype.updateScore = function(points) { + this.score += points; +}; + Game.prototype.checkLoser = function() { var fakeBoard = this.board.slice(0); upShifter(fakeBoard); @@ -181,7 +195,8 @@ Game.prototype.moveTile = function(tile, direction) { break; case 37: //left // console.log('left'); - leftShifter(this.board); + this.score += leftShifter(this.board); + console.log(this.score); break; case 39: //right // console.log('right'); From 30cc3e0b94486555ecd1551d46c20a9418a2999c Mon Sep 17 00:00:00 2001 From: emgord Date: Wed, 3 Feb 2016 15:32:05 -0800 Subject: [PATCH 24/46] merge left adding score complete --- javascripts/2048.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/javascripts/2048.js b/javascripts/2048.js index 2c4e09b..b775aec 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -37,13 +37,15 @@ var mergeLeftUp = function(array){ }; var mergeRightDown = function(array){ + var score = 0; var squished_array = []; while (array.length > 0) { if (array.length === 1) { squished_array.unshift(array[0]); array.splice(0, 1); } else if (array[array.length-1] === array[array.length-2]) { - squished_array.unshift(array[array.length-1] * 2); + var mergeVal = array[array.length-1] * 2; + squished_array.unshift( * 2); array.splice(array.length-2, 2); } else { squished_array.unshift(array[array.length-1]); From b883caa454d9a33c306149e6092497fdcec8af09 Mon Sep 17 00:00:00 2001 From: emgord Date: Wed, 3 Feb 2016 15:35:21 -0800 Subject: [PATCH 25/46] merge right downnow tracks score --- javascripts/2048.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/javascripts/2048.js b/javascripts/2048.js index b775aec..7f90468 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -45,14 +45,17 @@ var mergeRightDown = function(array){ array.splice(0, 1); } else if (array[array.length-1] === array[array.length-2]) { var mergeVal = array[array.length-1] * 2; - squished_array.unshift( * 2); + squished_array.unshift(mergeVal); + score += mergeVal; array.splice(array.length-2, 2); } else { squished_array.unshift(array[array.length-1]); array.splice(array.length-1, 1); } } - return squished_array; + return { squished_array: squished_array, + score: score + }; }; var leftShifter = function(board) { From 154cb0049fe8e53bc30f7c8bd9ff4a6b98e0beb8 Mon Sep 17 00:00:00 2001 From: emgord Date: Wed, 3 Feb 2016 15:40:54 -0800 Subject: [PATCH 26/46] right shift and down shift add to score --- javascripts/2048.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/javascripts/2048.js b/javascripts/2048.js index 7f90468..2d8c40c 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -82,6 +82,7 @@ var score = 0; }; var rightShifter = function(board) { + var score = 0; for (var row = 0; row < board.length; row++) { var nonzeros = []; for (var col = 0; col < board.length; col++) { @@ -90,7 +91,9 @@ var rightShifter = function(board) { } } if (nonzeros.length !== 0) { - nonzeros = mergeRightDown(nonzeros); + var mergeReturn = mergeRightDown(nonzeros); + nonzeros = mergeReturn.squished_array; + score += mergeReturn.score; } var numZeros = (board.length - nonzeros.length); @@ -98,9 +101,11 @@ var rightShifter = function(board) { var new_row = zeros.concat(nonzeros); board[row] = new_row; } + return score; }; var downShifter = function(board) { + var score = 0; for (var col = 0; col < board.length; col++) { var nonzeros = []; for (var row = 0; row < board.length; row++) { @@ -109,7 +114,9 @@ var downShifter = function(board) { } } if (nonzeros.length !== 0) { - nonzeros = mergeRightDown(nonzeros); + var mergeReturn = mergeRightDown(nonzeros); + nonzeros = mergeReturn.squished_array; + score += mergeReturn.score; } var numZeros = (board.length - nonzeros.length); var zeros = new Array(numZeros + 1).join('0').split('').map(parseFloat); @@ -118,6 +125,7 @@ var downShifter = function(board) { board[i][col] = new_col[i]; } } + return score; }; var upShifter = function(board) { From 2d1981ea16998d6ab31d72bceb11d76e5c48ff32 Mon Sep 17 00:00:00 2001 From: Kelly Date: Wed, 3 Feb 2016 15:41:37 -0800 Subject: [PATCH 27/46] add score for upshift, update moveTile to add score --- javascripts/2048.js | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/javascripts/2048.js b/javascripts/2048.js index 7f90468..93b1415 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -121,6 +121,7 @@ var downShifter = function(board) { }; var upShifter = function(board) { + var score = 0; for (var col = 0; col < board.length; col++) { var nonzeros = []; for (var row = 0; row < board.length; row++) { @@ -130,16 +131,18 @@ var upShifter = function(board) { } if (nonzeros.length !== 0) { - nonzeros = mergeLeftUp(nonzeros); + var mergeReturn = mergeLeftUp(nonzeros); + nonzeros = mergeReturn.squished_array; + score += mergeReturn.score; } var numZeros = (board.length - nonzeros.length); var zeros = new Array(numZeros + 1).join('0').split('').map(parseFloat); var new_col = nonzeros.concat(zeros); for (var i = 0; i < board.length; i++){ - board[i][col] = new_col[i]; - } - + board[i][col] = new_col[i]; + } } + return score; }; function arraysEqual(a1,a2) { @@ -192,20 +195,20 @@ Game.prototype.moveTile = function(tile, direction) { switch(direction) { case 38: //up // console.log('up'); - upShifter(this.board); + this.score += upShifter(this.board); break; case 40: //down - downShifter(this.board); + this.score += downShifter(this.board); // console.log('down'); break; case 37: //left // console.log('left'); this.score += leftShifter(this.board); - console.log(this.score); + break; case 39: //right // console.log('right'); - rightShifter(this.board); + this.score += rightShifter(this.board); break; } }; From 6fe0a331c42d3387815f4fe29ef8d59669f952a7 Mon Sep 17 00:00:00 2001 From: emgord Date: Thu, 4 Feb 2016 10:48:14 -0800 Subject: [PATCH 28/46] about to start refractoring. come back here if things get crazy --- javascripts/2048.js | 1 + 1 file changed, 1 insertion(+) diff --git a/javascripts/2048.js b/javascripts/2048.js index f0e5be7..1a3eb82 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -251,6 +251,7 @@ $(document).ready(function() { game.moveTile(tile, event.which); game.addTile(); game.clearBoard(); + console.log(game.score); game.drawBoard(); game.checkWinner(); if (game.win || game.lose) { From 8791c2d3b51b83675303be696103447e48124eff Mon Sep 17 00:00:00 2001 From: Kelly Date: Thu, 4 Feb 2016 11:18:59 -0800 Subject: [PATCH 29/46] create axisBuilder method and call it from leftShifter --- javascripts/2048.js | 44 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/javascripts/2048.js b/javascripts/2048.js index 1a3eb82..657d675 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -58,8 +58,38 @@ var mergeRightDown = function(array){ }; }; +var axisBuilder = function(boardLength, nonzeros, forward) { + // takes in the array of nonzeros, returns merged reconstructed row or column + // pretend we're doing a left shift of nonzeros = [2,2,2] + // forward true = right and down (mergeRightDown) + // forward false = left and up (mergeLeftUp) + var score = 0; + var mergeReturn; + if (nonzeros.length !== 0) { + if (forward) { + mergeReturn = mergeRightDown(nonzeros); + } else { + mergeReturn = mergeLeftUp(nonzeros); + } + nonzeros = mergeReturn.squished_array; + score += mergeReturn.score; + } + + var numZeros = (boardLength - nonzeros.length); + var zeros = new Array(numZeros + 1).join('0').split('').map(parseFloat); + var rebuiltArray; + if (forward){ + rebuiltArray = zeros.concat(nonzeros); + } else { + rebuiltArray = nonzeros.concat(zeros); + } + return { rebuiltArray: rebuiltArray, + score: score + }; +}; + var leftShifter = function(board) { -var score = 0; +var score = new Number(); for (var row = 0; row < board.length; row++) { var nonzeros = []; for (var col = 0; col < board.length; col++) { @@ -67,15 +97,9 @@ var score = 0; nonzeros.push(board[row][col]); } } - if (nonzeros.length !== 0) { - var mergeReturn = mergeLeftUp(nonzeros); - nonzeros = mergeReturn.squished_array; - score += mergeReturn.score; - } - - var numZeros = (board.length - nonzeros.length); - var zeros = new Array(numZeros + 1).join('0').split('').map(parseFloat); - var new_row = nonzeros.concat(zeros); + var axis = axisBuilder(board.length, nonzeros, false); + var new_row = axis.rebuiltArray; + score += axis.score; board[row] = new_row; } return score; From 61a558d7d78f877a7b6e98185ac3c48573333d6b Mon Sep 17 00:00:00 2001 From: Kelly Date: Thu, 4 Feb 2016 11:21:15 -0800 Subject: [PATCH 30/46] update rightShifter to call axisBuilder --- javascripts/2048.js | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/javascripts/2048.js b/javascripts/2048.js index 657d675..0161f42 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -106,7 +106,7 @@ var score = new Number(); }; var rightShifter = function(board) { - var score = 0; + var score = new Number(); for (var row = 0; row < board.length; row++) { var nonzeros = []; for (var col = 0; col < board.length; col++) { @@ -114,15 +114,9 @@ var rightShifter = function(board) { nonzeros.push(board[row][col]); } } - if (nonzeros.length !== 0) { - var mergeReturn = mergeRightDown(nonzeros); - nonzeros = mergeReturn.squished_array; - score += mergeReturn.score; - } - - var numZeros = (board.length - nonzeros.length); - var zeros = new Array(numZeros + 1).join('0').split('').map(parseFloat); - var new_row = zeros.concat(nonzeros); + var axis = axisBuilder(board.length, nonzeros, true); + var new_row = axis.rebuiltArray; + score += axis.score; board[row] = new_row; } return score; From a3631aca02200b3448b338746ab03b833a727bbe Mon Sep 17 00:00:00 2001 From: Kelly Date: Thu, 4 Feb 2016 11:23:35 -0800 Subject: [PATCH 31/46] update downShifter to call axisBuilder --- javascripts/2048.js | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/javascripts/2048.js b/javascripts/2048.js index 0161f42..8e17a2b 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -123,7 +123,7 @@ var rightShifter = function(board) { }; var downShifter = function(board) { - var score = 0; + var score = new Number(); for (var col = 0; col < board.length; col++) { var nonzeros = []; for (var row = 0; row < board.length; row++) { @@ -131,16 +131,11 @@ var downShifter = function(board) { nonzeros.push(board[row][col]); } } - if (nonzeros.length !== 0) { - var mergeReturn = mergeRightDown(nonzeros); - nonzeros = mergeReturn.squished_array; - score += mergeReturn.score; - } - var numZeros = (board.length - nonzeros.length); - var zeros = new Array(numZeros + 1).join('0').split('').map(parseFloat); - var new_col = zeros.concat(nonzeros); - for (var i = 0; i < board.length; i++){ - board[i][col] = new_col[i]; + var axis = axisBuilder(board.length, nonzeros, true); + var new_col = axis.rebuiltArray; + score += axis.score; + for (var i = 0; i < board.length; i++){ + board[i][col] = new_col[i]; } } return score; From e6fc2d3181bce62b25d2b531cf95be11e286597c Mon Sep 17 00:00:00 2001 From: Kelly Date: Thu, 4 Feb 2016 11:25:18 -0800 Subject: [PATCH 32/46] update upShifter to call axisBuilder --- javascripts/2048.js | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/javascripts/2048.js b/javascripts/2048.js index 8e17a2b..196e1ac 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -142,7 +142,7 @@ var downShifter = function(board) { }; var upShifter = function(board) { - var score = 0; + var score = new Number(); for (var col = 0; col < board.length; col++) { var nonzeros = []; for (var row = 0; row < board.length; row++) { @@ -151,17 +151,12 @@ var upShifter = function(board) { } } - if (nonzeros.length !== 0) { - var mergeReturn = mergeLeftUp(nonzeros); - nonzeros = mergeReturn.squished_array; - score += mergeReturn.score; + var axis = axisBuilder(board.length, nonzeros, false); + var new_col = axis.rebuiltArray; + score += axis.score; + for (var i = 0; i < board.length; i++){ + board[i][col] = new_col[i]; } - var numZeros = (board.length - nonzeros.length); - var zeros = new Array(numZeros + 1).join('0').split('').map(parseFloat); - var new_col = nonzeros.concat(zeros); - for (var i = 0; i < board.length; i++){ - board[i][col] = new_col[i]; - } } return score; }; From f35e4d5f6ce81489d87f32465bfa2b5d22708623 Mon Sep 17 00:00:00 2001 From: Kelly Date: Thu, 4 Feb 2016 11:26:53 -0800 Subject: [PATCH 33/46] update merge function names --- javascripts/2048.js | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/javascripts/2048.js b/javascripts/2048.js index 196e1ac..4ba3eea 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -14,7 +14,7 @@ var Game = function() { this.score = 0; }; -var mergeLeftUp = function(array){ +var mergeBackwards = function(array){ var score = 0; var squished_array = []; while (array.length > 0) { @@ -36,7 +36,7 @@ var mergeLeftUp = function(array){ }; }; -var mergeRightDown = function(array){ +var mergeForwards = function(array){ var score = 0; var squished_array = []; while (array.length > 0) { @@ -59,17 +59,13 @@ var mergeRightDown = function(array){ }; var axisBuilder = function(boardLength, nonzeros, forward) { - // takes in the array of nonzeros, returns merged reconstructed row or column - // pretend we're doing a left shift of nonzeros = [2,2,2] - // forward true = right and down (mergeRightDown) - // forward false = left and up (mergeLeftUp) var score = 0; var mergeReturn; if (nonzeros.length !== 0) { if (forward) { - mergeReturn = mergeRightDown(nonzeros); + mergeReturn = mergeForwards(nonzeros); } else { - mergeReturn = mergeLeftUp(nonzeros); + mergeReturn = mergeBackwards(nonzeros); } nonzeros = mergeReturn.squished_array; score += mergeReturn.score; From fc413f4bd9f64b396b7f3ae1f8b6e9eb34d02464 Mon Sep 17 00:00:00 2001 From: emgord Date: Thu, 4 Feb 2016 11:56:11 -0800 Subject: [PATCH 34/46] master shifter method working for left shift --- javascripts/2048.js | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/javascripts/2048.js b/javascripts/2048.js index 4ba3eea..d7c79a7 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -156,6 +156,35 @@ var upShifter = function(board) { } return score; }; +// forward is a boolean, true indicates down or right, row is a boolean, true is left or right, false is up or down +var shifter = function(board, forward, row) { + var score = new Number(); + var cell; + for (var outer = 0; outer < board.length; outer++) { + var nonzeros = []; + for (var inner = 0; inner < board.length; inner++) { + if (row) { + cell = board[outer][inner]; + } else { + cell = board[inner][outer]; + } + if (cell !== 0) { + nonzeros.push(cell); + } + } + var axisBuildReturn = axisBuilder(board.length, nonzeros, forward); + var new_axis = axisBuildReturn.rebuiltArray; + score += axisBuildReturn.score; + if (row){ + board[outer] = new_axis; + } else { + for (var i = 0; i < board.length; i++){ + board[i][inner] = new_axis[i]; + } + } + } + return score; +}; function arraysEqual(a1,a2) { return JSON.stringify(a1)==JSON.stringify(a2); @@ -215,7 +244,7 @@ Game.prototype.moveTile = function(tile, direction) { break; case 37: //left // console.log('left'); - this.score += leftShifter(this.board); + this.score += shifter(this.board, false ,true); break; case 39: //right From ec6b1a44f887d3b21d9f2767d0c2bad2e1cb1200 Mon Sep 17 00:00:00 2001 From: emgord Date: Thu, 4 Feb 2016 12:12:09 -0800 Subject: [PATCH 35/46] master shifter is working in all directions --- javascripts/2048.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/javascripts/2048.js b/javascripts/2048.js index d7c79a7..999fa89 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -179,7 +179,7 @@ var shifter = function(board, forward, row) { board[outer] = new_axis; } else { for (var i = 0; i < board.length; i++){ - board[i][inner] = new_axis[i]; + board[i][outer] = new_axis[i]; } } } @@ -236,20 +236,20 @@ Game.prototype.moveTile = function(tile, direction) { switch(direction) { case 38: //up // console.log('up'); - this.score += upShifter(this.board); + this.score += shifter(this.board, false, false); break; case 40: //down - this.score += downShifter(this.board); + this.score += shifter(this.board, true, false); // console.log('down'); break; case 37: //left // console.log('left'); - this.score += shifter(this.board, false ,true); + this.score += shifter(this.board, false, true); break; case 39: //right // console.log('right'); - this.score += rightShifter(this.board); + this.score += shifter(this.board, true, true); break; } }; From a2764273de5e649230242894f5d30cdf7a0337ae Mon Sep 17 00:00:00 2001 From: emgord Date: Thu, 4 Feb 2016 12:15:51 -0800 Subject: [PATCH 36/46] deleted unecessary shifter functions. code is now so dry --- javascripts/2048.js | 84 +++------------------------------------------ 1 file changed, 4 insertions(+), 80 deletions(-) diff --git a/javascripts/2048.js b/javascripts/2048.js index 999fa89..20c9fd2 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -84,78 +84,6 @@ var axisBuilder = function(boardLength, nonzeros, forward) { }; }; -var leftShifter = function(board) { -var score = new Number(); - for (var row = 0; row < board.length; row++) { - var nonzeros = []; - for (var col = 0; col < board.length; col++) { - if (board[row][col] !== 0) { - nonzeros.push(board[row][col]); - } - } - var axis = axisBuilder(board.length, nonzeros, false); - var new_row = axis.rebuiltArray; - score += axis.score; - board[row] = new_row; - } - return score; -}; - -var rightShifter = function(board) { - var score = new Number(); - for (var row = 0; row < board.length; row++) { - var nonzeros = []; - for (var col = 0; col < board.length; col++) { - if (board[row][col] !== 0) { - nonzeros.push(board[row][col]); - } - } - var axis = axisBuilder(board.length, nonzeros, true); - var new_row = axis.rebuiltArray; - score += axis.score; - board[row] = new_row; - } - return score; -}; - -var downShifter = function(board) { - var score = new Number(); - for (var col = 0; col < board.length; col++) { - var nonzeros = []; - for (var row = 0; row < board.length; row++) { - if (board[row][col] !== 0) { - nonzeros.push(board[row][col]); - } - } - var axis = axisBuilder(board.length, nonzeros, true); - var new_col = axis.rebuiltArray; - score += axis.score; - for (var i = 0; i < board.length; i++){ - board[i][col] = new_col[i]; - } - } - return score; -}; - -var upShifter = function(board) { - var score = new Number(); - for (var col = 0; col < board.length; col++) { - var nonzeros = []; - for (var row = 0; row < board.length; row++) { - if (board[row][col] !== 0) { - nonzeros.push(board[row][col]); - } - } - - var axis = axisBuilder(board.length, nonzeros, false); - var new_col = axis.rebuiltArray; - score += axis.score; - for (var i = 0; i < board.length; i++){ - board[i][col] = new_col[i]; - } - } - return score; -}; // forward is a boolean, true indicates down or right, row is a boolean, true is left or right, false is up or down var shifter = function(board, forward, row) { var score = new Number(); @@ -190,16 +118,12 @@ function arraysEqual(a1,a2) { return JSON.stringify(a1)==JSON.stringify(a2); } -Game.prototype.updateScore = function(points) { - this.score += points; -}; - Game.prototype.checkLoser = function() { var fakeBoard = this.board.slice(0); - upShifter(fakeBoard); - leftShifter(fakeBoard); - rightShifter(fakeBoard); - downShifter(fakeBoard); + shifter(fakeBoard, true, true); + shifter(fakeBoard, false, true); + shifter(fakeBoard, true, false); + shifter(fakeBoard, false, false); if (arraysEqual(fakeBoard, this.board)) { this.lose = true; } From d0b65dde1892d9cebfb1f27f0f26162dd6680614 Mon Sep 17 00:00:00 2001 From: Kelly Date: Thu, 4 Feb 2016 12:34:14 -0800 Subject: [PATCH 37/46] add h1 header and score to display on page and update CSS --- index.html | 40 ++++++++++++++++++++++------------------ javascripts/2048.js | 8 +++++++- stylesheets/2048.css | 13 +++++++++++++ 3 files changed, 42 insertions(+), 19 deletions(-) diff --git a/index.html b/index.html index 4a7e370..9f6c9f8 100644 --- a/index.html +++ b/index.html @@ -7,24 +7,28 @@ -
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+

2048

+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/javascripts/2048.js b/javascripts/2048.js index 20c9fd2..8053e06 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -189,6 +189,11 @@ Game.prototype.drawBoard = function(){ } }; +Game.prototype.updateScore = function(){ + $("#score").empty(); + $("#score").append(this.score); +}; + Game.prototype.clearBoard = function(){ $(".tile").remove(); }; @@ -198,6 +203,7 @@ $(document).ready(function() { // Any interactive jQuery functionality var game = new Game(); game.drawBoard(); + game.updateScore(); $('body').keydown(function(event){ @@ -208,8 +214,8 @@ $(document).ready(function() { game.moveTile(tile, event.which); game.addTile(); game.clearBoard(); - console.log(game.score); game.drawBoard(); + game.updateScore(); game.checkWinner(); if (game.win || game.lose) { console.log("Game Over, lost: " + game.lose + ", win: " + game.win); diff --git a/stylesheets/2048.css b/stylesheets/2048.css index 2a46a94..5369fe9 100644 --- a/stylesheets/2048.css +++ b/stylesheets/2048.css @@ -2,6 +2,19 @@ html { font: normal normal 30px/1 "Clear Sans", "Helvetica Neue", Arial, sans-serif; } +#all-content { + width: 18.5rem; + margin: auto; +} + +h1 { + text-align: left; +} + +#score { + text-align: right; +} + #gameboard { background: #bbada0; border-radius: 0.5rem; From a4878d4cd33db61e47dc69b8ee45de840fdf6cfe Mon Sep 17 00:00:00 2001 From: Kelly Date: Thu, 4 Feb 2016 14:21:54 -0800 Subject: [PATCH 38/46] Update color scheme --- index.html | 6 +++++- javascripts/2048.js | 20 ++++++++++---------- stylesheets/2048.css | 33 +++++++++++++++++++++------------ 3 files changed, 36 insertions(+), 23 deletions(-) diff --git a/index.html b/index.html index 9f6c9f8..14f7f4f 100644 --- a/index.html +++ b/index.html @@ -9,7 +9,11 @@

2048

-
+
+ SCORE +
+
+
diff --git a/javascripts/2048.js b/javascripts/2048.js index 8053e06..6d7892f 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -1,12 +1,12 @@ var Game = function() { - this.board = [[0, 0, 0, 0], - [0, 0, 0, 0], - [0, 0, 0, 0], - [0, 0, 0, 0]]; - // this.board = [[2, 2, 32, 0], - // [2, 2, 32, 0], - // [4, 2, 0, 0], - // [2, 2, 2, 0]]; + // this.board = [[0, 0, 0, 0], + // [0, 0, 0, 0], + // [0, 0, 0, 0], + // [0, 0, 0, 0]]; + this.board = [[2, 2, 32, 512], + [2, 16, 32, 256], + [4, 2, 64, 128], + [2048, 2, 1024, 8]]; this.addTile(); this.addTile(); this.win = false; @@ -190,8 +190,8 @@ Game.prototype.drawBoard = function(){ }; Game.prototype.updateScore = function(){ - $("#score").empty(); - $("#score").append(this.score); + $("#score-value").empty(); + $("#score-value").append(this.score); }; Game.prototype.clearBoard = function(){ diff --git a/stylesheets/2048.css b/stylesheets/2048.css index 5369fe9..72affd5 100644 --- a/stylesheets/2048.css +++ b/stylesheets/2048.css @@ -5,6 +5,7 @@ html { #all-content { width: 18.5rem; margin: auto; + position: relative; } h1 { @@ -12,11 +13,19 @@ h1 { } #score { - text-align: right; + text-align: center; + background-color: blue; + padding: 2%; + font-size: 0.6em; + width: 5rem; + position: absolute; + top: 0; + right: 0; + border-radius: 0.5rem; } #gameboard { - background: #bbada0; + background: #431c5d; border-radius: 0.5rem; height: 18.5rem; margin: 1rem auto; @@ -61,14 +70,14 @@ h1 { .tile[data-col=c2] { left: 9rem; } .tile[data-col=c3] { left: 13.5rem; } -.tile[data-val="2"] { color: #776e65; background-color: #eee4da; } -.tile[data-val="4"] { color: #776e65; background-color: #ede0c8; } -.tile[data-val="8"] { background-color: #f2b179; } -.tile[data-val="16"] { background-color: #f59563; } -.tile[data-val="32"] { background: #f67c5f; } +.tile[data-val="2"] { color: #431c5d; background-color: #bccbde; } +.tile[data-val="4"] { color: #431c5d; background-color: #cdd422; } +.tile[data-val="8"] { background-color: #e05915; } +.tile[data-val="16"] { background-color: #ff1a75; } +.tile[data-val="32"] { background: #3333ff; } .tile[data-val="64"] { background: #f65e3b; } -.tile[data-val="128"] { background: #edcf72; } -.tile[data-val="256"] { background: #edcc61; } -.tile[data-val="512"] { background: #edc850; } -.tile[data-val="1024"] { background: #edc53f; } -.tile[data-val="2048"] { background: #edc22e; } +.tile[data-val="128"] { background: #ff9800; } +.tile[data-val="256"] { background: #e040fb; } +.tile[data-val="512"] { color: #431c5d; background: #64ffda; } +.tile[data-val="1024"] { color: #431c5d; background: #97ff00; } +.tile[data-val="2048"] { background: #259b24; } From 05c48cc59e92cb17006e59767b7ca43cc232f43a Mon Sep 17 00:00:00 2001 From: emgord Date: Thu, 4 Feb 2016 14:35:38 -0800 Subject: [PATCH 39/46] color scheme on score --- javascripts/2048.js | 18 +++++++++--------- stylesheets/2048.css | 16 +++++++++++++--- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/javascripts/2048.js b/javascripts/2048.js index 6d7892f..710fceb 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -1,17 +1,17 @@ var Game = function() { - // this.board = [[0, 0, 0, 0], - // [0, 0, 0, 0], - // [0, 0, 0, 0], - // [0, 0, 0, 0]]; - this.board = [[2, 2, 32, 512], - [2, 16, 32, 256], - [4, 2, 64, 128], - [2048, 2, 1024, 8]]; + this.board = [[0, 0, 0, 0], + [0, 0, 0, 0], + [0, 0, 0, 0], + [0, 0, 0, 0]]; + // this.board = [[2, 2, 32, 512], + // [2, 16, 32, 256], + // [4, 2, 64, 128], + // [2048, 2, 1024, 8]]; this.addTile(); this.addTile(); this.win = false; this.lose = false; - this.score = 0; + this.score = 2036; }; var mergeBackwards = function(array){ diff --git a/stylesheets/2048.css b/stylesheets/2048.css index 72affd5..e7ca86e 100644 --- a/stylesheets/2048.css +++ b/stylesheets/2048.css @@ -1,5 +1,6 @@ html { font: normal normal 30px/1 "Clear Sans", "Helvetica Neue", Arial, sans-serif; + background-color: #e6e9f0; } #all-content { @@ -10,18 +11,27 @@ html { h1 { text-align: left; + color: #431c5d; + font-size: 3em; } #score { text-align: center; - background-color: blue; - padding: 2%; + background-color: #431c5d; + padding: 2.5%; font-size: 0.6em; - width: 5rem; + width: 3rem; position: absolute; top: 0; right: 0; border-radius: 0.5rem; + color: #e6e9f0; +} +#score-value{ + padding-top: 12px; + font-size: 2em; + font-weight: bold; + color: #bccbde; } #gameboard { From c81fa9ae711666696b7366bd8c7adb49777cf058 Mon Sep 17 00:00:00 2001 From: emgord Date: Thu, 4 Feb 2016 15:59:15 -0800 Subject: [PATCH 40/46] floating point animantion working --- index.html | 3 +++ javascripts/2048.js | 30 +++++++++++++++++++++++------- stylesheets/2048.css | 10 ++++++++++ 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/index.html b/index.html index 14f7f4f..706754a 100644 --- a/index.html +++ b/index.html @@ -13,6 +13,9 @@

2048

SCORE
+
+
+
diff --git a/javascripts/2048.js b/javascripts/2048.js index 710fceb..e932cfb 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -11,7 +11,7 @@ var Game = function() { this.addTile(); this.win = false; this.lose = false; - this.score = 2036; + this.score = 0; }; var mergeBackwards = function(array){ @@ -155,25 +155,41 @@ Game.prototype.addTile = function() { this.board[row][column] = value; }; +var floatPoints = function(points) { + $("#point-float").append("+" + points); + $("#point-float").animate({ + top: '-=100px', opacity: 0}, 1000, function(){ + $(this).css({'top': '+=100px', 'opacity':100}); + $("#point-float").empty(); + } ); +}; + Game.prototype.moveTile = function(tile, direction) { - // Game method here + var points; switch(direction) { case 38: //up // console.log('up'); - this.score += shifter(this.board, false, false); + points = shifter(this.board, false, false); + this.score += points; + if (points !== 0){ floatPoints(points); } break; case 40: //down - this.score += shifter(this.board, true, false); + points = shifter(this.board, true, false); + this.score += points; + if (points !== 0){ floatPoints(points); } // console.log('down'); break; case 37: //left // console.log('left'); - this.score += shifter(this.board, false, true); - + points = shifter(this.board, false, true); + this.score += points; + if (points !== 0){ floatPoints(points); } break; case 39: //right // console.log('right'); - this.score += shifter(this.board, true, true); + points = shifter(this.board, true, true); + this.score += points; + if (points !== 0){ floatPoints(points); } break; } }; diff --git a/stylesheets/2048.css b/stylesheets/2048.css index e7ca86e..8381c97 100644 --- a/stylesheets/2048.css +++ b/stylesheets/2048.css @@ -27,6 +27,16 @@ h1 { border-radius: 0.5rem; color: #e6e9f0; } + +#point-float { + position: absolute; + top: 6.5%; + right: 9%; + font-size: 1.2em; + font-weight: bold; + color: #ff1a75; + +} #score-value{ padding-top: 12px; font-size: 2em; From e9b23068bc09347cd9eadbd2c04801082bc2d414 Mon Sep 17 00:00:00 2001 From: emgord Date: Thu, 4 Feb 2016 16:05:22 -0800 Subject: [PATCH 41/46] changed position of point float --- index.html | 2 +- stylesheets/2048.css | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/index.html b/index.html index 706754a..4718c6f 100644 --- a/index.html +++ b/index.html @@ -15,7 +15,7 @@

2048

- +
diff --git a/stylesheets/2048.css b/stylesheets/2048.css index 8381c97..c30a885 100644 --- a/stylesheets/2048.css +++ b/stylesheets/2048.css @@ -31,7 +31,7 @@ h1 { #point-float { position: absolute; top: 6.5%; - right: 9%; + right: 6.5%; font-size: 1.2em; font-weight: bold; color: #ff1a75; From d9ff0abb781b76c4449b2cc6848af245327450c9 Mon Sep 17 00:00:00 2001 From: emgord Date: Fri, 5 Feb 2016 12:04:18 -0800 Subject: [PATCH 42/46] added new font for title --- index.html | 3 ++- javascripts/2048.js | 1 - stylesheets/2048.css | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/index.html b/index.html index 4718c6f..782a096 100644 --- a/index.html +++ b/index.html @@ -3,6 +3,7 @@ 2048 + @@ -15,7 +16,7 @@

2048

- +
diff --git a/javascripts/2048.js b/javascripts/2048.js index e932cfb..5581027 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -215,7 +215,6 @@ Game.prototype.clearBoard = function(){ }; $(document).ready(function() { - console.log("ready to go!"); // Any interactive jQuery functionality var game = new Game(); game.drawBoard(); diff --git a/stylesheets/2048.css b/stylesheets/2048.css index c30a885..c01ac13 100644 --- a/stylesheets/2048.css +++ b/stylesheets/2048.css @@ -12,7 +12,8 @@ html { h1 { text-align: left; color: #431c5d; - font-size: 3em; + font-size: 4em; + font-family: 'Neucha', cursive; } #score { From 1f9722f8038fc54755e21b195e1796d24d4766b5 Mon Sep 17 00:00:00 2001 From: emgord Date: Fri, 5 Feb 2016 15:00:46 -0800 Subject: [PATCH 43/46] handle if game is won or lost, and new game button --- index.html | 7 +++---- javascripts/2048.js | 42 +++++++++++++++++++++++++++++------------- stylesheets/2048.css | 14 ++++++++++++++ 3 files changed, 46 insertions(+), 17 deletions(-) diff --git a/index.html b/index.html index 782a096..ee73554 100644 --- a/index.html +++ b/index.html @@ -12,12 +12,11 @@

2048

SCORE -
-
+
-
+
+ -
diff --git a/javascripts/2048.js b/javascripts/2048.js index 5581027..0d04cd8 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -1,12 +1,16 @@ var Game = function() { - this.board = [[0, 0, 0, 0], - [0, 0, 0, 0], - [0, 0, 0, 0], - [0, 0, 0, 0]]; - // this.board = [[2, 2, 32, 512], - // [2, 16, 32, 256], - // [4, 2, 64, 128], - // [2048, 2, 1024, 8]]; + this.newGame(); +}; + +Game.prototype.newGame = function() { + // this.board = [[0, 0, 0, 0], + // [0, 0, 0, 0], + // [0, 0, 0, 0], + // [0, 0, 0, 0]]; + this.board = [[2, 2, 32, 512], + [2, 16, 320, 256], + [4, 2, 64, 128], + [2048, 90, 1024, 8]]; this.addTile(); this.addTile(); this.win = false; @@ -129,7 +133,6 @@ Game.prototype.checkLoser = function() { } }; - Game.prototype.checkWinner = function() { var flattenedBoard = [].concat.apply([],this.board); if (flattenedBoard.indexOf(2048) !== -1) { @@ -219,9 +222,10 @@ $(document).ready(function() { var game = new Game(); game.drawBoard(); game.updateScore(); - - $('body').keydown(function(event){ + if (game.lose || game.win) { + return; + } var arrows = [37, 38, 39, 40]; if (arrows.indexOf(event.which) > -1) { var tile = $('.tile'); @@ -232,9 +236,21 @@ $(document).ready(function() { game.drawBoard(); game.updateScore(); game.checkWinner(); - if (game.win || game.lose) { - console.log("Game Over, lost: " + game.lose + ", win: " + game.win); + if (game.win) { + $("#gameboard").append("

You WIN!!!!!!

"); + } else if (game.lose) { + $("#gameboard").append("

Game Over!

"); } } }); + + $('button').click(function(){ + $("#outcome").remove(); + game.newGame(); + game.clearBoard(); + game.drawBoard(); + game.updateScore(); + console.log("Hi"); + }); + }); diff --git a/stylesheets/2048.css b/stylesheets/2048.css index c01ac13..9bf240d 100644 --- a/stylesheets/2048.css +++ b/stylesheets/2048.css @@ -16,6 +16,20 @@ h1 { font-family: 'Neucha', cursive; } +#outcome { + color: yellow; + background-color: rgba(238, 204, 255, .9); + position: absolute; + width: 18.5em; + height: 18.5em; + border-radius: 0.5rem; + z-index: 1; +} +/*#lost.div > p { + position: absolute; + text-align: center; +}*/ + #score { text-align: center; background-color: #431c5d; From 33ec152b6ebd764ea193d631f61c64e92c4c0e8c Mon Sep 17 00:00:00 2001 From: Kelly Date: Fri, 5 Feb 2016 15:21:25 -0800 Subject: [PATCH 44/46] Style new game button --- javascripts/2048.js | 16 ++++++++-------- stylesheets/2048.css | 12 ++++++++++++ 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/javascripts/2048.js b/javascripts/2048.js index 0d04cd8..1a1a2d3 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -3,14 +3,14 @@ var Game = function() { }; Game.prototype.newGame = function() { - // this.board = [[0, 0, 0, 0], - // [0, 0, 0, 0], - // [0, 0, 0, 0], - // [0, 0, 0, 0]]; - this.board = [[2, 2, 32, 512], - [2, 16, 320, 256], - [4, 2, 64, 128], - [2048, 90, 1024, 8]]; + this.board = [[0, 0, 0, 0], + [0, 0, 0, 0], + [0, 0, 0, 0], + [0, 0, 0, 0]]; + // this.board = [[2, 2, 32, 512], + // [2, 16, 320, 256], + // [4, 2, 64, 128], + // [2048, 90, 1024, 8]]; this.addTile(); this.addTile(); this.win = false; diff --git a/stylesheets/2048.css b/stylesheets/2048.css index 9bf240d..2015d1d 100644 --- a/stylesheets/2048.css +++ b/stylesheets/2048.css @@ -3,6 +3,18 @@ html { background-color: #e6e9f0; } +button { + background: #2F4F4F; + color: #ffffe5; + border-radius: 0.5rem; + padding: 2.5%; + font-size: 0.5em; + position: absolute; + top: 16%; + right: 1%; + +} + #all-content { width: 18.5rem; margin: auto; From 59c36c04b73885bf687ddab922edc46ab9c06a57 Mon Sep 17 00:00:00 2001 From: emgord Date: Fri, 5 Feb 2016 15:21:43 -0800 Subject: [PATCH 45/46] added styling to win or lose --- javascripts/2048.js | 2 +- stylesheets/2048.css | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/javascripts/2048.js b/javascripts/2048.js index 0d04cd8..3f70b67 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -10,7 +10,7 @@ Game.prototype.newGame = function() { this.board = [[2, 2, 32, 512], [2, 16, 320, 256], [4, 2, 64, 128], - [2048, 90, 1024, 8]]; + [1048, 90, 1024, 8]]; this.addTile(); this.addTile(); this.win = false; diff --git a/stylesheets/2048.css b/stylesheets/2048.css index 9bf240d..0f77e43 100644 --- a/stylesheets/2048.css +++ b/stylesheets/2048.css @@ -17,7 +17,7 @@ h1 { } #outcome { - color: yellow; + color: #2F4F4F; background-color: rgba(238, 204, 255, .9); position: absolute; width: 18.5em; @@ -25,10 +25,14 @@ h1 { border-radius: 0.5rem; z-index: 1; } -/*#lost.div > p { - position: absolute; +div > p { + vertical-align: middle; text-align: center; -}*/ + font-size: 5em; + font-family: 'Neucha', cursive; + /*position: absolute; + top: 37%;*/ +} #score { text-align: center; From 97e7f91793a16725d59802e32b305ab7e2811574 Mon Sep 17 00:00:00 2001 From: emgord Date: Fri, 5 Feb 2016 15:55:45 -0800 Subject: [PATCH 46/46] bounce not working, back to previous iteration --- javascripts/2048.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/javascripts/2048.js b/javascripts/2048.js index 3f70b67..da6b5b6 100644 --- a/javascripts/2048.js +++ b/javascripts/2048.js @@ -3,14 +3,14 @@ var Game = function() { }; Game.prototype.newGame = function() { - // this.board = [[0, 0, 0, 0], - // [0, 0, 0, 0], - // [0, 0, 0, 0], - // [0, 0, 0, 0]]; - this.board = [[2, 2, 32, 512], - [2, 16, 320, 256], - [4, 2, 64, 128], - [1048, 90, 1024, 8]]; + this.board = [[0, 0, 0, 0], + [0, 0, 0, 0], + [0, 0, 0, 0], + [0, 0, 0, 0]]; + // this.board = [[2, 2, 32, 512], + // [2, 16, 320, 256], + // [4, 2, 64, 128], + // [1048, 90, 1024, 8]]; this.addTile(); this.addTile(); this.win = false;