CS368 Homework 6
Lec 1, Spring 2016
Due by 11:59 pm on Friday, April 22 (not accepted late)

Announcements

Check here periodically.

4/11/2016  Homework assigned. To ask questions about the homework and see questions posed by other students and their answers, go to: http://www.piazza.com/wisc/spring2016/cs368 and sign-in using your wisc.edu account.

Jumble Generator

Jumble™ is a word puzzle, often found in newspapers, where a person is presented with four "common" words whose letters have been scrambled. The goal is to unscramble the words and find the solution for a quip presented along with a cartoon (which typically involves a play on words).

For this homework you will write an interactive MATLAB program that lets a user get practice solving 6-letter words jumbles.

This homework will give you experience writing code with more complicated control flow, writing and using functions, and validating user input.

The program has been broken up into several pieces, some of which are provided for you. These pieces are:

  • getNumJumbles - a function which asks the user how many jumbles they wish to attempt
  • checkGuess - a function which checks whether a user's guess is the correct word
  • solveOneJumble - a function which lets the user solve one jumble
  • jumbleGenerator - the main script which runs everything

The steps below lead you through the implementation of the Jumble Generator program.

The getNumJumbles function (6 points)

The getNumJumbles function takes no arguments and returns a positive scalar integer value representing the number of jumbles the user wishes to attempt. The getNumJumbles function should ask the user for the number of jumbles they wish to attempt and then check if the value entered by the user is a valid (i.e., a positive scalar integer value). If the value is not valid, it should also display a message indicating why the value is not valid and the user should be asked to enter another value; if that value is not valid, it should display a message and the user should be asked again to enter another value; and so on. A value is not valid if

  • it is not a scalar (i.e., it is a vector or matrix). In this case, the getNumJumbles function should display

    Number must be a scalar
  • it is not an integer (e.g., 23.45). In this case, the getNumJumbles function should display

    Number must be an integer

    Hint: the rem command returns the remainder after division.

  • it is not positive. In this case, the getNumJumbles function should display

    Number must be positive

The function should continue to ask the user for a value until the user enters a valid value. For example, getNumJumbles() should result in the following in the Command Window if the same sequence of values is entered by the user (user-entered values are have been put in bold green to make it easier to distinguish user input from what the program is causing to be displayed):

Number of jumbles to attempt: [1 2]
Number must be a scalar
Try again: -8
Number must be positive
Try again: 2.3
Number must be an integer
Try again: 7

The value returned by getNumJumbles() in the example above is 7.

Complete the getNumJumbles function. Start with this getNumJumbles.m file and modify it.

Test your getNumJumbles function thoroughly. Make sure to try values so that each of the messages gets displayed. Also, make sure to check boundary conditions. Note: you do not need to check for (or worry about) the user entering a single character.

The checkGuess function

The checkGuess function takes two arguments (the guess and the correct word) and returns true (1) or false (0) depending on whether the guess and the correct word are the same (ignoring differences in case).

The checkGuess function has already been written and is provided in the checkGuess.m file. You should not change the checkGuess function.

The solveOneJumble function (8 points)

The solveOneJumble function takes one argument (the word the user is to guess) and does the following:

  • create a jumble out of it by scrambling the letters of the word (hint: use randperm, and don't forget to make sure the scrambled word is not the same as the original word)
  • repeatedly
    • display the jumbled word
    • ask the user to guess the correct word (or enter Q to quit)
    • if the user made a guess, check if the guess is correct (and if it is, display "Correct")
    • if the user decided to quit, display the correct word in the format: The word was: correct_word
    until the user has either guessed the correct word (i.e., solve the jumble) or has decided to quit
  • return true (1) or false (0) depending on whether the jumble was correctly solved

Note: the sovleOneJumble function should be case insensitive, i.e., it shouldn't matter if the user enters text as lower-case, upper-case, or a mixture of both.

Complete the solveOneJumble function. The file solveOneJumble.m has been provided as a starting point. To receive full credit, you must call the checkGuess function within your solveOneJumble function. See the sample run below for the user prompt and output displayed by calls to solveOneJumble.

The jumbleGenerator script (6 points)

We can now put all the pieces together. The main script should do the following:

  • Read in a list of 6-letter words from a file. (The code for this step has been given to you.)
  • Ask the user for the number of jumbles they wish to attempt (using getNumJumbles).
  • For each jumble to attempt,
    • select a word at random from the list of words (hint: use randperm)
    • use solveOneJumble to create a jumble from the word and ask the user to solve it
  • After all jumbles have been attempted, display the number of jumbles attempted and number of jumbles solved in the format: # jumbles attempted: N
    # jumbles solved: S

The file jumbleGenerator.m contains an outline for the Jumble Generator program as well as the code that reads in the list of 6-letter words. Use it as a starting point. Note that jumbleGenerator is a script, not a function. To play the game, you just run the jumbleGenerator script.

Below is sample run of the program (as before, user-entered values are shown in bold green).

Number of jumbles to attempt: 5
oeshwd
Enter guess or Q to quit: showed
Correct
btbari
Enter guess or Q to quit: rabbit
Correct
uraseq
Enter guess or Q to quit: quasar
guess does not contain the correct letters
uraseq
Enter guess or Q to quit: square
Correct
ignact
Enter guess or Q to quit: intact
guess does not contain the correct letters
ignact
Enter guess or Q to quit: ACTING
Correct
ylmpsi
Enter guess or Q to quit: smily
guess is not the correct length
ylmpsi
Enter guess or Q to quit: simple
guess does not contain the correct letters
ylmpsi
Enter guess or Q to quit: limpsy
ylmpsi
Enter guess or Q to quit: q
The word was: simply
# jumbles attempted: 5
# jumbles solved: 4

Tips and hints:

  • First, download the necessary files:

  • Use an incremental approach: break the program up into small tasks, work on one task at a time, and check that the program is doing what you expect it to do before moving on to the next task.

  • Write your getNumJumbles function first and test it thoroughly. Only once you are certain that it works correctly, should you move on to the solveOneJumble function.

  • Remember that your solveOneJumble function must call the checkGuess function and your jumbleGenerator script must call getNumJumbles and solveOneJumble.

Handing in

Upload your m-files to your Dropbox in Learn@UW. See these instructions for uploading files to a Learn@UW Dropbox. The files you should upload are:

  • getNumJumbles.m
  • solveOneJumble.m
  • jumbleGenerator.m

In order for your work to be considered to have been turned in on-time, you must upload your files to your Learn@UW Dropbox by 11:59 pm Friday, April 22.

Last Updated: 4/11/2016     © 2016 Beck Hasti, hasti@cs.wisc.edu