Problem with 2D Array

This is where you talk about the RCX software itself, installation issues, and programming talk.

Moderator: roger

Problem with 2D Array

Postby wirawan1988 » Tue Oct 09, 2007 6:59 am

Hello, I was wondering if its possible to put an object in a 2D array in Lejos, since I was able to do it normally in normal java. When I try to create the data within the array it would fail when it get to 15 0 from a 16 by 16 array. And it would fail on all the 15 giving me 225 object instead of 256 that i need.
Does anyone have any ideas. I need to store data that is already organized in object about 255 of them. Any help would be appreciated.
Here is my code,
Code: Select all
public class Main
{
   static int [][] mazeMap=new int[16][16];//the model for the maze with the given obstacles
   static int [][] mazeMetrics=new int [16][16];//Create the model of the map with the given metrics
   static Cell [][] maze=new Cell[16][16];
   
   /**
    *
    * @param args
    */
   public static void main(String []args)throws InterruptedException
   {
      
      ButtonExit be=new ButtonExit();
      Button.RUN.addButtonListener(be);
      Sensor.S3.setTypeAndMode(1,0x00);
      Sensor.S2.setTypeAndMode(1, 0x00);
      Sensor.S1.setTypeAndMode(1,0x00);
      Cell [][]temp=new Cell[16][16];
      /*for(int x=0;x<16;x++)
      {
         for(int y=0;y<16;y++)
         {
            try{
               maze[x][y]=new Cell(x,y,5,1111);
            }
            catch(ArrayIndexOutOfBoundsException e)
            {
               temp=x*100+y;
               break;
            }
         }
      }*/
      int temp2=0;
      for(int x=0;x<16;x++)
      {
         for(int y=0;y<16;y++)
         {
            
            temp[x][y]=new Cell(x,y,5,1111);
         }
            
      }
      while(true)
      {
         LCD.showNumber(temp2);
      }
      //end of main method
   }

   /**
    * Check the sensor to see where are the walls are and will return the char representation
    * NSEW
    * @return the char representation of the obstacles
    */
   public static int sensorCheck()
   {
      /*
       * get the raw from the sensor port
       * check first to see if sensor 1 is active
       * then drill down the search appending the values of direction
       */
      int junk=0;
      Sensor.S1.activate();
      if(Sensor.S1.readRawValue()<990&&Sensor.S1.readRawValue()>960)
      {
         //result="N";
         junk=1000;
      }
      if(Sensor.S1.readRawValue()<930&&Sensor.S1.readRawValue()>900)
      {
         //result="S";
         junk=100;
      }
      if(Sensor.S1.readRawValue()<900)
      {
         //result="NS";
         junk=1100;
      }
      Sensor.S3.activate();
      if(Sensor.S3.readRawValue()<850&&Sensor.S3.readRawValue()>800)
      {
         //result+="E";
         junk+=10;
      }
      if(Sensor.S3.readRawValue()<750&&Sensor.S3.readRawValue()>650)
      {
         //result+="W";
         junk+=1;
      }
      if(Sensor.S3.readRawValue()<625)
      {
         
         //result+="EW";
         junk+=11;
      }
      LCD.showNumber(junk);
      return junk;
   }
   /**
    * For the given x and y location of the maze it calculate the metric of that cell
    * the metric is based upon the distance to the goal in the center of the maze
    * @param x the x coordinate of the cell
    * @param y the y coordinate of the cell
    * @return the metric for the particular cell
    *
    */
   public static void updateMetric(int x, int y)
   {
      Stack foo=new Stack();
      //Update the distance values (if necessary)
      foo.clear();//Make sure the stack is empty
      foo.push((Cell)maze[x][y]);//Push the current cell (the one the robot is standing on) onto the stack
      while(!foo.empty())//Repeat the following set of instructions until the stack is empty:
      {
         Cell temp=(Cell)foo.pop();//Pull a cell from the stack
         int dist=temp.getMetric();
         if((dist-1)!=findLoweNeig(x,y))//Is the distance value of this cell = 1 + the minimum value of its open neighbors?
         {
            maze[x][y].setMetric(findLoweNeig(x,y)+1);//No -> Change the cell to 1 + the minimum value of its open neighbors and
            int neig=maze[x][y].getOpenNeigbor();
            if(neig>=1000)//N
            {
               foo.push(maze[x+1][y]);   
            }
            if(neig>=100)//S
            {
               foo.push(maze[x-1][y]);
            }
            if(neig>=10)//E
            {
               foo.push(maze[x][y+1]);
            }
            if(neig>=1)//W
            {
               foo.push(maze[x][y-1]);
            }
            //push all of the cell's open neighbors onto the stack to be checked
         }
         // Yes -> Do nothing
       }

   }
   /**
    * Method to update the maze models
    */
   public static void update(int x, int y)
   {
      maze[x][y].setMap(sensorCheck());
      updateMetric(x,y);
   }   
   public static int findLoweNeig(int x,int y)
   {
      int Onei=maze[x][y].getOpenNeigbor();
      int low=255;
      int Lnei=0;;
      if(Onei>=1000)//N
      {
         if(low<maze[x+1][y].getMetric())
         {
            low=maze[x+1][y].getMetric();
            Lnei=1000;
         }   
      }
      if(Onei>=100)//S
      {
         if(low<maze[x-1][y].getMetric())
         {
            low=maze[x-1][y].getMetric();
            Lnei=100;
         }
            
      }
      if(Onei>=10)//E
      {
         if(low<maze[x][y+1].getMetric())
         {
            low=maze[x][y+1].getMetric();
            Lnei=10;
         }
      }
      if(Onei>=1)//W
      {
         if(low<maze[x][y-1].getMetric())
         {
            low=maze[x][y-1].getMetric();
            Lnei=1;
         }   
      }
      return Lnei;
   }
   public static Cell getNorth(int x,int y)
   {
      return maze[x+1][y];
   }
   public static Cell getSouth(int x,int y)
   {
      return maze[x-1][y];
   }
   public static Cell getEast(int x,int y)
   {
      return maze[x][y+1];
   }
   public static Cell getWest(int x,int y)
   {
      return maze[x][y-1];
   }
}

