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

2048 Game #19

Open
wants to merge 71 commits into
base: adah/master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
71 commits
Select commit Hold shift + click to select a range
daa4570
Able to move tiles all to one side
Dreedle Feb 2, 2016
914a62b
Working on conditional to move tiles
Dreedle Feb 2, 2016
2ebd5ad
Playing with identifying tiles by column
Dreedle Feb 2, 2016
a0a81a2
Trying out a Tile function to make an array of tiles on keydown
Dreedle Feb 2, 2016
f3a9399
Able to move tiles using Tile objects
Dreedle Feb 2, 2016
2df69d2
Adding four directions for moving
Dreedle Feb 2, 2016
ad169a2
Adding row and col to numbers in the Tile function
Dreedle Feb 2, 2016
6f55cb6
Making function for Board
Dreedle Feb 2, 2016
a328b3b
Working on method to update the dom from the board
Dreedle Feb 2, 2016
e5f4216
Fixing merge conflict
Dreedle Feb 2, 2016
3bd076f
Fixing merge
Dreedle Feb 2, 2016
1bb7ba8
Deleting extra code
Dreedle Feb 2, 2016
4b4bcca
Able to amend gameboard div from board
Dreedle Feb 2, 2016
50e945e
Method to place tile on a board
Dreedle Feb 2, 2016
bfc204c
Debugging
Dreedle Feb 2, 2016
ead0238
Took out illegal breaks
Dreedle Feb 2, 2016
3e8fb2a
fix errors from atom
annaleeherrera Feb 2, 2016
c729845
Debug
Dreedle Feb 2, 2016
9c7d4b9
Better semicolons
Dreedle Feb 2, 2016
1af4fc9
Working out infinite loop
Dreedle Feb 2, 2016
47ef9eb
Debug
Dreedle Feb 3, 2016
c125531
Fixed infinite loop
Dreedle Feb 3, 2016
5729121
Able to add a random value but only in one position
Dreedle Feb 3, 2016
d84ee73
Able to create a tile that is the right color
Dreedle Feb 3, 2016
c821630
Debugging tile creation
Dreedle Feb 3, 2016
c62e096
Able to add random numbers only to top row
Dreedle Feb 3, 2016
40080e9
Able to add some random nnumbers but not in all positions
Dreedle Feb 3, 2016
d18fed8
Loop problem solved- now able to add random tiles until they fill the…
Dreedle Feb 3, 2016
5abe053
2 new tiles on new game
annaleeherrera Feb 3, 2016
d381f4c
Working on movetiles method
Dreedle Feb 3, 2016
5bbb300
Debugging movetiles
Dreedle Feb 3, 2016
190de46
Fixing bug in nextSpot
Dreedle Feb 3, 2016
0bf5f91
Changing response from nextspot method
Dreedle Feb 3, 2016
030eb2a
making an object as a response to nextspot
Dreedle Feb 4, 2016
e05f613
Refactoring nextSpot
Dreedle Feb 4, 2016
8afe4ca
Buggy but working on looking at next tile
Dreedle Feb 4, 2016
4bec2ee
Building movetile
Dreedle Feb 4, 2016
a3ca1e1
Debuggin move tile
Dreedle Feb 4, 2016
1baae84
Merge branch 'adah/master' of github.com:annaleeherrera/2048 into ada…
Dreedle Feb 4, 2016
6e66848
Values are able to merge...incorrectly
Dreedle Feb 4, 2016
ebeccf4
Passing in value to lookatspot
Dreedle Feb 4, 2016
9b88d3a
Getting better behavior
Dreedle Feb 4, 2016
1c9a7b8
Able to fill board up again, at least
Dreedle Feb 4, 2016
60ea8fd
Able to combine values and not be in an infinite loop
Dreedle Feb 4, 2016
10c8bb9
Able to add and take away numbers, seemingly randomly
Dreedle Feb 4, 2016
bedd245
Working on movetile
Dreedle Feb 4, 2016
06b22ce
Left is sort of working. Other directions arent
Dreedle Feb 4, 2016
a8184f8
To the left, tiles move farther but not all the way to the end
Dreedle Feb 4, 2016
c0f048f
Removing unnecessary line
Dreedle Feb 5, 2016
62bf9fb
Game works to the left and upward, without tile swooshing
Dreedle Feb 5, 2016
84393ba
Game is playable but doesn't do nice animation
Dreedle Feb 5, 2016
6983624
Removing extra line in keydown event
Dreedle Feb 5, 2016
9e59694
Fooling around
Dreedle Feb 5, 2016
0ca6901
foolin
Dreedle Feb 5, 2016
72355b7
Working on shifting tiles instead of adding/removing divs
Dreedle Feb 5, 2016
fd47ebd
Working on making tiles slide
Dreedle Feb 5, 2016
20569da
Adding ids to tiles
Dreedle Feb 6, 2016
6b4dc7a
Working on shiftTile method, but tiles are movign diagonally
Dreedle Feb 6, 2016
4faef05
Tiles all move to corner
Dreedle Feb 7, 2016
6d7e111
Tile movement works, but iterating is not working (only one movement …
Dreedle Feb 7, 2016
0200d30
Updating board array with each movement. Isn't working
Dreedle Feb 7, 2016
8875b51
Solved problem of innerhtml not changing
Dreedle Feb 7, 2016
3840e20
Improved logic, but game buggy still
Dreedle Feb 7, 2016
88aac10
Tiles look like they are running each other over (out of sequence)
Dreedle Feb 7, 2016
e0c6ca3
New tiles fadein
Dreedle Feb 7, 2016
180a639
Modifying direction of iteration, in progress
Dreedle Feb 7, 2016
c485556
Refactored to make it so looping occurs foward or backward. buggy
Dreedle Feb 7, 2016
a08e463
Moving next.status logic to just one function
Dreedle Feb 7, 2016
3cd0f67
Made it so old divs get deleted when tiles shift
Dreedle Feb 7, 2016
4d9a5b6
Last commit
Dreedle Feb 8, 2016
6f9d917
Removing superfluous comment
Dreedle Feb 8, 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
1 change: 0 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
<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>
214 changes: 201 additions & 13 deletions javascripts/2048.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,224 @@
var Game = function() {
// Game logic and initialization here
//HELPER- add array method to get random elements from arrays
Array.prototype.randomElement = function () {
return this[Math.floor(Math.random() * this.length)];
};

//TILES
//arguments to be passed in to create tiles are numbers
var Tile = function (row, col, val, id) {
this.id = id;
this.colNum = col;
this.rowNum = row;
this.valNum = val;
this.col = "c" + (this.colNum).toString(); //ie "c1"
this.row = "r" + (this.rowNum).toString(); //ie "r1"
this.val = (this.valNum).toString(); //ie "4"
};

Game.prototype.moveTile = function(tile, direction) {
// Game method here
Tile.prototype.nextSpot = function (direction, board) {

//edge is 0 or 3
var lookAtSpot = function (rowOrCol, edge, value, currRow, currCol, board) {
var response = { status: null, tile: null, row: null, col: null};
if (rowOrCol == edge) {
response.status = "wallOrDiff";
return response;
} else if (board.contents[currRow][currCol] == 0) {
response.status = "empty";
response.row = "r" + Number(currRow);
response.col = "c" + Number(currCol);
return response;
} else {
nextTile = board.contents[currRow][currCol];
if (nextTile.valNum == value) {
response.status = "match";
response.tile = nextTile;
response.row = nextTile.row;
response.col = nextTile.col;
return response;
} else {
response.status = "wallOrDiff";
response.tile = nextTile;
response.row = nextTile.row;
response.col = nextTile.col;
return response;
};
};
}

switch(direction) {
case 38: //up
console.log('up');
return lookAtSpot(this.rowNum, 0, this.valNum, this.rowNum-1, this.colNum, board);
break;
case 40: //down
console.log('down');
return lookAtSpot(this.rowNum, 3, this.valNum, (this.rowNum)+1, this.colNum, board);
break;
case 37: //left
console.log('left');
return lookAtSpot(this.colNum, 0, this.valNum, this.rowNum, (this.colNum)-1, board);
break;
case 39: //right
console.log('right');
return lookAtSpot(this.colNum, 3, this.valNum, this.rowNum,(this.colNum)+1, board);
break;
}
};
};

Tile.prototype.updateTileDiv = function () {
tileDiv = $(".tile[data-id=" + this.id + "]");
tileDiv.attr({
"data-row": this.row,
"data-col": this.col,
"data-val": this.val
});
tileDiv[0].innerHTML = this.val;
};

Tile.prototype.deleteTileDiv = function () {
tileDiv = $(".tile[data-id=" + this.id + "]");
tileDiv.remove();
};

//BOARD
var Board = function (array) {
this.tileCounter = 1;
this.contents = array;
};


Board.prototype.isFull = function () {
var full = true;
var boardArray = this.contents;
for (var i = 0; i < boardArray.length; i++) {
for (var j = 0; j < boardArray[i].length; j++) {
if (boardArray[i][j] == 0) {
full = false;
return full;
};
};
};
return full;
};


Board.prototype.findEmptyLocation = function () {
var columns = [0, 1, 2, 3], rows = [0, 1, 2, 3];
var tile = null, randRow = null, randCol = null, randVal = null;
while (tile == null) {
randRow = rows.randomElement();
randCol = columns.randomElement();
randVal = ['2','4'][Math.round(Math.random())];
if ((this.contents[randRow][randCol]) === 0) {
tile = new Tile(randRow, randCol, Number(randVal), this.tileCounter);
this.tileCounter += 1;
};
};
return tile;
};


//add a new tile in a random unoccupied spot
Board.prototype.placeRandomTile = function () {
//if the board is full, don't add another tile.
if (this.isFull()) {
} else {
newTile = this.findEmptyLocation();
this.placeTile(newTile);
};
};

//places a tile onto the board
//takes tile object as parameter
//don't immediately show the new tile- do fadein
Board.prototype.placeTile = function (tile) {
this.contents[tile.rowNum][tile.colNum]= tile;
var gameboard= $("#gameboard");
var newTile = $('<div class="tile" data-row="' + tile.row + '",' + ' data-col="' + tile.col + '" data-val=' + tile.val + ' data-id=' + tile.id + ' >'+ tile.valNum + '</div>').hide().fadeIn(100);
gameboard.append(newTile);
};

Board.prototype.moveTiles = function (direction) {
//iterate over board differently for different directions
//left or up vs right or down
if (direction == 37 || direction == 38) {
this.loopTilesForward(direction);
} else if (direction == 39 || direction == 40) {
this.loopTilesBackward(direction);
};
};


Board.prototype.loopTilesForward = function (direction) {
for (var i = 0; i < this.contents.length; i++) {
for (var j = 0; j < this.contents[i].length; j++) {
this.processOneTile(direction, i, j);
};
};
};

Board.prototype.loopTilesBackward = function (direction) {
for (var i = (this.contents.length-1); i >= 0; i--) {
for (var j = (this.contents[i].length-1); j >= 0; j--) {
this.processOneTile(direction, i, j);
};
};
};

Board.prototype.processOneTile = function (direction, i, j){
var current = this.contents[i][j];
//only work on spots on board that contain tiles
if (current != 0) {
var next = current.nextSpot(direction, this);
//keep moving a tile until it hits a wall or different value
while (next.status != "wallOrDiff") {
if (next.status == "empty") {
//when next is empty, ok to keep going
this.shiftTile(current, next, current.val);
next = current.nextSpot(direction, this);
} else if (next.status == "match") {
//if it's a match, do combine but then break.
//only allowed to collide once
var value = (current.val * 2);
this.shiftTile(current, next, value);
break;
};
};
};
};

Board.prototype.shiftTile = function (tile, next, value) {
//updates the tile, the div and the board
this.contents[tile.rowNum][tile.colNum] = 0;
tile.row = next.row;
tile.col = next.col;
tile.colNum = Number(next.col[1]);
tile.rowNum = Number(next.row[1]);
tile.val = value;
tile.valNum = Number(value);
tile.updateTileDiv();

//if there is an adjacent tile, can keep tile object around but need to
//get rid of the div
if (next.tile) {
next.tile.deleteTileDiv();
};
this.contents[tile.rowNum][tile.colNum] = tile;
};

///GAME
var Game = function() {
this.board = new Board([[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]);
this.board.placeRandomTile();
this.board.placeRandomTile();
};

$(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.board.moveTiles(event.which);
game.board.placeRandomTile();
}
});
});