Before you begin...

Make a copy of your boggle.c file as boggle2.c by invoking the command
georgeBush@sf3:~[120]$ cp boggle.c boggle2.c
Part 2 is to be modified using boggle2.c and submitted at the end of the session.


Overview - A more realistic Boggle game

From part 1, we noticed that at the start of the Boggle game, random letters are initialized for each cell of the tray. In the classic 4 x 4 game, the cells of the tray are filled using sixteen letter cubes (with one letter on each of the six faces of a cube). The sixteen cubes are simultaneously assigned in the actual game; however in our program we shall simulate something similar, but much simpler.

Imagine we have a bag in which all sixteen cubes are placed into the bag. Each time, only one letter cube is taken out from the bag. The cube is thrown and the upward-facing letter noted. This letter will be associated with a pre-specified location in the tray. The above is repeated for each letter cube picked from the bag until the bag is empty.

Getting Started

To simulate the above scenario, notice that we need two tasks of a random nature.
  1. The choice of a letter cube picked from the bag is a random task.
  2. With the letter cube chosen from task 1 above, the selection of one of the six letters of a cube is also random.
To make the game as realistic as possible, one of your TAs (who has nothing better to do) managed to obtain a pirated version of the 4 x 4 Boggle game for $1.99 at the nearby pasar malam (or night market). Upon opening up the box, he realized that there were no letter cubes provided. Instead there was a piece of paper with the cut-outs of the letter cubes printed on them. So they expected us to fold our own cubes??? What's more there were only eleven cutouts!!! No wonder so cheap!!!

Under these unfortunate circumstances, we will have to make do with what we have. A sample of the first four cut-outs is shown below. The cut-outs of all eleven cubes are provided in the file stored in cubes.txt.


   +---+
   | i |
   +---+---+---+---+
   | e | f | y | e |
   +---+---+---+---+
               | h |
               +---+

   +---+
   | e |
   +---+---+---+---+
   | p | t | s | l |
   +-- +---+---+---+
   | u |
   +---+

       +---+
       | h |
   +---+---+---+---+
   | e | n | i | p |
   +---+---+---+---+
           | s |
           +---+

       +---+
       | l |
   +---+---+---+---+
   | e | c | a | r |
   +---+---+---+---+
       | s |
       +---+

With the file provided, your initial task is to first "create" the letter cubes by letting each cube be denoted as a six-letter string in a node of a linked list. Use the appropriate structure definition for a node and define the type Cube such that a variable declared of this type will refer (or point) to a node of the linked list.
(Hint: It is so very similar to how the linked list for the dictionary in part 1 is defined, think of Cube as WordPtr)

For the example above, the nodes are created using the letters of the eleven cut-outs in order, i.e. the first node holds the string "iefyeh", second node holds the string "eptslu", third node holds "henips", and so on. Since we are given only eleven cubes, for a smaller tray, say 3 x 3, we only use the first nine cubes for the linked list. For a 4 x 4 tray, we would have exhausted all the cubes by the eleventh node. The twelfth and subsequent nodes would again be assigned with the cubes in order starting from the first cube in a cyclical manner.

In your InitTray function, declare a variable cubes of type Cube. The first task for the function would be to assign cubes with a linked list of "letter cubes" that is read from the file, created and returned by invoking the function CreateLetterCubes. The function prototype is given as

   Cube CreateLetterCubes();

Having created the linked list, your next task is to simply select a node at random and remove the selected node from the linked list. This would represent the removal of a letter cube from the bag. The string of the removed node will then be subjected to another random selection to choose the specific letter. This letter will be assigned to a cell in the tray according to the order you provided in part 1.

In your program, replace the assignment of a random alphabet with the function call PickCubeAndLetter(&cubes) and provide the linked list of letter cubes as argument. This latter function will randomly extract a node (representing a letter cube) from the linked list. The function will then return a random letter out of the six letters on that extracted cube. Note that each time the function PickCubeAndLetter is called, the linked list of letter cubes will become progressively shorter. The function prototype is given below. You are allowed to invoke other functions in PickCubeAndLetter if necessary.

   char PickCubeAndLetter(Cube *);