Advertisement
Guest User

Untitled

a guest
Feb 27th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $(document).ready(function() {
  2.   MatchGame.renderCards(MatchGame.generateCardValues(), $('#game'));
  3. });
  4.  
  5. var MatchGame = {};
  6.  
  7. /*
  8.   Sets up a new game after HTML document has loaded.
  9.   Renders a 4x4 board of cards.
  10. */
  11.  
  12. /*
  13.   Generates and returns an array of matching card values.
  14.  */
  15.  
  16. MatchGame.generateCardValues = function () {
  17.   var ordered = [];
  18.   for(card = 1; card <= 8; card++) {
  19.     ordered.push(card);
  20.     ordered.push(card);
  21.   }
  22.  
  23.   var random = [];
  24.   while(ordered.length > 0) {
  25.     random.push(ordered.splice(Math.floor(Math.random() * ordered.length), 1));
  26.   }
  27.  
  28.   return random;
  29. };
  30.  
  31. /*
  32.   Converts card values to jQuery card objects and adds them to the supplied game
  33.   object.
  34. */
  35.  
  36. MatchGame.renderCards = function(cardValues, $game) {
  37.   $game.data('flipped', []);
  38.   var colors = [25,55,90,160,220,265,310,360];
  39.   $game.empty();
  40.   for(i = 0; i < cardValues.length; i++) {
  41.     $e = $("<div class='col-xs-3 card'></div>");
  42.     $e.data('value', cardValues[i]);
  43.     $e.data('flipped', false);
  44.     $e.data('color', 'hsl(' + colors[cardValues[i] - 1] + ', 85%, 65%)');
  45.     $game.append($e);
  46.   }
  47.   $('.card').click(function() {
  48.     MatchGame.flipCard($(this), $('#game'));
  49.   });
  50. };
  51.  
  52. /*
  53.   Flips over a given card and checks to see if two cards are flipped over.
  54.   Updates styles on flipped cards depending whether they are a match or not.
  55.  */
  56.  
  57. MatchGame.flipCard = function($card, $game) {
  58.   if($card.data('flipped')) { return }
  59.   $card.data('flipped', true);
  60.   $card.css('background-color', $card.data('color'));
  61.   $card.text($card.data('value'));
  62.  
  63.   $game.data('flipped').push($card);
  64.  
  65.   if($game.data('flipped').length == 2) {
  66.     if($game.data('flipped')[0].data('value')[0] == $game.data('flipped')[1].data('value')[0]) {
  67.       $game.data('flipped')[0].css({backgroundColor: 'rgb(153,153,153)', color: 'rgb(204,204,204)'});
  68.       $game.data('flipped')[1].css({backgroundColor: 'rgb(153,153,153)', color: 'rgb(204,204,204)'});
  69.       $game.data('flipped', []);
  70.     } else {
  71.       var card1 = $game.data('flipped')[0];
  72.       var card2 = $game.data('flipped')[1];
  73.       window.setTimeout(function() {
  74.         card1.data('flipped', false);
  75.         card1.text('');
  76.         card1.css('background-color', 'rgb(32, 64, 86)');
  77.         card2.data('flipped', false);
  78.         card2.text('');
  79.         card2.css('background-color', 'rgb(32, 64, 86)');
  80.       }, 350);
  81.     }
  82.     $game.data('flipped', []);
  83.   }
  84.  
  85. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement