Guest User

Untitled

a guest
May 20th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.87 KB | None | 0 0
  1. /**
  2. * @note: it is a bit hard to code review without you making pull requests
  3. * on github so will do my best to just write it here.
  4. * also note on the UI, put the instructions first it was confusing
  5. * to see them on the bottom
  6. */
  7.  
  8. $(document).ready(function () {
  9. // no space between `function` and `()`
  10.  
  11. $('.restart').hide();
  12. // use single tab (this goes for the entire application)
  13.  
  14. var yourChosen = false;
  15. // isChosen
  16. // bool variables try to get in the habit of starting them with `is`
  17. // the benefit of that is you can see off the bat what that variable
  18. // is trying to do
  19.  
  20. var attacker = [];
  21. // this seems like should be an object. will get back to this
  22.  
  23. var defender = [];
  24. // this seems like should be an object. will get back to this
  25.  
  26. var charactersLeft = 3;
  27.  
  28. var gameOver = false;
  29.  
  30. var startPage = $("body");
  31. // this does not seem good. will probably need refactor. will get back to this
  32.  
  33. var characters = [{
  34. health: 100,
  35. attack: 12,
  36. counter: 5,
  37. name: "Pikachu",
  38. }, {
  39. health: 120,
  40. attack: 8,
  41. counter: 10,
  42. name: "Charizard",
  43. }, {
  44. health: 150,
  45. attack: 4,
  46. counter: 20,
  47. name: "Snorlax",
  48. }, {
  49. health: 180,
  50. attack: 2,
  51. counter: 25,
  52. name: "MewTwo",
  53. }
  54. ];
  55.  
  56. if (yourChosen === false) {
  57. // `yourChosen` and this whole `if` block seems unnecessary
  58. // once the user clicks on a `.available` pokemon, the jquery at the end
  59. // of the function will then display the `.available` pokemon to be
  60. // selected as opponents
  61. // therefore you can remove `yourChosen`
  62.  
  63. $(".textBox").text("Message: Please Select Character, TURN UP SOUND to hear epicness!!!");
  64. // perhaps add this to the html file and just make it as the default text
  65. // then you can remove this line
  66. // use single quotes throughout only (this only applies to javascript file)
  67.  
  68. startPage.on("click", ".available", function () {
  69. // why not put the click event handler just on the `.available` divs?
  70. // $('.available').on('click', function() {})
  71.  
  72. document.getElementById('audiotag').play().autoplay;
  73. // this is not necessary as you already placed the `autoplay` attribute on the
  74. // html file
  75.  
  76. $(this).appendTo('#chosenChar').removeClass("available").addClass("chosenOne");
  77. // this is a bit difficult to read try formatting chains like so:
  78. // $(this)
  79. // .appendTo('#chosenChar')
  80. // .removeClass('available')
  81. // .addClass('chosenOne');
  82. // note: do not use ids. classes are preferred
  83.  
  84. attacker.push(characters[$(this).attr("value")].health);
  85. attacker.push(characters[$(this).attr("value")].attack);
  86. attacker.push(characters[$(this).attr("value")].counter);
  87. attacker.push(characters[$(this).attr("value")].name);
  88. // seems like it would be easier to add a key/value into the a
  89. // character object and change the characters array into an object
  90.  
  91. // var attacker;
  92.  
  93. // var characters = {
  94. // pikachu: {
  95. // health: 100,
  96. // attack: 12,
  97. // counter: 5,
  98. // name: "Pikachu",
  99. // },
  100. // ...
  101. // };
  102.  
  103. // attacker = characters[$(this).attr('value').toLowerCase()];
  104.  
  105. $(".textBox").text("Great, now please select your opponent! Also if your sound still isn't up, your missing out on epicness");
  106. $(".available").appendTo("#enemyAvail").removeClass("available").addClass('enemyChoose');
  107. });
  108. }
  109.  
  110. yourChosen = true;
  111. // can remove
  112.  
  113. $(".restart").on("click", function(){
  114. location.reload();
  115. })
  116. // seems like this should be displayed only when game is over
  117. // doesn't make sense to allow the user to restart at the beginning
  118. // of the game
  119. // missing semicolon at the end
  120.  
  121. function opponentChoose() {
  122. // `chooseOpponent` sounds better
  123. if (yourChosen === true) {
  124. startPage.on("click", ".enemyChoose", function () {
  125. // it seems that `opponentChoose` function is not even necessary
  126. // you can place this click event on top next to the other click event
  127. $(this).appendTo("#currentOpp").removeClass("enemyChoose").addClass('fightHim');
  128. // place this at the bottom of the function similiar to where you have
  129. // it in the other click event handler
  130. defender.push(characters[$(this).attr("value")].health);
  131. defender.push(characters[$(this).attr("value")].attack);
  132. defender.push(characters[$(this).attr("value")].counter);
  133. defender.push(characters[$(this).attr("value")].name);
  134. // same as above can alter the characters object
  135.  
  136. // var defender;
  137.  
  138. // var characters = {
  139. // pikachu: {
  140. // health: 100,
  141. // attack: 12,
  142. // counter: 5,
  143. // name: "Pikachu",
  144. // },
  145. // ...
  146. // };
  147.  
  148. // defender = characters[$(this).attr('value').toLowerCase()];
  149.  
  150. $(".textBox").text("Now attack!!!! He's trying to take your lunch money");
  151.  
  152. // i would have the attack button hidden until this point
  153. // then you can get rid of the battle function
  154. });
  155. }
  156. }
  157.  
  158. //fight opponent
  159. function battle() {
  160. // this function is unnecessary
  161.  
  162. $(".attackButton").on("click", function () {
  163. // with the above refactor this function is less difficult to read
  164. // if (attacker.health > 0) {
  165. // defender.health -= attacker.attack;
  166. // attacker.health -= defender.counter;
  167.  
  168. // $('.textBox')
  169. // .text([
  170. // 'You attacked opponent for',
  171. // attacker.attack,
  172. // 'damage.',
  173. // 'Opponent attacked you for',
  174. // defender.counter
  175. // ].join(' '));
  176. // // btw you can't use es6 without a compilier so stick to es5 for now
  177.  
  178. // $('.chosenOne .card-footer')
  179. // .text(['Health:', attacker.health].join(' '));
  180.  
  181. // $('.fightHim .card-footer')
  182. // .text(['Health:', defender.health].join(' '));
  183.  
  184. // // not good to nest if statements but oh well
  185. // if (defender.health <= 0) {
  186. // charactersLeft--;
  187.  
  188. // $('.textBox')
  189. // .text([
  190. // 'You have defeated',
  191. // defender.name,
  192. // 'and you have',
  193. // charactersLeft,
  194. // 'characters remaining'
  195. // ].join(' '));
  196.  
  197. // $('.fightHim').remove();
  198.  
  199. // defender = {};
  200.  
  201. // // opponentChoose here doesn't actually do anything. the reason
  202. // // being is that the click event handler is still attached to the
  203. // // elements. you never actually turned the handlers `off`.
  204. // // therefore i recommend turning off the click even handler until
  205. // // this time
  206. // if (!charactersLeft) {
  207. // // in js 0 is a falsey value
  208. // // this is the same
  209. // }
  210. // }
  211. // } else {
  212. // // this is the same
  213. // }
  214. if (attacker[0] > 0) {
  215. defender[0] = defender[0] - attacker[1];
  216. attacker[0] = attacker[0] - defender[2];
  217. $(".textBox").text(`You attacked opponent for ${attacker[1]} damage. Opponent attacked you for ${defender[2]}`);
  218. $(".chosenOne .card-footer").text(`Health: ${attacker[0]}`);
  219. $(".fightHim .card-footer").text(`Health: ${defender[0]}`);
  220. attacker[1] += attacker[1];
  221. // why does their attack get stronger?
  222. if (defender[0]<= 0){
  223. charactersLeft--;
  224. $(".textBox").text(`You have defeated ${defender[3]}, and you have ${charactersLeft} characters remaining`);
  225. $(".fightHim").remove();
  226. defender = [];
  227. opponentChoose();
  228. if(charactersLeft === 0){
  229. $(".textBox").text("You win big man!!!! Hit restart to play again");
  230. $(".restart").show();
  231. }
  232. }
  233. }else{
  234. $(".textBox").text("You lose loser!!! Hit restart to attempt to win, but I highly doubt it");
  235. $(".restart").show();
  236. }
  237. })
  238. }
  239.  
  240. opponentChoose();
  241. // can remove
  242.  
  243. battle();
  244. // can remove
  245.  
  246. // why all this blank space?
  247.  
  248.  
  249.  
  250.  
  251.  
  252. });
Add Comment
Please, Sign In to add comment