简体   繁体   English

计数邻居时Conway的生活游戏越界-Java

[英]Conway's game of life out of boundaries when counting neighbours - java

I am trying to create the game of life in java but I have difficulty writing the part that checks the number of neighbours. 我正在尝试用Java创建生活游戏,但是我很难编写检查邻居数量的部分。 I understand that the problem is when the program gets to the edges of the grid it won't work because the indexes are greater/smaller than the bounds of the array. 我知道问题是当程序到达网格的边缘时它将无法工作,因为索引大于/小于数组的边界。 So the problem is in my Neighbours(). 所以问题出在我的Neighbours()中。 I am not sure how to fix it, I tried expanding the if statements and I also tried putting the whole set of statements in a while loop. 我不确定如何解决它,我尝试扩展if语句,还尝试将整个语句放入while循环中。 The program seems to be working unless there are live cells at the edges of the grid. 除非网格边缘没有活动单元格,否则该程序似乎正在运行。 Any suggestions on this? 有什么建议吗? Thanks in advance. 提前致谢。

import java.io.*;
import java.util.Scanner;

public class LifeGrid
{
  public int[][] grid;
  public int[][] newgrid;
  public int getX() 
  {
    return grid[0].length;
  }
  public int getY() 
  {
    return grid.length;
  }

  public int getcurrentgen()
  {
    return currentgen;
  }

  public int currentgen=0;


  // modify neighbours out of boundary problem.
  int Neighbours(int x, int y)
  {
    int neighbours = 0;

      if (grid[y][x-1] == 1)
      { neighbours++; }
      if (grid[y][x+1] ==1)
      { neighbours++; }
      if (grid[y+1][x-1] ==1)
      { neighbours++; }
      if (grid[y+1][x+1] ==1)
      { neighbours++; }
      if (grid[y+1][x] ==1)
      { neighbours++; }
      if (grid[y-1][x-1] ==1)
      { neighbours++; }
      if (grid[y-1][x+1] ==1)
      { neighbours++; }
      if (grid[y-1][x] ==1)
      { neighbours++; }


    return neighbours;
  }

  public LifeGrid(int x, int y, String filename)
  {
    grid = new int [y][x];
    newgrid = new int[y][x];
    File input = new File(filename);
    Scanner sc;
    try
    {
      sc = new Scanner(input);
    }
    catch (FileNotFoundException e)
    { 
      System.out.println("File error");
      return;
    }
    for ( y=0; y< getY(); y++)
    {
      String line = sc.nextLine();

      for( x = 0; x < getX(); x++)
      {
    if (line.charAt(x) == '*')
    {
      grid[y][x] = 1;
    }
    else 
    {
      grid[y][x] = 0;
    }
      }
    }
   }

   public void run()
   {
    show();
    while(getcurrentgen() < 3)
    {

      setup();
      grid = newgrid;
      currentgen++;
      show();
    }
   }

   public void setup()
   {
    for (int y = 0; y < getY(); y++)
    {
    for (int x = 0;x < getX();x++)
    {

      if (grid[y][x]== 1)
      {
    if (Neighbours(x,y) < 2)
    {
      newgrid[y][x] = 0;
    }
    if (Neighbours(x,y) > 3)
    {
      newgrid[y][x] = 0;
    }
    if (Neighbours(x,y) == 3 || Neighbours(x,y) == 2)
    {
      newgrid[y][x] = 1;
    }
      }
      if(grid[y][x]==0)
      {
        if(Neighbours(x,y) == 3)
       {
        newgrid[y][x]= 1;
       }

      }


    }

   }
   }

   public void show()
   {
    for(int y =0; y < getY(); y++)
    {
      for(int x = 0; x < getX(); x++)
      {
    System.out.print(grid[y][x]);
      }
      System.out.println();
    }
    System.out.println("Current generation: "+getcurrentgen());
   }


}

you need to add checks for all your points to make sure they are not on boundary. 您需要为所有点添加检查以确保它们不在边界上。 This means checking for both x and y coordinates: 这意味着要同时检查x和y坐标:

    if (x > 0 && grid[y][x - 1] == 1) {
        neighbours++;
    }
    if (x < grid[y].length - 1  && grid[y][x + 1] == 1) {
        neighbours++;
    }
    if (x > 0 && y < grid.length - 1 && grid[y + 1][x - 1] == 1) {
        neighbours++;
    }
    if (x < grid[y].length - 1 && y < grid.length - 1 && grid[y + 1][x + 1] == 1) {
        neighbours++;
    }
    if (y < grid.length - 1 && grid[y + 1][x] == 1) {
        neighbours++;
    }
    if (x > 0 && y > 0 && grid[y - 1][x - 1] == 1) {
        neighbours++;
    }
    if (y > 0 && x < grid[y].length - 1 && grid[y - 1][x + 1] == 1) {
        neighbours++;
    }
    if (y > 0 && grid[y - 1][x] == 1) {
        neighbours++;
    }

int Neighbours(int x, int y) is called with x=0 and y=0, right? int Neighbours(int x, int y)用x = 0和y = 0调用int Neighbours(int x, int y) ,对吗?

How do you then evaluate grid[y-1][x-1] ? 然后如何评估grid[y-1][x-1]

Where you have 你在哪里

if (grid[y][x-1] == 1)

You just need to skip if this would go out of bounds: 如果超出范围,您只需要跳过:

if (x > 0 && grid[y][x-1] == 1)

And similar for all of the others. 其他所有类似。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM