master
..
rw-r--r--
4.8 KB
rw-r--r--
5.7 KB
rw-r--r--
4.2 KB
rw-r--r--
2.6 KB
rw-r--r--
3.1 KB

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 10x10 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

  2. 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.