Page 1 of 2 12 LastLast
Results 1 to 20 of 27
Like Tree4Likes

Thread: Game of Life , Adam & Eve

  1. #1
    rapito is offline Member
    Join Date
    Sep 2011
    Posts
    13
    Rep Power
    0

    Unhappy Game of Life , Adam & Eve

    Hello!
    I'm having an issue with an assignment I got form a professor. I must do a Game of Life with Java, Im still new with it but can understand most of it (i think)
    Anyway im having trouble with my Cells in my application, they don't "obey me".

    I made 2 arrays to check for the new and current generation. the next generation checks the current and updates the cells into this array and after that's done I copy the next generation array into the current generation. All this in an update method. Logically it should work, but It's not doing what it should do.

    They all start kind of passive even tough they are not doing the right thing, then they all say they are going to live and use the Spiral Force against me and everyone bites the Apple ><
    (Im just kidding with a Anime Tengen Toppa Gurren Lagann)


    Game of Life , Adam &amp; Eve-.png

    Game of Life , Adam &amp; Eve-b.png

    This is the class that does everything:
    Java Code:
    package agame.applet;
    
    import java.awt.*;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.image.BufferStrategy;
    import java.awt.Color;
    import java.util.Random;
    
    
    public class GameCanvas extends Canvas implements Runnable, KeyListener, MouseListener
    {
    	
    	private static final long serialVersionUID = -6921315748304590036L;
    
    	static int FWidth = 800,
    			   FHeight = 600;
    	public static int map[][] = new int[500][500];
    	
    	public long period =10;
    	public BufferStrategy Buffer;
    	public static Graphics graphics;
    	
    	public MegamanEntity megaman;
    	public static CellEntity cells[][] = new CellEntity[500][500],
    				  newGen[][] = new CellEntity[500][500];
    	
    	public int bC = 0; // bullet Count
    	
    	private Thread t;
    	
    	public GameCanvas()
    	{
    		this.setIgnoreRepaint(true);
    		this.setBounds(0,0,FWidth,FHeight);
    		this.setBackground(Color.white);
    		this.addKeyListener(this);
    		this.addMouseListener(this);
    		this.setVisible(true);
    		
    		megaman = new MegamanEntity("idle.png",200,200);
    		for (int px=0; px<(FWidth/10)-2 ; px++)
    			for(int py=0; py<(FHeight/10)-4; py++)
    			cells[px][py] = new CellEntity("dCell.png",px*10+1,py*10+1,px,py);
    	}
    	
    	public void addNotify()
    	{
    		super.addNotify();
    		this.createBufferStrategy(2);
    		this.Buffer = this.getBufferStrategy();
    		requestFocus();
    		
    		startGame();
    	}
    
    	private void startGame()
    	{
    		if(t==null)
    		{
    				t = new Thread(this);
    				t.start();
    		}
    		
    	}
    	
    	public void run()
    	{
    		while (true)
    		{
    			long beginTime = System.currentTimeMillis();
    			
    			Update();
    			Render();
    			Draw();
    			
    			long timeTaken = System.currentTimeMillis()-beginTime,
    				 sleepTime = period - timeTaken;
    			try{
    			Thread.sleep(sleepTime);
    			}catch(Exception e)
    			{
    				//System.out.println(e.getMessage());
    			}
    		}
    	}
    
    	public void Draw() 
    	{
    		
    			Buffer.show();
    			if(graphics != null)
    			{
    				graphics.dispose();
    			}
    	
    		
    	}
    
    	public void Render() 
    	{
    		graphics = Buffer.getDrawGraphics();
    		
    		graphics.setColor(Color.white);
    		graphics.fillRect(0, 0, FWidth, FHeight);
    		graphics.setColor(Color.GRAY);
    		
    		for (int px=0; px<(FWidth/10)-2 ; px++)
    		{
    			for(int py=0; py<(FHeight/10)-4; py++){
    			graphics.fillRect(px*10, py*10, 9, 9);
    			cells[px][py].Draw(graphics);
    			map[px][py]=0;
    			}
    		}
    		
    		
    //		megaman.Draw(graphics);
    //		graphics.drawString("a", 0, 0);
    //		megaman.Dx = 20;
    //		megaman.Dy = 0;
    	}
    
    	public void Update()
    	{
    		newGen = cells.clone();
    		
    		for (int px=0; px<(FWidth/10)-2 ; px++)
    		for(int py=0; py<(FHeight/10)-4; py++)			
    		cells[px][py].survive();
    		cells=newGen.clone();
    		try {
    			Thread.sleep(100);
    		} catch (InterruptedException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    
    	}
    
    	@Override
    	public void keyPressed(KeyEvent e) {
    		
    		
    		if(e.getKeyCode() == KeyEvent.VK_C)
    		{
    			Random a = new Random();
    			for(int i=0;i<a.nextInt(50);i++)
    				{
    				int x = a.nextInt(10),y = a.nextInt(20);
    				newGen[x][y].birth();
    				}
    				
    			for(int x=30;x<33;x++) cells[x][35].birth();
    		}
    	}
    
    
    	@Override
    	public void mouseClicked(MouseEvent e) {
    		if(e.getButton()==1)
    		{
    			int x=e.getX(),y=e.getY();
    			for (int px=0; px<(FWidth/10)-2 ; px++)
    			for(int py=0; py<(FHeight/10)-4; py++)
    				if(x <= cells[px][py].getX()+7 && x>= cells[px][py].getX() && y<= cells[px][py].getY()+7 && y >= cells[px][py].getY())
    					cells[px][py].birth();
    		}	
    		else 
    		{
    			for(int x=0;x<77;x++)
    				for(int y=0;y<55;y++)
    					cells[x][y].suicide();
    		}
    
    		
    	}
    
    }
    And this is the Class for the Sinner Cells >_>
    Java Code:
    package agame.applet;
    
    public class CellEntity extends Entity{
    
    	private boolean alive = false;
    	private int idx_x,idx_y;
    	private CellEntity[][] copy;
    	
    	public CellEntity(String ref, int x, int y,int ab,int ac) {
    		super(ref, x, y);
    		this.setIdx_x(ab);
    		this.setIdx_y(ac);
    		this.setHorizontalMovement(0);
    		this.setVerticalMovement(0);
    	}
    
    	public void survive()
    	{
    		
    		int vivas = fam();
    		if(isAlive() && (vivas>3 || vivas<2))
    			GameCanvas.newGen[idx_x][idx_y].suicide();
    		
    		
    		else if(isAlive()==false && vivas==3)
    			GameCanvas.newGen[idx_x][idx_y].birth();
    		
    	}
    	
    	public boolean isAlive() {
    		return alive;
    	}
    
    	public void birth() {
    		changeImage("cell.png");
    		alive = true;
    	}
    	
    	public void suicide() {
    		changeImage("dCell.png");
    		alive = false;
    	}
    
    	public int fam() {
    		
    		int fam=0;
    		if(idx_x>0 && idx_x<77 && idx_y>0 && idx_y<55  ){
    		
    			
    		if(GameCanvas.cells[this.idx_x-1][this.idx_y-1].isAlive())fam++;
    		if(GameCanvas.cells[this.idx_x-1][this.idx_y].isAlive())fam++;
    		if(GameCanvas.cells[this.idx_x-1][this.idx_y+1].isAlive())fam++;
    
    		if(GameCanvas.cells[this.idx_x+1][this.idx_y-1].isAlive())fam++;
    		if(GameCanvas.cells[this.idx_x+1][this.idx_y].isAlive())fam++;
    		if(GameCanvas.cells[this.idx_x+1][this.idx_y+1].isAlive())fam++;
    		
    		if(GameCanvas.cells[this.idx_x][this.idx_y-1].isAlive())fam++;
    		if(GameCanvas.cells[this.idx_x][this.idx_y+1].isAlive())fam++;
    		
    		
    		
    		}
    		else if(idx_x==77)
    		{
    			if(idx_y>0 && GameCanvas.cells[this.idx_x-1][this.idx_y-1].isAlive())fam++;
    			if(GameCanvas.cells[this.idx_x-1][this.idx_y].isAlive())fam++;
    			if(idx_y<55 && GameCanvas.cells[this.idx_x-1][this.idx_y+1].isAlive())fam++;
    			
    			if(idx_y>0 && GameCanvas.cells[this.idx_x][this.idx_y-1].isAlive())fam++;
    			if(idx_y<55 && GameCanvas.cells[this.idx_x][this.idx_y+1].isAlive())fam++;
    		}
    		else if(idx_x==0)
    		{
    			if(idx_y>0 && GameCanvas.cells[this.idx_x+1][this.idx_y-1].isAlive())fam++;
    			if(GameCanvas.cells[this.idx_x+1][this.idx_y].isAlive())fam++;
    			if(idx_y<55 && GameCanvas.cells[this.idx_x+1][this.idx_y+1].isAlive())fam++;
    			
    			if(idx_y>0 && GameCanvas.cells[this.idx_x][this.idx_y-1].isAlive())fam++;
    			if(idx_y<55 && GameCanvas.cells[this.idx_x][this.idx_y+1].isAlive())fam++;
    		}
    		else if(idx_y==0)
    		{
    			if(idx_x>0 && GameCanvas.cells[this.idx_x+1][this.idx_y+1].isAlive())fam++;
    			if(idx_x<77 && GameCanvas.cells[this.idx_x-1][this.idx_y+1].isAlive())fam++;
    			if(GameCanvas.cells[this.idx_x][this.idx_y+1].isAlive())fam++;
    			
    			if(idx_x>0 && GameCanvas.cells[this.idx_x+1][this.idx_y].isAlive())fam++;
    			if(idx_x<77 && GameCanvas.cells[this.idx_x-1][this.idx_y].isAlive())fam++;
    		}
    		else if(idx_y==55)
    		{
    			if(idx_x>0 && GameCanvas.cells[this.idx_x+1][this.idx_y-1].isAlive())fam++;
    			if(idx_x<77 && GameCanvas.cells[this.idx_x-1][this.idx_y-1].isAlive())fam++;
    			if(GameCanvas.cells[this.idx_x][this.idx_y-1].isAlive())fam++;
    			
    			if(idx_x>0 && GameCanvas.cells[this.idx_x+1][this.idx_y].isAlive())fam++;
    			if(idx_x<77 && GameCanvas.cells[this.idx_x-1][this.idx_y].isAlive())fam++;
    		}
    		
    		return fam;
    		
    	}
    
    
    
    	@Override
    	public String toString() {
    		return "CellEntity [alive=" + alive + ", position=" + this.getIdx_x() + ","+this.getIdx_y()+"]";
    	}
    
    	public int getIdx_y() {
    		return idx_y;
    	}
    
    	public void setIdx_y(int idx_y) {
    		this.idx_y = idx_y;
    	}
    
    	public int getIdx_x() {
    		return idx_x;
    	}
    
    	public void setIdx_x(int idx_x) {
    		this.idx_x = idx_x;
    	}
    	
    
    }
    PLease HELP!!!!!


    ---Edit---------------------------------------------
    | It Works YAY!!! =D |
    | Here is the Source Code Enjoy it! |
    | TDO.zip |
    ----------------------------------------------------
    Last edited by rapito; 09-24-2011 at 03:32 AM. Reason: Problem Solved

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,574
    Rep Power
    23

    Default Re: Game of Life , Adam & Eve

    What kind of help are you looking for? This is only part of your code so it can't be compiled and tested.
    The only thing I can do is give some advice:
    Have you tried debugging the code by adding printlns to show the logic flow and the values of variables as they change?
    Fubarable and DarrylBurke like this.

  3. #3
    rapito is offline Member
    Join Date
    Sep 2011
    Posts
    13
    Rep Power
    0

    Default Re: Game of Life , Adam & Eve

    I can't find any mistakes on my code, but maybe someone here do.
    I already posted what i want it to do.
    I already did the printlns and everything is working as it should be.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,574
    Rep Power
    23

    Default Re: Game of Life , Adam & Eve

    everything is working as it should be.
    That's hard to understand.
    Why are you looking for help if the code is working as it should?

    I notice you have several hardcoded/magic numbers in your code.
    What are they supposed to mean?

    Coding like this:
    if (x == 55 && y != 22)
    makes it hard to understand what the code is doing.
    It is better to define constant variables with names that shows what the logic is doing.
    final int Alive = 55;
    final int Dieing = 22;
    ....
    if (x == Alive && y != Dieing)

  5. #5
    rapito is offline Member
    Join Date
    Sep 2011
    Posts
    13
    Rep Power
    0

    Default Re: Game of Life , Adam & Eve

    The specific size of the array is 77x55 that's what it is.
    When I check for the printlns result its O.K but when it starts drawing it doesn't here's the project file so you can compile it and check it.

    TDO.zip

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,574
    Rep Power
    23

    Default Re: Game of Life , Adam & Eve

    Then use something like this:
    final int NbrRows = 55;
    final int NbrCols = 77;

    When testing code sometimes a smaller grid can make it easier to test with. With all your hardcoded numbers throughout the code it will be hard to change the size of the grid. With the definition at one point in the code its an easy change:
    final int NbrRows = 21; //55;
    final int NbrCols = 35; //77;

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,574
    Rep Power
    23

    Default Re: Game of Life , Adam & Eve

    I get this warning:
    Java Code:
    MegamanEntity.java:64: warning: [empty] empty statement after if
    		if(facingRIGHT);
    		               ^

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,574
    Rep Power
    23

    Default Re: Game of Life , Adam & Eve

    Some suggestions for the code:
    Cycle through the change in life states showing different colors for the new life cell and for the dying cells.
    Allow the user to change the cycle times to easily see the change in state for the cells.

    How does the user setup the initial living cells? Need a menu help item for this.

  9. #9
    rapito is offline Member
    Join Date
    Sep 2011
    Posts
    13
    Rep Power
    0

    Default Re: Game of Life , Adam & Eve

    Thanks for the tips.
    About the warning (Megaman), ignore it, It's just some unused Entity, this Game of Life application is a Mod from a earlier game I was developing,
    About the user setting up the initial living cells and everything else, They all start Dead, I want it to work first before adding custom Spawning for the game."
    That's why I have so many hardcoded numbers and methods, as for generating specifically 77 cols, and 55 rows with the frame width and heigth.
    What I really don't get is why isn't it doing what I want to.

    If i have this row(0 are dead, 1 are living cells):

    00000
    01110
    00000

    the next generation should be like this:

    00100
    00100
    00100

    What happens with my code is this:

    00000
    00000
    00000

    My first thought was: OK, maybe its checking the same array, then i specifically made it kill/give birth to the cells of the clone array, while checking how many living cells are surrounding the original cell.
    But it's still killing the same array.
    Why is this happening.

    I made it prinlns of the surrounding living cells and state of the 3 cells when I press the C key IF they have more than 0 alive around them
    but if i make it println on the method run (survive()) it prints none.

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,574
    Rep Power
    23

    Default Re: Game of Life , Adam & Eve

    I just tried working with your code to make an easy testing version.
    I tried reducing the width/height and immediately get a NullPointerException.
    Your code has too many hardcode literals. Like the 77 and 55. They should be computed.

    For testing it'd be nice to have a 8 x 8 grid that you could select some live cells on. And enter this loop:
    begin loop
    Select which cells will come alive and which ones will die and paint them in a different color. Wait a short time
    Then change the cells to their new status.
    And display the new grid will all the live cells
    end loop
    Have a variable that will control how long the grid is displayed between cycles.

    Have a button that would allow you to stop the cycling at any point and restart it.
    Fubarable and rapito like this.

  11. #11
    rapito is offline Member
    Join Date
    Sep 2011
    Posts
    13
    Rep Power
    0

    Default Re: Game of Life , Adam & Eve

    I will try that and Will let you know how it turns out :)
    Thanks!
    Last edited by rapito; 09-23-2011 at 10:07 PM.

  12. #12
    rapito is offline Member
    Join Date
    Sep 2011
    Posts
    13
    Rep Power
    0

    Default Re: Game of Life , Adam & Eve

    Sorry for the double post..
    Here's the turn out... still getting the same problem :(
    TDO.zip

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,574
    Rep Power
    23

    Default Re: Game of Life , Adam & Eve

    Did you add the debugging features I recommended?
    Allow different size of grid.
    Allow user to specify the initial alive cells.
    Add extra cycle to show the dying cells and the new cells with a new color
    Allow user to change the cycle times and stop the cycling.

  14. #14
    rapito is offline Member
    Join Date
    Sep 2011
    Posts
    13
    Rep Power
    0

    Default Re: Game of Life , Adam & Eve

    I'm just missing the Color Cycle, but for everything else is like this:
    The game starts Paused with all the cells dead:
    To set the living cells just move the mouse around with the left click pressed
    To start the cycle Press B
    To Pause it press V
    To Generate Random Living Cells Press C
    Middle Mouse Click Erases the Whole Screen
    Right Click Kills the clicked cell (Can be dragged)

  15. #15
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,574
    Rep Power
    23

    Default Re: Game of Life , Adam & Eve

    Can you change the cycle speed? Other than changing the program.
    Have you looked at the Color cycling? That should make debugging easier if you are having problems with that part of the logic.

  16. #16
    rapito is offline Member
    Join Date
    Sep 2011
    Posts
    13
    Rep Power
    0

    Default Re: Game of Life , Adam & Eve

    The User can't but in the code there are two parts where i make the Game Thread Sleep...
    It has 4 Methods:
    It Renders,Draws,Updates the Values,and finally it sleeps for a calculated amount of time...
    Inside the Update() method it Clones the current Generation then Calculates the next Generation and puts swaps the cloned array with the original...
    After that it Sleeps again... this is the one i used to pause between cycles.

    How should I Implement the changing of speed according to my code?

  17. #17
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,574
    Rep Power
    23

    Default Re: Game of Life , Adam & Eve

    When you scan for the next cycle to be alive or dead, set the color for those squares to be the changing colors
    and draw the grid. Wait some, scan over the grid and change the coming alive to be alive and the dying to be dead and draw the grid with the final alive or dead colors.

  18. #18
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,574
    Rep Power
    23

    Default Re: Game of Life , Adam & Eve

    What are the rules you are using to determine life and death?
    My GoL program cycles with 3 in a row to horizontal then vertical and again and again.
    Your game just clears the 3 cells.

  19. #19
    rapito is offline Member
    Join Date
    Sep 2011
    Posts
    13
    Rep Power
    0

    Default Re: Game of Life , Adam & Eve

    I know that's the problem ><
    These are my Coding Rules:
    Java Code:
                   int vivas = fam();
    		if(isAlive() && (vivas>3 || vivas<2))
    			Juego.newGen[idx_x][idx_y].suicide();
    		
    		else if(!isAlive() && vivas==3)
    			Juego.newGen[idx_x][idx_y].birth();
    If the cell is alive And the live cells around it are Greater than 3 or less than 2
    it commits suicide >:)
    Else if The cell is Dead and has Exactly 3 live cells surrounding it is reborn. D:

  20. #20
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,574
    Rep Power
    23

    Default Re: Game of Life , Adam & Eve

    Do a manual check on my program. What should be the result of 3 in a row?

Page 1 of 2 12 LastLast

Similar Threads

  1. game of life, using 2d array
    By aramiky818 in forum New To Java
    Replies: 1
    Last Post: 09-09-2011, 05:47 AM
  2. Game of Life GUI Error
    By Lavace in forum New To Java
    Replies: 3
    Last Post: 01-03-2011, 01:57 PM
  3. Game of life methods help
    By bigskers76 in forum New To Java
    Replies: 26
    Last Post: 12-12-2009, 03:11 PM
  4. game of life
    By bigskers76 in forum New To Java
    Replies: 10
    Last Post: 12-09-2009, 05:21 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
  •