/* "guessing.js" is a simple guessing game written to introduce students to the basics of game programming. Written by David Johnston in 2016 whilst employed by the NSW Department of Education. */ // game constants ------------------------------------------------------------- const TITLE = "Guessing Game"; const MAXNUM = 20; const CLUELOWER = "It's lower than that."; const CLUEHIGHER = "It's higher than that."; const REPSUCCESS = "Yes! You got it!"; const REPINIT = "Guess the correct number before your score reaches zero!"; const NAIREPLY = "That doesn't appear to be an integer. Try again."; // insults at: [http://goo.gl/AH99cv] datahamster.com/autoinsult/ const R00 = "Ha! Ha! You're wrong!"; const R01 = "Nope! Try again."; const R02 = "Sigh. You're not very good at this."; const ENTERKEY = 13; // the keycode for the enter key // game variables ------------------------------------------------------------- var theNumber = Math.floor(Math.random() * MAXNUM); var guess = "0"; // will be *string* entered by player var score = MAXNUM; //score counts down from this number var replies = [R00, R01, R02]; var repliesIndex = 0; var comment = "This will be changed later in the code."; var gameIsOver = false; // get and setup the page title ----------------------------------------------- var pageTitle = document.getElementById("pageTitle"); pageTitle.innerHTML = TITLE; var headingArea = document.getElementById("headingArea"); headingArea.innerHTML = TITLE; // get and setup (if needed) the other page elements -------------------------- var scoreArea = document.getElementById("scoreArea"); scoreArea.innerHTML = "Score: " + MAXNUM; var instructionArea = document.getElementById("instructionArea"); instructionArea.innerHTML = "Guess an integer between 0 and " + MAXNUM + "."; var playArea = document.getElementById("playArea"); var commentArea = document.getElementById("commentArea"); commentArea.innerHTML = REPINIT; var guessField = document.getElementById("guessField"); var guessLength = ((MAXNUM.toString()).length).toString(); guessField.setAttribute("maxlength", guessLength); guessField.setAttribute("size", guessLength); var guessButton = document.getElementById("guessButton"); guessButton.setAttribute("onclick","checkGuess()"); var resetButton = document.getElementById("resetButton"); resetButton.setAttribute("onclick","location.reload()"); // listen for the enter key --------------------------------------------------- document.onkeyup = checkLift; function checkLift(e) { if (e.keyCode === ENTERKEY) checkGuess(); } // give the guess field the focus --------------------------------------------- window.onload = selectGuessField; function selectGuessField() { guessField.select(); guessField.focus(); } // the game "loop" ------------------------------------------------------------ function checkGuess() { // ***** get inputs ***** guess = parseFloat(guessField.value); // ***** calculate new state ***** if (Number.isInteger(guess)) // an integer was entered { if (guess == theNumber) // the guess was correct (collision detection) { comment = REPSUCCESS; gameIsOver = true; } else // the guess was incorrect { repliesIndex = Math.floor(Math.random() * replies.length); score = score - 1; if (guess > theNumber) // the guess is too large { comment = replies[repliesIndex] + " " + CLUELOWER; } else // the guess must be too small (equality is already handled) { comment = replies[repliesIndex] + " " + CLUEHIGHER; } } } else // an integer was not entered { comment = NAIREPLY; } // ***** render new state ***** commentArea.innerHTML = comment; scoreArea.innerHTML = "Score: " + score; if (gameIsOver) { playArea.innerHTML = "▶ ▶ ▶  " + guess + "  ◀ ◀ ◀"; } else { selectGuessField(); } }