/* a little game: memory in javascript memory is a simple game: the preparation: 1) take 4 or 5 cards 2) duplicate each card; now we have 8 or 10 cards 3) deal the cards randomly in a square, face down */ function random(x) { return Math.floor(x*Math.random()) } function shuffle(Q) { var R, T, J; for (J = Q.length - 1; J > 0; J--) { R = random(J + 1); T = Q[J]; Q[J] = Q[R]; Q[R] = T; } return Q; } var deal = function(deck){ $('td').each(function(){ $(this).html("
" + deck.pop() + "
") }) } /* main */ jQuery(document).ready(function($){ var deck = 'one two three four five six seven eight'.split(' '); deck = deck.concat(deck); // duplicates deck = shuffle(deck); deal(deck); //$('td').addClass('facedown'); $('td').hover( function(){$(this).addClass('active')}, function(){$(this).removeClass('active')} ) // flip $('td').click(function(){ $(this).addClass('faceup'); if (('.faceup').length){ var A = $(this).html(); } else { var B = $(this).html(); } if (A == B) { $('.faceup').removeClass('faceup').addClass('.matched'); } else { $('.faceup').removeClass('faceup') } ) }); /* $('td').toggle( function(){ $(this).find('p').css('visibility', 'visible').parent().addClass('chosen'); }, function(){ $(this).find('p').css('visibility', 'invisible').parent().removeClass('chosen') }) $(this).removeClass('facedown').addClass('faceup'); if ($('.faceup').length == 2){ if( $('.faceup').get(0).html() == $('.faceup').get(1).html() ) { $('.faceup').removeClass('faceup').addClass('matched'); } } else { $('.faceup').removeClass('.faceup').addClass('facedown'); } the game: 1) the user flips a card (call it 'a') 2) then the user flips another card ('b') 3) if a == b, remove both 4) if not, flip them down 5) repeat until all cards are gone */ // the board: // we'll use a| s per | // "dealing" consists of randomly selecting values from the deck, and placing those values in a | (here) | .
/*
the game:
cards have state - [faceup|facedown]
$('td').click(function(){
$(this).addClass('faceup');
if ($('.facedown').length == 0)) {
alert('game over!')
} else {
- is the flipped card the only faceup card?
-- yes: flip again.
-- no: flip all cards down.
- are there two faceup cards?
-- yes: remove them both from the board.
}
*/