diff --git a/index.html b/index.html index 4d20248..cc6ac96 100644 --- a/index.html +++ b/index.html @@ -1,8 +1,25 @@ -
-" + this.values[i][j] + " | ";
+ }
+ board += "
***WINNER***
"); + },775); + } +}; + +var Tile = function($td) { + this.$td = $td; + this.$div = $td.find('div'); + this.value = $td.find('div').text(); +}; + +Tile.prototype.matches = function(tile) { + var match = false; + if ( this.value === tile.value ) { + match = true; + } + return match; +}; + +Tile.prototype.showTile = function() { + this.$div.css('opacity','1'); +}; + +Tile.prototype.hideTile = function() { + this.$div.animate({opacity: 0},750); +}; + +Tile.prototype.removeTile = function() { + this.$td.fadeOut(750); +}; + +Tile.prototype.sameTileAs = function($tile) { + return this.$td.is($tile); +}; + +var setClicked = function($tile) { + var clicked = new Tile($tile); + clicked.showTile(); + return clicked; +}; + +var action = function(event) { + var $tile = $(this); + var game = event.data.game; + if ( game.clicked1 === null ) { + game.clicked1 = setClicked($tile); + } else if ( !game.clicked1.sameTileAs($tile) ) { + game.clicked2 = setClicked($tile); + if ( game.clicked2.matches(game.clicked1) ) { + game.removeClickedTiles(); + game.checkWinner(); + } else { + game.unsetClickedTiles(); + } + } +}; + +Game.prototype.initBoard = function(chgvalues) { + var self = this; + if ( chgvalues ) { + self.takenValues = "#"; + self.values = self.genValues(); + } + $(self.boardElements.boardHtmlElementId).empty(); + $(self.boardElements.boardHtmlElementId).append(self.genHtml()); + $(self.boardElements.tileHtmlElementClass).click({ + game:self + },action); +}; + +var Button = function(type,htmlElementId) { + this.type = type; + this.htmlElementId = htmlElementId; +}; + +Button.prototype.initButton = function(game) { + var chgvalues = false; + if ( this.type === "New" ) { + chgvalues = true; + } + $(this.htmlElementId).click(function() { + $(game.boardElements.msgHtmlElementId).empty(); + game.initBoard(chgvalues); + game.clicked1 = null; + game.clicked2 = null; + game.remainingMatches = game.boardElements.height*game.boardElements.width/2; + }); +}; + +var gameSetup = function(gameElements) { + var game = new Game({ + height: gameElements.height, + width: gameElements.width, + boardHtmlElementId: gameElements.boardHtmlElementId, + msgHtmlElementId: gameElements.msgHtmlElementId, + tileHtmlElementClass: gameElements.tileHtmlElementClass + }); + game.initBoard(true); + var newButton = new Button("New",gameElements.newButtonHtmlElementId); + newButton.initButton(game); + var resetButton = new Button("Reset",gameElements.resetButtonHtmlElementId); + resetButton.initButton(game); +}; + +$(document).ready(function() { + // Code only works for boards with an even number of tiles + gameSetup({ + height: 3, + width: 6, + boardHtmlElementId: "#board", + msgHtmlElementId: "#msg", + tileHtmlElementClass: ".tile", + newButtonHtmlElementId: "#new" , + resetButtonHtmlElementId: "#reset" + }); + gameSetup({ + height: 4, + width: 4, + boardHtmlElementId: "#board2", + msgHtmlElementId: "#msg2", + tileHtmlElementClass: ".tile2", + newButtonHtmlElementId: "#new2" , + resetButtonHtmlElementId: "#reset2" + }); +});