gpt4 book ai didi

java - 我的 Game of Life 程序文件阅读器出现问题

转载 作者:行者123 更新时间:2023-12-02 04:16:23 25 4
gpt4 key购买 nike

 package edu.bsu.cs121.mamurphy;

import java.util.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;

// Maurice Murphy
// CS121
// 10/17/15

public class GameOfLifeMain extends JFrame {

// Intitial reading and printing of the world

public GameOfLifeMain() {
super("Game of Life");
setSize(600, 445);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(1, 2));
Test test1 = new Test();
test1.setBackground(Color.LIGHT_GRAY);
panel.add(test1);
setContentPane(panel);
setVisible(true);
Temp one = new Temp(test1);
one.start();
}

public static void main(String[] asd) {
new GameOfLifeMain();
System.out.println();

}
}

class Temp extends Thread {
Test anim;

public Temp(Test anim) {
this.anim = anim;

}

public void run()// for each instance of test begin will be executed
{
anim.begin();
}
}

class Test extends JPanel

{
final static int numOfRow = 25, numOfCol = 75;
final static char DOT = '.';
static char[][] grid = new char[numOfRow + 2][numOfCol + 2];
static char[][] nextgrid = new char[numOfRow + 2][numOfCol + 2];
boolean sameFlag;
boolean blankFlag;

public static void init(char[][] grid, char[][] nextgrid) {
for (int numOfRow = 0; numOfRow <= numOfRow + 1; numOfRow++) {
for (int numOfCol = 0; numOfCol <= numOfCol + 1; numOfCol++) {
grid[numOfRow][numOfCol] = DOT;
nextgrid[numOfRow][numOfCol] = DOT;
}
}
}

public static void pause() {
try {
Thread.currentThread();
Thread.sleep(1000L);
} catch (InterruptedException e) {
}
}

public void begin() {
init(grid, nextgrid);
read(grid);
repaint(); // calls paintComponent
pause();
while (sameFlag == true && blankFlag == false) {
nextGen(grid, nextgrid);
}
}

public static void read(char[][] grid) {
Scanner world = new Scanner(System.in);
System.out.println("Type the file name of the world you'd like to create.");
String fileName = world.nextLine();
{
try {
world = new Scanner(new File(fileName));
} catch (Exception ex) {
System.out.println("Please insert a valid file name.");

}
;

for (int numOfRow = 1; numOfRow <= numOfRow; numOfRow++) {
String s = world.next();
for (int numOfCol = 1; numOfCol <= numOfCol; numOfCol++) {
grid[numOfRow][numOfCol] = s.charAt(numOfCol - 1);
}

}
}
}

public void print(Graphics g) {
int x, y;
y = 20;
for (int numOfRow = 1; numOfRow <= numOfRow; numOfRow++) {
x = 20;
for (int numOfCol = 1; numOfCol <= numOfCol; numOfCol++) {
g.drawString("" + grid[numOfRow][numOfCol], x, y);
x = x + 7;
}
y = y + 15;
}
}

public static int neighbors(char[][] grid, int r, int c) {
// counts the # of closest neighbors that are X's
int count = 0;
for (int numOfRow = r - 1; numOfRow <= r + 1; numOfRow++) {
for (int numOfCol = c - 1; numOfCol <= c + 1; numOfCol++) {
if (grid[numOfRow][numOfCol] == 'X') {
count++;
}
}
}
if (grid[r][c] == 'X') {
count = count - 1;
}
return count;
}

public static void nextGen(char[][] grid, char[][] nextgrid) {
for (int numOfRow = 1; numOfRow <= numOfRow; numOfRow++) {
for (int numOfCol = 1; numOfCol <= numOfCol; numOfCol++) {
if (grid[numOfRow][numOfCol] == 'X') {
int count = 0;
{
if (grid[numOfRow][numOfCol - 1] == 'X')
count = count + 1;
if (grid[numOfRow][numOfCol + 1] == 'X')
count = count + 1;
if (grid[numOfRow - 1][numOfCol] == 'X')
count = count + 1;
if (grid[numOfRow - 1][numOfCol - 1] == 'X')
count = count + 1;
if (grid[numOfRow - 1][numOfCol + 1] == 'X')
count = count + 1;
if (grid[numOfRow + 1][numOfCol - 1] == 'X')
count = count + 1;
if (grid[numOfRow + 1][numOfCol] == 'X')
count = count + 1;
if (grid[numOfRow + 1][numOfCol + 1] == 'X')
count = count + 1;
}

if (count == 2 || count == 3) {
nextgrid[numOfRow][numOfCol] = 'X';
} else
nextgrid[numOfRow][numOfCol] = DOT;
}
if (grid[numOfRow][numOfCol] == DOT) {
int count1 = 0;
{
if (grid[numOfRow][numOfCol - 1] == 'X')
count1 = count1 + 1;
if (grid[numOfRow][numOfCol + 1] == 'X')
count1 = count1 + 1;
if (grid[numOfRow - 1][numOfCol] == 'X')
count1 = count1 + 1;
if (grid[numOfRow - 1][numOfCol - 1] == 'X')
count1 = count1 + 1;
if (grid[numOfRow - 1][numOfCol + 1] == 'X')
count1 = count1 + 1;
if (grid[numOfRow + 1][numOfCol - 1] == 'X')
count1 = count1 + 1;
if (grid[numOfRow + 1][numOfCol] == 'X')
count1 = count1 + 1;
if (grid[numOfRow + 1][numOfCol + 1] == 'X')
count1 = count1 + 1;
}
if (count1 == 3)
nextgrid[numOfRow][numOfCol] = 'X';
}
}
}
}

public static void copy(char[][] grid, char[][] nextgrid) {
for (int i = 0; i < numOfRow + 1; i++) {
for (int j = 0; j < numOfCol + 1; j++) {
grid[i][j] = nextgrid[i][j];
}
}
}

public static boolean isEmpty(char[][] grid, char[][] nextgrid) {
boolean blankFlag = true;
for (int i = 0; i < numOfRow + 1; i++) {
for (int j = 0; j < numOfCol + 1; j++) {
if (grid[i][j] != DOT) {
blankFlag = false;
}
}
}
return blankFlag;
}

public static boolean isSame(char[][] grid, char[][] nextgrid) {
boolean sameFlag = false;
for (int i = 0; i < numOfRow + 1; i++) {
for (int j = 0; j < numOfCol + 1; j++) {
if (grid[i][j] == nextgrid[i][j]) {
sameFlag = true;
break;
}
}
}
return sameFlag;
}

public void paintComponent(Graphics g) {
super.paintComponent(g);// erases panel Contents
g.setColor(Color.black);
if (sameFlag == false && blankFlag == false) {
print(g);// or whatever method you use to print the world
} else {
if (sameFlag == true) {
g.drawString("The worlds are repeating!", 10, 250);
}
if (blankFlag == true) {
g.drawString("The world is blank!", 10, 250);
}
}
}

}

抱歉没有输入所有代码。

非常感谢任何帮助。

在您所有答案的帮助下,我已经解决了这个问题。不管出于什么原因,Eclipse 没有读取到这些文件位于项目的文件夹中,所以我只是删除并读取了这些文件(请注意,在同一个位置),突然间它又开始工作了。

现在我当前的问题是弄清楚为什么我的程序没有进入下一代生命。

具体来说,我需要能够询问用户是否想继续下一代的生活。

输出应该是你想进入下一代吗?输入 y 表示"is",输入 n 退出程序。

最佳答案

您的部分问题是您正在捕获一般异常,因此任何事情都可能出错。您应该打印异常中的堆栈跟踪以帮助调试此问题,因为它将为您提供有关错误实际位置的更准确信息。即,是打开文件还是从打开的文件创建扫描仪。希望您明白为什么不应该捕获一般异常。

请注意,这用于调试,因为打印堆栈跟踪是 not good general practice

关于java - 我的 Game of Life 程序文件阅读器出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33267426/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com