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

!! #21

Open
wants to merge 46 commits into
base: hwdg/master
Choose a base branch
from
Open

!! #21

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
b677699
Added some functionality to keep track of free spaces
daphnegold Feb 2, 2016
764ca63
Merge branch 'hwdg/master' of github.com:wesleywillis/2048 into hwdg/…
daphnegold Feb 2, 2016
64cefd2
add method to remove free spaces
yourFriendWes Feb 2, 2016
1301163
Some stuff for moving tiles
daphnegold Feb 2, 2016
46ff70c
add case for moving tiles right
yourFriendWes Feb 2, 2016
9caeb96
add buggy logic for shift tile left so we can both work on debugging
yourFriendWes Feb 2, 2016
c5ea9b3
maybe some things with updating the board methods
daphnegold Feb 3, 2016
ef0f366
some stuff
daphnegold Feb 3, 2016
884d7e3
refactor move left code
daphnegold Feb 3, 2016
c6dd1ec
finished logic for tile movement to the left
yourFriendWes Feb 3, 2016
ebe9d26
Pieces move in every direction yay
daphnegold Feb 3, 2016
8ca522a
things move around the board without merging weirdly, began merging t…
daphnegold Feb 3, 2016
9fd8da5
change findFreeSpace method
yourFriendWes Feb 3, 2016
c9ebdf6
Fixed bug with deleting the wrong tile in a merge
daphnegold Feb 3, 2016
14fb8f5
Merge in all directions now
daphnegold Feb 4, 2016
3c5c553
add method to indicate game is over - needs debugging
yourFriendWes Feb 4, 2016
b83e880
Fixed issue (mostly) with slide animation on merge tiles
daphnegold Feb 4, 2016
5e26fa9
fix undefined bug triggered by gameLost
yourFriendWes Feb 4, 2016
143f7e2
fix css bug for game over, and disable keydown event when game over.
yourFriendWes Feb 4, 2016
176f6ca
Merge pull request #1 from wesleywillis/gameover
wesleywillis Feb 4, 2016
fb321f6
added score
daphnegold Feb 4, 2016
86bf509
Merge branch 'hwdg/master' of github.com:wesleywillis/2048 into hwdg/…
daphnegold Feb 4, 2016
5abb547
remove hideboard event on gameLost
yourFriendWes Feb 4, 2016
d717a0e
Merge branch 'hwdg/master' of github.com:wesleywillis/2048 into hwdg/…
yourFriendWes Feb 4, 2016
48dbe1d
add retry button for lost game that refreshes page for new game
yourFriendWes Feb 4, 2016
5989fbe
Tiles no longer spawn if nothing moved on the board
daphnegold Feb 4, 2016
8a0c41e
Merge branch 'hwdg/master' of github.com:wesleywillis/2048 into hwdg/…
daphnegold Feb 4, 2016
3e285ff
moveTile method no longer takes tile as a parameter
daphnegold Feb 5, 2016
0b5ff97
buggy code - trying to allow user to keep playing after they win
yourFriendWes Feb 5, 2016
573f0b3
Merge branch 'hwdg/master' of github.com:wesleywillis/2048 into hwdg/…
yourFriendWes Feb 5, 2016
4c37585
added class names for game won and matching css
yourFriendWes Feb 5, 2016
1ba0ced
add lisa frank colors that are lovely
yourFriendWes Feb 5, 2016
5dd9ad5
CSS for Lisa Frank Theme
daphnegold Feb 5, 2016
2d45e06
keep playing is allowed now after winning
yourFriendWes Feb 5, 2016
3e8ec38
Merge branch 'hwdg/master' of github.com:wesleywillis/2048 into hwdg/…
yourFriendWes Feb 5, 2016
bc9e14a
Done
daphnegold Feb 5, 2016
2b991fa
changed background color
daphnegold Feb 5, 2016
4b9d7f2
style tweaking
yourFriendWes Feb 5, 2016
04a498c
Merge branch 'hwdg/master' of github.com:wesleywillis/2048 into hwdg/…
yourFriendWes Feb 5, 2016
8a7c031
Added more CSS
daphnegold Feb 5, 2016
7b3b18d
game lose condition possibly fixed
daphnegold Feb 5, 2016
69fb38b
Fixed bug with losing game conditions and deleted unused code
daphnegold Feb 6, 2016
7509db7
Added images rather than links to tiles
daphnegold Feb 6, 2016
d705b63
Fixed small image problem
daphnegold Feb 6, 2016
2c8214d
Seriously though, fixed the losing game logic. Users have to attempt …
daphnegold Feb 7, 2016
8766901
Adjusted the probability of getting a 2 or a 4 on tile spawn
daphnegold Feb 9, 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
Binary file added images/lf1.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/lf10.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/lf11.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/lf12.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/lf13.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/lf2.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/lf3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/lf4.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/lf5.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/lf6.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/lf7.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/lf8.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/lf9.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,18 @@
<link rel="stylesheet" media="all" href="stylesheets/2048.css"/>
<script type="text/javascript" src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
<script type="text/javascript" src="javascripts/2048.js"></script>
<link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Berkshire+Swash" />
</head>
<body>
<div class="heading">
<div id="score">0</div>
</div>
<div class="above-game">
</div>
<div class="game-message">
<div class="lower">
</div>
</div>
<div id="gameboard">
<div class="cells">
<div class="cell"></div>
Expand Down
330 changes: 299 additions & 31 deletions javascripts/2048.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,331 @@
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}

