了解数组访问(处理)

发布于 2024-12-10 12:27:49 字数 1883 浏览 0 评论 0原文

大家好,有人可以帮助我理解兰顿蚂蚁草图中的这段特定代码吗?

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

}

Hi could anybody help me understand this particular piece of code from this Langton's Ant sketch.

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

I don't exactly understand what is actually happening here, here is the rest of the code for context. (originally from here 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();
  }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

GRAY°灰色天空 2024-12-17 12:27:49

您提到的行:

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

大致类似于:

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

为了方便起见,它只是语法简写。

The line you mention:

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

is broadly similar to:

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

It's just syntactic shorthand, for convenience.

︶ ̄淡然 2024-12-17 12:27:49

您的代码创建一个长度为 2 的新 int 数组,并使用给定表达式初始化元素。它相当于:

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

Your code creates a new array of int with length 2, and initialises the elements with the given expressions. It is equivalent to:

antLoc = new int[2];
antLoc[0] = rows/2;
antLoc[1] = columns/2;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文