1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
# Learning Profile for Assignment #2 Question #9
## Name: Mo Khan
### Student ID: 3431709
1. Problem Statement
Create a 10x10 matrix as a 2D array. See a sampel array below.
| | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
| 0 | [0, 0] | [1, 0] | [2, 0] | [3, 0] | [4, 0] | [5, 0] | [6, 0] | [7, 0] | [8, 0] | [9, 0] |
| 1 | [0, 1] | [1, 1] | [2, 1] | [3, 1] | [4, 1] | [5, 1] | [6, 1] | [7, 1] | [8, 1] | [9, 1] |
| 2 | [0, 2] | [1, 2] | [2, 2] | [3, 2] | [4, 2] | [5, 2] | [6, 2] | [7, 2] | [8, 2] | [9, 2] |
| 3 | [0, 3] | [1, 3] | [2, 3] | [3, 3] | [4, 3] | [5, 3] | [6, 3] | [7, 3] | [8, 3] | [9, 3] |
| 4 | [0, 4] | [1, 4] | [2, 4] | [3, 4] | [4, 4] | [5, 4] | [6, 4] | [7, 4] | [8, 4] | [9, 4] |
| 5 | [0, 5] | [1, 5] | [2, 5] | [3, 5] | [4, 5] | [5, 5] | [6, 5] | [7, 5] | [8, 5] | [9, 5] |
| 6 | [0, 6] | [1, 6] | [2, 6] | [3, 6] | [4, 6] | [5, 6] | [6, 6] | [7, 6] | [8, 6] | [9, 6] |
| 7 | [0, 7] | [1, 7] | [2, 7] | [3, 7] | [4, 7] | [5, 7] | [6, 7] | [7, 7] | [8, 7] | [9, 7] |
| 8 | [0, 8] | [1, 8] | [2, 8] | [3, 8] | [4, 8] | [5, 8] | [6, 8] | [7, 8] | [8, 8] | [9, 8] |
| 9 | [0, 9] | [1, 9] | [2, 9] | [3, 9] | [4, 9] | [5, 9] | [6, 9] | [7, 9] | [8, 9] | [9, 9] |
Assume that a robot is placed in position [0, 0]. Now randomly generate a move.
The move could take the robot to one of the eight possible adjacent slots:
1. up
1. down
1. left
1. right
1. left-up-corner
1. left-down-corner
1. right-up-corner
1. right-down-corner
These slots are represented by {1, 2, 3, 4, 5, 6, 7, 8}.
However, at `[0, 0]`, the robot only has three possible slots to move to:
1. right
2. down
3. right-down-corner
Create another robot called R2 and place it on `[9, 9]`.
Now randomly generate an integer in the range of `[1 to 8]`.
This first random integer corresponds to a possible move for Robot R1.
If the move is valid, then move R1 to its new slot.
A move is invalid if it takes the robot out of bounds of the `[10 x 10]` matrix.
If the move is invalid,
then keep generating random integers until a valid move is found.
Repeat this procedure for the second Robot R2.
If both R1 and R2 are in the same slot then:
1. stop.
1. print the final slot.
1. print the sequence of random numbers that led R1 to this slot.
1. the print the sequence of random numbers that led R2 to the same slot.
Implement this program with a `Robot` class and a `MovingRobot` subclass.
1. Description of the Code
The program includes a `Robot` class and a `MovingRobot` class that inherits
from Robot. Before the `MovingRobot` can advance to the next position based on
a direction, it will check to see if it is possible to advance without hitting
the bounds of the 9x9 grid.
1. Errors and Warnings
This program does not require input from a user.
This program depends on a random number generator to randomly generate the
direction to move the robot towards. This program attempts to check the grid
boundaries before advancing to the next position.
1. Sample Input and Output
1. Screenshot from the initial draw of the program.
```bash
==== Question 9 ====
| | | | | | | | | | |
|1| | | | | | | | | |
| | | | | | | | | | |
| | | | | | | | | | |
| | | | | | | | | | |
| | | | | | | | | | |
| | | | | | | | | | |
| | | | | | | | | | |
| | | | | | | | | |2|
| | | | | | | | | | |
R1 (1, 0): [4]
R2 (8, 9): [3]
```
1. Screenshot from the final draw of the program.
```bash
==== Question 9 ====
| | | | | | | | | | |
| | | |X| | | | | | |
| | | | | | | | | | |
| | | | | | | | | | |
| | | | | | | | | | |
| | | | | | | | | | |
| | | | | | | | | | |
| | | | | | | | | | |
| | | | | | | | | | |
| | | | | | | | | | |
R1 (1, 3):
[4,4,2,7,6,1,4,2,7,6,7,2,4,3,6,3,3,2,7,5,6,2,7,3,1,2,7,7,6,6,4,3,2,1,1,1,2,2,7,3,2,4,2]
R2 (1, 3):
[3,5,7,4,1,5,2,7,1,2,1,1,1,1,6,2,7,3,5,7,4,3,2,7,3,4,6,2,7,3,6,3,3,3,2,6,2,5,3,1,6,4,1]
```
1. Discussion
This was fun. Instead of passing around x and y coordinate, I would have
preferred to extract a `Coordinate` class that can check the boundaries of the
grid.
|