My Problem is when my agent gets to 0,0 , 0,9 etc i am trying to stop it from going to 9,0, or 9,9 etc like this in this example
(0,0) N -1.0 (9,0)
(9,0) N -1.0 (8,0)
(8,0) W -1.0 (8,9)
I want it to move in-between the gird and not take short by going back around the grid. Eg, instead of goind form (0,0) to (9,0) go to (0,1) 0r (1 ,0) etc.
I tried something simple like this code, to check if the x and y values are greater than the x and y values for number of rows and columns or if x and y is less than 0
public boolean Notvalid(int x, int y) {
return (x > cr.NUM_ROWS || y > cr.NUM_COLUMNS || x < 0 || y < 0);
}
and call this method when setting the x and y co-ordinates
public GridState(int xpos, int ypos) {
if (!Notvalid(x, y)) {
x = xpos;
y = ypos;
} else {
x = cr.START_ROW;
y = cr.START_COL;
}
}
Anybody know an easier way to handle a rule like this?
It seems like there are three problems with your code:
0
to9
andcr.NUM_ROWS
is10
, you should usex >= cr.NUM_ROWS
orx > cr.NUM_ROWS - 1
instead ofx > cr.NUM_ROWS
. (same for columns)x
andy
), and not the position where the agent is about to go (xpos
,ypos
).x = cr.START_ROW
, it seems like you are exactly doing what you do not want to do, i.e. set the agent to0
when it goes to10
.Try this: