CS1101C Lab 4 (Odd Week)

City Building Simulation

The deadline for this lab question is Friday 30 March 2007, 23:59:59 hours.

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

Background

The purpose of this lab is to familiarize you with the notion of 2d arrays. There are two main objectives:

  1. Declaration of 2D arrays
  2. Simple processing on 2D arrays

City Building Simulation

In this lab, we are going to have a little fun by writing a program to simulate simple city building (not unlike some games out there). The bird’s eye view of a city is represented as a two dimensional character arrays of size 20 x 20. Each “square” is a character which represents the status of a plot of land in the city, for example:

'.'  = empty land
'='  = horizontal road
'#'  = vertical road
'@'  = house

To locate a square (a plot of land) in the city, we can make use of the row and column coordinates, hence city[row][column] should uniquely identify a plot of land. The coordinate of the top left corner of the city is [0][0], and the top right corner of the city is at [0][19].

The city starts off as totally empty, i.e. all squares are '.'. Then the user will issue the following building commands:

  1. Build Road Build a vertical/horizontal road in the city.
  2. Build House Build a house in the city.

Your program will then attempt to carry out the commands and change the city accordingly. The summary of the additional input from user as well as conditions for each of the commands are summarized below:

Command Additional User Input Conditions Effects on City
Build Road
  • row, col : specifies the starting square to build the road.
  • length : length of the road to be built
  • direction: 1 = Horizontal (to the right), 2 = Vertical (downwards)
  • The starting square must be within the city.
  • Road building will overwrite previous road.
  • Road building will stop when the edge of city is reached.
  • Road building will stop when there is a house in the target square.
  • Horizontal road will be represented as '=' in the city.
  • Vertical road will be represented as '#' in the city.
Build House
  • row, col : specifies the target square to build the house.
  • The target square must be within the city.
  • The target square must empty, i.e. no road, no other house.
  • The target square must be adjacent to a road (either above, left, right or below).
  • The house will be represented as '@' in the city.

Other than the above commands, you are required to output the final city into a file named “city.txt” when the user choose to exit.

So, the overall flow of your program is as follows:

  1. Print the current state of city.
  2. Ask user for command (build road, house or exit).
  3. Get further information according to the command.
  4. Carry out the command and report the outcome.
  5. Go back to step 1 if the command is not “exit”.
  6. Print the final state of city into output file “city.txt”.

Sample Runs

Assuming that the executable is build, one sample execution is shown below. Note that comments preceding with “//” are there to explain the output. User input is shown in bold.

$ ./build
....................        // the starting city, all squares empty.
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
1.Road 2.House 3.Exit ==> 1
Row ==> 0
Column ==> 0
Length ==> 25
1.Horizontal 2.Vertical ==> 1       // try to build a horizontal road at [0][0] of length 25.

RESULT: 20 squares of road built    // only 20 squares can be built.

====================                // new road.
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
1.Road 2.House 3.Exit ==> 1
Row ==> 0
Column ==> 10
Length ==> 10
1.Horizontal 2.Vertical ==> 2       // try to build a vertical road at [0][10] of length 10.

RESULT: 10 squares of road built    // all 10 squares can be built.

==========#=========                // earlier road "overwritten".
..........#.........
..........#.........
..........#.........
..........#.........
..........#.........
..........#.........
..........#.........
..........#.........
..........#.........                // the end of vertical road.
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
1.Road 2.House 3.Exit ==> 2
Row ==> 10
Column ==> 10                       // try to build a house at [10][10].

RESULT : House built successfully

==========#=========
..........#.........
..........#.........
..........#.........
..........#.........
..........#.........
..........#.........
..........#.........
..........#.........
..........#.........
..........@.........                // can be built: there is a road on top.
....................
....................
....................
....................
....................
....................
....................
....................
....................
1.Road 2.House 3.Exit ==> 1
Row ==> 10
Column ==> 5
Length ==> 15
1.Horizontal 2.Vertical ==> 1       // try to build a horizontal road at [10][5] of length 15.

RESULT: 5 squares of road built     // only 5 square of road can be built.

==========#=========
..........#.........
..........#.........
..........#.........
..........#.........
..........#.........
..........#.........
..........#.........
..........#.........
..........#.........
.....=====@.........                // stopped by a house.
....................
....................
....................
....................
....................
....................
....................
....................
....................
1.Road 2.House 3.Exit ==> 2
Row ==> 3
Column ==> 3                        // try to build house at [3][3].

RESULT: House cannot be built!      // failed: no adjacent road.

==========#=========
..........#.........
..........#.........
..........#.........
..........#.........
..........#.........
..........#.........
..........#.........
..........#.........
..........#.........
.....=====@.........
....................
....................
....................
....................
....................
....................
....................
....................
....................
1.Road 2.House 3.Exit ==> 2
Row ==> 10
Column ==> 10                       // try to build house at [10][10].

RESULT: House cannot be built!      // failed: already has a house at target square.

==========#=========
..........#.........
..........#.........
..........#.........
..........#.........
..........#.........
..........#.........
..........#.........
..........#.........
..........#.........
.....=====@.........
....................
....................
....................
....................
....................
....................
....................
....................
....................
1.Road 2.House 3.Exit ==> 2
Row ==> 0
Column ==> 1                        // try to build a house at [0][1].

RESULT: House cannot be built!      // failed: [0][1] is a road.

==========#=========
..........#.........
..........#.........
..........#.........
..........#.........
..........#.........
..........#.........
..........#.........
..........#.........
..........#.........
.....=====@.........
....................
....................
....................
....................
....................
....................
....................
....................
....................
1.Road 2.House 3.Exit ==> 2
Row ==> 1
Column ==> 0                        // try to build house at [1][0].

RESULT : House built successfully   // successful.

==========#=========
@.........#.........                // House ok : road above.
..........#.........
..........#.........
..........#.........
..........#.........
..........#.........
..........#.........
..........#.........
..........#.........
.....=====@.........
....................
....................
....................
....................
....................
....................
....................
....................
....................
1.Road 2.House 3.Exit ==> 3         // exit the program.

City written into output file.      // write into the file "city.txt".

$ less -e city.txt                  // the content of output file.
==========#=========
@.........#.........
..........#.........
..........#.........
..........#.........
..........#.........
..........#.........
..........#.........
..........#.........
..........#.........
.....=====@.........
....................
....................
....................
....................
....................
....................
....................
....................
....................

$                                   // you are done!! phew!!

Hints and Tips

Here's some suggestions on the possible functions you can write. You are free to come up with your own design. However, you must modularize your program.


This document, index.html, has been accessed 16 times since 25-Jun-24 11:57:13 +08. This is the 1st time it has been accessed today.

A total of 8 different hosts have accessed this document in the last 445 days; your host, nsrp-source.comp.nus.edu.sg, has accessed it 8 times.

If you're interested, complete statistics for this document are also available, including breakdowns by top-level domain, host name, and date.