Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. // Alert box using SweetAlert2 - https://limonte.github.io/sweetalert2
  2. $(document).ready(function() {
  3.  
  4. // Variables
  5. var holding = [],
  6. moves,
  7. disksNum = 7,
  8. minMoves = 127,
  9. $canves = $('#canves'),
  10. $restart = $canves.find('.restart'),
  11. $tower = $canves.find('.tower'),
  12. $scorePanel = $canves.find('#score-panel'),
  13. $movesCount = $scorePanel.find('#moves-num'),
  14. $ratingStars = $scorePanel.find('i'),
  15. rating = 3;
  16.  
  17. // Set Rating and final Score
  18. function setRating(moves) {
  19. if (moves === 127) {
  20. $ratingStars.eq(2).removeClass('fa-star').addClass('fa-star-o');
  21. rating = 2;
  22. } else if (moves >= 128 && moves <= 228) {
  23. $ratingStars.eq(1).removeClass('fa-star').addClass('fa-star-o');
  24. rating = 1;
  25. } else if (moves >= 229) {
  26. $ratingStars.eq(0).removeClass('fa-star').addClass('fa-star-o');
  27. rating = 0;
  28. }
  29. return { score: rating };
  30. };
  31.  
  32. // Init Game
  33. function initGame(tower) {
  34. $tower.html('');
  35. moves = 0;
  36. $movesCount.html(0);
  37. holding = [];
  38. for (var i = 1; i <= disksNum; i++) {
  39. tower.prepend($('<li class="disk disk-' + i + '" data-value="' + i + '"></li>'));
  40. }
  41. $ratingStars.each(function() {
  42. $(this).removeClass('fa-star-o').addClass('fa-star');
  43. });
  44. }
  45.  
  46. // Game Logic
  47. function countMove() {
  48. moves++;
  49. $movesCount.html(moves);
  50.  
  51. if (moves > minMoves - 1) {
  52. if ($tower.eq(1).children().length === disksNum || $tower.eq(2).children().length === disksNum) {
  53. swal({
  54. allowEscapeKey: false,
  55. allowOutsideClick: false,
  56. title: 'Congratulations! You Won!',
  57. text: "Boom Shaka Lak",
  58. type: 'success',
  59. confirmButtonColor: '#8bc34a',
  60. confirmButtonText: 'Play again!'
  61. }).then(function(isConfirm) {
  62. if (isConfirm) {
  63. initGame($tower.eq(0));
  64. }
  65. })
  66. }
  67. }
  68.  
  69. setRating(moves);
  70. }
  71.  
  72. function tower(tower) {
  73. var $disks = tower.children(),
  74. $topDisk = tower.find(':last-child'),
  75. topDiskValue = $topDisk.data('value'),
  76. $holdingDisk = $canves.find('.hold');
  77.  
  78. if ($holdingDisk.length !== 0) {
  79. if (topDiskValue === holding[0]) {
  80. $holdingDisk.removeClass('hold');
  81. } else if (topDiskValue === undefined || topDiskValue > holding[0]) {
  82. $holdingDisk.remove();
  83. tower.append($('<li class="disk disk-' + holding[0] + '" data-value="' + holding[0] + '"></li>'));
  84. countMove();
  85. }
  86. } else if ($topDisk.length !== 0) {
  87. $topDisk.addClass('hold');
  88. holding[0] = topDiskValue;
  89. }
  90. }
  91.  
  92. initGame($tower.eq(0));
  93.  
  94. // Event Handlers
  95. $canves.on('click', '.tower', function() {
  96. var $this = $(this);
  97. tower($this);
  98. });
  99.  
  100. $restart.on('click', function() {
  101. swal({
  102. allowEscapeKey: false,
  103. allowOutsideClick: false,
  104. title: 'Are you sure?',
  105. text: "Your progress will be Lost!",
  106. type: 'warning',
  107. showCancelButton: true,
  108. confirmButtonColor: '#8bc34a',
  109. cancelButtonColor: '#e91e63',
  110. confirmButtonText: 'Yes, Restart Game!'
  111. }).then(function(isConfirm) {
  112. if (isConfirm) {
  113. initGame($tower.eq(0));
  114. }
  115. })
  116. });
  117. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement