Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arh/master - 2048 Project #15

Open
wants to merge 50 commits into
base: arh/master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
cbbe623
Add arrow key functionality to move a single tile. Placement is sligh…
lacuchilla Feb 1, 2016
73266b8
add loop to start being able to increment how far to one direction ti…
hougland Feb 2, 2016
6c8c5c4
Add ability to move tile one place and have it register on the back e…
lacuchilla Feb 2, 2016
4c06a24
remove debugging statements
hougland Feb 2, 2016
c7d07eb
add backend logic for square moving left
hougland Feb 2, 2016
8891371
Begin work on getting the right arrow key to function, not working yet.
lacuchilla Feb 2, 2016
fad7cbb
remove front end stuff
hougland Feb 2, 2016
f010384
Add up key back end functionality
lacuchilla Feb 2, 2016
8113567
get down button working
hougland Feb 2, 2016
1eef911
edit comments in down functionality
hougland Feb 2, 2016
d27f162
Fixed left key functionality. Now works.
lacuchilla Feb 2, 2016
64d0b3e
begin work on tile collision function
lacuchilla Feb 3, 2016
9e62490
in processes of switching down foreach loop into for loop
hougland Feb 3, 2016
178b3f5
fix but so down arrow correctly moves tiles down now
hougland Feb 3, 2016
2509554
fix tiles moving up so they do it correctly now
hougland Feb 3, 2016
34efbcf
Add thoughts file to project to record thought processes and organize…
lacuchilla Feb 3, 2016
6290850
fix tile movement going up againnnnn:
hougland Feb 3, 2016
5dda373
move tile movement functionality into its own methods
hougland Feb 3, 2016
85eee4e
lol fixing logic againnnn for getting tiles to slide right when you h…
hougland Feb 3, 2016
75381c8
debugg downTileCollision function to work
hougland Feb 3, 2016
4c5a908
get left tile collision function working:
hougland Feb 3, 2016
73b3649
begin working on right keypress tile collision function
hougland Feb 3, 2016
cc1c057
Got function to work to pick a random number within a range. Added fi…
lacuchilla Feb 4, 2016
7d7b345
Merge branch 'arh/master' of github.com:lacuchilla/2048 into arh/master
lacuchilla Feb 4, 2016
8850554
tiles will collide when you press right
hougland Feb 4, 2016
e54f609
Merge branch 'arh/master' of github.com:lacuchilla/2048 into arh/master
hougland Feb 4, 2016
ccb7fe2
is game lost function works to check if gamebaord is full, not yet if…
hougland Feb 4, 2016
ee7b56e
Add code to determine indices of empty spaces
lacuchilla Feb 4, 2016
40207d2
Add more logic to pull a two or a 4 and assign it to an empty value i…
lacuchilla Feb 4, 2016
50ce935
Merge branch 'arh/master' of github.com:lacuchilla/2048 into arh/master
lacuchilla Feb 4, 2016
f33e909
add logic to is game lost function to check if any tiles are next to …
hougland Feb 4, 2016
4c5a2bf
Merge branch 'arh/master' of github.com:lacuchilla/2048 into arh/master
hougland Feb 4, 2016
3285c43
Add functions to add tiles and check empty spots on tiles. Functions …
lacuchilla Feb 4, 2016
88c8f14
Merge branch 'arh/master' of github.com:lacuchilla/2048 into arh/master
lacuchilla Feb 4, 2016
de04752
Integrated working add tile and get blank spaces functions into main …
lacuchilla Feb 4, 2016
0395e33
Add function to check if a 2048 tile is present. It wooooooorks.
lacuchilla Feb 4, 2016
60f0c99
begin adding scoring functionality
hougland Feb 4, 2016
eb32920
Merge branch 'arh/master' of github.com:lacuchilla/2048 into arh/master
hougland Feb 4, 2016
6e487f1
Ch ch ch ch chaaaaaanges
lacuchilla Feb 4, 2016
d3f70ab
Merge branch 'arh/master' of github.com:lacuchilla/2048 into arh/master
lacuchilla Feb 4, 2016
abb9297
merge issue
hougland Feb 4, 2016
2953e0d
Merge branch 'arh/master' of github.com:lacuchilla/2048 into arh/master
hougland Feb 4, 2016
e6c9237
add console.log score to all 4 directional keys
lacuchilla Feb 4, 2016
7e1b262
minor changes for debugging
hougland Feb 5, 2016
665374a
Add back if that mysteriously went missing.
lacuchilla Feb 5, 2016
d4f5be7
Merge branch 'arh/master' of github.com:lacuchilla/2048 into arh/master
hougland Feb 5, 2016
5291d9a
debugging
hougland Feb 5, 2016
c7c7788
Debugging tile movement and tile collision functions. Still buggy.
lacuchilla Feb 5, 2016
c161005
add basic front end
hougland Feb 5, 2016
de5ccb8
reset initialize info to place 2 random tiles like a reallll game
hougland Feb 5, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions amysplayground.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
var gameBoard = [[0, 2, 0, 0], [0, 2, 0, 0], [0, 2048, 4, 0], [0, 0, 4, 0]];
var tile2048present = false;