class ButtonExit implements ButtonListener
{
   public boolean isrunpressed=false;
   public void buttonPressed(Button b)
   {
       if(b.equals(Button.RUN))
           isrunpressed=true;
   }
   public void buttonReleased(Button b)
   {
   }
}


and here is the code for the object
Code: Select all
public class Cell
{
   private int X,Y,metric,map;
   public Cell (int met, int ma)
   {
      this.metric=met;
      this.map=ma;
   }
   public Cell (int x,int y,int met,int ma)
   {
      this.X=x;
      this.Y=y;
      this.metric=met;
      this.map=ma;
   }
   public void setMetric(int m)
   {
      metric=m;
   }
   public void setMap(int m)
   {
      map=m;
   }
   
   public int getOpenNeigbor()
   {
      return 1111-map;
   }
   public int getMetric()
   {
      return metric;
   }
   public int getMap()
   {
      return map;
   }
}
wirawan1988
New User
 
Posts: 2
Joined: Wed Aug 22, 2007 5:01 am

Postby bbagnall » Sun Oct 21, 2007 3:46 pm

Are you using lejos 3.0? We noticed a bug with arrays in leJOS NXJ that might have been introduced in the leJOS RCX 3.0 code (NXJ uses much of the same code as the RCX leJOS).
User avatar
bbagnall
Site Admin
 
Posts: 392
Joined: Fri Aug 04, 2006 4:03 pm

Postby frede » Sat Mar 08, 2008 12:33 pm

bbagnall wrote:Are you using lejos 3.0? We noticed a bug with arrays in leJOS NXJ that might have been introduced in the leJOS RCX 3.0 code (NXJ uses much of the same code as the RCX leJOS).


Hi I am experiencing an array upload problem, is that what the bug is about, and how do i resolve it?
Thanks in advance!
frede
New User
 
Posts: 5
Joined: Sat Mar 08, 2008 12:32 pm
Location: Denmark


Return to RCX Software

Who is online

Users browsing this forum: Yahoo [Bot] and 0 guests

more stuff