CIS 554 Scala 2: Playfair Cipher
Fall 2013, David Matuszek

Purpose of this assignment

To get you started programming functionally in Scala.

General idea

Implement the Playfair Cipher (see the Wikipedia article) for enciphering and deciphering messages. Use this variant:

Assignment details

Here is an example input to be encoded:

An anonymous reader sends word of a proof-of-concept Google Chrome browser
extension that steals users' login details. The developer, Andreas Grech,
says that he is trying to raise awareness about security among end users,
and therefore chose Chrome as a test-bed because of its reputation as the
safest browser.

With keyword "Pennsylvania", here is the code block:

 p   e   n   s   y 
 l   v   a   i   b 
 c   d   f   g   h 
 k   m   o   q   r 
 t   u   w   x   y

Here is an example encoded output:

fafaw aermw yqnvm vqyns genwm hwoln kqwow ofkpf nexcq wqfvp
dckqu vhzwn ynmyz unsig wazcl wpxnv ipxey mpiqf asmvw lbvpx
dymvd vaken obefm yinhq pdgyb npxfb zcsvp xzbas cxqki bynfn
bonsn yniar wuynd tqbzp vowad sefxe ymnie fzcym ndqkp dfryn
dckqu vinlw nyzlv mvyfl xenmg axpmy etwlx lwain zcnyf onyzl
kqxny m

Name your project Playfair, your package playfair, and your "main" object Playfair. The main method will interact with the user: It will ask whether to encode, decode, or quit; then (unless quitting) it will ask for the keyword, then it will ask for the name of a file to be encoded/decoded. It will read in the file, encode or decode it, and display the result. This should be repeated until the user quits the program.

Create a class Coder(keyword: String) with an encode(plainText: String): String method and a decode(secretText: String): String method. Use additional classes and objects as you see fit.


Text to be enciphered will be a single String of arbitrary text. It will be mixed case, with spaces, newlines, and punctuation marks. The return value should be all lowercase; letters should be blocks of 5, with a single space between blocks (the last block may contain fewer than 5 characters); and there will be ten blocks per line (the last line may have fewer blocks). There should be no whitespace at the beginning or the end, and only a single space or a single newline between blocks. All the punctuation should be discarded.


Text to be decoded will be in the format produced by the encoder. The result will have the same structure as the encoded text (blocks of five letters, whitespace as specified above. However, note that the number of blocks, and the number of characters per block, may not be the same as in the encoded file.

Testing and comments

Every method, except main, should be tested with Scalatest. TDD (Test-Driven Development) is strongly recommended. Running the tests should not request input or produce output. Your program will also be tested with our unit tests, so be sure all parameters and results are exactly as specified.

Every method should have Scaladoc comments. Be sure to use the @author tag in the comment for your Playfair object, so we know who you are.

Special rules

The following rules apply to all code except the Scalatest tests.

The reasons for these rules are twofold: (1) They are intended to get you using some of the unique Scala constructs, and (2) It is easy to write a program to count these keywords.

Due date

Please turn in to Canvas your zip file by Wednesday, November 13 , before 6:00am.