function getFreeSpaces(){
var freeSpaces = [];
// 3. Iterate through the rows and the columns in the gameboard
for(var i = 0; i < 4; i++){
for(var j = 0; j < 4; j++){
// and find all that are empty
if (gameBoard[i][j] === 0){
// keep a list of the indices in a new array saved as a variable
freeSpaces.push([i, j]);
}
}
}
return freeSpaces;
};

function addOneTile(){
var num = Math.random();

var result;
if (num < 0.9) {
result = 2;
} else {
result = 4;
}

var openSpots = this.getFreeSpaces();
var selectedSpot = openSpots[Math.floor(Math.random() * openSpots.length)];
gameBoard[selectedSpot[0]][selectedSpot[1]] = result;
console.log(selectedSpot);
return result;
}

function isThere2048(){
for(var i = 0; i < 4; i++){
for(var j = 0; j < 4; j++){
// and find all that are empty
if (gameBoard[i][j] === 2048){
// keep a list of the indices in a new array saved as a variable
tile2048present = true;
}
}
}
return tile2048present;
}
console.log(getFreeSpaces());
console.log(addOneTile());
console.log(isThere2048());
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<div class="cell"></div>
<div class="cell"></div>
</div>
<div class="tile" data-row="r1", data-col="c1" data-val="2">2</div>

</div>
</body>
</html>
305 changes: 291 additions & 14 deletions javascripts/2048.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,316 @@
var Game = function() {
// Game logic and initialization here
this.gameBoard = [[0, 0, 0, 0], [0, 2, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]];
this.gameBoard = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]];
this.gameLost = false;
this.addOneTile();
this.addOneTile();
this.showBoard();
this.score = 0;
};


