From 4484f3bd3b43725198090a954b886e20b6baa13e Mon Sep 17 00:00:00 2001 From: clenclen Date: Thu, 25 Feb 2016 16:48:49 -0500 Subject: [PATCH 1/2] create board created board that is clickable. unable to figure out how to randomize spans. --- index.html | 129 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) diff --git a/index.html b/index.html index 4d20248..89f9296 100644 --- a/index.html +++ b/index.html @@ -2,7 +2,136 @@ Memory exercise + + + + +

memory

+
+
+
pick a card
+ + From c4713882af4d823333093e7523d42b5d7bc42652 Mon Sep 17 00:00:00 2001 From: clenclen Date: Wed, 2 Mar 2016 12:17:24 -0500 Subject: [PATCH 2/2] added shuffle function shuffle function and more --- index.html | 115 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 66 insertions(+), 49 deletions(-) diff --git a/index.html b/index.html index 89f9296..d267b4d 100644 --- a/index.html +++ b/index.html @@ -37,8 +37,8 @@

memory

var Tile = function(text) { this.text = text this.div = $("
" + this.text +"
"); - - this.genHTML(this.div) + console.log("text", this.text) + $(".memoryboard").append(this.div) var self = this this.div.click(function(){ @@ -46,63 +46,86 @@

memory

}) } - Tile.prototype.handleClick = function() { - + Tile.prototype.handleClick = function() { this.flip(); scoreBoard.push(this) - + console.log("scoreBoard:", scoreBoard) if (scoreBoard.length < 2) { console.log("waiting for other click") return; } - tile1 = scoreBoard[0], - tile2 = scoreBoard[1] + var tile1 = scoreBoard[0]; + var tile2 = scoreBoard[1]; - - if (tile1.matches(tile2)){ - msg("matched") - scoreBoard = [] + if (tile1.matches(tile2)) { + console.log("matches") + setTimeout(function() { + tile1.div.css("visibility", "hidden") + tile2.div.css("visibility", "hidden") + + }, 1000); + msg("matched") } else { - msg("try again") - scoreBoard = []; - return + console.log("nah") + setTimeout(function() { + tile1.unflip() + tile2.unflip() + }, 1000); + msg("try again") } - + scoreBoard = []; } - Tile.prototype.genHTML = function(div) { - $(".memoryboard").append(div); - } - Tile.prototype.flip = function() { - console.log("flipped") - this.div.addClass("flipped") + console.log("flipped"); + this.div.addClass("flipped"); } Tile.prototype.unflip = function() { - this.div.removeClass("flipped") + this.div.removeClass("flipped"); } Tile.prototype.matches = function(otherTile) { - if(this.text == otherTile.text){ - self = this - setTimeout(function() { - self.div.css("visibility", "hidden") - otherTile.div.css("visibility", "hidden") - console.log("matches") - }, 1000) - return true - }else{ - setTimeout(function(){ - tile1.unflip() - tile2.unflip() - }, 1000); + console.log("this.text:" + this.text); + console.log("othertile:" + otherTile.text); + if (this.text == otherTile.text) { + console.log("return true"); + return true; + } else { + console.log("return false"); return false; } } + function shuffle(array) { + var counter = array.length; + + // While there are elements in the array + while (counter > 0) { + // Pick a random index + var index = Math.floor(Math.random() * counter); + + // Decrease counter by 1 + counter--; + + // And swap the last element with it + var temp = array[counter]; + array[counter] = array[index]; + array[index] = temp; + } + + return array; + } + + function removeAndPrintElements(array) { + while (array.length > 0) { + var firstElem = array.splice(0, 1); + var tile = new Tile(firstElem[0]); + } + } + var msg = function(msg) { $(".msg").text(msg) } @@ -113,25 +136,19 @@

memory

$(document).ready(function(){ - var tilesNames = ["A", "A", "B", "B", "C", "C"] - - for (i=0; i < tilesNames.length; i++) { - - // x = Math.random() - // y = x * (tilesNames.length - 1 ) - // z = Math.floor(y) - // console.log(z) - - var text = tilesNames[i] - - var tile = new Tile(text); - - } + var array = ["A","A","B","B","C","C"]; + var arraylength = array.length + var text = shuffle(array) + removeAndPrintElements(text) }); + // prevent it from clicking itself + // take away global variables + // shhoudl the code go into the "matches" method or the "handleClick" method? +