Понимание доступа к массиву (обработка) - PullRequest
0 голосов
/ 18 октября 2011

Привет, может кто-нибудь помочь мне понять этот конкретный кусок кода из этого наброска муравья Лэнгтона.

antLoc = new int[]{rows/2,columns/2};

Я не совсем понимаю, что на самом деле здесь происходит, вот остальная часть кода для контекста,(первоначально отсюда http://www.openprocessing.org/visuals/?visualID=13653)

boolean[][] state;
int[] antLoc;
int antDirection;

int squareSize = 5;
int columns, rows;

color bgCol = color(0,128,128);
color antCol = color (255,0,0);
color sqCol = color(128,128,128);

void setup(){
  size(800,600);
  background(bgCol);
  columns = width/squareSize;
  rows = height/squareSize;

  state = new boolean[rows][columns];
  for(int j = 0; j < rows; j++){
    for(int i = 0; i < columns; i++){
      state[j][i] = false;
    }
  }
  antLoc = new int[]{rows/2,columns/2};
  antDirection = 1;
}

void drawScene(){
  fill(sqCol);
  for(int j = 0; j < rows; j++){
    for(int i = 0; i < columns; i++){
      if(state[j][i]){
        rect(i*squareSize,j*squareSize,squareSize,squareSize);
      }
    }
  }
  fill(antCol);
  rect(antLoc[1]*squareSize,antLoc[0]*squareSize,squareSize,squareSize);
}

void turnLeft(){
  if (antDirection > 1){
    antDirection--;
  } else{
    antDirection = 4;
  }
}

void turnRight(){
  if (antDirection < 4){
    antDirection++;
  } else {
    antDirection = 1;
  }
}

void moveForward(){
  if (antDirection == 1){
    antLoc[0]--;
  }
  if (antDirection == 2){
    antLoc[1]++;
  }
  if (antDirection == 3){
    antLoc[0]++;
  }
  if (antDirection == 4){
    antLoc[1]--;
  }
}

void updateScene(){
  moveForward();
  if (state[antLoc[0]][antLoc[1]] == false){
    state[antLoc[0]][antLoc[1]] = true;
    turnRight();
  } else {
    state[antLoc[0]][antLoc[1]] = false;
    turnLeft();
  }
}

void draw(){
  background(bgCol);
  drawScene();
  for(int i = 0; i < 10; i++){
      updateScene();
  }

}

Ответы [ 2 ]

1 голос
/ 18 октября 2011

Упомянутая вами строка:

antLoc = new int[]{rows/2,columns/2};

в целом похожа на:

antLoc = new int[2];
antLoc[0] = rows / 2;
antLoc[1] = columns / 2;

Для удобства это просто синтаксическая стенография.

1 голос
/ 18 октября 2011

Ваш код создает новый массив int длиной 2 и инициализирует элементы с заданными выражениями. Это эквивалентно:

antLoc = new int[2];
antLoc[0] = rows/2;
antLoc[1] = columns/2;
...