Before you begin...

Make a copy of your boggle.c file as boggle2.c by invoking the command
piyoPiyo@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 - Wrap-Around Boggle

In part 1, adjacency of letters is defined on a flat plane. For part 2, we are going to expand on the notion of adjacency. Given the following 4 x 4 tray

   +---+---+---+---+
   | S | Q | E | T |
   +---+---+---+---+
   | N | E | N | I |
   +---+---+---+---+
   | N | O | A | L |
   +---+---+---+---+
   | I | C | L | Y |
   +---+---+---+---+
Take for example the top-left letter S, other than N, E and Q being adjacent, by wrapping around the vertical edges, T and I are also adjacent to S. Wrapping around the horizontal edges will make I and C adjacent to S. Finally, wrapping along the top-right to bottom-left diagonal will make Y adjacent to S. With wrap-around adjacency, the eight corresponding neighbours of S is shown below.
   +---+---+---+
   | Y | I | C |
   +---+---+---+
   | T | S | Q |
   +---+---+---+
   | I | N | E |
   +---+---+---+
With wrap-around adjacency defined, a word such as "question" can now be located on the tray.
   +---+---+---+---+
   | S | Q | E | T |
   +---+---+---+---+
   | N | E | N | I |
   +---+---+---+---+
   | N | O | A | L |
   +---+---+---+---+
   | I | C | L | Y |
   +---+---+---+---+

Getting Started

Your first task is to modify your program to cater to wrap-around adjacency. You need only modify the function WordInTray or any other functions invoked by it. If necessary, you can define other functions.

That's not all...

Having completed the above task, you will notice that it is now rather difficult to figure out the path of the letters especially when it jumps off the tray and lends on the opposite side. One good way is to list down the positions as a set of triplets <A,B,C> where A denotes the letter, B denotes the row and C denotes the column. Let A = 1 and B = 1 represent the top-left corner. The sequence of triplets forming the word "question" is therefore
<QU,1,2> <E,2,2> <S,1,1> <T,1,4> <I,4,1> <O,3,2> <N,2,1>
Firstly, we will need to obtain the sequence of positions of individual letters forming the word. In the case whereby there are multiple sequences for a particular word, you need only consider one of them. The list of positions is to be implemented as a linked list with the following node structure.
   typedef struct PosStruct Pos;
   struct PosStruct {
      int x;
      int y;
      Pos *next;
   };
All you need to do now is to modify the WordInTray function (and any other function(s) it invokes) such that if a word is found, the linked list of positions will be returned by the function; otherwise nothing (i.e. NULL) will be returned. As such the function prototype becomes
   Pos *WordInTray(Cell [][], char *);

With the linked list constructed you can proceed to invoke another function DisplayPos that prints out the sequence of positions according to the above print out. The corresponding function prototype is as follows:

   void DisplayPos(Cell [][], Pos *);
The corresponding PlayBoggle function has to be modified accordingly. This portion of the code has been provided for you as shown below. The modifications are in bold.
void PlayBoggle(Cell tray[][SIZE], WordPtr dict)
{   
   char word[101];
   WordPtr wordList = NULL;
   Pos *pos;
      
   /* Initialize the tray with random letters */
   InitTray(tray);
   /* Display the tray with letters */
   PrintTray(tray);
   
   /* Read in a word */
   printf("\nEnter word (any number to quit): ");
   gets(word);
               
   /*
      Check if a word has its first char as a digit.
      If yes, then terminate the loop
   */
   while (!isdigit(word[0]))
   {
      /* Reset the visited flag of all the cells on the tray */
      ResetFlag(tray);
                  
      /* Condition 1: A word should be 3 letters or more */
      if (strlen(word) > 2)
                  
         /* Condition 2: Check if the word is on the tray */
         if ( (pos = WordInTray(tray,word)) != NULL )
                   
           /* Condition 3: Check if the word is in the dictionary */
            if (WordInDict(dict, word))
            
               /* Condition 4: Check if the word has been previously found */
               if (!WordInList(wordList,word))
               {
                  /* Add the valid word into the list of found words */
                  AddWord(&wordList,word);
                  
                  /* Print the list of words already found */
                  printf("\n--- Word(s) Found ---\n");
                  PrintList(wordList);
                  printf("---------------------\n\n");
   
                  /* Display the adjacency of letters */
                  DisplayPos(tray,pos);
                  
                  /* Display the tray with letters again */
                  PrintTray(tray);
               }
             else
                  printf("\"%s\" has already been found\n", word);
            else
               printf("\"%s\" is not found in the dictionary\n", word);
         else   
            printf("\"%s\" is not found on the tray\n", word);
      else
         printf("Word must be at least 3 letters\n");
                  
      /* Read in another word */
      printf("\nEnter another word (any number to quit): ");
      gets(word);
   }
   printf("\nProgram Terminated...\n");   
}                 

Sample Output

piyoPiyo@sf3:~[123]$ a.out
+---+---+---+---+
| S | Q | E | T |
+---+---+---+---+
| N | E | N | I |
+---+---+---+---+
| N | O | A | L |
+---+---+---+---+
| I | C | L | Y |
+---+---+---+---+

Enter word (any number to quit): inconsequential

--- Word(s) Found ---
1	inconsequential
---------------------

<I,4,1> <N,3,1> <C,4,2> <O,3,2> <N,2,1> <S,1,1> <E,2,2> <QU,1,2> <E,1,3> <N,2,3> <T,1,4> <I,2,4> <A,3,3> <L,3,4>

+---+---+---+---+
| S | Q | E | T |
+---+---+---+---+
| N | E | N | I |
+---+---+---+---+
| N | O | A | L |
+---+---+---+---+
| I | C | L | Y |
+---+---+---+---+

Enter another word (any number to quit): question

--- Word(s) Found ---
1	inconsequential
2	question
---------------------

<QU,1,2> <E,2,2> <S,1,1> <T,1,4> <I,4,1> <O,3,2> <N,2,1> 

+---+---+---+---+
| S | Q | E | T |
+---+---+---+---+
| N | E | N | I |
+---+---+---+---+
| N | O | A | L |
+---+---+---+---+
| I | C | L | Y |
+---+---+---+---+

Enter another word (any number to quit): 123

Program Terminated...
piyoPiyo@sf3:~[124]$