Game Development Reference
In-Depth Information
int
[][] options = {
{0,1},{-1,0},{1,0},{0,-1}
};
int
[] best =
new int
[2];
int
bestCount=0;
int
mid_d = Integer.
MAX_VALUE
;
for
(
int
i=0;i<options.
length
;i++){
int
tempCol = m.col+options[i][0];
int
tempRow = m.row+options[i][1];
if
(gv.checkCollision(tempRow,tempCol)){
continue
;
}
int
mx = tempCol*TILE_SIZE+TILE_SIZE/2;
int
my = tempRow*TILE_SIZE+TILE_SIZE/2;
//calculates Monte Carlo distance
int
distance = Math.
abs
(hx-mx)+Math.
abs
(hy-my);
if
(distance < mid_d){
best[0] = i;
mid_d = distance;
bestCount = 1;
}
else if
(distance == mid_d){
best[1] = i;
bestCount = 2;
}
}
if
(bestCount == 1){
return
best[0];
}
else if
(bestCount == 2){
if
(backDir == -1){
return
best[(Math.
random
()>0.5?0:1)];
}
else if
(best[0]!=backDir && best[1]!=backDir){
return
best[(Math.
random
()>0.5?0:1)];
Search WWH ::
Custom Search