Untitled


SUBMITTED BY: Guest

DATE: Aug. 25, 2016, 11:40 p.m.

FORMAT: Text only

SIZE: 2.5 kB

HITS: 1014

  1. /*
  2. Description: "guessing.js" is a simple guess the number game made to illustrate the basics
  3. of game programming.
  4. Author: David Johnston
  5. Date: 2016
  6. Version: 1
  7. Usage: Invoked via guessing.html
  8. */
  9. // game constants -------------------------------------------------------------
  10. const TITLE = "Guessing Game";
  11. const MAXNUM = 20;
  12. const CLUELOWER = "It's lower than that.";
  13. const CLUEHIGHER = "It's higher than that.";
  14. const REPSUCCESS = "Yes! You got it!";
  15. const NAIREPLY = "That doesn't appear to be an integer. Try again.";
  16. const R00 = "Ha! Ha! You're wrong!";
  17. const R01 = "Nope! Try again.";
  18. const R02 = "Sigh. You're not very good at this.";
  19. const R03 = "You god-awful shovel-full of fermenting garbage.";
  20. const R04 = "You nauseating bunch of illiterate rat cysts.";
  21. const R05 = "You insolent lump of unimpressive hogwash.";
  22. const ENTERKEY = 13; // the keycode for the enter key
  23. // game variables -------------------------------------------------------------
  24. var theNumber = Math.floor(Math.random() * MAXNUM);
  25. var guess = "0"; // will be a *string* entered by player
  26. var score = MAXNUM; // score counts down from this number
  27. var replies = [R00, R01, R02, R03, R04, R05];
  28. var repliesIndex = 0;
  29. var comment = "This will be changed later in the code.";
  30. var gameIsOver = false;
  31. // get and set up the page title ----------------------------------------------
  32. var pageTitle = document.getElementById("pageTitle");
  33. pageTitle.innerHTML = TITLE;
  34. var headingArea = document.getElementById("headingArea");
  35. headingArea.innerHTML = TITLE;
  36. // get and setup (if needed) the other page elements --------------------------
  37. var scoreArea = document.getElementById("scoreArea");
  38. scoreArea.innerHTML = "Score: " + MAXNUM;
  39. var instructionArea = document.getElementById("instructionArea");
  40. instructionArea.innerHTML = "Guess an integer between 0 and " + MAXNUM + ".";
  41. var playArea = document.getElementById("playArea");
  42. var commentArea = document.getElementById("commentArea");
  43. commentArea.innerHTML = REPINIT;
  44. var guessField = document.getElementById("guessField");
  45. var guessLength = ((MAXNUM.toString()).length).toString();
  46. guessField.setAttribute("maxlength", guessLength);
  47. guessField.setAttribute("size", guessLength);
  48. var guessButton = document.getElementById("guessButton");
  49. guessButton.setAttribute("onclick","checkGuess()");
  50. var resetButton = document.getElementById("resetButton");
  51. resetButton.setAttribute("onclick","location.reload()");

comments powered by Disqus