Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Help with my game of life program

  1. #1
    Junior Member thatni**a's Avatar
    Join Date
    Dec 2011
    Location
    London
    Posts
    5
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

    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:

    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.
    Last edited by thatni**a; January 4th, 2012 at 06:57 PM.


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default 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)?
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Junior Member thatni**a's Avatar
    Join Date
    Dec 2011
    Location
    London
    Posts
    5
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.

  5. #5
    Junior Member thatni**a's Avatar
    Join Date
    Dec 2011
    Location
    London
    Posts
    5
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.)
    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.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with my game of life program

    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.

  7. #7
    Junior Member thatni**a's Avatar
    Join Date
    Dec 2011
    Location
    London
    Posts
    5
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.

  9. #9
    Junior Member thatni**a's Avatar
    Join Date
    Dec 2011
    Location
    London
    Posts
    5
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.
    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.
    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++;
        }    
    }

  10. #10
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default 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.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with my game of life program

    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.

Similar Threads

  1. Can't run Game of Life programm
    By cutekill0 in forum What's Wrong With My Code?
    Replies: 24
    Last Post: September 13th, 2011, 08:30 AM
  2. Conways Game of Life. Long Death option. Please help..
    By byako in forum What's Wrong With My Code?
    Replies: 8
    Last Post: March 9th, 2011, 08:49 AM
  3. Read in file and store in 2D array start of The Game of Life
    By shipwills in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 2nd, 2011, 09:52 AM
  4. Game of Life GUI Error
    By Lavace in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 3rd, 2011, 09:15 AM
  5. Replies: 1
    Last Post: March 28th, 2009, 07:21 AM

Tags for this Thread