The Objectives
The purpose of this assignment is to give you practice with:
- recursion
- using command line arguments
- using matrices
- and file handling.
As always, you should continue to practice using top-down design and good implementation techniques.
The Background
Autofill in paint uses a recursive algorithm to "spread" the paint to pixels of the same color. Looking at the example below, the "fill" tool is in the middle of the A, when clicking the tool ONCE, it fills in the rest of the area that is in white.
Notice that the fill did NOT extend into the black (or non-white) colored pixels or beyond the borders. Any non-white colored pixel within borders is a natural barrier that does not allow the fill to spread. Notice if there is a "crack" in the design like in the example below, it will spread beyond, but still does not fill in the natural barrier.
If there is any white space connected to the original location to fill, it will be changed. Autofill looks at locations UP, DOWN, LEFT and RIGHT from the original spot, and continues the spreading action in the next pixel.
The Picture File
The "Picture" file is no more than a text file with .'s and x's. The .'s are the white spaces, while the x's are the natural borders. (x's cannot be changed). Here is a sample below:
.....xx......
....x..xx......
....x....xxxx..
...x...... xxx
..x......
...x...... xx.
...x..xxx...x.x
...x..x..x.x...
...xxxx...x....
......
Here is a sample of the picture file. matrix1.txt
As a programmer, you will:
- NOT know the size of the text file (rows and columns)
- be required to use a try/catch in order to check to see if the file is present
The Task
Your assignment will be to autofill a given "picture" when the user gives the file name of the "picture file", and an x/y coordinate to start the fill. The program will display the picture BEFORE the autofill and display and write the results after the autofill.
Program Requirements
Your program must:
- use command line arguments. At the command line the user must enter (in this order):
- the name of the executable file (e.g. proj1.py),
- the name of the picture file
- x coordinate to start the fill.
- y coordinate to start the fill.
- python proj2.py matrix1.txt 0 0
- Using a try/catch, determine if the file is present. If not, exit the program. (You will need to reserach this!!)
- Display the content of the picture file to the screen in a box (matrix) form
- use a RECURSIVE autofill function to fill all items that are not filled. Iterative versions will receive no credit.
- write the results to the SCREEN and FILE.
- the results file will be created using the file name entered at command line:
- (at command line): matrix1.txt
- (result file name): matrix1-results.txt
- close any files that you have opened as soon as you have finished using them.
Sample Run
Here is a sample of output of this program. Please note that the sample output only shows what is expected from your program if the user is actually entering everything as instructed. This is not a test of the program at all, but just a sample for your clarification.
Matrix BEFORE Autofill
.....xx......
....x..xx......
....x....xxxx..
...x...... xxx
..x......
...x...... xx.
...x..xxx...x.x
...x..x..x.x...
...xxxx...x....
......
Matrix AFTER autofill, starting a location 0,0
xxxxxxx......
xxxxx..xx......
xxxxx....xxxx..
xxxx...... xxx
xxx......
xxxx...... xx.
xxxx..xxx...xxx
xxxx..xxxx.xxxx
xxxxxxxxxxxxxxx
xxxxxxxxxxxxxxx
Extra Credit
Allow other SINGLE characters as the fill. (OTHER than 'x'.) You must specify the character in the last spot in your command line. Also, in this extra credit, whatever coordinate is given, it will use that character as the character to be filled.
Example 1
python proj2.py matrix3.txt 6 12 Y
Matrix BEFORE autofill
......
...... *......
...... ***......
...... *.**......
...... *..**......
...... *....**......
...... *.....**......
...... **********......
...... *...... **......
.....*...... **.....
....**...... ***....
..*****...... *****...
......
Matrix AFTER autofill, starting at location 6, 12
......
...... *......
...... ***......
...... *Y**......
...... *YY**......
...... *YYYY**......
...... *YYYYY**......
...... **********......
...... *...... **......
.....*...... **.....
....**...... ***....
..*****...... *****...
......
Example 2
python proj2.py matrix4.txt 6 12 Y
Matrix BEFORE autofill
______
______*______
______***______
______*_**______
______*__**______
______*____**______
______*_____**______
______**********______
______*______**______
_____*______**_____
____**______***____
__*****______*****___
______
Matrix AFTER autofill, starting at location 6, 12
______
______*______
______***______
______*Y**______
______*YY**______
______*YYYY**______
______*YYYYY**______
______**********______
______*______**______
_____*______**_____
____**______***____
__*****______*****___
______
Example 3
python proj2.py matrix3.txt 2 13 A
Matrix BEFORE autofill
......
...... *......
...... ***......
...... *.**......
...... *..**......
...... *....**......
...... *.....**......
...... **********......
...... *...... **......
.....*...... **.....
....**...... ***....
..*****...... *****...
......
Matrix AFTER autofill, starting at location 2, 13
......
...... A......
...... AAA......
...... *.AA......
...... *..AA......
...... *....AA......
...... A.....AA......
...... AAAAAAAAAA......
...... *...... AA......
.....*...... AA.....
....**...... AAA....
..*****...... AAAAA...
......