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);
}
}
}
}
Sorry about not putting in all the code.
Any help is greatly appreciated.
I have figured out the issue thanks to the help of all your answers. For whatever reason, Eclipse wasn't reading that the files were in the folder with the project, so I simply deleted and readded the files (in the same place mind you) and all of a sudden it started working again.
Now my current issue is to figure out why my program isn't proceeding onto the next generation of life.
Specifically, I need to be able to ask the user if they want to continue onto the next generation of life.
The output should be Do you want to go to the next generation? Type y for yes, type n to quit the program.