Java中的交互式生活游戏

Java中的交互式生活游戏,java,arrays,processing,interactive,conways-game-of-life,Java,Arrays,Processing,Interactive,Conways Game Of Life,我想写一个互动的生活游戏,我可以在游戏中手动插入滑翔机。 我希望它工作的方式是我有一个滑翔机按钮,按下它后,我可以将光标移动到我希望滑翔机设置在网格上的位置,然后单击网格,滑翔机将集成到生活游戏中。 我正在使用处理,我正在使用这个草图作为启动代码 这是在鼠标按下时创建新单元格的代码(一次一个) 我不知道如何制作一个数组来存储滑翔机单元的位置。每个单元都是一个10像素的正方形,因此如果我想构建它,我知道如何映射它,但不确定如何将其粘贴到阵列中,然后将其集成到网格中。这里有两个不同的功能,如何将网格

我想写一个互动的生活游戏,我可以在游戏中手动插入滑翔机。 我希望它工作的方式是我有一个滑翔机按钮,按下它后,我可以将光标移动到我希望滑翔机设置在网格上的位置,然后单击网格,滑翔机将集成到生活游戏中。 我正在使用处理,我正在使用这个草图作为启动代码

这是在鼠标按下时创建新单元格的代码(一次一个)


我不知道如何制作一个数组来存储滑翔机单元的位置。每个单元都是一个10像素的正方形,因此如果我想构建它,我知道如何映射它,但不确定如何将其粘贴到阵列中,然后将其集成到网格中。

这里有两个不同的功能,如何将网格中的单元从死变为活,以及如何在执行之前显示更改。数组“gliderArray”存储你的滑翔机形状,通过遍历数组并用数组中的任何内容替换底层网格,将其应用到网格上。。。 至于显示,您要么为显示的单元格设置一个不同的状态,它们将要更改,要么重新绘制它们的矩形。。。这个解决方案是第二种方法

void draw() {

  //Draw grid
  for (int x=0; x<width/cellSize; x++) {
    for (int y=0; y<height/cellSize; y++) {
      if (cells[x][y]==1) {
        fill(alive); // If alive
      }
      else {
        fill(dead); // If dead
      }
      rect (x*cellSize, y*cellSize, cellSize, cellSize);
    }
  }
  // Iterate if timer ticks
  if (millis()-lastRecordedTime>interval) {
    if (!pause) {
      iteration();
      lastRecordedTime = millis();
    }
  }
  //Glider shape is :
  //OXO
  //OOX
  //XXX
  //where O is a dead cell and X is an alive cell.
  int [][] gliderArray = new int [][] {
    { 0, 1, 0 }
    , 
    { 0, 0, 1 }
    , 
    { 1, 1, 1 }
  };
  // Create  new cells manually on pause
  if (pause) {
    // Map and avoid out of bound errors
    int xCellOver = int(map(mouseX, 0, width, 0, width/cellSize));
    xCellOver = constrain(xCellOver, 0, width/cellSize-1);
    int yCellOver = int(map(mouseY, 0, height, 0, height/cellSize));
    yCellOver = constrain(yCellOver, 0, height/cellSize-1);
    if (glider) {
      // Map again because glider takes +- 1 cells on each direction
      xCellOver = constrain(xCellOver, 1, width/cellSize-2);
      yCellOver = constrain(yCellOver, 1, height/cellSize-2);
    }
    if (mousePressed) {
      // Check against cells in buffer
      if (!glider) {
        if (cellsBuffer[xCellOver][yCellOver]==1) { // Cell is alive
          cells[xCellOver][yCellOver]=0; // Kill
          fill(dead); // Fill with kill color
        }
        else { // Cell is dead
          cells[xCellOver][yCellOver]=1; // Make alive
          fill(alive); // Fill alive color
        }
      } 
      else {
        for (int i=-1; i<=1; i++) {
          for (int j=-1; j<=1; j++) {
            cells[xCellOver+j][yCellOver+i] = gliderArray[i+1][j+1];
          }
        }
      }
    }
    else {
      for (int x=0; x<width/cellSize; x++) {
        for (int y=0; y<height/cellSize; y++) {
          cellsBuffer[x][y] = cells[x][y];
          if (glider && x >= xCellOver-1 && x <= xCellOver+1 && y >= yCellOver-1 && y <= yCellOver+1) {
            for (int i=-1; i<=1; i++) {
              for (int j=-1; j<=1; j++) {
                if (x == xCellOver+j && y == yCellOver+i) fill(gliderArray[i+1][j+1] == 1? color(255, 125, 0) : dead);
              }
            }
            rect (x*cellSize, y*cellSize, cellSize, cellSize);
          } 
          else if (x == xCellOver && y == yCellOver) {
            fill(cellsBuffer[x][y] == 1? color(0,0,255) : color(255, 125, 0));
            rect (x*cellSize, y*cellSize, cellSize, cellSize);
          }
        }
      }
    }
  }
}
另一个签入无效键按下()


