我被分配了以下任务:
创建一个10x10矩阵作为2D数组。假设将机器人放置在位置[0,0]。现在随机产生一个招式。此举可能会将机器人带到八个可能的相邻插槽之一– {上,下,左,右,左上角,左下角,右上角和右下角} –这些时隙由{1,2,3,4,5,6,7,8}表示。但是,在[0,0]下,机械手只有三个可能的插槽可移至–右,下,右下角。 创建另一个名为R2的机器人并将其放置在[9,9]上。 现在随机生成一个介于[1到8]范围内的整数。该第一随机整数对应于机器人R1的可能移动。如果移动有效,则将R1移至新插槽。如果移动将机器人带出[10x10]矩阵的边界,则该移动无效。如果移动无效,则继续生成随机整数,直到找到有效的移动为止。 对第二个机器人R2重复此过程。 如果R1和R2都在同一插槽中,则停止,打印最后一个插槽,打印将R1引导到该插槽的随机数序列,并打印将R2引导到同一插槽的随机数序列。 通过Robot类和MovingRobot子类来实现此程序。
方法,变量,返回类型和参数都是由讲师指定的,不能更改。
```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());
}
}```
我得到:
线程“主”中的异常java.lang.OutOfMemoryError:Java堆空间 在java.base / java.util.Arrays.copyOf(Arrays.java:3721)在 java.base / java.util.Arrays.copyOf(Arrays.java:3690)在 java.base / java.util.ArrayList.grow(ArrayList.java:237)在 java.base / java.util.ArrayList.grow(ArrayList.java:242)在 java.base / java.util.ArrayList.add(ArrayList.java:485)在 java.base / java.util.ArrayList.add(ArrayList.java:498)在 MovingRobot.move(MovingRobot.java:60)位于 MovingRobot.main(MovingRobot.java:97)