CS1101C Practical Exam

Session 1 (0800 - 0945 hours)

Scrabble

The name of your C program file must be called scrabble.c, files with any other name will not be marked.

The deadline for this lab is Wednesday 14 November 2007, 09:45:59 hours. Strictly no submissions will be accepted after the deadline.

Background

Scrabble is a popular board game in which players places letters on a board to form words.

Board

The board is a 15 x 15 board. Each tile consists of a single letter and is placed on a single square in the board. We wish to simulate a simple version of Scrabble to determine if it is possible to place words on the board by following simple rules.

Empty Board

The board beings without any tiles, and is shown below. Each empty square is indicated by a full-stop (or period). The top left-hand square of the board has coordinates (0,0), and the top right-hand square of the board has coordinates (0,14).

. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 

First Word

The placement of the first word can be anywhere on the board, either horizontally or vertically. For simplicity, assume that the user will never place any tiles outside of the board. Here, we place the word HELLO starting from coordinates (3,5) and going in a horizontal direction.

. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . H E L L O . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 

Second and Subsequent Words

The placement of second and subsequent words must follow some simple rules:
  1. At least one letter in the new word must match an existing tile on the board.
  2. At least one new tile must be placed on the board.
  3. If any letter in the new word is not the same as an existing tile on the board, the word is immediately rejected.

Look at the example above where the word HELLO has already been placed. Suppose we now attempt to place the word SNOW at coordinates (2,7), vertically. This is unsuccessful because the letter N in SNOW clashes with the letter L at coordinates (2,8). It violates Rule 3 above.

Now suppose we attempt to place the word BYE at coordinates (2,4) vertically. This is unsuccessful because it violates Rule 1 above.

Again, suppose we attempt to place the word HE at coordinates (3,5) horizontally. This is unsuccessful because it violates Rule 2 above; no new tile has been placed on the board.

Finally, suppose we attempt to place place the word SLOW at coordinates (2,7) vertically. This is successful, so we update the Scrabble board. We print out the Scrabble board only when the placement of a word has been successful.

. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . S . . . . . . . 
. . . . . H E L L O . . . . . 
. . . . . . . O . . . . . . . 
. . . . . . . W . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 

Text File

The list of words to be placed are stored in a sample text file called “scrabble.txt” which has been copied into your directory by the pesetup command. Each line in the text file consists of three integers followed by a word of between 1 to 15 characters in length. You may assume that there is at least one line in the text file, and all integers in the text file are valid. The number of lines in the text file are unknown.

The contents of the sample text file is shown below:

3 5 1 heLlo
2 7 2 snOw
0 14 2 Bye
2 7 2 slOw
3 7 2 LO
3 7 2 lolly
3 7 2 loWly

Let us look at the first line. The coordinates of the starting letter are (3,5). The next integer 1 indicates that we wish to place the word HELLO horizontally.

Now look at the second line. The coordinates of the starting letter are (2,7). The next integer 2 indicates that we wish to place the word SNOW vertically.

Notice that you must convert all letters in the word to upper case letters.

Sample Output

The following is the sample output using the above sample file. Remember that we will test your program with other input files. Do not print the lines “Begin Sample Output” and “End Sample Output”.
===== Begin Sample Output =====

Attempting to place the word HELLO at coordinates (3, 5) horizontally.
Successful!
Scrabble Board:
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . H E L L O . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 

Attempting to place the word SNOW at coordinates (2, 7) vertically.
Unsuccessful.

Attempting to place the word BYE at coordinates (0, 14) vertically.
Unsuccessful.

Attempting to place the word SLOW at coordinates (2, 7) vertically.
Successful!
Scrabble Board:
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . S . . . . . . . 
. . . . . H E L L O . . . . . 
. . . . . . . O . . . . . . . 
. . . . . . . W . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 

Attempting to place the word LO at coordinates (3, 7) vertically.
Unsuccessful.

Attempting to place the word LOLLY at coordinates (3, 7) vertically.
Unsuccessful.

Attempting to place the word LOWLY at coordinates (3, 7) vertically.
Successful!
Scrabble Board:
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . S . . . . . . . 
. . . . . H E L L O . . . . . 
. . . . . . . O . . . . . . . 
. . . . . . . W . . . . . . . 
. . . . . . . L . . . . . . . 
. . . . . . . Y . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
===== End Sample Output =====

You are reminded to follow the sample output exactly; else marks will be deducted.

We will test your programs with other (more complicated) text input files.

You may assume that the user will never place any tiles outside of the board.

Do not use any structures or any form of dynamic memory allocation (using malloc or calloc) in your program, else no credit will be given.

Remember to submit your program frequently using the submit scrabble.c command, and check your submission using the check command.

All the best!

UNIX commands

Some useful UNIX commands (in case you forgot what you did in Lab 0):

  1. dir”: lists all the files in the directory.
  2. cp a.txt b.txt”: copies a.txt to b.txt.
  3. mv a.txt b.txt”: moves / renames a.txt to b.txt.
  4. cat a.txt”: shows the contents of a.txt.