0
프로그램을 실행하면 while 루프가 처음 실행 된 후에도 출력이 그대로 유지됩니다. 각 패스 후에 testgrid가 copygrid의 새 값으로 설정되지 않는 이유는 무엇입니까?Conway의 Life of Life 루프 논리
루프이 아닌 전체 문제 가능성이
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();
}
}
이것은 디버거를 사용하여 읽어야 할 부분입니다. 그런 다음 프로그램을 한 줄씩 살펴보고 무슨 일이 일어나는지 확인하십시오. 누군가 여기서 당신에게 답을 알려주는 것은 당신을 빼앗을 것입니다. 진지하게, 그것은 쉽고 아마도 전문가를위한 가장 중요한 프로그래머 기술입니다. – Kon
시작하기 좋은 곳은 함수 사용법을 배우는 것입니다. 그것은 당신의 삶을 훨씬 쉽게 만들어 줄 것이고, 그것은 당신의 코드를 훨씬 쉽게 읽을 수있게 할 것입니다. –
디버거를 사용하는 것이 좋은 팁이지만 일부 변수/디버그 메시지 만 인쇄하면 시작할 수 있습니다. 그리고 실제로 함수를 사용해야합니다. 재사용이 편리합니다. – keyser