java - Java 中的互动生命游戏

标签 java arrays processing interactive conways-game-of-life

我正在尝试编写一个互动的生活游戏,我可以在游戏字段中手动插入滑翔机。 我希望它如何工作是我有一个滑翔机按钮,按下它后,我可以将光标移动到我想要在网格上设置滑翔机的位置,然后单击网格滑翔机集成到生活游戏中。 我正在使用处理,并且使用这个草图作为启动代码。 http://www.openprocessing.org/sketch/95216

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

// Create  new cells manually on pause
  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);

    // Check against cells in buffer
    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 if (pause && !mousePressed) { // And then save to buffer once mouse goes up
    // Save cells to buffer (so we operate with one array keeping the other intact)
    for (int x=0; x<width/cellSize; x++) {
      for (int y=0; y<height/cellSize; y++) {
        cellsBuffer[x][y] = cells[x][y];
      }
    }
  }

滑翔机形状是:

氧化
OOX
XXX

其中 O 是死细胞,X 是活细胞。

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


  }

我不知道如何制作一个存储滑翔机单元位置的数组。每个单元格都是一个 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);
          }
        }
      }
    }
  }
}

您还需要一个全局 boolean 值:

boolean glider = false;

另一个检查 void keyPressed()

if (key == 'g') glider = !glider;

关于java - Java 中的互动生命游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21472316/

相关文章:

c++ - 指向二维数组的指针(为什么有效)

javascript - 如何检查JavaScript数组中是否存在一个值,继续加0.15直到不存在,然后将该值添加到数组末尾?

javascript - 预定义函数 display() 时如何覆盖效果并更改数组每个元素中的效果

java - 碧 Jade 报告中的时区转换和日期格式?

java - 是否可以让一个类(class)有多个超类(class)?

java - 为什么 PowerMockito 没有正确模拟此类?

php - 十进制数的排序问题

java - 如何使用 Java-WS 缓存 WSDL

java - 如何阻止图像保存在 RAM 中,即使它们所代表的变量已分配给新图像

java - 在处理中无限循环保存文件