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都没有读到文件位于项目文件夹中,所以我只是删除并读取文件(在同一个地方提醒你),突然间它再次开始工作。
现在我的问题是找出为什么我的计划没有进入下一代生活。
具体来说,我需要能够询问用户是否想继续下一代生活。
输出应为Do you want to go to the next generation? Type y for yes, type n to quit the program.
答案 0 :(得分:1)
你的部分问题是你正在捕捉一般异常,所以任何事情都可能出错。您应该从异常中打印堆栈跟踪以帮助调试此问题,因为它将为您提供有关错误实际位置的更准确信息。即,是打开文件还是从打开的文件创建扫描程序。希望你看到为什么不应该抓住一般例外。
请注意,这只是 用于调试,因为打印堆栈跟踪是not good general practice。
答案 1 :(得分:1)
文件必须位于目录项目中。 您会看到错误消息替换您的来源:
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
答案 2 :(得分:0)
您可能需要提供文件的完整路径。表示文件在系统中的确切位置。
此外,将文件作为项目中的资源会更容易。这样,您可以轻松地将其作为资源加载为名称
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream is = classloader.getResourceAsStream("test.txt");