2017-03-25 13 views
-1

私は、今何時間も正しく動作し、何らかの理由で私がちょうどうんざりしているこのゲームを、runLife()クラスまたはcountNeighbors()クラスのいずれかにしようとしています。問題は、構文ではなく、いくつかの論理的なエラーが発生したこと、私は問題Conwayのライフロジックのゲーム

..*.. 
..*.. 
..*.. 

に戻るのではなく、その後

..... 
.***. 
..... 

とを取得するとき、私の入力テスト・ケースなど

..*.. 
..*.. 
..*.. 

それはなります

..**. 
.*.*. 
..... 

私のコードは次のとおりです。

import java.util.Scanner; 
import java.io.*; 
public class GameOfLife 
{ 
//variables used, 24 and 80 are the default size. 
private int rows = 24; 
private int cols = 80; 
private String [][] current = new String[rows][cols]; 
private String [][] next = new String[rows][cols]; 
// primary constructor that takes in 2 integers and sets the rows and cols to them 
public GameOfLife(int r, int c) 
{ 
    rows = r; 
    cols = c; 
    if(r<0){ 
     rows = rows * -1; 
    } 
    if(c<0){ 
     cols = cols * -1; 
    } 
    if (r==0||c==0){ 
     System.err.print("Incorrect parameters, setting to default"); 
     rows = 24; 
     cols = 80; 
    } 
    for (int x = 0; x <= rows - 1; x++){ 
     for (int y = 0; y <= cols - 1; y++){ 
      int q = (int)(Math.random()*100); 
      if(q%2 == 0){ 
       current[x][y]="dead"; 
      } 
      else{ 
       current[x][y]="alive";} 
     } 
    } 
} 
// secondary constructor that takes in no parameters 
public GameOfLife() 
{ 
    for (int x = 0; x <= rows - 1; x++){ 
     for (int y = 0; y <= cols - 1; y++){ 
      int r = (int)(Math.random()*100); 
      if(r%2 == 0){ 
       current[x][y]="dead"; 
      } 
      else{ 
       current[x][y]="alive";} 
     } 
    } 
} 

//constructor that takes in a string double array in the graphical style as the parameter to make as the first generation 
public GameOfLife(String[][] s){ 
    rows = s.length; 
    cols = s[0].length; 
    for (int x = 0; x <= rows - 1; x++){ 
     for (int y = 0; y <= cols - 1; y++){ 
      if (s[x][y].equals("*")){ 
       current[x][y]="alive";} 
      else {current[x][y]="dead";} 
     } 
    } 
} 

//uses the game rules to run the game and progress to the next generation 
public void runLife(){ 
    //loops and checks neighbors to abide by the rules of the game 
    for (int x = 0; x <= rows - 1; x++){ 
     for (int y = 0; y <= cols - 1; y++){ 
      //checks the alive rules for each one 
      int p=countNeighbors(x,y); 
      if(p<=1){next[x][y]="dead";} 
      if(p==2){ 
       if(current[x][y].equals("alive")){ 
        next[x][y]="alive";} 
       else{next[x][y]="dead";}} 
      if(p==3){next[x][y]="alive";} 
      if(p>3){next[x][y]="dead";} 
     } 
    } 
    current = next; 
} 
//counts the neighbors of a specified location 
public int countNeighbors(int row, int col){ 
    int q=0; 
    for (int x = row -1; x <= row + 1 ; x++) 
    { 
     for (int y = col -1 ; y <= col + 1 ; y++) 
     { 
      boolean go=true; 
      if((x < 0 || y < 0)){go=false;} 
      if(x>=rows){go=false;} 
      if(y>=cols){go=false;} 
      if ((x == row) && (y==col)){ go=false;} 
      if(go==true){if(current[x][y].equals("alive")){q++;}} 

     } 
    } 
    return q; 
} 
//makes the array into a string of graphics that is a good visualization of the code in work 
public String toGraphic(){ 
    String s = ""; 
    for(int x = 0; x <= rows - 1; x++){ 
     for(int y = 0; y <= cols - 1; y++){ 
      if(current[x][y].equals("alive")){ 
       s=s+"*"; 
      } 
      else{ 
       s=s+" ";} 
     } 
     s= s+"\n"; 
    } 
    return s; 
} 
//continuously prints out the toGraphic option to visualise what happens every pass 
public void animate(int t, int o){ 
    //checking for incorrect parameters 
    if(t<0){ 
     t = t * -1; 
    } 
    if(o<0){ 
     o = o * -1; 
    } 
    if(o==0){ 
     System.err.print("You have selected no amount"); 
    } 
    //looping the toGraphic method a set amount of times 
    int q=0; 
    while(q<=t){ 
     long start = System.currentTimeMillis(); 
     long end = 0; 
     System.out.println(toGraphic()); 
     while ((end-start)<=o){ 
      end = System.currentTimeMillis(); 
     } 
     q++; 
     runLife(); 
    } 
} 
} 

答えて

0

runLifeは、第一世代のために働くcurrent、に基づいてnextを埋めます。しかし、将来の世代ごとにnextにあるものをcurrentにコピーする必要があります。

ステートメントcurrent = nextは、内容が同じ2つの異なる配列ではなく、同じ配列になります。

関連する問題