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