创建将单元存储为二维坐标(x,y)的阵列。x、 y是要绘制的滑翔机实例左上角的位置。我建议这样做,而不是计算滑翔机的中间,因为10,10没有一个好的中心([5,5],[5,6],[6,6]和[6,5]是四个中心点)。
  //create gliders on press
  if (pause && gliderSelected && mousePressed) {
    // Map and avoid out of bound errors
    int xCellOver = int(map(mouseX, 0, width, 0, width/cellSize));
    xCellOver = constrain(xCellOver, 0, width/cellSize-1);
    int yCellOver = int(map(mouseY, 0, height, 0, height/cellSize));
    yCellOver = constrain(yCellOver, 0, height/cellSize-1);

    //here i thought of maybe creating an array of cells that map the glider and then running a loop to change the grid cell status according to the glider array 


  }
void draw() {

  //Draw grid
  for (int x=0; x<width/cellSize; x++) {
    for (int y=0; y<height/cellSize; y++) {
      if (cells[x][y]==1) {
        fill(alive); // If alive
      }
      else {
        fill(dead); // If dead
      }
      rect (x*cellSize, y*cellSize, cellSize, cellSize);
    }
  }
  // Iterate if timer ticks
  if (millis()-lastRecordedTime>interval) {
    if (!pause) {
      iteration();
      lastRecordedTime = millis();
    }
  }
  //Glider shape is :
  //OXO
  //OOX
  //XXX
  //where O is a dead cell and X is an alive cell.
  int [][] gliderArray = new int [][] {
    { 0, 1, 0 }
    , 
    { 0, 0, 1 }
    , 
    { 1, 1, 1 }
  };
  // Create  new cells manually on pause
  if (pause) {
    // Map and avoid out of bound errors
    int xCellOver = int(map(mouseX, 0, width, 0, width/cellSize));
    xCellOver = constrain(xCellOver, 0, width/cellSize-1);
    int yCellOver = int(map(mouseY, 0, height, 0, height/cellSize));
    yCellOver = constrain(yCellOver, 0, height/cellSize-1);
    if (glider) {
      // Map again because glider takes +- 1 cells on each direction
      xCellOver = constrain(xCellOver, 1, width/cellSize-2);
      yCellOver = constrain(yCellOver, 1, height/cellSize-2);
    }
    if (mousePressed) {
      // Check against cells in buffer
      if (!glider) {
        if (cellsBuffer[xCellOver][yCellOver]==1) { // Cell is alive
          cells[xCellOver][yCellOver]=0; // Kill
          fill(dead); // Fill with kill color
        }
        else { // Cell is dead
          cells[xCellOver][yCellOver]=1; // Make alive
          fill(alive); // Fill alive color
        }
      } 
      else {
        for (int i=-1; i<=1; i++) {
          for (int j=-1; j<=1; j++) {
            cells[xCellOver+j][yCellOver+i] = gliderArray[i+1][j+1];
          }
        }
      }
    }
    else {
      for (int x=0; x<width/cellSize; x++) {
        for (int y=0; y<height/cellSize; y++) {
          cellsBuffer[x][y] = cells[x][y];
          if (glider && x >= xCellOver-1 && x <= xCellOver+1 && y >= yCellOver-1 && y <= yCellOver+1) {
            for (int i=-1; i<=1; i++) {
              for (int j=-1; j<=1; j++) {
                if (x == xCellOver+j && y == yCellOver+i) fill(gliderArray[i+1][j+1] == 1? color(255, 125, 0) : dead);
              }
            }
            rect (x*cellSize, y*cellSize, cellSize, cellSize);
          } 
          else if (x == xCellOver && y == yCellOver) {
            fill(cellsBuffer[x][y] == 1? color(0,0,255) : color(255, 125, 0));
            rect (x*cellSize, y*cellSize, cellSize, cellSize);
          }
        }
      }
    }
  }
}
boolean glider = false;
if (key == 'g') glider = !glider;