CMSC 106 Project #4 Fall 2001


Due date: 11:00 pm on Wednesday, April 3

1. Purpose

In this project you will write a program which uses arrays to store and process data, and which reads input until the end of file or a specific value is seen.

Your project will be an extension of the Sokoban game of Project 3. Using arrays, you will be able to use more interesting game boards, and you will be able to handle more than one box and target location. As before, the player will move within a grid of points surrounded by walls. The program will read and process a series of moves for the player until it reaches the end of the input.

2. Project description

As you read this section, you may also want to refer to the ``Sample output'' section below.


2.1 Setting up the game board and starting conditions

Your program will read the game board setup and the starting conditions from an ASCII text file using standard input redirection. The first line of the input will consist of two integers. The first one will be the number of rows in the game board. The second one will be the number of columns in the game board. The number of rows must be a positive integer less than or equal to 50, and the number of columns must be a positive integer less than or equal to 100. If the number of rows or the number of columns is invalid, the program must print a message beginning with the exact word "ERROR:". The message must contain the invalid row or column value, preceded by the word "rows" for rows or "columns" for columns. If both values are invalid, they must both be printed in a single message; otherwise, only the one incorrect value is to be printed. If either the row or column values are invalid, then the program must skip the remainder of the input without attempting to process any moves.

Following this line will be a series of lines representing the squares of the game board. Each row will contain a series of characters:

Note that the game board will be inside a rectangle of the given size, but there may be some points of the rectangle which are actually outside the playing area. All points outside the playing area will be empty. There will be no blanks between items in a row, but there may be 1 or more blanks at the end of a row. If the number of rows and columns in the first input line is valid, then the number of rows to be read will equal the first number read in the first input line, and the number of items in each row will equal the second number read in the first input line. The program may assume that there will always be exactly 1 player. There will be no limit to the number of boxes or goals, other than the number of available locations on the game board.

The next line will contain a list of moves for the player. Each move will be represented by a single character:

The program will continue reading and processing moves until it reaches the end of the input file.

2.2 Playing the game

After the program has read in and checked the game board setup, it will start processing the moves for the player. The basic rules for moving the player are the same as in Project 3:

Note that since there may be multiple boxes, a box may also be blocked by another box. This means that only one box may be pushed at a time.

2.3 Output

As each move is read, the program must check whether it is valid, as in Project 3. If the player is blocked from moving by a wall, or a box is blocked by a wall, the program must print out the appropriate message, as in Project 3. However, if a box is blocked from moving by another box, the program must print a new message:

Box blocked by box moving M at r c!

where M is L for left, R for right, U for up, or D for down,r is the row and c is the column for the player's current position. (Rows and columns are numbered starting with 1, as in Project 3.)

The program must also keep a count of the number of successful moves and the number of successful pushes of a box. A move is counted as successful if the player moves into an empty square (including a goal). A move is counted as a successful push if the player moves into the square previously occupied by a box, moving the box to the next square. Note that any particular move is considered either a successful move or a successful push, but not both.

After the end of the input has been reached and the last move has been processed, the program must print the message:

********** Final Board **********

preceded by a blank line. Following the message, the program must print out the final game board configuration. The game board is printed using the same characters used for the input, including the asterisk ('*') for a box located on a goal. (If the player is located on a goal, that location is simply shown as 'O'). Following the game board configuration, the program must print the message:

Moves: m, Pushes: p, Points n

where m is the number of successful moves, p is the number of successful pushes, and n is the number of points. One point is scored for each box which is located on a goal at the end of the game. If a box is located on every goal, the program must also print the message:

Congratulations, you have solved this level!

on the next line. (Note that there may be more boxes than goals, or more goals than boxes.)

3. Project requirements

All your C programs in this course should be written in ANSI C, which means they must compile and run correctly with cc -std1 -trapuv on the OIT UNIX Class Cluster. You will lose credit if your program generates any warning messages when it is compiled. Even if you already know what they are, you may not use any C language features other than those introduced in Chapters 1 through 8 of your textbook, plus those presented in lecture while these chapters were covered. Note that as a result user-defined functions may not be used. In addition, although they are covered in the allowable chapters, neither the goto nor the continue statement may be used, and the break statement may not be used in any loop. Lastly, your program must contain only one single return statement at its end, and may not use the exit() library function at all. Using C features not in these chapters, or using the goto statement, the exit() library function, multiple returns, or break or continue in loops, will result in losing credit. Points will also be deducted for excessive amounts of duplicate code.

Your program must have a comment near the top which contains your name, login ID, student ID, your section number, your TA's name, and an original description of the action and operation of the program. Do not put your alias in this comment! Your program should be written using good programming style and formatting, as discussed in class and throughout your textbook. For this project, style is considered to consist of:



4. Developing your program

You may want to skip this section at first, read the rest of the project, and come back to study it carefully when you are about to begin writing your program.

Even if you didn't need to refer much to the methods presented in the corresponding sections in the earlier project assignments, you will need to begin reading these carefully now because as the assignments get larger and more complex these techniques for debugging and program development will become indispensable. Remember, one of the important topics you must learn in this course is how to track down and fix both syntax and semantic errors in your code. You will need to follow the procedures described, as well as be able to describe an error and what you have already done to find it, and bring a printout of your source code if you need to come for assistance with it during office hours.

4.1 Possible development steps