var Game = function() {
// Game logic and initialization here
this.board = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]];
this.newTile = true;
this.spawnTile();
this.spawnTile();
this.newTile = false;
this.score = 0;
};

Game.prototype.findFreeSpace = function() {
var spaceJam = [];
var self = this;
for(var r=0; r<4; r++){
for(var c=0; c<4; c++){
if (this.board[r][c] === 0) {
spaceJam.push(r.toString() + c.toString());
}
}
}

if (spaceJam.length === 0 && !this.movesLeft()){
self.gameLost();
}else if (this.newTile) {
return spaceJam[Math.floor(Math.random() * spaceJam.length)];
}
};

Game.prototype.movesLeft = function() {
var r, c;

for (r=0; r<4; r++) {
for (c=0; c<3; c++) {
if (this.board[r][c] === 0) {
break;
} else if (this.board[r][c].attr('data-val') === this.board[r][c+1].attr('data-val')) {
return true;
}
}
}
for (c=0; c<4; c++) {
for (r=0; r<3; r++) {
if (this.board[r][c] === 0) {
break;
} else if (this.board[r][c].attr('data-val') === this.board[r+1][c].attr('data-val')) {
return true;
}
}
}
return false;
};

Game.prototype.spawnTile = function() {
var val = [2, 4][Math.floor(Math.random() * 2)];
var row = getRandomInt(0, 3);
var col = getRandomInt(0, 3);
var val = Math.random() < 0.9 ? 2 : 4;
space = this.findFreeSpace();
if (space !== undefined) {
row = space[0];
col = space[1];

new_tile = $('<div class="tile"></div>').attr('data-row', "r" + row).attr('data-col', "c" + col).attr('data-val', val).html(val);

$(".cells").after(new_tile);

new_tile = $('<div class="tile"></div>').attr('data-row', "r" + row).attr('data-col', "c" + col).attr('data-val', val).html(val);
this.board[row][col] = new_tile;
}
};

$(".cells").after(new_tile);
Game.prototype.moveLeft = function() {
var rowClone, colClone, cloneIndex, r, c;

this.board[row][col] = val;
for(r=0; r<4; r++){
cloneIndex = 0;
rowClone = new Array(5).join('0').split('').map(parseFloat);
for(c=0; c<4; c++){
if (this.board[r][c] !== 0){
rowClone[cloneIndex] = this.board[r][c];
cloneIndex++;
}
}

for (c=0; c<4; c++){
this.newTile = this.newTile || (rowClone[c] !== this.board[r][c]);
}
this.board[r] = rowClone;
}
};

Game.prototype.moveRight = function() {
var rowClone, colClone, cloneIndex, r, c;

for(r=0; r<4; r++){
cloneIndex = 3;
rowClone = new Array(5).join('0').split('').map(parseFloat);
for(c=3; c>=0; c--){
if (this.board[r][c] !== 0){
rowClone[cloneIndex] = this.board[r][c];
cloneIndex--;
}
}

for (c=0; c<4; c++){
this.newTile = this.newTile || (rowClone[c] !== this.board[r][c]);
}
this.board[r] = rowClone;
}
};

Game.prototype.moveUp = function() {
var rowClone, colClone, cloneIndex, r, c;

for(c=0; c<4; c++){
cloneIndex = 0;
colClone = new Array(5).join('0').split('').map(parseFloat);
for(r=0; r<4; r++){
if (this.board[r][c] !== 0){
colClone[cloneIndex] = this.board[r][c];
cloneIndex++;
}
}

for(r=0; r<4; r++){
this.newTile = this.newTile || (colClone[r] !== this.board[r][c]);
this.board[r][c] = colClone[r];
}
}
};

Game.prototype.moveDown = function() {
var rowClone, colClone, cloneIndex, r, c;

for(c=0; c<4; c++){
cloneIndex = 3;
colClone = new Array(5).join('0').split('').map(parseFloat);
for(r=3; r>=0; r--){
if (this.board[r][c] !== 0){
colClone[cloneIndex] = this.board[r][c];
cloneIndex--;
}
}
for(r=3; r>=0; r--){
this.newTile = this.newTile || (colClone[r] !== this.board[r][c]);
this.board[r][c] = colClone[r];
}
}
};

