Introduction to (Web) Programming

Program 4: guessing games

Due date: Thursday, March 7 at 1:30pm

Instructions


The guessing games

The primary goal of this assignment is to build two different guessing games. The first is a version of the “classic” high-low guessing game: the computer “thinks” of a number, the human player tries to guess that number in as few guesses as possible; the computer reports whether guesses are too low, too high or correct. Try playing the solution version which asks you to guess a number between 1 (inclusive) and 100 (exclusive). You can play for a different range of values by playing from the console as in:

> humanGuesses(50, 60);

The second game is a version of the word-guessing game “hangman”. It expects two human players (the computer is just the mediator). The first human player enters some alphabetic text (ideally an actual word). The computer then presents a masked version of that word (for instance, loop would appear as ***). The other player then tries to discover the word by guessing one letter at a time. After each guess, the computer responds whether the letter is in the word and if so how many times it occurs. It then repeats the process, but now revealing where in the word the guessed letter occurs. For instance, if the secret word is loop and the player guesses o, the o’s are revealed as *OO* (the game is case insensitive and guesses are displayed by the computer as uppercase letters). Try playing the solution version.


The problems themselves

  1. Complete the function isAlphaChar(c) so that it returns true if c is an alphabetic character (so it must have an uppercase or lowercase letter in its position 0 and be of length 1) and false otherwise. Examples:

     > isAlphaChar('b')
     true
     > isAlphaChar('D')
     true
     > isAlphaChar('')
     false
     > isAlphaChar('!')
     false
     > isAlphaChar('TooManyCharacters')
     false

    (Hint: the problem is meant to be simple and very similar to one on Lab 4.)

  2. Complete the function isAlpha(s) so that it returns true if s is not empty and consists entirely of alphabetic characters (i.e., lowercase or uppercase letters). Use a while loop that repeatedly calls isAlphaChar. Examples:

     > isAlpha('q')
     true
     > isAlpha('coMputerScience')
     true
     > isAlpha('')
     false
     > isAlpha('Wow!')
     false
     > isAlpha('Da5id')
     false
  3. Complete the function has(s, c) so that it returns true if character c occurs in string s and false otherwise. Case sensitive. Again, use a while loop. Examples:

     > has('somewhere', 'w')
     true
     > has('What? Repeat that.', '?')
     true
     > has ('What?', 'w')
     false
  4. Complete the function function unmaskChar(source, c) so that it returns a string indicating where the character c occurs in the string source. The returned string should be the same length as source and consisting of c and MASK_CHAR (a globally defined symbolic constant) with c in the all the places where it occurs in source and MASK_CHAR in all the other positions. Examples:

     > unmaskChar('loop', 'o')
     "*oo*"
     > unmaskChar('Alphabet', 'a')    // case sensitive!
     "****a***"

    (Hint: you may want to base your solution on the code for the mask function from the previous homework which is supplied for you near the bottom of the hw4.js file. The major addition you need is an if/else in the body of the loop.)

  5. Complete the function mesh(masked1, masked2) so that it returns a new string that is the same length as masked1 which consists of the combined revealed symbols in the two strings. In other words, if the two strings were “lined up” as in:

     *PP**
     ***LE

    the result would be:

    *PPLE

    The idea is that combined with a prudent use of unmaskChar, mesh can help you implement the word-guessing game by revealing newly guessed letters. Examples:

     > mesh('*pp**', '***le')
     "*pple"
     > mesh('*pple', 'A****')
     "Apple"

    The next set of problems require writing small REPLs that will be helpful for building our guessing games.

  6. Complete promptAlphaChar(message) so that it repeatedly prompts the user with the message until the user enters a single alphabetic character in which case it returns that character or the user “quits” in which case it returns null. Quitting is defined by canceling the prompt or entering an empty string and then confirming that the user wishes to quit. This function is mostly completed for you.

  7. Complete promptAlpha(message) so that it does the same thing as promptAlphaChar except that it accepts arbitrary length alphabetic strings instead of just single letters. It should require only minor variations from the solution to the previous problem.

  8. Complete getValidInteger(low, high) so that it repeatedly prompts the user for a number in that range (inclusive for low, and exclusive for high) until the user enters a valid integer in that range, then it returns that integer. As with the previous two problems, canceling or clicking Ok without entering any text in the prompt box should confirm with the user if they want to quit, and, if so, return null.


    The games themselves.

  9. Complete humanGuesses(low, high) so that it plays the first guessing game described near the start of this document. Use randomRange and getValidInteger. Follow the model of the solution.

  10. Complete wordmask() so that it plays the second guessing game described near the start of this document (our version of “hangman”). Use promptAlpha, promptAlphaChar, has, unmaskChar, and mesh. You are welcome to use the supplied mask and countOccurrences. Follow the model of the solution.


Restrictions and requirements

Any of your functions that return values should have a single return statement as the last statement within the body of the function.

Only problems 6-10 should use prompt, confirm or alert.

If you are not sure if you can use a specific construct or operation, just ask.


To experiment more directly with my solutions, you can open up on a console while viewing this page and try examples such as:

 > isAlphaChar('x')
 
 > isAlpha('Hello world!')
 
 > has('Apple', 'p')
 
 > unmaskChar('wow', 'w')
 
 > mesh('***put*r', 'c*****er')
 
 > promptAlphaChar('Enter a letter: ')
 
 > promptAlpha('Enter a word: ')
 
 > getValidInteget(20, 30)
 
 > humanGuesses();
 
 > wordmask(30, 40);