2014-11-21 105 views
1

我想在java中創建生活的遊戲,但我有困難編寫檢查鄰居數量的部分。我明白,問題是當程序到達網格邊緣時它將無法工作,因爲索引比數組的邊界更大/更小。所以問題出在我的鄰居()。我不知道如何解決這個問題,我嘗試擴展if語句,並且還嘗試將整個語句集放在while循環中。該程序似乎正在工作,除非在網格邊緣有活細胞。對此有何建議?提前致謝。康威的生活遊戲的邊界計數鄰居 - java

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


} 
+0

看看這篇文章,[約翰·康威在的Java Swing生命遊戲(http://java-articles.info/articles/?p=504 ),看看生活是如何計算在邊界上的。 – 2014-11-21 21:03:52

回答

1

int Neighbours(int x, int y)被稱爲x = 0和y = 0,對吧?

那麼您如何評估grid[y-1][x-1]

+0

謝謝,我有那部分工作。現在我越來越奇怪的結果。 「生育」規則並不總是按預期的那樣工作。你能給我一個提示嗎? – Croatoan 2014-11-21 22:36:40

+0

@Croatoan我猜生日問題與newgrid和grid都有關,它們都指向內存中的同一個數組。需要將網格中的數據複製到newgrid中的每一代新數組中。 – Andreas 2015-04-23 14:44:54

1

如果你有

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

你只需要跳過這是否會出界:

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

而對所有其他的類似。

+0

我曾嘗試過,沒有工作。現在再次嘗試,它的工作......謝謝 – Croatoan 2014-11-21 22:01:28

1

你需要添加檢查所有點,以確保他們不在邊界上。這意味着檢查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++; 
    }