CIT 590 Assignment 11: Cryptograms
Spring 2013, David Matuszek

Purpose of this assignment

General idea of the assignment

Write a program to encode a randomly chosen quote, and let the user figure out the solution. If you are unfamiliar with cryptograms, see http://www.cryptograms.org/ for an explanation. On that page there is also a Play tab that you can click to try to solve a cryptogram.

The display

In your GUI, you should have a text area that is 40 characters wide an 8 lines high. The lines are used as follows: code, guess, blank, code, guess, blank, code, guess, blank, code, guess. The "code" lines are where the cryptogram is displayed; the "guess" lines are where your attempted solution is displayed; and the "blank" lines are just for neatness.

Use a monospace font (see java.awt.Font) so that letters line up nicely, and your display looks like this:

Leh gyls ahwq wtq gyls ahwsp; kp'e
Use your head and your heart; it's

typ hrhsgpakto, jlp kp'e w epwsp.
not everything, but it's a start.

and not like this:

Leh gyls ahwq wtq gyls ahwsp; kp'e
Use your head and your heart; it's

typ hrhsgpakto, jlp kp'e w epwsp.
not everything, but it's a start.

Use the quotes from fortunes.txt, which I have downloaded from http://silgro.com/fortunes.htm. These quotes are divided into five categories:

You can put this file in your project directory (not your src or bin subdirectory) and read it in as follows:

  File quotes = new File("quotes.txt");
  Reader reader = new FileReader(quotes);
  BufferedReader lineReader = new BufferedReader(reader);
  String line;
  while ((line = lineReader.readLine()) != null) {
      // Do something with the line
  }

Use a Combo Box to let the user choose one of the five categories, or "Any category."

Construct a random code for the quote. In the above example, a→w, b→j, etc. There is one restriction on the random encoding: No letter is allowed to map to itself. Every letter must be encoded as a different letter.

Capital letters use the same encoding, but capital letters should be displayed as capital letters, and small letters as small letters. Spaces and punctuation marks are displayed as themselves.

Decoding (which the user does) is the reverse of encoding, except that all letters initially map to a blank, meaning that the user hasn't guessed that letter yet.

None of the quotes in this file is longer than 80 characters (not counting the category code at the beginning), but some quotes have an author, which can extend the quote to 107 characters. Three lines of 40 characters each should give you enough room for any of these quotes.

You can set the TextArea to a (one) String, not several, so you have to compose that string yourself, as "code\nguess\n\ncode\nguess\n\ncode\nguess". Do not break words across lines; every line break should be at a space.

For user input, provide a TextField in which the user can type an even number of characters: Each character in the first half is replaced by the corresponding character in the second half. For example, interpret wtqand to mean w→a, t→n, q→d. If the user enters an odd number of characters, append a blank to the end to make it even (blank means erase the previous guess for that character.) The first half of the user entry must be letters only (it's okay to restrict input to lowercase, which is easier to type anyway); the second half may be letters and/or blanks. Reject any input that does not follow these rules.

Get and process the input when the user presses Enter in the text field. This means updating the larger TextArea, erasing the TextField, and checking if the user has solved the cryptogram.

New guesses can replace previous guesses. If the user has already guessed w→a, t→n, q→d, but then enters typpar, the result is w→' ', t→p, q→d, y→a, p→r. Not only is the old value for t (which was n) replaced by the new value (p), but also since the user has decided y→a, w can no longer be a, and reverts to a blank (meaning not guessed). The site mentioned above, www.cryptograms.org, sadly does not do this housekeeping for the user.

Repeated letters are legal (if silly), and the translations are done left to right. So hkkzwhen means h→w, k→e, z→n. The first mapping of k, k→h, gets replaced by k→e. This will have the (probably undesired) side effect that if some other letter mapped to h, say a→h, the decoding for that letter will change to be a blank.

Recognize when the user has solved the puzzle, and bring up a congratulatory (message) dialog box.

Have buttons for:

Optional button, only if you have the rest working:

The unit tests

In this assignment, I am not specifying exactly what methods and what variables to use. You are taking more of the responsibility for software design; I'm just specifying what the program should look like and do. So...

Grading

Correctness. Style. Documentation. Good unit tests.

Due date

Before 6am Friday April 12. Submit your zipped project file to Canvas. No other form of submission will be accepted.