Game.prototype.moveTile = function(tile, direction) {
// Game method here
Game.prototype.moveTile = function(direction) {
switch(direction) {
case 38: //up
console.log('up');
row = parseInt(tile[0].getAttribute('data-row').slice(-1));
tile.attr('data-row', "r" + (row - 1));
this.moveUp();
this.mergeUp();
this.moveUp();
this.updateDisplay();
this.spawnTile();
this.newTile = false;
break;

case 40: //down
console.log('down');
row = parseInt(tile[0].getAttribute('data-row').slice(-1));
tile.attr('data-row', "r" + (row + 1));
this.moveDown();
this.mergeDown();
this.moveDown();
this.updateDisplay();
this.spawnTile();
this.newTile = false;
break;

case 37: //left
console.log('left');
col = parseInt(tile[0].getAttribute('data-col').slice(-1));
tile.attr('data-col', "c" + (col - 1));
this.moveLeft();
this.mergeLeft();
this.moveLeft();
this.updateDisplay();
this.spawnTile();
this.newTile = false;
break;

case 39: //right
console.log('right');
col = parseInt(tile[0].getAttribute('data-col').slice(-1));
tile.attr('data-col', "c" + (col + 1));
this.moveRight();
this.mergeRight();
this.moveRight();
this.updateDisplay();
this.spawnTile();
this.newTile = false;
break;
}
};

Game.prototype.mergeLeft = function() {
for(var r=0; r<4; r++){
for(var c=0; c<3; c++){
dom = this.board[r][c];
nextDom = this.board[r][c+1];

if (dom !== 0 && nextDom !== 0) {
val = parseInt(dom.attr('data-val'));
nextVal = parseInt(nextDom.attr('data-val'));
if (val === nextVal){
score = val*2;
this.updateScore(score);
nextDom.attr('data-val', score).text(score);
this.board[r][c] = 0;
dom.remove();
}
}
}
}
};

Game.prototype.mergeRight = function() {
for(var r=0; r<4; r++){
for(var c=3; c>=1; c--){
dom = this.board[r][c];
nextDom = this.board[r][c-1];

if (dom !== 0 && nextDom !== 0) {
val = parseInt(dom.attr('data-val'));
nextVal = parseInt(nextDom.attr('data-val'));
if (val === nextVal){
score = val*2;
this.updateScore(score);
nextDom.attr('data-val', score).text(score);
this.board[r][c] = 0;
dom.remove();
}
}
}
}
};

Game.prototype.mergeUp = function() {
for(var c=0; c<4; c++){
for(var r=0; r<3; r++){
dom = this.board[r][c];
nextDom = this.board[r+1][c];

if (dom !== 0 && nextDom !== 0) {
val = parseInt(dom.attr('data-val'));
nextVal = parseInt(nextDom.attr('data-val'));
if (val === nextVal){
score = val*2;
this.updateScore(score);
nextDom.attr('data-val', score).text(score);
this.board[r][c] = 0;
dom.remove();
}
}
}
}
};

Game.prototype.mergeDown = function() {
for(var c=0; c<4; c++){
for(var r=3; r>=1; r--){
dom = this.board[r][c];
nextDom = this.board[r-1][c];

if (dom !== 0 && nextDom !== 0) {
val = parseInt(dom.attr('data-val'));
nextVal = parseInt(nextDom.attr('data-val'));
if (val === nextVal){
score = val*2;
this.updateScore(score);
nextDom.attr('data-val', score).text(score);
this.board[r][c] = 0;
dom.remove();
}
}
}
}
};

Game.prototype.updateScore = function(score) {
if (score === 2048){
this.gameWon();
}else{
this.score += score;
$("#score").text(this.score);
}
};

Game.prototype.updateDisplay = function() {
for(var r=0; r<4; r++){
for(var c=0; c<4; c++){
if (this.board[r][c] !== 0) {
dom = this.board[r][c];
dom.attr('data-row', "r" + r).attr("data-col", "c" + c);
}
}
}
};

Game.prototype.gameLost = function () {
console.log("Game Over");
$('body').off("keydown");
$('.game-message').addClass('game-over');
$(".game-over").prepend("<p>Game over!</p>");
$(".lower").append("<a class='retry-button' href='javascript:history.go(0)'>Try again</a>");
};

Game.prototype.gameWon = function () {
var game = this;
console.log("Game Won");
$('body').off("keydown");
$('.game-message').addClass('game-won');
$(".game-won").prepend("<p>You Won!</p>");
$(".lower").append($("<a class='playing-button' href='#'>Keep Playing?</a>").click(function(event){
$(".lower a").remove();
$(".game-won p").remove();
$('.game-message').removeClass('game-won');

$('body').keydown(function(event){
game.keyHandling(event);
});
}));
};

Game.prototype.keyHandling = function(event) {
var arrows = [37, 38, 39, 40];
if (arrows.indexOf(event.which) > -1) {
var tile = $('.tile');
this.moveTile(event.which);
}
};

$(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.keyHandling(event);
});
});
Loading