/*
"guessing.js" is a simple guess the number game created to illustrate
the basics of gaming.
Usage: this file is invoked from guessing.html
Author: David Johnston
Date: 2016
Version: 1
*/
// game constants --------------------------------------------------
const TITLE = "Guessing Game";
const MAXNUM = 30;
const CLUELOWER = "It's lower than that.";
const CLUEHIGER = "It's higher than that";
const REPSUCCESS = "Yes! You got it!";
const REPINIT = "Guess the correct number before the score reaches zero.";
const NAIREPLY = "That doesn't appear to be an integer. Try again.";
const R00 ="You second-hand mound of goofy rotting vegetation.";
const R01 = "Ha! Ha! You're wrong";
const R02 = "You pitiful mouthful of rotting lizard droppings.";
const R03 = "You manky dollop of yeasty rotting vegetation.";
const R04 = "Nope! Try again.";
const R05 = "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, R03, R04, R05];
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 enter key -------------------------------------------------------
document.onkeyup = checkLift;
function checkLift(e)
{
if (e.keyCode === ENTERKEY)
checkGuess();
}
// give the guess field focus -------------------------------------------------
window.onload = selectGuessField;
function selectGuessField()
{
guessField.select();
guessField.focus();
}
// the game "loop" ------------------------------------------------------------
function checkGuess()
{
// *** get inputs ***
guess = parseFloat(guessField.value);
// *** calculate the new state ***
if (Number.isInteger(guess)) // an integer was entered
{
if (guess == theNumber) // the guess was correct (collision detection)
{
comment = REPSUCCESS;
gameIsOver = true;
}
// ******UP TO HERE************************************
}
}