These steps are a suggestion only; you can follow others if you prefer, since there are many ways to develop any program. However, whatever steps you do choose, you should be certain to write small parts of your program at a time and test each one to verify that it works before going on!


  1. You could begin by declaring a two-dimensional array of the size necessary for the game board. The first line of the input file will tell you how much of that array will be used. You may also want to initialize the array.

  2. Like the previous project, it is usually easiest to put all error checking off until the end of the development. Make sure your input files do not contain any errors until you are ready to test that. Do all of the reading of the input file to set up the game board.

  3. Add the code to read player moves. For now, don't process anything you read - just make sure you can read the input and stop at the correct point.

  4. Make sure you print the game board array to make sure everything is correct before playing the game.

  5. Add the code inside of the loop to process the player moves. Develop and test each possibility individually before going on.

  6. Add the code to check for errors in the input values.

  7. Make sure you check the code to be sure that all messages to the user fit the description.

  8. Lastly, thoroughly check your entire program after finishing it before submitting it. Create several input files testing them all by hand. (You may want to use graph paper to represent the game board.)


4.2 Finding compilation errors


Invalid #define

If every line (or most lines) which use a symbolic constant is flagged as an invalid statement, the error is most likely in the definition of the constant itself.


4.3 Program debugging

The most common error in C programs with arrays is using invalid subscripts to refer to array elements (falling off one edge or other of an array).


Segmentation fault (core dumped)

Getting this error when a program runs usually means it has referred to an invalid area of memory, which is often caused by incorrect array subscripts. To find the problem, add debug printfs to narrow down where in your program the problem occurs. Before every reference to an array element appearing in that section of code you can add debug printfs which display the values which will be used as subscripts. For instance, if your code contains a statement like


             printf("%d", array[i][j]);
            


then add test statements above it to print the values of i and j. Verify by running the program that i is not larger than the maximum row subscript for the array, and j is not larger than the maximum column subscript. Falling off an edge of the array won't necessarily lead to a core dump, it may just cause incorrect results.

\n for debug printfs

For technical reasons, all debug printfs used must end in \n. Otherwise, if your program has a core dump, some (or all) of the debug print results may not be displayed before the program halts, defeating the purpose of adding the debug printfs.

core file

A core dump creates a file named "core" which contains the contents of your memory space at the time of the program crash. This file can get large and is not useful at all for our purposes, so it should be removed before going on to prevent exceeding your quota of disk space.


5. Academic integrity statement

Any evidence of unauthorized use of computer accounts or cooperation on projects will be submitted to the Student Honor Council, which could result in an XF for the course, suspension, or expulsion from the University. Projects are to be written INDIVIDUALLY. For academic honesty purposes, projects are to be considered comparable to a take-home exam. Any cooperation or exchange of ideas which would be prohibited on an exam is also prohibited on a project assignment, and WILL BE REPORTED to the Honor Council.


VIOLATIONS OF ACADEMIC HONESTY INCLUDE:


  1. failing to do all or any of the work on a project by yourself, other than assistance from the instructional staff.

  2. using any ideas or any part of another student's project, or copying any other individual's work in any way.

  3. giving any parts or ideas from your project, including test data, to another student.

  4. having programs on an open account or on a PC that other students can access.

  5. transferring any part of a project to or from another student or individual by any means, electronic or otherwise.


IT IS THE RESPONSIBILITY, UNDER THE UNIVERSITY HONOR POLICY, OF ANY STUDENT WHO LEARNS OF AN INCIDENT OF ACADEMIC DISHONESTY TO REPORT IT TO THEIR INSTRUCTOR.

6. Submitting your project

Your project must be electronically submitted by the date above to avoid losing credit. No projects more than two days late will be accepted for credit without prior permission or a valid medical excuse, as described on your syllabus. Only the project which you electronically submit, according to the procedures provided, can be graded; it is your responsibility to test your program and verify that it works properly before submitting. Lost passwords or other system problems do not constitute valid justifications for late projects, so do not put off working on your program or wait to submit it at the last minute!

Turn in your assignment using the ``submit'' program as before, except using ``4'' for the project number. You are to submit only the .c file containing your source code, not the executable version of your program! If your program is in a file named ``game.c'', submit would be run as submit 4 game.c.

Before you submit your project, you must exactly follow the specific submission checklist in the ``Testing projects before submitting'' handout separately posted by your instructor!

7. Sample output

Assuming the name of the executable version of the program is ``game.x'', here is a sample execution for one input data set. The content of the input file is shown as displayed by the UNIX ``cat'' command. Following the data file contents, the results of running the program is shown. The data files shown here will be considered the ``primary input'' (and ``it's corresponding output'') for grading purposes.

Be sure to test your program against a variety of inputs, so you are sure it works in all circumstances!


% cat p4.in1

11 19
----WWWWW----------
----W---W----------
----WB--W----------
--WWW--BWW---------
--W--B-B-W---------
WWW-W-WW-W---WWWWWW   
W---W-WW-WWWWW--XXW
W-B--B----------XXW
WWWWW-WWW-WOWW--XXW
----W-----WWWWWWWWW
----WWWWWWW--------
LULLDDLLLLUULLULLDRRRRRRRRRRRRRRRRLL


% game.x < p4.in1

Wall moving L at 9 12!
Box blocked moving R at 8 17!

********** Final Board **********
----WWWWW----------
----W---W----------
----WB--W----------
--WWW--BWW---------
--W--B-B-W---------
WWW-W-WW-W---WWWWWW
W---WBWW-WWWWW--XXW
W-------------O-X*W
WWWWW-WWW-W-WW--XXW
----W-----WWWWWWWWW
----WWWWWWW--------
Moves: 18, Pushes: 16, Points 1



Steve Scolnik
2002-03-14