机器人在网格中的位置和方向 [英] Position and orientation of robot in a grid
问题描述
我想制作一个10x10的网格并将机器人放置在位置(10,1)(左下).我希望该机器人能够向前移动,向左/向右旋转并拾取/放置网格中的对象.当放置在任何位置时,网格中应该有数字,可以显示在该位置放置了多少个对象,就像这样:
I want to make a 10x10 grid and put the robot in position (10,1) (bottom left). I want this robot to be able to move forward, turn left/right and to pick up/put objects in a grid. When put in any position, there should be number in a grid which shows how many objects is put in this position, just like this:
..........
...1......
..2.......
....3.....
..........
..........
......9...
.....4....
.........1
..........
我们不会在网格中看到机器人.我有两节课.班级机器人:
We will not see the robot in a grid. I have two classes. Class Robot:
public class Robot {
private Area area;
private Robot rob;
public Robot(Area area){
this.area = area;
rob = new Robot(area);
}
public void Right(){
}
public void Left(){
}
public void Forward(){
}
public void Put(){
}
public void PickUp(){
}
public (?) getPosition(){ // should return robot's position
}
}
班级区域:
private int numberOfObjects;
private Robot robot;
private static final int X = 10;
private static final int Y = 10;
private Object [][] area; // grid
public Area(){ // defines a grid and robot
area = new Area[X][Y];
for(int a=0;a<X;a++){
for(int b=0;b<Y;b++)
area[a][b]=".";
}
numberOfObjects = 0; // grid is initially empty
Area ar = new Area();
robot = new Robot(ar);
}
public void Put(int x,int y){ // put the object to position (x,y)
area[x][y]=numberOfObjects++;
}
public void PickUp(int x,int y){ // pick up the object in position (x,y)
if(area[x][y]!=null){
area[x][y]=numberOfObjects--;
}
}
public void PrintAGrid(){
for(int r=0;r<X;r++){
for(int c=0;c<Y;c++)
System.out.print(area[r][c]+" ");
System.out.println();
}
System.out.println();
}
}
如何将机器人放置在(10,1)位置?如何声明和设置其方向(即在右侧)?我想写其他方法会很容易,所以我不关注它.
How can I put the robot in position (10,1)? How can I declare and set its orientation (i.e. on the right)? I guess it will be easy to write other methods, so I do not focus on it.
推荐答案
您的代码有几个问题.
- 为什么在类
Robot
中有一个Robot
的实例?您根本没有使用过该实例! -
private Object [][] area;
应该是int[][] area
.您总是在其中保存int
,对吧? - 如果我正确理解您的要求,则您对
pick
和put
的实现不正确.
- Why do you have an instance of
Robot
inside the classRobot
? You have not used that instance at all! private Object [][] area;
should beint[][] area
. You always saveint
in this, right?- If I understand your requirements correctly, Your implementation of
pick
andput
is not correct.
在这里可以帮助您解决问题.我不得不考虑是否应该将Robot
放在Grid
中,或者应该是另一种方式.我以Robot
中的Grid
结尾.
可能是Grid
可能是单身人士.
Here is a help how you can solve the problems. I had to think several times if Robot
should be in Grid
or it should be the other way. I ended up with Grid
in Robot
.
May be Grid
could be a singleton.
这是我们的Grid
public class Grid {
private int[][] numberOfObjects = new int[10][10];
public void put(int x, int y) {
numberOfObjects[y][x]++;
}
public void pick(int x, int y) {
numberOfObjects[y][x]--;
}
}
您可以用Point
替换参数int x, int y
.
这是机器人
public class Robot {
private static final int NORTH = 0, EAST = 1, SOUTH = 2, WEST = 3;
private int direction;
private int x, y;
private Grid grid;
public Robot(Grid grid) {
this.x = 0;
this.y = 0;
this.grid = grid;
direction = NORTH;
}
public void right() {
direction++;
if (direction == 4) {
direction = 0;
}
}
public void left() {
direction--;
if (direction == -1) {
direction = 3;
}
}
public void forward() {
if (direction == NORTH) {
y--;
} else if (direction == SOUTH) {
y++;
} else if (direction == EAST) {
x++;
} else if (direction == WEST) {
x--;
}
}
public void put() {
grid.put(x, y);
}
public void pick() {
grid.pick(x, y);
}
}
这篇关于机器人在网格中的位置和方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!