在计算邻居时,康威的生命游戏超出界限 - java

时间:2014-11-21 20:58:32

标签: java

我正在尝试用java创建生活游戏,但我很难编写检查邻居数量的部分。我知道问题是当程序到达网格的边缘时它不会起作用,因为索引大于/小于数组的边界。所以问题在于我的邻居()。我不确定如何解决它,我尝试扩展if语句,我也尝试将整个语句集合放在while循环中。除非网格边缘有活细胞,否则该程序似乎正在工作。有什么建议吗?提前谢谢。

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


}

3 个答案:

答案 0 :(得分:2)

您需要为所有积分添加检查,以确保它们不在边界上。这意味着要检查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++;
    }

答案 1 :(得分:1)

调用

int Neighbours(int x, int y)时x = 0且y = 0,对吗?

您如何评估grid[y-1][x-1]

答案 2 :(得分:1)

你在哪里

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

如果这超出界限,你只需要跳过:

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

与所有其他人类似。