0

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)

KDLH
  • 23
  • 6
  • x and y have values, but `moves` is `null`. – azurefrog Nov 06 '19 at 22:18
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – azurefrog Nov 06 '19 at 22:19
  • OK I have initialized the array at the beginning of the subclass. Now I am getting a out of memory error. I am assuming something I am doing in my while loop is incorrect. – KDLH Nov 06 '19 at 22:44
  • 2
    I'm confused by this problem -- regardless of your code, with both robots making random moves, there's no guarantee that they will _ever_ be in the same slot at the same time, and storing an infinite amount of moves is guaranteed to run you out of memory... – robsiemb Nov 06 '19 at 23:08
  • That may be the case but I am not sure how to avoid that from happening because I can't add additional methods such as after so many failed attempts to stop. Is that where my memory error is coming from or is there something wrong with the loops in my code? – KDLH Nov 06 '19 at 23:25
  • I haven't looked too deeply at your code, but I don't see how you can solve this without running out of memory via the requirement to store all the moves. In the simplest case, its totally valid for both bots to move 1 space away and then back to their starting square forever -- they'll never meet but the storage requirements are huge. I suspect there is something else in the assignment that was missed (one possibility would be a max number of moves to try, another would be that if you revisit a square, you forget all the moves since you were last there -- but that still won't terminate). – robsiemb Nov 06 '19 at 23:55
  • So many issues! Your sameSlot method has a typo -- you are comparing X to X and Y to X. You are not storing moved in your List, you are storing positions. You probably wanted to store the move. Your printMoves doesn't print the moves at all, just the current position. I'm not sure why you are never terminating. Even with the typo, I would think that they would eventually arrive at the same location. But put a count on the number of moves and quit after you hit max count. – Zag Nov 07 '19 at 00:04
  • To debug this, print out the positions after every move. It will probably become clear. Or just look at them in a debugger, if you've learned that tool. I suspect you have a typo in the validate but I'm not willing to scan it that carefully. Oh yeah, I forgot above, your case 6 is missing a break; You'd see that once you moved the saving of the move to where it belongs. – Zag Nov 07 '19 at 00:17
  • OK I have made all the changes noted and cleaned up the code a but and still getting a memory error. I checked the assignment and there is nothing about a counter and no variable or method in the attributes list that I have missed so cannot implement a method of that type. I am thinking its got something to do with my loops not working correctly and something is infinite. – KDLH Nov 07 '19 at 16:04

0 Answers0