Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 康威';s的生命循环逻辑游戏_Java_Arrays_Conways Game Of Life - Fatal编程技术网

Java 康威';s的生命循环逻辑游戏

Java 康威';s的生命循环逻辑游戏,java,arrays,conways-game-of-life,Java,Arrays,Conways Game Of Life,当我运行程序时,我的输出在第一次运行while循环后保持不变。为什么每次通过后testgrid没有设置为copygrid的新值 循环直接位于注释//Run for x generations下 package assignment2; /** * @author jaw209 * Date: 2/24/14 * Purpose: Conway's Game of Life Program */ import java.io.*; import java.util.*; import ja

当我运行程序时,我的输出在第一次运行while循环后保持不变。为什么每次通过后testgrid没有设置为copygrid的新值

循环直接位于注释//Run for x generations下

package assignment2;
/**
 * @author jaw209
 * Date: 2/24/14
 * Purpose: Conway's Game of Life Program
 */
import java.io.*;
import java.util.*;
import javax.swing.*;

public class Assignment2 {

    private static int livecount;
    static char[][] copygrid = new char[30][30];


    public static void main(String[] args) throws FileNotFoundException {

        String inputfile;
        String output;
        String generations;
        char[][] testgrid = new char[30][30];
//Fills array copygrid with -        
        for(int i = 0; i < 30; i++){
            for(int x= 0; x < 30; x++){
                copygrid[i][x] = '-';
                }
            }

//Input file = L:\Java 2\Assignment2\Sample input.txt  

//Inputs from file to char array        
        inputfile = JOptionPane.showInputDialog ("Where is the input file?   Ex: C:\\users\\public\\desktop\\input.txt ");
        Scanner input = new Scanner (new FileReader(inputfile)); 
        char[] chararray = new char[904];
        String allvalues = null;
        do {
            String values = input.next();
            allvalues = allvalues + values;
        }
        while(input.hasNextLine());
        chararray = allvalues.toCharArray();

//Reads values in chararray into multidimensional array       
        char[][] grid1 =  new char[30][30]; 
        int i = 4;
        for(int row = 0; row < 30; row++){
            for(int col = 0; col < 30; col++){
                grid1 [row][col] = chararray[i]; 
                i++;
                    System.out.print(grid1[row][col]);
                        }
                    System.out.println();
                    }

//Finds how many generations should be calculated        
        generations = JOptionPane.showInputDialog ("How many generations should be calculated?");
        int gens = Integer.parseInt(generations);

//Prompts for output file
        output = JOptionPane.showInputDialog ("Where is the output file?");
        PrintWriter out = new PrintWriter(output); 

//Runs the cycle once      
        for (int row = 0; row < 30; row++){
            for (int col = 0; col < 30; col ++){
                if (status(grid1[row][col])){
                    liveSurrounding(grid1, row, col);
                    moves(row, col);
                }
                else if (!status(grid1[row][col])){
                    //run 3 checker
                  if (liveSurrounding(grid1, row, col) == 3){
                  copygrid[row][col] = 'X';
                  }     
                }

            }
        }
        System.out.println();
        System.out.print("Generation: 1");
        printCopy();

//Run again for x generations
        int count = 1;
        while (count <= gens){
        //copies old value of copygrid into new array    
            for(int e = 0; e < 30; e++){
                for(int f = 0; f < 30; f++){
                testgrid[e][f] = copygrid[e][f];
                }
            }  

       //Reset copy grid to blank
            for(int v = 0; v < 30; v++){
            for(int x= 0; x < 30; x++){
                copygrid[v][x] = '-';
                }
            }

       //Run through generation methods
            for (int row = 0; row < 30; row++){
            for (int col = 0; col < 30; col ++){
                if (status(testgrid[row][col])){
                    liveSurrounding(testgrid, row, col);
                    moves(row, col);
                }
                else if (!status(testgrid[row][col])){
                    //run 3 checker
                  if (liveSurrounding(testgrid, row, col) == 3){
                  copygrid[row][col] = 'X';
                  }     
                }
            }
        }

        System.out.println();
        int oneoff = count+1;
        System.out.print("Generation: " + oneoff);
        printCopy();
        count++;
        }

    }


//Check to see if cell is live or dead    
    public static boolean status(char value){  
            if (value == 'X'){
                return true;
            } else {
            return false;
            }
        }

//See if neighbor is alive or dead    
    public static int liveSurrounding(char [][] grid, int a, int b){

        livecount = 0;

        if (a > 0 && grid[a-1][b] == 'X'){
            livecount++;
        }
        if (a > 0 && b < grid.length - 1 && grid[a-1][b+1] == 'X'){
            livecount++;
        } 
        if (b < grid.length - 1 && grid[a][b+1] == 'X'){
            livecount++;
        }
        if (a < grid.length - 1 && b < grid.length - 1 && grid[a+1][b+1] == 'X'){
            livecount++;
        }
        if (a < grid.length - 1 && grid[a+1][b] == 'X'){
            livecount++;
        }
        if (a < grid.length - 1 && b > 0 && grid[a+1][b-1] == 'X'){
            livecount++;
        }
        if (b > 0 && grid[a][b-1] == 'X'){
            livecount++;
        }
        if (a > 0 && b > 0 && grid[a-1][b-1] == 'X'){
            livecount++;
        }
        else {
        grid[a][b] = '-';
        }
        return livecount;
    }   

//Adjust alive cells for each condition    
    public static char[][] moves(int a, int b){

        switch(livecount){
            case 0: copygrid[a][b] = 'X'; break;
            case 1: copygrid[a--][b] = 'X'; copygrid[a][b] = '-'; break;
            case 2: copygrid[a][b++] = 'X'; copygrid[a][b] = '-'; break;
            case 3: copygrid[a][b--] = 'X'; copygrid[a][b] = '-'; break;
            case 4: copygrid[a++][b] = 'X'; copygrid[a][b] = '-'; break;
            case 5: copygrid[a--][b++] = 'X'; copygrid[a][b] = '-'; break;
            case 6: copygrid[a++][b--] = 'X'; copygrid[a][b] = '-'; break; 
            case 7: copygrid[a--][b--] = 'X'; copygrid[a][b] = '-';  break;
            case 8: copygrid[a++][b++] = 'X';copygrid[a][b] = '-';  break;
            default:
        }

                    return copygrid;         
    }    

//method to print out formatted copygrid    
    public static void printCopy(){
for (int row = 0; row < 30; row++){
    System.out.println();
    for (int col = 0; col < 30; col++){
            System.out.print(copygrid[row][col]);
            }
        }
System.out.println();
    }

}
包分配2;
/**
*@author jaw209
*日期:2014年2月24日
*目的:康威的生命游戏计划
*/
导入java.io.*;
导入java.util.*;
导入javax.swing.*;
公共课堂作业2{
私有静态int-livecount;
静态字符[][]copygrid=新字符[30][30];
公共静态void main(字符串[]args)引发FileNotFoundException{
字符串输入文件;
字符串输出;
串代;
char[][]testgrid=new char[30][30];
//用-
对于(int i=0;i<30;i++){
对于(int x=0;x<30;x++){
copygrid[i][x]='-';
}
}
//输入文件=L:\Java 2\Assignment2\Sample Input.txt
//从文件到字符数组的输入
inputfile=JOptionPane.showInputDialog(“输入文件在哪里?例如:C:\\users\\public\\desktop\\input.txt”);
扫描仪输入=新扫描仪(新文件读取器(inputfile));
char[]chararray=新字符[904];
字符串allvalues=null;
做{
字符串值=input.next();
所有值=所有值+值;
}
while(input.hasNextLine());
chararray=allvalues.toCharArray();
//将字符数组中的值读入多维数组
char[][]grid1=新字符[30][30];
int i=4;
对于(int行=0;行<30;行++){
for(int col=0;col<30;col++){
grid1[row][col]=chararray[i];
i++;
系统输出打印(网格1[行][列]);
}
System.out.println();
}
//查找应计算多少代
generations=JOptionPane.showInputDialog(“应该计算多少代?”);
int gens=Integer.parseInt(代);
//提示输入输出文件
output=JOptionPane.showInputDialog(“输出文件在哪里?”);
PrintWriter out=新的PrintWriter(输出);
//循环运行一次
对于(int行=0;行<30;行++){
for(int col=0;col<30;col++){
if(状态(网格1[行][列]){
生活环境(网格1、行、列);
移动(行、列);
}
如果(!status(grid1[row][col]),则为else{
//运行3个检查器
if(livearounding(grid1,row,col)==3){
copygrid[row][col]=“X”;
}     
}
}
}
System.out.println();
系统输出打印(“生成:1”);
打印副本();
//再次运行x代
整数计数=1;
而(计数0和网格[a-1][b]='X'){
livecount++;
}
如果(a>0&&b0&&grid[a+1][b-1]=='X'){
livecount++;
}
如果(b>0&&grid[a][b-1]='X'){
livecount++;
}
如果(a>0&&b>0&&grid[a-1][b-1]=='X'){
livecount++;
}
否则{
网格[a][b]='-';
}
返回livecount;
}   
//针对每种情况调整活细胞
公共静态字符[][]移动(int a、int b){
交换机(livecount){
案例0:copygrid[a][b]=“X”;中断;
案例1:copygrid[a--][b]='X';copygrid[a][b]='-';break;
案例2:copygrid[a][b++]='X';copygrid[a][b]='-';break;
案例3:copygrid[a][b--]='X';copygrid[a][b]='-';break;
案例4:copygrid[a++][b]='X';copygrid[a][b]='-';break;
案例5:copygrid[a--][b++]='X';copygrid[a][b]='-';break;
案例6:copygrid[a++][b--]='X';copygrid[a][b]='-';break;
案例7:copygrid[a--][b--]='X';copygrid[a][b]='-';break;
案例8:copygrid[a++][b++]='X';copygrid[a][b]='-';break;
违约:
}
返回copygrid;
}    
//方法打印出格式化的copygrid
公共静态无效打印副本(){
对于(int行=0;行<30;行++){
System.out.println();
for(int col=0;col<30;col++){
系统输出打印(复制网格[行][列]);
}
}
System.out.println();
}
}

这可能不是问题的全部,但我对这段代码非常怀疑:

//Adjust alive cells for each condition
public static char[][] moves(int a, int b){

    switch(livecount){
        case 0: copygrid[a][b] = 'X'; break;
        case 1: copygrid[a--][b] = 'X'; copygrid[a][b] = '-'; break;
        case 2: copygrid[a][b++] = 'X'; copygrid[a][b] = '-'; break;
        case 3: copygrid[a][b--] = 'X'; copygrid[a][b] = '-'; break;
        case 4: copygrid[a++][b] = 'X'; copygrid[a][b] = '-'; break;
        case 5: copygrid[a--][b++] = 'X'; copygrid[a][b] = '-'; break;
        case 6: copygrid[a++][b--] = 'X'; copygrid[a][b] = '-'; break;
        case 7: copygrid[a--][b--] = 'X'; copygrid[a][b] = '-';  break;
        case 8: copygrid[a++][b++] = 'X';copygrid[a][b] = '-';  break;
        default:
    }

    return copygrid;
}
我建议编写一些测试,以确保它能做到您认为它能做到的事情。我想没有

我记得这个问题,一个细胞的下一个状态取决于它的当前状态和它自己的活邻居数。此代码似乎影响不同单元的状态,具体取决于特定单元的活动邻居计数。它还将大多数单元格设置为“X”,然后立即将它们再次设置为“-”


对于在代码中的此处和其他位置使用字段还是返回值,似乎也存在一些混淆。

这是您需要了解如何使用调试器的地方。然后逐行检查你的程序,看看发生了什么。有人在这里向你指出答案会让你失去这个。说真的,这很简单,而且可能是专业人士最重要的编程技能。学习如何使用funct是一个很好的起点