Reputation:
There are no errors showing up in eclipse, but when I go to run the code, nothing happens in the application. It is broken up into three classes. The first class contains the if statements. I am wondering if my issue lies here.
package course.infsci0017.lab04;
import java.util.Scanner;
import javax.swing.JFrame;
public class GameOfLife {
public static void main(String[] args) {
int testCase = 1;
JFrame frame = new JFrame();
frame.setSize(500, 500);
frame.setTitle("Conway's Game of Life");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Cell[][] universe = new Cell[100][100];
for (int x = 0; x < universe.length-1; x++) {
for (int y = 0; y < universe[x].length-1; y++) {
universe[x][y] = new Cell();
}
}
if (testCase == 1) {
universe[3][2].calculateNext(3);
universe[3][2].updateCurrent();
universe[3][3].calculateNext(3);
universe[3][3].updateCurrent();
universe[3][4].calculateNext(3);
universe[3][4].updateCurrent();
} else if (testCase == 2) {
universe[49][50].calculateNext(3);
universe[49][50].updateCurrent();
universe[49][51].calculateNext(3);
universe[49][51].updateCurrent();
universe[50][49].calculateNext(3);
universe[50][49].updateCurrent();
universe[50][50].calculateNext(3);
universe[50][50].updateCurrent();
universe[51][50].calculateNext(3);
universe[51][50].updateCurrent();
} else {
}
UniverseComponent component = new UniverseComponent(universe);
frame.add(component);
frame.setVisible(true);
Scanner in = new Scanner(System.in);
String input = in.nextLine();
while (input.length() == 0) {
int neighborCount = 0;
for (int x=1; x<universe.length-2; x++) {
for (int y=1; y<universe[x].length-2; y++) {
neighborCount = 0;
if (universe[x-1][y-1].isAlive()) {
neighborCount++;
}
if (universe[x-1][y].isAlive()) {
neighborCount++;
}
if (universe[x-1][y+1].isAlive()) {
neighborCount++;
}
if (universe[x][y-1].isAlive()) {
neighborCount++;
}
if (universe[x][y+1].isAlive()) {
neighborCount++;
}
if (universe[x+1][y-1].isAlive()) {
neighborCount++;
}
if (universe[x+1][y].isAlive()) {
neighborCount++;
}
if (universe[x+1][y+1].isAlive()) {
neighborCount++;
}
universe[x][y].calculateNext(neighborCount);
}
}
for (int x=1; x<universe.length-2; x++) {
for (int y=1; y<universe[x].length-2; y++) {
universe[x][y].updateCurrent();
}
}
component.repaint();
input = in.nextLine();
}
in.close();
}
}
Class 2
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;
public class UniverseComponent extends JComponent {
private Cell[][] universe;
public UniverseComponent(Cell[][] universe) {
super();
this.universe = universe;
}
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
for (int x=0; x<universe.length-1; x++) {
for (int y=0; y<universe[x].length-1; y++) {
if (universe[x][y].isAlive()) {
g2.fillRect(x*5, y*5, 4, 4);
}
}
}
}
}
Class 3
public class Cell {
private boolean current;
private boolean next;
public Cell() {
current = false;
next = false;
}
public Cell(int neighborCount) {
calculateNext(neighborCount);
updateCurrent();
}
public void calculateNext(int neighborCount) {
if (current) {
if ((neighborCount < 2) || (neighborCount > 3)) {
next = false;
} else {
next = true;
}
} else {
if (neighborCount == 3) {
next = true;
} else {
next = false;
}
}
}
public void updateCurrent() {
current = next;
}
public boolean isAlive() {
return current;
}
}
If any one has any idea what how to fix this please let me know. Feel free to copy and paste code if necessary. It will not let me post an image because i do not have enough reputation. Apologies
Upvotes: 0
Views: 420
Reputation: 4258
Your while loop says
while (input.length() == 0)
But if you type anything into the input at all, it will not be equal to zero, so the loop will not be executed and your program will exit.
The program seems to be designed to only allow the user to hit 'enter' without typing any words at the console. That is, the loop will be executed if you type nothing but enter, otherwise the program will exit.
This is obviously not your own code, since it doesn't appear to have a bug after all but your usage of it is incorrect. Please be more honest with your questions (and I hope you haven't been plagiarising).
Upvotes: 1