Help with my game of life program
I need help with my game of life program I have to write for an assignment. I need to create the game of life using an input called "testLab8.lif" and have the game of life run on a GUI. I have two classes, lifegrid (this has all the methods need to compute the game of life) and lifemain (this class has the main method inside it to run the program).
I'm a beginner to java and this is my first post on this website (or any forums site).
I'm having a problem with lifegrid class, I'm trying to call the run() method in the show() method so that the generations are printed using the show(). Here is lifegrid class: (I get an error saying "cannot find symbol:variable old/New/swap" it means for the line System.out.println("Generations: " + run(old, New, swap)); this line is in the show() method at the bottom.
Code Java:
import java.util.Scanner;
import javax.swing.*;
import java.io.File;
import java.io.FileNotFoundException;
public class lifegrid
{
int[][] grid;
lifegrid(int x, int y,String filename )throws FileNotFoundException {grid = new int[y][x]; File file = new File(filename);
Scanner scanner = new Scanner (file);
int i = 0;
int j = 0;
while (scanner.hasNextLine()){
while (scanner.hasNext()){
if (scanner.next().equals("*"))
grid[i][j] = 1;
else
grid[i][j] = 0;
i++;
}
j++;
}
}
public int getX() {return grid[0].length;}
public int getY() {return grid.length;}
public int getCell(int x, int y) {return grid[y][x];}
public void setCell(int x, int y, int v) {grid[y][x] = v;}
[I]//This method prints the initial grid and should print the generations (but I get an error with line run(old, New, swap)); [/I]
public void show()
{
for (int r = 0; r < getX(); r++){
for (int s = 0; s < getY(); s++)
System.out.println(grid[r][s]);
}
System.out.println("Generations: " + run(old,New,swap));
}
[I]//computes the neighbours[/I]
public static int neighbours(int x, int y, boolean[][] grid)
{
int count = 0;
if (x > 0){
if (grid[x-1][y])
count++;
}
if (x < x-1){
if (grid[x+1][y])
count++;
}
if (grid[x][y-1]){
count++;
}
if (grid[x][y+1]){
count++;
}
if ((x > 0) && (y > 0)){
if (grid[x-1][y-1])
count++;
}
if (x > 0){
if (grid[x-1][y+1])
count++;
}
if (x < x-1){
if (grid[x+1][y-1])
count++;
}
if ((x < 0) && (y < 0)){
if (grid[x+1][y+1])
count++;
}
return count;
}
[I]// This method uses the game of life rules to work out whether a cell is alive or dead and this method also prints the generations.
//The generations should be printed using the show() method[/I]
public void run(boolean[][] old, boolean[][] New, boolean[][] swap)
{
for (int x = 1; x < old.length - 1 ; x++){
for (int y = 1; y < old.length - 1; y++){
if (neighbours (x, y, old) == 3)
old[x][y] = true;
if (neighbours (x, y, old) < 2)
old[x][y] = true;
if (neighbours (x, y, old) > 3)
old[x][y] = false;
}
}
for (int i = 0; i < old.length; i++){
for (int j = 0; j < old.length; j++){
old[i][j] = New[i][j];
System.out.println("Generation: " + New[i][j]);
}
}
//Prints Generations
for (int x = 0; x < swap.length; x++){
for (int y = 0; y < swap.length; y++){
if (swap [x][y] = true){
System.out.print("[*]");
if (swap[x][y] = false){
System.out.print("[]");
}
}
}
}
}
}
Here is my other class lifemain:
Code Java:
import java.io.FileNotFoundException;
class lifemain
{
public static void main(String[] args)throws FileNotFoundException {
lifegrid life = new lifegrid(6, 10, "testLab8.lif");
life.show();
}
}
Here is link to see exactly what I need to do for the assignment. I am on part J.2 point 5: http://www.cs.rhul.ac.uk/Internal/Fo...S1801/ass2.pdf
Thanks very much in advance for the help really appreciated as I'm stressing out about this. :eek:
Re: Help with my game of life program
Please highlight your code using tags as found in my signature.
System.out.println("Generations: " + run(old,New,swap));
Now then, It can't find those symbols...why would that be? possibly because you haven't defined them anywhere at all in the code (par of-course as local method parameters)? :)
Re: Help with my game of life program
Hi,
Thanks for replying. Would I need to define them at the top of my program, where I have defined int[][] grid?
(Might sound stupid that question above, but I am very new to Java)
Thanks
An will do highlight code
Re: Help with my game of life program
What are those variables used for? Do any other methods need to see them?
Does their value need to be save past the end of the method where they are used?
If they are only used in one method, then define them inside of the method.
If other methods need to access them, then define them outside of the method as class variables.
Re: Help with my game of life program
The variables are use used to display the run() method but I not sure if is how to do it. (What I'm basically trying to with that command is to display my run() method using the show() method.
They need to be repeated to constantly show the different generations. They are used in one method and need to be called/accessed by another method in the same class.
__________________________________________________ __________________________________________________ _____________________________
I have changed the "lifegrid" class (below) as follows so that the run() method will be displayed using the show() method and ultimately the game of life should be displayed. Everything compiles fine but, nothing appears on the command line except blank lines. (Its like the program is running but nothing is displayed does anyone know why this happens.)
Code Java:
import java.util.Scanner;
import javax.swing.*;
import java.io.File;
import java.io.FileNotFoundException;
public class lifegrid
{
int[][] grid;
boolean[][] old;
boolean[][] New;
boolean[][] swap;
lifegrid(int x, int y,String filename )throws FileNotFoundException {grid = new int[y][x]; File file = new File(filename);
Scanner scanner = new Scanner (file);
int i = 0;
int j = 0;
while (scanner.hasNextLine()){
while (scanner.hasNext()){
if (scanner.next().equals("*"))
grid[i][j] = 1;
else
grid[i][j] = 0;
i++;
}
j++;
}
}
public int getX() {return grid[0].length;}
public int getY() {return grid.length;}
public int getCell(int x, int y) {return grid[y][x];}
public void setCell(int x, int y, int v) {grid[y][x] = v;}
public void show()
{
for (int r = 0; r < getX(); r++){
for (int s = 0; s < getY(); s++)
System.out.println(grid[r][s]);
}
System.out.println("Generations: " + run(old,New,swap));
}
public static int neighbours(int x, int y, boolean[][] grid)
{
int count = 0;
if (x > 0){
if (grid[x-1][y])
count++;
}
if (x < x-1){
if (grid[x+1][y])
count++;
}
if (grid[x][y-1]){
count++;
}
if (grid[x][y+1]){
count++;
}
if ((x > 0) && (y > 0)){
if (grid[x-1][y-1])
count++;
}
if (x > 0){
if (grid[x-1][y+1])
count++;
}
if (x < x-1){
if (grid[x+1][y-1])
count++;
}
if ((x < 0) && (y < 0)){
if (grid[x+1][y+1])
count++;
}
return count;
}
public boolean[][] run(boolean[][] old, boolean[][] New, boolean[][] swap)
{
for (int x = 1; x < old.length - 1 ; x++){
for (int y = 1; y < old.length - 1; y++){
if (neighbours (x, y, old) == 3)
old[x][y] = true;
if (neighbours (x, y, old) < 2)
old[x][y] = true;
if (neighbours (x, y, old) > 3)
old[x][y] = false;
}
}
for (int i = 0; i < old.length; i++){
for (int j = 0; j < old.length; j++){
old[i][j] = New[i][j];
}
return New;
}
//Prints Generations
for (int x = 0; x < swap.length; x++){
for (int y = 0; y < swap.length; y++){
if (swap[x][y] = true){
System.out.print("[*]");
if (swap[x][y] = false){
System.out.print("[]");
}
}
}
return swap;
}
return old;
}
}
BTW this is what I am trying to do:5. Add a method run() which computes successive generations and prints
them using your show() method. You will need to maintain two grids:GUI interface 191
the original and the new. We suggest you have three grid variables old,
new and swap and two actual grids which are swapped at the end of each
generation.
Re: Help with my game of life program
Quote:
nothing appears on the command line except blank lines.
You should be seeing something besides blank lines.
Add some text to your print outs to see what they are doing. Something like this.
System.out.println("r=" + r + ", s=" + s + ", grid=" + grid[r][s]);
Add some more printlns to show where the execution is going.
Re: Help with my game of life program
Would anyone know I can incorporate a GUI into my program so that the gameoflife is shown/pops up in a separate window (as a GUI).
Thanks for the help you've given me norm I appreciate it.
Re: Help with my game of life program
A GUI starts with a JFrame. Then you add panels to that and and more components to those panels.
Re: Help with my game of life program
I have writen the game of life algorithm but I just need to add the GUI. I've tryed to add the GUI using a JFrame but I get errors saying cannot find symbol errors.
Code Java:
class TFrame
extends JFrame
{
TFrame()
{
setTitle("Game of Life");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
}
I Added this piece of code above and get a cannot find symbol error.
Code Java:
import java.util.Scanner;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.Color;
import java.io.File;
import java.io.FileNotFoundException;
import java.awt.*;
import java.awt.event.*;
public class life {
public int[][] grid;
public int[][] grid2;
int currentGeneration = 0;
int x, y;
String filename;
public int count = 0;
public static void main(String[] args) throws FileNotFoundException {
life lifegrid = new life(6, 10, "testLab8.lif");
lifegrid.show();
}
life(int x, int y, String filename) throws FileNotFoundException {
this.grid = new int[x][y];
File file = new File(filename);
Scanner keyboard = new Scanner(file);
String line;
int lines = 0;
while (keyboard.hasNextLine()) {
line = keyboard.nextLine();
int i = 0;
for (i = 0; i < line.length(); i++) {
if (line.charAt(i) == '*') {
grid[lines][i] = 1;
} else {
grid[lines][i] = 0;
}
}
lines++;
}
}
public int getX() {return grid[0].length;}
public int getY() {return grid.length;}
public int getCell(int y, int x) {return grid[x][y];}
public void setCell(int y, int x, int v) {grid[x][y] = v;}
public void show()
{
for (int x = 0; x < getY(); x++) {
for (int y = 0; y < getX(); y++) {
System.out.print(grid[x][y]);
/* if (getCell(x, y) == 1){
livingCells.setColor(Color.black);
livingCells.fillRect(x * 4, y * 4, 4, 4);
}
else {
livingCells.setColor(Color.white);
livingCells.fillRect(x * 4, y * 4, 4, 4);
}*/
}
}
}
public int neighbours(int x, int y) {
int count = 0;
if (x > 0){
if (grid[x][y] == grid[x-1][y])
count++;
}
if (x < x-1){
if (grid[x][y] == grid[x+1][y])
count++;
}
if (grid[x][y] == grid[x][y-1]){
count++;
}
if (grid[x][y] == grid[x][y+1]){
count++;
}
if ((x > 0) && (y > 0)){
if (grid[x][y] == grid[x-1][y-1])
count++;
}
if (x > 0){
if (grid[x][y] == grid[x-1][y+1])
count++;
}
if (x < x-1){
if (grid[x][y] == grid[x+1][y-1])
count++;
}
if ((x < 0) && (y < 0)){
if (grid[x][y] == grid[x+1][y+1])
count++;
}
return count;
}
public void run()
{
for (int x = 1; x < getY(); x++){
for (int y = 1; y < getX(); y++){
if (neighbours (y, x) == 3){
if (neighbours (y, x) < 2 || neighbours(x, y) > 3){
setCell(y, x, 1);
}
else {
setCell(x, y, 0);
}
}
else {
if (neighbours(x, y) ==3) {
setCell(x, y, 1);
}
else {
setCell(x, y, 0);
}
}
}
}
for (int i = 0; i < getY(); i++) {
for (int j = 0; j < getX(); j++){
grid[y][x] = grid2[y][x];
}
}
currentGeneration++;
}
}
Re: Help with my game of life program
Post the full and exact error message you're receiving, along with the code it is referring to.
Re: Help with my game of life program
Quote:
I get errors saying cannot find symbol
Please copy and paste here the full text of the error messages if you want help with them.