Untitled


SUBMITTED BY: Guest

DATE: Aug. 26, 2016, 1:46 a.m.

FORMAT: Text only

SIZE: 3.4 kB

HITS: 699

  1. /*
  2. "guessing.js" is a simple guess the number game created to illustrate
  3. the basics of gaming.
  4. Usage: this file is invoked from guessing.html
  5. Author: David Johnston
  6. Date: 2016
  7. Version: 1
  8. */
  9. // game constants --------------------------------------------------
  10. const TITLE = "Guessing Game";
  11. const MAXNUM = 30;
  12. const CLUELOWER = "It's lower than that.";
  13. const CLUEHIGER = "It's higher than that";
  14. const REPSUCCESS = "Yes! You got it!";
  15. const REPINIT = "Guess the correct number before the score reaches zero.";
  16. const NAIREPLY = "That doesn't appear to be an integer. Try again.";
  17. const R00 ="You second-hand mound of goofy rotting vegetation.";
  18. const R01 = "Ha! Ha! You're wrong";
  19. const R02 = "You pitiful mouthful of rotting lizard droppings.";
  20. const R03 = "You manky dollop of yeasty rotting vegetation.";
  21. const R04 = "Nope! Try again.";
  22. const R05 = "Sigh. You're not very good at this.";
  23. const ENTERKEY = 13; // the keycode for the enter key
  24. // game variables -------------------------------------------------------------
  25. var theNumber = Math.floor(Math.random() * MAXNUM);
  26. var guess = "0"; // will be *string* entered by player
  27. var score = MAXNUM; // score counts down from this number
  28. var replies = [R00, R01, R02, R03, R04, R05];
  29. var repliesIndex = 0;
  30. var comment = "This will be changed later in the code.";
  31. var gameIsOver = false;
  32. // get and setup the page title -----------------------------------------------
  33. var pageTitle = document.getElementById("pageTitle");
  34. pageTitle.innerHTML = TITLE;
  35. var headingArea = document.getElementById("headingArea");
  36. headingArea.innerHTML = TITLE;
  37. // get and setup (if needed) the other page elements --------------------------
  38. var scoreArea = document.getElementById("scoreArea");
  39. scoreArea.innerHTML = "Score: " + MAXNUM;
  40. var instructionArea = document.getElementById("instructionArea");
  41. instructionArea.innerHTML = "Guess an integer between 0 and " + MAXNUM + ".";
  42. var playArea = document.getElementById("playArea");
  43. var commentArea = document.getElementById("commentArea");
  44. commentArea.innerHTML = REPINIT;
  45. var guessField = document.getElementById("guessField");
  46. var guessLength = ((MAXNUM.toString()).length).toString();
  47. guessField.setAttribute("maxlength", guessLength);
  48. guessField.setAttribute("size", guessLength);
  49. var guessButton = document.getElementById("guessButton");
  50. guessButton.setAttribute("onclick","checkGuess()");
  51. var resetButton = document.getElementById("resetButton");
  52. resetButton.setAttribute("onclick","location.reload()");
  53. // listen for enter key -------------------------------------------------------
  54. document.onkeyup = checkLift;
  55. function checkLift(e)
  56. {
  57. if (e.keyCode === ENTERKEY)
  58. checkGuess();
  59. }
  60. // give the guess field focus -------------------------------------------------
  61. window.onload = selectGuessField;
  62. function selectGuessField()
  63. {
  64. guessField.select();
  65. guessField.focus();
  66. }
  67. // the game "loop" ------------------------------------------------------------
  68. function checkGuess()
  69. {
  70. // *** get inputs ***
  71. guess = parseFloat(guessField.value);
  72. // *** calculate the new state ***
  73. if (Number.isInteger(guess)) // an integer was entered
  74. {
  75. if (guess == theNumber) // the guess was correct (collision detection)
  76. {
  77. comment = REPSUCCESS;
  78. gameIsOver = true;
  79. }
  80. // ******UP TO HERE************************************
  81. }
  82. }

comments powered by Disqus