I have been given the following assignment:
Create a 10x10 matrix as a 2D array. 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 – {up, down, left, right, left-up-corner, left-down-corner, right-up-corner, and 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 – right, down, 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 [10x10] 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 stop, print the final slot, print the sequence of random numbers that led R1 to this slot, and 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.
The methods, variables, return types, and parameters are all specified by the instructor and cannot be changed.
```public class Robot {
int x;
int y;
public static final int DOWN = 1;
public static final int LEFT = 2;
public static final int LEFT_DOWN_CORNER = 3;
public static final int LEFT_UP_CORNER = 4;
public static final int RIGHT = 5;
public static final int RIGHT_DOWN_CORNER = 6;
public static final int RIGHT_UP_CORNER = 7;
public static final int UP = 8;
//getters
public int getX() {
return x;
}
public int getY() {
return y;
}
//setters
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
//constructor
public Robot(int x, int y) {
this.x = x;
this.y = y;
}
}//end Robot class
import java.util.ArrayList;
public class MovingRobot extends Robot{
ArrayList<Integer> moves;
int nextMove;
//constructor
public MovingRobot(int x, int y){
super(x, y);
}
public boolean validateNextMove() {
if ((nextMove == LEFT || nextMove == LEFT_UP_CORNER || nextMove == LEFT_DOWN_CORNER) && (this.getY() == 0))
return false;
if ((nextMove == RIGHT || nextMove == RIGHT_UP_CORNER || nextMove == RIGHT_DOWN_CORNER) && (this.getY() == 9))
return false;
if ((nextMove == UP || nextMove == LEFT_UP_CORNER || nextMove == RIGHT_UP_CORNER) && (this.getX() == 0))
return false;
if ((nextMove == DOWN || nextMove == LEFT_DOWN_CORNER || nextMove == RIGHT_DOWN_CORNER) && (this.getX() == 9))
return false;
else
return true;
}
public int generateNextMove(){
return (int)(Math.random()*8) + 1;
}
public static boolean sameSlot(Robot r1, Robot r2){
if ((r1.getX() == r2.getX()) && (r1.getY() == r2.getY()))
return true;
else
return false;
}
public String printMoves(){
String movesReport = "";
for (var nextMove: moves) {
if (nextMove == DOWN) movesReport += "Down, ";
if (nextMove == LEFT) movesReport += "Left, ";
if (nextMove == LEFT_DOWN_CORNER) movesReport += "Left Down Corner, ";
if (nextMove == LEFT_UP_CORNER) movesReport += "Left Up Corner, ";
if (nextMove == RIGHT) movesReport += "Right, ";
if (nextMove == RIGHT_DOWN_CORNER) movesReport += "Right Down Corner, ";
if (nextMove == RIGHT_UP_CORNER) movesReport += "Right Up Corner, ";
if (nextMove == UP) movesReport += "Up, ";
}
return movesReport;
}
public void move(){
do nextMove = generateNextMove();
while (validateNextMove() == false);
switch (nextMove) {
case 1: //LEFT
this.x -= 1;
moves.add(nextMove);
break;
case 2: //LEFT DOWN
this.x -= 1;
this.y += 1;
moves.add(nextMove);
break;
case 3: //LEFT UP
this.x -= 1;
this.y -= 1;
moves.add(nextMove);
break;
case 4: //RIGHT
this.x += 1;
moves.add(nextMove);
break;
case 5: // RIGHT DOWN
this.x += 1;
this.y += 1;
moves.add(nextMove);
break;
case 6: // RIGHT UP
this.x += 1;
this.y -= 1;
moves.add(nextMove);
break;
case 7: // UP
this.y -= 1;
moves.add(nextMove);
break;
case 8: //DOWN
this.y += 1;
moves.add(nextMove);
break;
default: System.out.print("invalid move");
}
}
public static void main(String[] args) {
var r1 = new MovingRobot(0,0);
var r2 = new MovingRobot(9, 9);
r1.moves = new ArrayList<>();
r2.moves = new ArrayList<>();
while (!sameSlot(r1, r2)) {
r1.move();
if (!sameSlot(r1, r2)) {
r2.move();
}
}
System.out.println("Robot 1's moves were: " + r1.printMoves());
System.out.println("Robot 2's moves were: " + r2.printMoves());
}
}```
I am getting:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at java.base/java.util.Arrays.copyOf(Arrays.java:3721) at java.base/java.util.Arrays.copyOf(Arrays.java:3690) at java.base/java.util.ArrayList.grow(ArrayList.java:237) at java.base/java.util.ArrayList.grow(ArrayList.java:242) at java.base/java.util.ArrayList.add(ArrayList.java:485) at java.base/java.util.ArrayList.add(ArrayList.java:498) at MovingRobot.move(MovingRobot.java:60) at MovingRobot.main(MovingRobot.java:97)