Guessing game


SUBMITTED BY: FatCookies

DATE: Sept. 9, 2016, 2:22 a.m.

FORMAT: Text only

SIZE: 4.2 kB

HITS: 4740

  1. /*
  2. "guessing.js" is a simple guessing game written to introduce students to
  3. the basics of game programming. Written by David Johnston in 2016 whilst
  4. employed by the NSW Department of Education.
  5. */
  6. // game constants -------------------------------------------------------------
  7. const TITLE = "Guessing Game";
  8. const MAXNUM = 20;
  9. const CLUELOWER = "It's lower than that.";
  10. const CLUEHIGHER = "It's higher than that.";
  11. const REPSUCCESS = "Yes! You got it!";
  12. const REPINIT = "Guess the correct number before your score reaches zero!";
  13. const NAIREPLY = "That doesn't appear to be an integer. Try again.";
  14. // insults at: [http://goo.gl/AH99cv] datahamster.com/autoinsult/
  15. const R00 = "Ha! Ha! You're wrong!";
  16. const R01 = "Nope! Try again.";
  17. const R02 = "Sigh. You're not very good at this.";
  18. const ENTERKEY = 13; // the keycode for the enter key
  19. // game variables -------------------------------------------------------------
  20. var theNumber = Math.floor(Math.random() * MAXNUM);
  21. var guess = "0"; // will be *string* entered by player
  22. var score = MAXNUM; //score counts down from this number
  23. var replies = [R00, R01, R02];
  24. var repliesIndex = 0;
  25. var comment = "This will be changed later in the code.";
  26. var gameIsOver = false;
  27. // get and setup the page title -----------------------------------------------
  28. var pageTitle = document.getElementById("pageTitle");
  29. pageTitle.innerHTML = TITLE;
  30. var headingArea = document.getElementById("headingArea");
  31. headingArea.innerHTML = TITLE;
  32. // get and setup (if needed) the other page elements --------------------------
  33. var scoreArea = document.getElementById("scoreArea");
  34. scoreArea.innerHTML = "Score: " + MAXNUM;
  35. var instructionArea = document.getElementById("instructionArea");
  36. instructionArea.innerHTML = "Guess an integer between 0 and " + MAXNUM + ".";
  37. var playArea = document.getElementById("playArea");
  38. var commentArea = document.getElementById("commentArea");
  39. commentArea.innerHTML = REPINIT;
  40. var guessField = document.getElementById("guessField");
  41. var guessLength = ((MAXNUM.toString()).length).toString();
  42. guessField.setAttribute("maxlength", guessLength);
  43. guessField.setAttribute("size", guessLength);
  44. var guessButton = document.getElementById("guessButton");
  45. guessButton.setAttribute("onclick","checkGuess()");
  46. var resetButton = document.getElementById("resetButton");
  47. resetButton.setAttribute("onclick","location.reload()");
  48. // listen for the enter key ---------------------------------------------------
  49. document.onkeyup = checkLift;
  50. function checkLift(e)
  51. {
  52. if (e.keyCode === ENTERKEY)
  53. checkGuess();
  54. }
  55. // give the guess field the focus ---------------------------------------------
  56. window.onload = selectGuessField;
  57. function selectGuessField()
  58. {
  59. guessField.select();
  60. guessField.focus();
  61. }
  62. // the game "loop" ------------------------------------------------------------
  63. function checkGuess()
  64. {
  65. // ***** get inputs *****
  66. guess = parseFloat(guessField.value);
  67. // ***** calculate new state *****
  68. if (Number.isInteger(guess)) // an integer was entered
  69. {
  70. if (guess == theNumber) // the guess was correct (collision detection)
  71. {
  72. comment = REPSUCCESS;
  73. gameIsOver = true;
  74. }
  75. else // the guess was incorrect
  76. {
  77. repliesIndex = Math.floor(Math.random() * replies.length);
  78. score = score - 1;
  79. if (guess > theNumber) // the guess is too large
  80. {
  81. comment = replies[repliesIndex] + " " + CLUELOWER;
  82. }
  83. else // the guess must be too small (equality is already handled)
  84. {
  85. comment = replies[repliesIndex] + " " + CLUEHIGHER;
  86. }
  87. }
  88. }
  89. else // an integer was not entered
  90. {
  91. comment = NAIREPLY;
  92. }
  93. // ***** render new state *****
  94. commentArea.innerHTML = comment;
  95. scoreArea.innerHTML = "Score: " + score;
  96. if (gameIsOver)
  97. {
  98. playArea.innerHTML = "▶ ▶ ▶  "
  99. + guess + "  ◀ ◀ ◀";
  100. }
  101. else
  102. {
  103. selectGuessField();
  104. }
  105. }

comments powered by Disqus