Lab 2 Part 2: Flood!

The Mayor of your town is interested to know how flood-prone the town is. He has employed you to write a simple simulation of a flood hitting the town. The name of your file must be flood.c.

The Town

The size of the town is fixed as 10 rows by 10 columns. The layout of the town is given as follows:

.++.+.++++
.+.......+
.+.+++++.+
.+.+...+.+
.+.+.+.+.+
.+.+++.+.+
.+.....+.+
.+++++++.+
........++
...++++++.

The symbols represent:

. - Road, empty space (floodable area)
+ - Buildings (non-floodable area)

You should create a two-dimensional array of char to store the town. You may use the following array initializer if you wish (copy and paste):

    char town[10][10] = {
        {'.', '+', '+', '.', '+', '.', '+', '+', '+', '+'},
        {'.', '+', '.', '.', '.', '.', '.', '.', '.', '+'},
        {'.', '+', '.', '+', '+', '+', '+', '+', '.', '+'},
        {'.', '+', '.', '+', '.', '.', '.', '+', '.', '+'},
        {'.', '+', '.', '+', '.', '+', '.', '+', '.', '+'},
        {'.', '+', '.', '+', '+', '+', '.', '+', '.', '+'},
        {'.', '+', '.', '.', '.', '.', '.', '+', '.', '+'},
        {'.', '+', '+', '+', '+', '+', '+', '+', '.', '+'},
        {'.', '.', '.', '.', '.', '.', '.', '.', '+', '+'},
        {'.', '.', '.', '+', '+', '+', '+', '+', '+', '.'}
    };

Your algorithm must be a general solution and work for all possible configurations of a 10 x 10 town, not just the configuration given above.

Let's Do It!

In your simple simulation, we want to see what happens when a flood starts from a particular place in the town. Your program must do the following:

  1. Keep looping, printing the current state of the town and prompting for the user to enter the co-ordinates of a starting flood.
  2. You can assume that the user will always enter two integers separate by one space.
  3. The co-ordinates is in the format "rowNumber columnNumber", where "1 1" represents the North-West corner or top left hand corner, "1 10" represents the North-East corner or top right hand corner, "10 1" represents the South-West corner or bottom left hand corner, "10 10" represents the South-East corner or bottom right hand corner, and so on.
  4. Note that the numbering of the rows and columns start from 1 (and not 0).
  5. The program quits if any of the integers are 0 or less.
  6. If the user enters any integer 11 or greater, output an error message (as shown in the sample output).
  7. There is no effect if the co-ordinates specify a building or a place that is already flooded.
  8. The flood always spreads in four directions only: North, South, East, and West. It never spreads diagonally, i.e. North-West, North-East, South-West, or South-East.
  9. Once a flood starts, it will fill up as much of the town as it can.
  10. The areas of the town that are flooded are to be marked with the letter 'W' (upper case W indicating Water).
  11. Print out the number of new squares flooded.
  12. The flood never spreads beyond the 10 x 10 square.
  13. Your program output must follow the sample run exactly.

See the following sample run (user's input is in bold):


.++.+.++++
.+.......+
.+.+++++.+
.+.+...+.+
.+.+.+.+.+
.+.+++.+.+
.+.....+.+
.+++++++.+
........++
...++++++.

Enter co-ordinates to flood (e.g.: 1 1): 11 10

Invalid input. Please try again.

.++.+.++++
.+.......+
.+.+++++.+
.+.+...+.+
.+.+.+.+.+
.+.+++.+.+
.+.....+.+
.+++++++.+
........++
...++++++.

Enter co-ordinates to flood (e.g.: 1 1): 1 2

0 new squares flooded.

.++.+.++++
.+.......+
.+.+++++.+
.+.+...+.+
.+.+.+.+.+
.+.+++.+.+
.+.....+.+
.+++++++.+
........++
...++++++.

Enter co-ordinates to flood (e.g.: 1 1): 2 1

19 new squares flooded.

W++.+.++++
W+.......+
W+.+++++.+
W+.+...+.+
W+.+.+.+.+
W+.+++.+.+
W+.....+.+
W+++++++.+
WWWWWWWW++
WWW++++++.

Enter co-ordinates to flood (e.g.: 1 1): 4 1

0 new squares flooded.

W++.+.++++
W+.......+
W+.+++++.+
W+.+...+.+
W+.+.+.+.+
W+.+++.+.+
W+.....+.+
W+++++++.+
WWWWWWWW++
WWW++++++.

Enter co-ordinates to flood (e.g.: 1 1): 8 9

30 new squares flooded.

W++W+W++++
W+WWWWWWW+
W+W+++++W+
W+W+WWW+W+
W+W+W+W+W+
W+W+++W+W+
W+WWWWW+W+
W+++++++W+
WWWWWWWW++
WWW++++++.

Enter co-ordinates to flood (e.g.: 1 1): 10 10

1 new square flooded.

W++W+W++++
W+WWWWWWW+
W+W+++++W+
W+W+WWW+W+
W+W+W+W+W+
W+W+++W+W+
W+WWWWW+W+
W+++++++W+
WWWWWWWW++
WWW++++++W

Enter co-ordinates to flood (e.g.: 1 1): 0 1

Tips and Hints

  1. You are reminded to plan out your functions and algorithm carefully.
  2. Ensure that you follow the sample output exactly, e.g.: when to print out "square" and when to print out "squares".

All The Best!