簡體   English   中英

二維陣列位置跟蹤處理

[英]2d array position track on processing

這是我通過處理編寫的不完整的TILEWORLD游戲,其中包含20個障礙物,10個孔,10個圖塊和一個特工。 代理會隨機移動,而其他對象則不會移動。 在agentmove功能中,agent進入障礙物,然后停止,但是我希望Agent避免進入障礙物,因此我如何跟蹤Agent的先前位置,即如果agent的下一個動作是障礙物所在的位置在,它避免嗎?

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

假設您的代理只能移動1個空間,您可以采用的一種方法是:

  • 擔任當前代理職位。

  • 檢查當前位置+1或-1(沿它們的移動方向)以查看其是否為有效位置。

  • 如果是,請繼續。 如果不是,請停止移動方法。

例如:如果業務代表在位置(0,0),並且他們向下移動1個空格。 檢查位置(1,0),以查看該空間的位置。 如果是牆/范圍外,請盡早退出move方法。

 //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

 }
  • 您將座席在網格中的位置作為[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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM