CS1101C Lab 4 (Odd Week)

Cross Stitching Simulation

The deadline for this lab question is Wednesday 26 October 2005, 23:59:59 hours.

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

Background

Your friend Dory has just discovered (to your horror) the joy of Cross Stitching. As you know, waterproof stitch canvas is not easy to come by. So you have to make sure Dory's stitch design looks ok before Dory lay her itchy fing....erm fins on the canvas.

As a somewhat competent programmer, you've decided to write a program to simulate the whole stitching process.

Cross Stitching

The canvas consists of 20 rows and 20 columns, i.e. 400 stitch points in total. There are nine different colour threads, represented by the numbers 1,2,3,...,9. The number zero can be used to represent an empty stitch point. The canvas starts off as empty (completely unstitched).

A cross stitch pattern consist of five stitches arranged as

    1
   234
    5
Note the the colours 1,2,3,4 and 5 are chosen as example only, the thread can be of any colours. The above is the default pattern.

There are four stitch patterns:

Horizontal (from left to right):

    1  1  1  1  1
   234234234234234
    5  5  5  5  5
The example above shows five cross stitch patterns arranged horizontally.

Vertical (from top to bottom):

    1
   234
    5
    1
   234
    5
    1
   234
    5
The example shows three cross stitch patterns arranged vertically.

Diagonal (from upper left to lower right):

    1
   234
    5 1
     234
      5 1
       234
        5 1
         234
          5
The example shows four cross stitch patterns arranged diagonally.

Flower Pattern:

        1
       234
        5
     1  1  1
    234234234
     5  5  5
        1
       234
        5
The example shows five patterns stitched in a prearranged way (flower pattern).

Stitch Simulation Details

Suppose the rows of the canvas are numbered from top to bottom as 0 to 19, and the columns from left to right as 0 to 19, we can easily identify any stitch point. We can then specify a stitch process by:
  1. One of the four ways to stitch.
  2. The starting stitch point
  3. Number of patterns repeated
A single stitch process can then be specified in one line, with the format:
command_number row column number_of_pattern(optional)

Examples

1 3 5 2
Horizontal stitch starting at row 3, column 5, and repeat for 2 patterns.
3 10 12 3
Diagonal stitch starting at row 10, column 12, and repeat for 3 patterns.
4 15 16
Flower stitch at row 15, column 16.

Changing Colour Threads

Additionally, we can change the colour threads used by using the command number "5", followed by five colour numbers.

Example

5 6 9 3 1 8
The above will change the colour threads in a pattern to
   6
  931
   8
Lastly, the command number "6" will be used to terminate the whole simulation.

C Program Requirements

Your program should read from the file stitchCommand.txt, which contains a number of command lines. Each command line will specify one of the operations as described above. The program outputs the canvas into the file canvas.txt and terminates when the command number "6" is read.

You can assume the inputs are always correct and contains only valid integers. However, it is possible that only a partial pattern can be stitched successfully sometimes (e.g. stitching on the edge of the canvas). The guideline is "Perform ALL possible stitches in a command (partial patterns must still be stitched), only ignore those stitches that are out of the canvas".

Note that any new stitching overwrites any existing stitching that is already on the canvas.

Sample Run

Suppose the file stitchCommand.txt contains the following lines:
1 3 4 2
2 3 19 3
3 15 3 4
5 9 7 1 8 3
4 10 10
1 10 1 1
6
As an illustration, the canvas after each command line is shown below. Note that in your program, you only need to produce the final canvas in the output file. There is no need to show any intermediate canvas. Use "." to indicate an empty stitch point.
1 3 4 2         //Horizontal: Row 3, Column 4, 2 patterns

Canvas:
....................
....................
....1..1............
...234234...........
....5..5............
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................

///////////////////////////////////////////////////////////

2 3 19 3        //Vertical: Row 3, Column 19, 3 patterns

....................
....................
....1..1...........1
...234234.........23
....5..5...........5
...................1
..................23
...................5
...................1
..................23
...................5
....................
....................
....................
....................
....................
....................
....................
....................
....................    //Note the partial pattern

///////////////////////////////////////////////////////////

3 15 3 4                //Diagonal: Row 15, Column 3. 4 Patterns.

....................
....................
....1..1...........1
...234234.........23
....5..5...........5
...................1
..................23
...................5
...................1
..................23
...................5
....................
....................
....................
...1................
..234...............
...5.1..............
....234.............
.....5.1............
......234...........    //Note the partial pattern

///////////////////////////////////////////////////////////

5 9 7 1 8 3             //Change colour threads to
                            9
                           718
                            3
....................
....................
....1..1...........1
...234234.........23
....5..5...........5
...................1
..................23
...................5
...................1
..................23
...................5
....................
....................
....................
...1................
..234...............
...5.1..............
....234.............
.....5.1............
......234...........    //No change to the canvas

///////////////////////////////////////////////////////////

4 10 10                 //Flower: Row 10, Column 10

....................
....................
....1..1...........1
...234234.........23
....5..5...........5
...................1
..........9.......23
.........718.......5
..........3........1
.......9..9..9....23
......718718718....5
.......3..3..3......
..........9.........
.........718........
...1......3.........
..234...............
...5.1..............
....234.............
.....5.1............
......234...........    //Note the flower pattern and colour threads

///////////////////////////////////////////////////////////

1 10 1 1                //Horizontal: Row 10, Column 1. 1 Pattern.

....................
....................
....1..1...........1
...234234.........23
....5..5...........5
...................1
..........9.......23
.........718.......5
..........3........1
.9.....9..9..9....23
718...718718718....5
.3.....3..3..3......
..........9.........
.........718........
...1......3.........
..234...............
...5.1..............
....234.............
.....5.1............
......234...........    //Note the colour threads

///////////////////////////////////////////////////////////

6                       //Exit

....................
....................
....1..1...........1
...234234.........23
....5..5...........5
...................1
..........9.......23
.........718.......5
..........3........1
.9.....9..9..9....23
718...718718718....5
.3.....3..3..3......
..........9.........
.........718........
...1......3.........
..234...............
...5.1..............
....234.............
.....5.1............
......234...........    //This canvas is stored in "canvas.txt"

///////////////////////////////////////////////////////////

Tips


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

A total of 12 different hosts have accessed this document in the last 445 days; your host, 216.73.216.39, has accessed it 1 times.

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