Advertisement
Guest User

Untitled

a guest
Nov 21st, 2014
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.57 KB | None | 0 0
  1. $(document).ready(function() {
  2. var timeArray = new Array(3,4,5,6,7,8,9,10);
  3. var shortestTime = timeArray[7];
  4. var fastestPony = {};
  5. var index;
  6.  
  7. var pony = {
  8. name: "pony",
  9. raceTime: -1,
  10. selected: ""
  11. };
  12.  
  13. //change the color of the pony when the user clicks on it
  14. $('.pony').bind('click', function() {
  15. $('.pony').removeClass('selectedPony');
  16. $(this).addClass('selectedPony');
  17.  
  18. //get the pony that the user selected
  19. pony.selected = $(this);
  20. });
  21.  
  22. $('#startButton').click(function() {
  23. if (pony.selected == "") {
  24. alert("Please click the pony you think will win the race.");
  25. }
  26. else {
  27. for (i = 1; i <= 8; i++) {
  28. //get a random number from the timeArray
  29. index = Math.floor(Math.random() * timeArray.length);
  30. pony.raceTime = timeArray[index];
  31.  
  32. //pull the random race time number out of the array
  33. //so it can't be assigned to another horse
  34. timeArray.splice(index, 1);
  35.  
  36. //get the fastest pony
  37. if (pony.raceTime < shortestTime) {
  38. shortestTime = pony.raceTime;
  39. fastestPony = $('#pony' + i);
  40. }
  41.  
  42. //award the winner after the ponies have reached the finish line
  43. if (i == 8) {
  44. fastestPony.addClass('winner').append(' - Winner!');
  45. }
  46.  
  47. //send the horses on their way to race!
  48. $('#pony' + i).animate({left: '320px'}, pony.raceTime * 1000);
  49. }
  50. }
  51. });
  52.  
  53. //reset the ponies back to the starting line by reloading the page
  54. $('#resetButton').click(function() {
  55. document.location.reload(true);
  56. });
  57. });
  58.  
  59. $(function() { // just pass in a function; it's the same as using $(document).ready()
  60. var ponies = $(".pony"), // Again: I never thought I'd write "ponies" in code
  61. raceButton = $("#raceButton"); // use a single button for race and reset
  62.  
  63. ponies.click(function (event) {
  64. ponies.removeClass('selected');
  65. $(this).addClass('selected');
  66. // no need to store the selection; we can always find it, now that it's got a "selected" class
  67. });
  68.  
  69. // use one button for both start and reset
  70. raceButton.click(function() {
  71. // If we have a winner, the race is pretty much over
  72. // If we have no winner, and no elements currently animating, we're ready to race
  73. // Otherwise we're racing; don't do anything
  74.  
  75. if( ponies.filter(".winner").length !== 0 ) {
  76. reset();
  77. } else if( ponies.filter(".winner, :animated").length === 0 ) {
  78. race();
  79. }
  80. });
  81.  
  82. function race() {
  83. // check for selection
  84. if( ponies.filter(".selected").length === 0 ) {
  85. alert("Please click the pony you think will win the race.");
  86. return; // return here instead of having a big else-block
  87. }
  88.  
  89. var times = [], i, l;
  90.  
  91. // disable the race button
  92. raceButton.prop("disabled", true);
  93.  
  94. // build time array; don't hardcode it
  95. for( i = 0, l = ponies.length ; i < l ; i++ ) {
  96. times.push(i);
  97. }
  98.  
  99. // Use .each() instead of relying on all the divs having numbered IDs.
  100. ponies.each(function () {
  101. var index = Math.floor(Math.random() * times.length),
  102. time = times[index],
  103. pony = $(this);
  104.  
  105. times.splice(index, 1);
  106.  
  107. // Use the onComplete callback to identify the winner
  108. pony.animate({left: '320px'}, {
  109. duration: (time + 3) * 1000, // only here do we set the minimum time by adding 3
  110. complete: function () {
  111. // If this was the fastest one
  112. if( time === 0 ) {
  113. pony
  114. .addClass("winner")
  115. .append("<span>Winner!</span>"); // use a span or something, so it's easier to remove later
  116.  
  117. // alert the gambler and enable the reset button
  118. if( pony.hasClass("selected") ) {
  119. alert("You win tons of money or something");
  120. } else {
  121. alert("You lost. Boo-hoo");
  122. }
  123. raceButton.prop("disabled", false).val("Reset");
  124. }
  125. }
  126. });
  127. });
  128. }
  129.  
  130. // just reset everything yourself; no need to reload
  131. function reset() {
  132. ponies
  133. .stop() // stop any running animations
  134. .removeClass("selected") // clear selection
  135. .removeClass("winner") // clear winner
  136. .css({ left: '0px' }) // move 'em back
  137. .children("span").remove(); // remove the label
  138. raceButton.val("Race!");
  139. }
  140. });
  141.  
  142. function raceInProgress() {
  143. return ponies.filter(":animated").length > 0;
  144. }
  145.  
  146. function raceDecided() {
  147. return ponies.filter(".winner").length > 0
  148. }
  149.  
  150. function raceNotStarted() {
  151. return !raceInProgress() && !raceDecided();
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement