这是我通过处理编写的一个不完整的TILEWORLD游戏,其中包含20个障碍,10个洞,10个拼贴和一个代理。代理随机移动,其他对象不移动。在agentmove功能中,代理进入障碍,它停止,但我希望代理避免进入障碍,所以我如何跟踪代理的先前位置,如果代理的下一步是障碍的位置at,它避免了吗?
int min_x = 0;
int min_y = 0;
int max_x = 400;
int max_y = 600;
int grid_size = 10;
int Hoo[][] = new int[60][40];
int numhole=10;
int numtile =10;
int numobs=20;
PVector agent=getRandomLoc();
PVector hole1;
PVector tile;
PVector obstacle;
int x,y;
int i,j;
int a,b;
final int hole=1;
final int til=2;
final int obs=3;
final int STOPPED = 0;
final int RUNNING = 1;
int agentState = STOPPED;
void settings()
{
size( max_x, max_y );
}
void setup() {
ellipseMode( CORNER );
agentState = RUNNING;
for(x=0; x<60; x++){
for(y=0;y<40;y++){
Hoo[x][y]=0;
}}
while(numhole>0)
{
hole1=getRandomLoc();
i=(int)hole1.x/grid_size;
j=(int)hole1.y/grid_size;
if(Hoo[j][i]==0){
Hoo[j][i]=hole;
numhole--;
}
}
while(numobs>0)
{
obstacle=getRandomLoc();
i=(int)obstacle.x/grid_size;
j=(int)obstacle.y/grid_size;
if(Hoo[j][i]==0){
Hoo[j][i]=obs;
numobs--;
}
}
while(numtile>0)
{
tile=getRandomLoc();
i=(int)tile.x/grid_size;
j=(int)tile.y/grid_size;
if(Hoo[j][i]==0){
Hoo[j][i]=til;
numtile--;
}
}
}
void draw() {
background( #ffffff );
stroke( #cccccc );
for ( int x=min_x; x<=max_x; x+=grid_size ) {
line( x,min_y,x,max_y );
}
for ( int y=min_y; y<=max_y; y+=grid_size ) {
line( min_x,y,max_x,y );
}
for(int x=0; x< 60; x++){
for(int y=0; y<40; y++){
if(Hoo[x][y]==obs){
stroke( #cccccc );
fill( #cccccc );
rect( y*grid_size,x*grid_size, grid_size, grid_size );
}}}
for(int x=0; x< 60; x++){
for(int y=0; y<40; y++){
if(Hoo[x][y]==hole){
stroke( #cccccc );
fill( #000000 );
rect( y*grid_size,x*grid_size, grid_size, grid_size );
}}}
for(int x=0; x< 60; x++){
for(int y=0; y<40; y++){
if(Hoo[x][y]==til){
stroke( #cccccc );
fill( #cc00cc );
rect( y*grid_size,x*grid_size, grid_size, grid_size );
noFill();
}}}
if ( agentState == RUNNING ) {
makeRandomMove();
agentmove();
delay(100);
}
stroke( #0000ff );
fill( #0000ff );
ellipse( agent.x, agent.y, grid_size, grid_size );
}
void agentmove()
{
int a=(int)agent.x/grid_size;
int b=(int)agent.y/grid_size;
if(Hoo[a][b]==obs)
noLoop();
if(Hoo[a][b]==hole)
{
noStroke();
fill( #ffffff );
ellipse( agent.x, agent.y, grid_size, grid_size );
background(#000000);
}
}
void mouseClicked() {
if ( agentState == STOPPED ) {
agentState = RUNNING;
}
else {
agentState = STOPPED;
}
}
PVector getRandomLoc() {
return( new PVector(
((int)random(min_x,max_x+1)/grid_size)*grid_size,
((int)random(min_y,max_y+1)/grid_size)*grid_size ));
}
void makeRandomMove() {
int direction = (int)random( 0,4 );
switch( direction ) {
case 0: // north
agent.y -= grid_size;
if ( agent.y < min_y ) {
agent.y = max_y - grid_size;
}
break;
case 1: // west
agent.x -= grid_size;
if ( agent.x < min_x ) {
agent.x = max_x - grid_size;
}
break;
case 2: // south
agent.y += grid_size;
if ( agent.y > max_y ) {
agent.y = min_y;
}
break;
case 3: // east
agent.x += grid_size;
if ( agent.x > max_x ) {
agent.x = min_x;
}
break;
}
}
答案 0 :(得分:0)
假设您的代理只能移动1个空格,您可以采取一种方式:
采取当前代理人的位置。
检查当前位置+1或-1(在它们移动的方向上)以查看它是否是有效位置。
如果是,请继续。如果不是,请停止移动方法。
例如:如果代理位于(0,0)位置,并且它们向下移动1个空格。检查位置(1,0)以查看该空间的位置。如果它是一个墙/越界,请尽早退出移动方法。
//Agent position is at Hoo[0][0] - Lets represent it with a/b
//A random wall was generated at Hoo[1][0]
//We are attempting to move down 1 space.
if (Hoo[a + 1][b] == obs){
//The position has an obstacle. Return/deny movement
}
答案 1 :(得分:0)
[x,y]
位置。[x,y]
位置。[x+1,y]
位置有障碍物。这是一个小例子:
boolean[][] grid = new boolean[3][3];
int playerX = 0;
int playerY = 0;
void setup() {
grid[1][1] = true;
}
void draw() {
background(255);
drawGrid();
drawPlayer();
}
void keyPressed() {
if (keyCode == UP) {
up();
} else if (keyCode == DOWN) {
down();
} else if (keyCode == LEFT) {
left();
} else if (keyCode == RIGHT) {
right();
}
}
void up() {
//don't let the player move off the screen
if (playerY > 0) {
//don't let the player move over an occupied cell
if (!grid[playerX][playerY-1]) {
//move the player
playerY--;
}
}
}
void down() {
//don't let the player move off the screen
if (playerY < 2) {
//don't let the player move over an occupied cell
if (!grid[playerX][playerY+1]) {
//move the player
playerY++;
}
}
}
void left() {
//don't let the player move off the screen
if (playerX > 0) {
//don't let the player move over an occupied cell
if (!grid[playerX-1][playerY]) {
//move the player
playerX--;
}
}
}
void right() {
//don't let the player move off the screen
if (playerX < 2) {
//don't let the player move over an occupied cell
if (!grid[playerX+1][playerY]) {
//move the player
playerX++;
}
}
}
void drawGrid() {
//loop through each column
for (int column = 0; column < grid.length; column++) {
//loop through each row
for (int row = 0; row < grid[column].length; row++) {
float w = width/3.0; //width of cell
float h = height/3.0; //height of cell
float x = column * w; //x of square
float y = row * h; //y of square
if (grid[column][row]) {
//if grid is populated, draw black
fill(0);
} else {
//if grid is populated, draw white
fill(255);
}
//draw the grid cell
rect(x, y, w, h);
}
}
}
void drawPlayer() {
float w = width/3.0; //width of cell
float h = height/3.0; //height of cell
float x = playerX * w; //x of square
float y = playerY * h; //y of square
//green
fill(0, 255, 0);
//draw the player cell
rect(x, y, w, h);
}