Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. var MatchGame = {};
  2.  
  3. /*
  4. Sets up a new game after HTML document has loaded.
  5. Renders a 4x4 board of cards.
  6. */
  7. $(document).ready(function() {
  8. var $game = $('#game');
  9. var randomCards = MatchGame.generateCardValues();
  10. console.log(randomCards);
  11. MatchGame.renderCards(randomCards, $game);
  12. });
  13.  
  14. /*
  15. Generates and returns an array of matching card values.
  16. */
  17.  
  18. MatchGame.generateCardValues = function () {
  19. var orderedArray = [];
  20. for (i = 1; i <= 8; i++) {
  21. orderedArray.push(i);
  22. orderedArray.push(i);
  23. }
  24. var randomArray = [];
  25. var x = 0;
  26. var y = 16;
  27. while ( x < 16) {
  28. var index = Math.floor(Math.random() * y);
  29. randomArray.push(orderedArray[index]);
  30. orderedArray.splice(index, 1);
  31. y--;
  32. x++;
  33. }
  34. return randomArray;
  35. };
  36.  
  37. /*
  38. Converts card values to jQuery card objects and adds them to the supplied game
  39. object.
  40. */
  41.  
  42. MatchGame.renderCards = function(cardValues, $game) {
  43. $game.empty();
  44. $game.data('flippedCards', []);
  45.  
  46. var colorArray = [
  47. 'hsl(25,85%,65%)',
  48. 'hsl(55,85%,65%)',
  49. 'hsl(90,85%,65%)',
  50. 'hsl(160,85%,65%)',
  51. 'hsl(220,85%,65%)',
  52. 'hsl(265,85%,65%)',
  53. 'hsl(310,85%,65%)',
  54. 'hsl(360,85%,65%)'
  55. ];
  56.  
  57. for (i = 0; i < cardValues.length; i++) {
  58.  
  59. var value = cardValues[i];
  60. var color = colorArray[value - 1];
  61. var data = {value: value, color: color, flipped: false};
  62.  
  63. var $cardCreator = $('<div class="col-xs-3 card"></div>');
  64. $cardCreator.data(data);
  65.  
  66. $game.append($cardCreator);
  67. }
  68.  
  69. $('.card').click(function() {
  70. MatchGame.flipCard($(this), $('#game'));
  71. });
  72.  
  73. };
  74.  
  75. /*
  76. Flips over a given card and checks to see if two cards are flipped over.
  77. Updates styles on flipped cards depending whether they are a match or not.
  78. */
  79.  
  80. MatchGame.flipCard = function($card, $game) {
  81.  
  82. if ($card.data('flipped')) {
  83. return;
  84. }
  85.  
  86. $card.css('background-color', $card.data('color')).text($card.data('value')).data('flipped', true);
  87.  
  88. var flippedCards = $game.data('flippedCards');
  89. var number = $card.data('value');
  90.  
  91. flippedCards.push(number);
  92. console.log(flippedCards);
  93.  
  94. if (flippedCards.length == 2) {
  95.  
  96. if (flippedCards[0].data('value') === flippedCards[1].data('value')) {
  97. var matching = {
  98. backgroundColor: 'rgb(153,153,153)',
  99. color: 'rgb(204,204,204)'
  100. };
  101.  
  102. flippedCards[0].css(matching);
  103. flippedCards[1].css(matching);
  104. } else {
  105. var resetCss = {
  106. backgroundColor: 'rgb(32,64,86)',
  107. color: 'rgb(255,255,255)'
  108. };
  109.  
  110. flippedCards[0].css(resetCss).text('').data('flipped', false);
  111. flippedCards[1].css(resetCss).text('').data('flipped', false);
  112. flippedCards = [];
  113. }
  114.  
  115. }
  116.  
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement