Advertisement
Guest User

js

a guest
Nov 17th, 2019
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.52 KB | None | 0 0
  1. var NUM_BALLS = 6; // num of balls for selection, it must be in range of CODE_LENGTH..8
  2. var CODE_LENGTH = 4; // set this number in the range of 1..5
  3. var NUM_ATTEMPTS = 7; // change this number to have less or more attempts in range of 1..8
  4. var MAX_NUM_ATTEMPTS = 8; // do not change this number
  5.  
  6. var peg_selected = 0;
  7. var attempt_code;
  8. var current_attempt_id;
  9. var start = new Date();
  10. var btn_initial_top;
  11. var url = "http://indigo.eecs.yorku.ca:3000/post";
  12. //when you are in Part2 of the project (server side coding), you
  13. //shoudl comeback here and change the url to localhost.
  14. var myName;
  15.  
  16. window.onload = function()
  17. {
  18. createGameBoard(); //draw the game board
  19.  
  20. //read CSS to define the button initial position
  21. var step = parseInt($(".attempt").css("margin-top"))
  22. + parseInt($(".attempt").css("height"));
  23. var attemptHeight = parseInt($(".attempt").css("height"));
  24. btn_initial_top = parseInt($("#acceptcode").css("top"))
  25. - (MAX_NUM_ATTEMPTS - NUM_ATTEMPTS) * step;
  26.  
  27. //set game board height.
  28. $("#gameboard").css("height", NUM_ATTEMPTS * step + attemptHeight+"px");
  29.  
  30. //game player will enter their name here
  31. myName = prompt("Please enter your name", "");
  32. $('#name').text(myName);
  33.  
  34. initGameBoard();
  35.  
  36. // start the timer
  37. setInterval(function()
  38. {$("#timer").text(parseInt((new Date() - start) / 1000) + "s");}, 1000);
  39.  
  40. }
  41.  
  42. /*
  43. * Create the game board, includes
  44. * one line to display the code images - "coderow"
  45. * 8 attempts
  46. * 1 accept button
  47. * 8 peg selections
  48. */
  49. function createGameBoard(){
  50.  
  51. //add code images (dummy code)
  52. for (var i = 1; i <= CODE_LENGTH; i++){
  53. var newImg = document.createElement("img");
  54. $(newImg).attr("id", "code" + i);
  55. //add a dummy image
  56. $(newImg).attr("src", "./images/hole.png");
  57. $("#coderow").append(newImg);
  58. }
  59.  
  60. //add attempts
  61. for (var i = NUM_ATTEMPTS; i > 0; i--){
  62.  
  63. //for each attempt, we create a div
  64. var newDiv = document.createElement("div");
  65. // set its id and class. Hint: to set its class, you can write: $(newDiv).attr("class", "attempt");
  66. //...
  67. //...
  68.  
  69. // create a span, and set its id and class
  70. var newSpan = document.createElement("span");
  71. //...
  72. //...
  73. // then add 5 images including ids and classes. The img source could be empty or could be the hole.png
  74. for (var j = 1; j <= CODE_LENGTH; j++){
  75. //...
  76. //...
  77. //...
  78. //...
  79. $(newSpan).append(newImg);
  80. }
  81. //append the span to the div
  82. $(newDiv).append(newSpan);
  83.  
  84. // create a new span for displaying result of the end-user attempt, set id and append it to the div
  85. //...
  86. //...
  87. //...
  88.  
  89. // append each div to the game board
  90. $("#gameboard").append(newDiv);
  91. }
  92.  
  93. //add Accept button inside a <div>
  94. var newDiv = document.createElement("div");
  95. $(newDiv).attr("id", "acceptcode");
  96. var newInput = document.createElement("input");
  97. $(newInput).attr("type", "button");
  98. $(newInput).attr("name", "Accept");
  99. $(newInput).attr("value", "Accept");
  100. $(newInput).click(process_attempt); // set onclick event handler
  101. $(newDiv).append(newInput);
  102. // add this button div to the game board
  103. $("#gameboard").append(newDiv);
  104.  
  105. // add peg selection
  106. // create 8 img elements and
  107. // show each of the 8 marbles images with shadow from the images folder, also set their id and event handler
  108. for (var i = 1; i <= NUM_BALLS; i++){
  109. //...
  110. //...
  111. //...
  112. // set onclick event handler and pass event.data.id as a parameter
  113. $(newImg).click({id: i}, select_peg);
  114. $("#pegselection").append(newImg);
  115. }
  116.  
  117. }
  118.  
  119. /*
  120. * Initiate the game board
  121. * Reset all the holds
  122. * Reset the btn position and its visibility
  123. * Send a "generate code" request to the server
  124. */
  125. function initGameBoard(){
  126. //reset holds
  127. for (var i = NUM_ATTEMPTS; i > 0; i--){
  128. for (var j = 1; j <= CODE_LENGTH; j++){
  129. //reset the image on each hole
  130. $("#attempt" + i + "_" + j).attr("src", "./images/hole.png");
  131. $("#attempt" + i + "_" + j).css({'opacity' : 0.3});
  132. }
  133. //reset the "attempt#result" to empty
  134. $("#attempt" + i + "result").empty();
  135. }
  136.  
  137. //reset the button's position and visibility
  138. current_attempt_id = 0;
  139. var step = parseInt($(".attempt").css("margin-top"))
  140. + parseInt($(".attempt").css("height"));
  141. $("#acceptcode").css({'top' : btn_initial_top + 'px'});
  142. $("#acceptcode").css({'visibility' : 'visible'});
  143.  
  144. // show the cover to hide code
  145. $("#cover").css({'visibility' : 'visible'});
  146.  
  147. //send request to server to start a new game.
  148. $.post(url+'?data='+JSON.stringify({
  149. 'name':myName, //client's identity on the server
  150. 'action':'generateCode'}),
  151. response);
  152.  
  153. }
  154.  
  155. /*
  156. * Activate an attempt
  157. * @param id is the attempt id to be activated
  158. */
  159. function activateAttempt(id){
  160. //remove onclick event for all holes
  161. $(".imgAttempt").off("click");
  162.  
  163. //reset the visibility of the current attempt,
  164. //and add onclick event to the holes in this attempt
  165. for (var i = 1; i <= CODE_LENGTH; i++){
  166. $("#attempt"+id+"_"+i).css({'opacity' : 1});
  167. $("#attempt"+id+"_"+i).click({id: i}, process_hole);
  168. }
  169.  
  170. current_attempt_id = id;
  171.  
  172. //reset the attempt code array
  173. attempt_code = new Array(CODE_LENGTH).fill(0);
  174. }
  175.  
  176. /*
  177. * OnClick event handler for holes
  178. * @param event.data.id is the hole's id in this attempt
  179. */
  180. function process_hole(event){
  181. if (peg_selected != 0){
  182. //display the selected ball on the hold
  183. $(this).attr("src", "./images/ball_" + peg_selected + ".png");
  184. attempt_code[event.data.id-1] = peg_selected;
  185. }else{
  186. //no ball was selected
  187. alert("Please select the ball!")
  188. }
  189. }
  190.  
  191. /*
  192. * OnClick event handler for the Accept button
  193. * send request to the server
  194. */
  195. function process_attempt(){
  196. //console.dir("this is processing attempt");
  197. if (!attempt_code.includes(0)){
  198. //move the button up and display the result
  199. var step = parseInt($(".attempt").css("margin-top"))
  200. + parseInt($(".attempt").css("height"));
  201.  
  202. $(this).parent().css({'top' : btn_initial_top
  203. - current_attempt_id * step + 'px'});
  204.  
  205. //send the attempt_code to server for evaluation
  206. $.post(
  207. url+'?data='+JSON.stringify({
  208. 'name':myName,
  209. 'action':'evaluate',
  210. 'attempt_code':attempt_code,
  211. 'current_attempt_id':current_attempt_id
  212. }),
  213. response
  214. );
  215. // hide the btn to wait for server's response
  216. $(this).parent().css({'visibility' : 'hidden'});
  217. }else{
  218. //the attempt is not completed.
  219. alert("Please complete your attempt!");
  220. }
  221.  
  222. }
  223.  
  224. /*
  225. * Event handler for server's response
  226. * @param data is the json format string sent from the server
  227. */
  228. function response(data, status){
  229. var response = JSON.parse(data);
  230. console.log(data);
  231. if (response['action'] == 'generateCode'){
  232.  
  233. myName = response['nameID'];
  234.  
  235. // acttion: Generate Code
  236. activateAttempt(1); //activate the first attempt
  237. peg_selected = 0; //no peg should be selected
  238. //reset the visibility of every shadow_balls
  239. for (var i = 1; i <= NUM_BALLS; i++){
  240. $("#shadow"+i).css({'opacity' : 1});
  241. }
  242.  
  243. //reset timer
  244. start = new Date();
  245.  
  246. } else if (response['action'] == 'evaluate'){
  247. // acttion: Evaluate
  248. // after receiving the server's response,
  249. // then make the button <div> visible
  250. $("#acceptcode").css({'visibility' : 'visible'});
  251.  
  252. //read data from the json object that send back from the server
  253. var win = response['win'];
  254. var num_match = response['num_match'];
  255. var num_containing = response['num_containing'];
  256. var num_not_in = response['num_not_in'];
  257. var code = response['code']
  258.  
  259. //display the number of balls that match the code
  260. displayResult(num_match, "black");
  261. //display the number of balls in the code
  262. displayResult(num_containing, "white");
  263. //display the number of balls not in the code
  264. displayResult(num_not_in, "empty");
  265.  
  266. if (current_attempt_id < NUM_ATTEMPTS && !win){
  267. //haven't won yet, game will continue
  268. //activate the next attempt
  269. current_attempt_id++;
  270. activateAttempt(current_attempt_id);
  271. } else {
  272. //game ended, display result, hide button
  273. $("#acceptcode").css({'visibility' : 'hidden'});// hide button <div>
  274. $("#cover").css({'visibility' : 'hidden'}); // hide code cover to display the code
  275. displayCode(code); // display the code
  276. win? alert("GG! You win. Click enter to play again.") // won!!!
  277. : alert("Uh Oh, Click enter to try again!"); // lost!!!
  278. initGameBoard();
  279. }
  280. }
  281. }
  282.  
  283. /*
  284. * Display result in "attempt#result" span
  285. * @param num is the number of images to display
  286. * @param color is the color of the image
  287. */
  288. function displayResult(num, color){
  289. while (num > 0){
  290. //add image to result
  291. var newImg = document.createElement("img");
  292. $(newImg).attr("src", "./images/"+color+"_peg.png");
  293. $("#attempt" + current_attempt_id + "result").append(newImg);
  294. num--;
  295. }
  296. }
  297.  
  298. /*
  299. * Display the code when the client completed the game
  300. * The client won the game or lost the game after 8 attempts.
  301. * @param code is the code to display
  302. */
  303. function displayCode(code){
  304. for (var i = 1; i <= CODE_LENGTH; i++){
  305. $("#code"+i).attr("src", "./images/ball_"+ code[i-1] +".png");
  306. }
  307. }
  308.  
  309. /*
  310. * Event handler for peg selection
  311. * @param event.data.id is the peg id to be selected
  312. */
  313. function select_peg(event){
  314. peg_selected = event.data.id;
  315. //reset the visibility of every balls
  316. for (var i = 1; i <= NUM_BALLS; i++){
  317. $("#shadow"+i).css({'opacity' : 0.45});
  318. }
  319. //increase the visibility of the selected ball
  320. $(this).css({'opacity' : 1});
  321. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement