Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By Norm

Thread: Game of life - first Java ever

  1. #1
    JavaDeva is offline Member
    Join Date
    Feb 2012
    Posts
    1
    Rep Power
    0

    Default Game of life - first Java ever

    Hay all.

    Im new to java. (just decided java over Cpp a few days ago while trying to make this same program in Cpp)

    So...


    id just like to hear what you all think of the following.

    Its my first try at this (Java, not coding, ive Coded AS2 before and used a little time (a week only) around Cpp)
    Im having a little trouble warping my head around the graphics part of this, and some on the OOP, but i hope ill get the hang of it.

    Any how. i sat down and decided to make a Game of life implamatation, and this is what i ended up with following random tuts on game programming .

    Are there any one here who can tell me what i dit good and bad, habbits and so one (pleas, not my spelling, i cant spell due to a mental neclect, so.)

    And if posible, can any one (one who can spell) make a tut over this file, i think it might help alot of people out there.

    hers the code (made in Eclipse, package named GOL:)

    Running.java
    Java Code:
    package GOL;
    import javax.swing.*;																// I need tis for graphics and stuf.
    
    
    public class Running{																//this is my driving class. everything happens here.
    
    	public static void main(String[] args) throws InterruptedException {			// main. the first thing the program calls.
    	Window nr1 = new Window(12);														// this creates a new Window called nr1. Window is a class, it also gets the surviaval number for the GAME OF LIFE.
    																					// higher survial number means fewer Cells will live in the initial run.
    	
    	nr1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);								// exit command
    	nr1.setSize(418,465);															// this is the size of the before named Window
    	nr1.setVisible(true);															// with out this, my window will be inviseble, but still running.
    	
    	int runs = 10000;																// this sets the amounts of Generations for my game of life
    	
    	while(runs > 1) {																// the while loops sycles trough all of the needed updates and the sleep function.
    		runs--;				//use one generation.
    		Thread.sleep(60);	//sets the program to "pause" for 60 milliseconds.
    		nr1.update();		//update my Window nr1. (see in Window.java) 
    		}
    	}
    }
    Window.java
    Java Code:
    package GOL;
    
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.event.*;
    
    public class Window extends JFrame{				//window is a JFrame
    	
    	private MapOfCells Map1;					//i need an object that displaces my cells, this is called Map1
    	
    	public Window(int survival) {				//When Window is first created, do the following
    		super("Pixel life - by Michlas");			//Set the title of the window to be "Game of life tutorial"
    		Map1 = new MapOfCells (1,1,survival);	//initiate the Map1 at XY 1,1 and use the survival number of the Window.
    		
    		add(Map1);								//make sure to add the Map1 to the Frame.
    	}
    
    	public void update() {						//Each run, Running calls for the windows update to go trough again.
    		Map1.update();							// make sure Map1 updates.
    	}
    }
    MapOfCells.java
    Java Code:
    package GOL;														//This object is the map that contains a 400*400 array of Cells that can be alive (true) or death (false)
    
    import java.awt.*;
    import java.util.Random;											//when we initiates this game, we need a randomizer, so that our map wont be the same each time.
    
    import javax.swing.*;
    
    public class MapOfCells extends JPanel{								//this object is a panel, to allow it to use paint.
    	
    	private int xPos, yPos;											//our map will have a x and y position.
    	
    	
    	int xy = 1;														//ForeFun
    	int redBox = 200;												//ForeFun
    	
    	
    	private static int surviv;										//this is the survival posiabilit, higher numbers give fewer survivors.
    	
    	private Color c = new Color(0,0,0);								//to show if cells are alive, we use WHITE (or 255,255,255 RGB), initialy, all cells are death (BLACK or 0,0,0)
    	
    	private static boolean[][] mapP = new boolean[400][400];		//The cells are all a Boolean(true/false atribute), having 400*400 spaces.
    	private boolean[][] mapNew = new boolean[400][400];				//when we calculate the cells survival ods, we need a place to store them temperray.
    
    	public void paint(Graphics g) {									//Painting it all each run.
    		super.paintComponent(g);									//paint it in my Frame.
    		
    		Random number = new Random();								//ive added this for play with colors.
    		
    		for(int i= 0;i<400;i++) {									//a fore loop that cucles trough 400 numbers.
    			for(int k= 0;k<400;k++) {								//a fore loop that cucles trough 400 numbers.
    				
    				if(mapP[i][k] == false) {
    					c = new Color(0,0,0);
    				}
    				if(mapP[i][k] == true) {
    					c = new Color(number.nextInt(55)+200,number.nextInt(155)+20,number.nextInt(155)+20);
    				}
    				g.setColor(c);
    				g.drawLine(xPos+i, yPos+k, xPos+i, yPos+k);
    				
    			}														//end of a fore loop that cucles trough 400 numbers.
    		}															//end of a fore loop that cucles trough 400 numbers.
    		
    		g.drawRect(0, 0, 400, 400);
    		g.drawString("My game of life ~~ The Pixe life", 10, 412);
    		
    		c = new Color(230,30,30);									//ForeFun
    		g.setColor(c);												//ForeFun
    		g.fillRect(redBox+10, 412, 3, 10);							//ForeFun
    	}
    	
    	public MapOfCells(int myXPos, int myYPos, int mysurviv) {
    		surviv = mysurviv;
    		xPos = myXPos;
    		yPos = myYPos;
    		initMap();
    	}
    	
    	public void calcMap() {											//the loope caled at each update to update teh cells tates and reset stuf.
    		resetMapNew();
    		cellState();
    		mapToNewMap();
    	}
    																	
    	public void resetMapNew() {										//a function for reseting the newMap to all death
    		
    		for(int i= 0;i<400;i++) {									//a fore loop that cucles trough 400 numbers.
    			for(int k= 0;k<400;k++) {								//a fore loop that cucles trough 400 numbers.
    				
    				mapNew[i][k] = false;								//set mapNew at the current loop to false (death)
    				
    			}														//end of a fore loop that cucles trough 400 numbers.
    		}															//end of a fore loop that cucles trough 400 numbers.
    	}
    	
    	public void mapToNewMap() {										//copieng newMap ower to MapP
    		
    		for(int i= 0;i<400;i++) {									//end of a fore loop that cucles trough 400 numbers.
    			for(int k= 0;k<400;k++) {								//end of a fore loop that cucles trough 400 numbers.
    				
    				mapP[i][k] = mapNew[i][k];							//coppy current NewMap to MapP
    				
    			}														//end of a fore loop that cucles trough 400 numbers.
    		}															//end of a fore loop that cucles trough 400 numbers.
    	}
    	
    	
    	
    	public void cellState() {										//This function calculates and sets all Cells one by one.
    
    		for(int i = 1;i<399;i++) {									//start i and k at 1 and end at 399, this way, we will have a blank line around the map of death cells.
    			for(int k = 1;k<399;k++) {
    				int alives = countNibors(i,k);						//use countNibors to tell how many live nabors the current cell have.
    
    				if (mapP[i][k] == false) {							// if the current cell is death, only the result of 3 nabors will turn it alive, all else will continue death.
    					if(alives == 3) {
    						mapNew[i][k] = true;
    					}
    					else {
    						mapNew[i][k] = false;
    					}
    				}
    
    				if (mapP[i][k] == true) {							// if the current cell is alive, the result of 2 or 3 nabors will keep it alive, all else will kill it.
    					if(alives == 2) {
    						mapNew[i][k] = true;
    					}
    					else if(alives == 3) {
    						mapNew[i][k] = true;
    					}
    					else {
    						mapNew[i][k] = false;
    					}
    				}
    			}
    		}
    	}
    	
    	public int countNibors(int i, int k) {							//this function counts the 8 cells around the chosen cell, and tells how many are alive.
    		int liveOnes = 0;
    		for(int x = -1;x<=1;x++){
    			for(int y = -1;y<=1;y++){
    				if (x != 0 || y != 0) {
    					if(mapP[i+x][k+y] == true) {
    						liveOnes += 1;
    					}
    				}
    			}
    		}
    		return (liveOnes);
    	}
    	
    	
    	public static void initMap() {									//At the call of this function, the MapP will be randomly generated.
    		
    		Random number = new Random();
    		for(int i= 0;i<400;i++) {
    			for(int k= 0;k<400;k++) {
    				int cellState = number.nextInt(surviv);				//This sets the ods for survival acording to the number we chose in the running file.
    				if(cellState >= 1) {
    					mapP[i][k] = false;
    				}
    				else {
    					mapP[i][k] = true;
    				}
    			}
    		}
    	}
    	public void update() {											//each time Window update is called. this update is called two.
    		
    		if(redBox > 390) {											//ForeFun
    			xy = 2;													//ForeFun
    		}															//ForeFun
    		if(redBox < 200) {											//ForeFun
    			xy = 1;													//ForeFun
    		}															//ForeFun
    		if(xy == 1) {												//ForeFun
    			redBox++;												//ForeFun
    		}															//ForeFun
    		else {														//ForeFun
    			redBox--;												//ForeFun
    		}															//ForeFun
    		
    		calcMap();													//Calculate the new map
    		repaint();													//paint the map, and all else in the PaintComponent statement.
    	}
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Game of life - first Java ever

    Couple of comments.
    Don't use the a class name that is a Java SE class name: Window.

    Don't use hardcoded numbers in so many places in the code: 400. Define an int variable (final static) and use the variable all over the code.
    The same for the values you put in alives. What does a 2 or a 3 mean? Use a variable with a name that defines what it is: IsAlive and IsDead for example.
    DarrylBurke likes this.

Similar Threads

  1. Game of Life , Adam & Eve
    By rapito in forum New To Java
    Replies: 26
    Last Post: 09-24-2011, 03:34 AM
  2. Game of life methods help
    By bigskers76 in forum New To Java
    Replies: 26
    Last Post: 12-12-2009, 03:11 PM
  3. game of life
    By bigskers76 in forum New To Java
    Replies: 10
    Last Post: 12-09-2009, 05:21 AM
  4. 2d array help- Game of Life
    By n3philim in forum New To Java
    Replies: 2
    Last Post: 07-23-2009, 12:01 AM
  5. A more efficient Game of Life
    By unreal4evr in forum New To Java
    Replies: 3
    Last Post: 03-27-2009, 03:08 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •