#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    1
    Rep Power
    0

    Need Help With Conway Game Of Life


    hello everyone! being a complete beginner in JAVA, I need help with game of life code I have been doing for my uni assignment. I've implemented the code but I don't think the program for game of life is simply meant to do this? Can I have it working without the implementing the GUI on Eclipse? soo confused
    Code:
     import java.util.Scanner;
    import javax.swing.*;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.awt.*;
    
     
    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();
    	/*	JFrame frame = new JFrame();
    		frame.setBackground(Color.blue);
    		frame.setTitle("Game Of Life");
    		frame.setSize(200, 200);
    		
    		
    		frame.setVisible(true);
    		
    		JPanel panel = new JPanel();
    		frame.add(panel);
    		
    		JButton button = new JButton("Start");
    		panel.add(button);
    		
    	*/
    		
    		 
    		 
    	}
     
    		life(int x, int y, String filename) throws FileNotFoundException {
    			
    		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 z) {grid[x][y] = z;}
     
        public void show()
     {
         for (int x = 0; x < getY(); x++) {
        	 for (int y = 0; y < getX(); y++) {
    	      
    	      /* if (getCell(x, y) == 1){
    		 g.setColor(Color.black);
    		 g.fillRect(x * 4, y * 4, 4, 4);
    	     }
    	     else {
    		 g.setColor(Color.white);
    		 g.fillRect(x * 4, y * 4, 4, 4);
    		 }*/
    	      if (grid[x][y] == 1) System.out.print("*");
              else
                  System.out.print("-"); 
    	      
    	 }
    	 System.out.println();
         }
     }
     
        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(x, y) > 3 || neighbours (y, x) < 2){
    			    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 y = 0; y < getY(); y++) {
    	for (int x = 0; x < getX(); x++){
    	    grid[y][x] = grid2[y][x];
    	}
        }
          currentGeneration++;
        }
    any help would be appreciated!
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    Can I have it working without the implementing the GUI
    The GUI makes it so much better. You should try to get it to work.
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,905
    Rep Power
    481
    In neighbors you have this suspicious looking
    Code:
    	if (x < x-1){
    	    if (grid[x][y] == grid[x+1][y])
    		count++;
    	}
    Last edited by b49P23TIvg; December 28th, 2012 at 08:19 PM. Reason: highlight the error
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    1
    Rep Power
    0
    Sorry I had to make a new account since I forgot the password to the old and requested a new one but never really got a password reset email >.>

    Thank you for pointing out that bit of the code! and for the GUI, being completely new to it and from watching a few tutorial videos and notes, I've come up with this:
    Code:
    import java.util.Scanner;
    import javax.swing.*;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.awt.*;
    
     
    public class life extends Canvas{
     
    	
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 6961784928801246920L;
    	/**
    	 * 
    	 */
    	
    	public int[][] grid;
    	public int[][] newgrid;
    	int currentGeneration = 0;
    	int x, y; //where x is rows and y is coloumns 
    	String filename;
            public int count = 0;
          /*  Image myImage;
           * 
           */
            public void paint(Graphics g) {
          	  
            	g.setColor(Color.WHITE);
            	g.fillRect(10,10, 200, 200);
           	 
           	 for (int x = 0; x < getY(); x++) {
               	 for (int y = 0; y < getX(); y++) {	
           		if (getCell(x, y) == 1){
           		 g.setColor(Color.black);
           		 g.fillRect(x * 4, y * 4, 4, 4);
           	     }
           	     else {
           		 g.setColor(Color.white);
           		 g.fillRect(x * 4, y * 4, 4, 4);
           		 }
            }
            
           }
            }
     
    	public static void main(String[] args) throws FileNotFoundException{
    		life lifegrid = new life(20, 20, "testLab8.lif");
    		lifegrid.show();
    			    		
    		JFrame frame = new JFrame();
    		frame.setTitle("Game Of Life");
    		frame.setSize(400, 400);
    		frame.setVisible(true);
    		frame.add(lifegrid);
    		
    		JPanel panel = new JPanel();
    		frame.add(panel);
    		
    		JButton button = new JButton("Start");
    		panel.add(button);
    		
    			 
    	}
    but all I'm getting at the moment is a plane window with a start button (which I will implement later on) but no white colored rectangle and all I'm guessing I have to do something else on the code for the canvas bit to work?
  8. #5
  9. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    What is your plan for drawing the grid and for keeping track of the live squares?

    If you are getting errors compiling the code and need help fixing them, copy the full text of the error messages and post them.
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,905
    Rep Power
    481
    I simplified Conway's 4 rules as
    1) "the cell is alive if there are 3 neighbors"
    2) "the cell remains alive if there are 2 neighbors"
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo