java - Position and orientation of robot in a grid -


i want make 10x10 grid , put robot in position (10,1) (bottom left). want robot able move forward, turn left/right , pick up/put objects in grid. when put in position, there should number in grid shows how many objects put in position, this:

.......... ...1...... ..2....... ....3..... .......... .......... ......9... .....4.... .........1 .......... 

we not see robot in grid. have 2 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  } } 

class area:

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 grid , 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 empty     area ar = new area();     robot = new robot(ar); }  public void put(int x,int y){ // put object position (x,y)     area[x][y]=numberofobjects++; }  public void pickup(int x,int y){ // pick 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(); } } 

how can put robot in position (10,1)? how can declare , set orientation (i.e. on right)? guess easy write other methods, not focus on it.

there several issues code.

  1. why have instance of robot inside class robot? have not used instance @ all!
  2. private object [][] area; should int[][] area. save int in this, right?
  3. if understand requirements correctly, implementation of pick , put not correct.

here how can solve problems. had think several times if robot should in grid or should other way. ended grid in robot. may grid singleton.

here our 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]--;     } } 

you can replace parameters int x, int y point.

and here robot

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);     } } 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -