Untitled


SUBMITTED BY: Guest

DATE: Aug. 29, 2016, 2:41 a.m.

FORMAT: Text only

SIZE: 3.2 kB

HITS: 1104

  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 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 = "Ha! Ha! You're wrong!";
  18. const R01 = "Nope! Try again.";
  19. const R02 = "Sigh. You're not very good at this.";
  20. const R03 = "You god-awful shovel-full of fermenting garbage.";
  21. const R04 = "You nauseating bunch of illiterate rat cysts.";
  22. const R05 = "You insolent lump of unimpressive hogwash.";
  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 a *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 set up 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 the enter key ---------------------------------------------------
  54. document.onkeyup = checkLift;
  55. function checkLift(e)
  56. {
  57. if (e.keycode === ENTERKEY)
  58. checkGuess();
  59. }
  60. // give the guess field the 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. // I AM UP TO HERE !!!!!!!!!!!!!!!!!
  72. }

comments powered by Disqus