$(document).ready(function() {
console.log("ready to go!");
// Any interactive jQuery functionality
var game = new Game();
console.log(game.gameBoard);

$('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
var self = this;
if (self.gameLost === false) {
switch(direction) {
case 38: //up
console.log('up');
this.gameBoard = [[0, 2, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]];
$('.tile').animate({ top: '-=134'}, 100);
self.upMoveTiles();
self.upTileCollision();
self.upMoveTiles();
self.isGameLost();
self.addOneTile();
console.log(self.gameBoard);
self.isGameLost();
console.log(self.score);
console.log(self.gameLost);
this.showBoard();
break;
case 40: //down
console.log('down');
self.downMoveTiles();
self.downTileCollision();
self.downMoveTiles();
self.isGameLost();
self.addOneTile();
console.log(self.gameBoard);
self.isGameLost();
console.log(self.score);
console.log(self.gameLost);
this.showBoard();
break;
case 37: //left
console.log('left');
self.leftMoveTiles();
self.leftTileCollision();
self.leftMoveTiles();
self.isGameLost();
self.addOneTile();
console.log(self.gameBoard);
self.isGameLost();
console.log(self.score);
console.log(self.gameLost);
this.showBoard();
break;
case 39: //right
console.log('right');
self.rightMoveTiles();
self.rightTileCollision();
self.rightMoveTiles();
self.isGameLost();
self.addOneTile();
console.log(self.gameBoard);
self.isGameLost();
console.log(self.score);
console.log(self.gameLost);
this.showBoard();
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.prototype.upMoveTiles = function(){
var self = this;
// iterate through each "row" (each array within gameboard array)
self.gameBoard.forEach(function(row, rowIndex){
// iterate through each "column" (each item within a row)
row.forEach(function(column, columnIndex){
// don't look at tiles that don't have a a value to them, AND don't look left if column is farthest left
if (column !== 0 && rowIndex !== 0) {
// iterate through each item further left of current item
for (var i = rowIndex - 1; i >= 0; i--) {
// increment numSpaces to move up by 1 if the next tile up is 0
if (self.gameBoard[i][columnIndex] === 0) {
self.gameBoard[i][columnIndex] = column;
self.gameBoard[i + 1][columnIndex] = 0;
}
}
}
});
});
};

game.moveTile(tile, event.which);
Game.prototype.downMoveTiles = function(){
var self = this;
// iterate through each "row" (each array within gameboard array)
for (var k = 2; k >= 0; k--) {
var row = self.gameBoard[k], rowIndex = k;
// iterate through each "column" (each item within a row)
for (var j = 0; j <= 3; j++) {
// row.forEach(function(column, columnIndex){
var column = row[j], columnIndex = j;
// don't look at tiles that don't have a a value to them, AND don't look left if column is farthest down already
if (column !== 0) {
// iterate through each item further up of current item
for (var i = rowIndex; i < 3; i++) {
// increment numSpaces to move down by 1 if the next num down is 0
if (self.gameBoard[i + 1][columnIndex] === 0) {
self.gameBoard[i + 1][columnIndex] = column;
self.gameBoard[i][columnIndex] = 0;
}
}
}
}
}
};

Game.prototype.leftMoveTiles = function(){
var self = this;
// iterate through each "row" (each array within gameboard array)
self.gameBoard.forEach(function(row, rowIndex){
// iterate through each "column" (each item within a row)
row.forEach(function(column, columnIndex){
var numSpaces = 0;
// don't look at tiles that don't have a a value to them, AND don't look left if column is farthest left
if (column !== 0 && columnIndex !== 0) {
// iterate through each item further left of current item
for (var i = columnIndex; i >= 0; i--) {
// increment numSpaces to move left by 1 if the next left over is 0
if (row[i - 1] === 0) {
row[i - 1] = column;
row[i] = 0;
}
}
}
});
});
});
};

Game.prototype.rightMoveTiles = function(){
var self = this;
// iterate through each "row" (each array within gameboard array)
for (var m = 0; m < self.gameBoard.length; m++) {
// iterate through each "column" (each item within a row)
var row = self.gameBoard[m], rowIndex = m;

for (var j = 3; j >= 0; j--) {
var column = row[j], columnIndex = j;
// don't look at tiles that don't have a a value to them, AND don't look right if column is farthest right
if (column !== 0) {
for (var i = columnIndex + 1; i <= 3; i++) {
// increment numSpaces to move right by 1 if the next right over is 0
if (row[i] === 0) {
// select current tile and move it appropriate num spaces right
row[i] = column;
row[i - 1] = 0;
}
}
}
}
}
};

Game.prototype.upTileCollision = function(){
var self = this;
for(var i = 1; i <= 3; i++){
for(var j = 0; j <= 3; j++){
if(self.gameBoard[i][j] > 0){
if(self.gameBoard[i - 1][j] === self.gameBoard[i][j]){
self.gameBoard[i - 1][j] *= 2;
self.score += self.gameBoard[i - 1][j];
self.gameBoard[i][j] = 0;
}
}
}
}
};

Game.prototype.downTileCollision = function(){
var self = this;
for (var i = 3; i >= 1; i--) { // iterate through each row
for (var j = 0; j <= 3; j++) { // iterate through each tile in each row
if (self.gameBoard[i][j] > 0) {
if (self.gameBoard[i - 1][j] === self.gameBoard[i][j]) {
self.gameBoard[i][j] *= 2;
self.score += self.gameBoard[i][j];
self.gameBoard[i - 1][j] = 0;
}
}
}
}
};

Game.prototype.leftTileCollision = function(){
var self = this;
for (var i = 0; i <= 3; i++) { // iterate through each row
for (var j = 1; j <= 3; j++) { // iterate through each tile in each row
if (self.gameBoard[i][j] > 0) {
if (self.gameBoard[i][j - 1] === self.gameBoard[i][j]) {
self.gameBoard[i][j] *= 2;
self.score += self.gameBoard[i][j];
self.gameBoard[i][j - 1] = 0;
}
}
}
}
};

Game.prototype.rightTileCollision = function(){
var self = this;
for (var i = 0; i <= 3; i++) { // iterate through each row
for (var j = 3; j >= 1; j--) { // iterate through each tile in each row
if (self.gameBoard[i][j] > 0) {
if (self.gameBoard[i][j - 1] === self.gameBoard[i][j]) {
self.gameBoard[i][j] *= 2;
self.score += self.gameBoard[i][j];
self.gameBoard[i][j - 1] = 0;
}
}
}
}
};

Game.prototype.flattenNums = function() {
var flattened = this.gameBoard.reduce(function(a, b) {
return a.concat(b);
}, []);
return flattened;
};

Game.prototype.isGameLost = function() {
// check if any number in flattened game board is 0
var flattenedNums = this.flattenNums(), isGameLost = true, self = this;
for (var i = 0; i < flattenedNums.length; i++) {
if (flattenedNums[i] === 0) {
isGameLost = false;
}
}

// check if any tiles can be combined with the tile to the right, or below
for (var m = 0; m <= 3; m++) {
for (var j = 0; j <= 3; j++) {
if (self.gameBoard[m][j] === self.gameBoard[m][j + 1]){
isGameLost = false;
}
if (self.gameBoard[m + 1] !== undefined && self.gameBoard[m][j] === self.gameBoard[m + 1][j]) {
isGameLost = false;
}
}
}
self.gameLost = isGameLost;
return isGameLost;
};

Game.prototype.getFreeSpaces = function(){
var freeSpaces = [];
// 3. Iterate through the rows and the columns in the gameboard
for(var i = 0; i <= 3; i++){
for(var j = 0; j <= 3; j++){
// and find all that are empty
if(this.gameBoard[i][j] === 0){
// keep a list of the indices in a new array saved as a variable
freeSpaces.push([i, j]);
}
}
}
return freeSpaces;
};

Game.prototype.addOneTile = function(){
var num = Math.random();

var result;
if (num < 0.9) {
result = 2;
} else {
result = 4;
}

var openSpots = this.getFreeSpaces();
var selectedSpot = openSpots[Math.floor(Math.random() * openSpots.length)];
this.gameBoard[selectedSpot[0]][selectedSpot[1]] = result;
console.log(this.gameBoard[selectedSpot[0]][selectedSpot[1]]);
console.log(selectedSpot, result);
return result;
};

Game.prototype.showBoard = function() {
var self = this;
$('.tile').remove();

for (var r = 0; r <= 3; r++) {
for (var c = 0; c <= 3; c++) {
if (this.gameBoard[r][c] > 0) {
var tile = this.gameBoard[r][c];
$('#gameboard').append('<div class="tile" data-row=r' + r + ' data-col=c' + c + ' data-val=' + tile + '>' + tile + '</div>');
}
}
}
};
Loading