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 9 of 9

Thread: Conways Game of Life. Long Death option. Please help..

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Conways Game of Life. Long Death option. Please help..

    Okay, so for my Java class we have been instructed to make a java program that implements Conway's Game of Life. Where, when run, a grid shows, allows the user to declare which cells are "alive (green cells)", and runs through by checking the rules (rule 1. if a dead cell has 3 neighbors, make it alive, rule 2. if an alive cell has 2 OR 3 neighbors, keep alive, rule 3. otherwise make the cell "dead(white cells") I have that part of the code running and working correctly.
    But the second part of the assignment asks me to make it so that there is a phase between it being alive and dead, meaning if an alive cell is going to be dead in the next step, fill the cell with a different color (i.e. yellow) to show that it is going to die, and then kill it on its next run through the grid.

    here is a link to my assignment with detailed instructions if mine didn't make sense:
    cs205: Problem Set 1: Game of Life
    (instructions are at the very end of the page)

    CellState.java is the one that basically creates dead and alive cells.
    I have been trying to make a method in there to create this "dying" state for the cell so that I can make a conditional statement in the ConwayLifeCell.java to make these "dying" state cells before actually killing them.
    The problem I am running into is that since alive in the CellState.java is already a boolean, I'm not sure how to implement another true or false in the class to tell whether it's dying or dead. Please help.. I've attached a .zip file with the whole source for this program.. please ignore the ExtremeLifeCell.java one, that one is just an example class to allow us students to have a basic idea of how it's suppose to run.

    Please and thank you
    Attached Files Attached Files


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Conways Game of Life. Long Death option. Please help..

    You might want to look into using an enum instead of a boolean. That way you can easily check against three different conditions.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Conways Game of Life. Long Death option. Please help..

    I know that this program is using enumerations, since it was a package given to me, but I'm not quite sure how enumerations work in these cases..

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Conways Game of Life. Long Death option. Please help..

    After a quick search of "java enum" (to be blatantly honest, I even misspelled it as "java enu"), these two tutorials are listed. They're pretty good.

    Enums
    Enum Types (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Mar 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Conways Game of Life. Long Death option. Please help..

    Thank you for those quick tutorials.
    So.. I understand what they actually do, but I still don't understand how I would incorporate it into this program itself..
    there is a .createDead() and .createAlive() in the CellState.java that will create a dead cell or an alive cell. From my pseudo code, I'm pretty sure I am going to be needing .createDying() or something of the sort for those dying cells.

    // CS205 Fall 2006
    // CellState.java
     
    import java.awt.Color;
     
    public class CellState {
    	// OVERVIEW: A CellState is an immutable object that represents
    	// the state of a cell, either alive or dead.
     
    	private boolean alive;
     
    	private CellState(boolean isalive)
    	// EFFECTS: Initializes this to alive if isalive is true,
    	// otherwise initializes this to the dead state.
    	{
    		this.alive = isalive;
    	}
     
    	static public/* nonnull */CellState createAlive()
    	// EFFECTS: Returns an alive cell state.
    	{
    		return new CellState(true);
    	}
     
    	static public/* nonnull */CellState createDead()
    	// EFFECTS: Returns a dead cell state.
    	{
    		return new CellState(false);
    	}
     
    	public Color getColor() 
    	// EFFECTS: Returns the display color for this state
    	{
    		if (alive) return Color.green;
    		else return Color.white;
     
    	}
     
    	public boolean equals(CellState s) {
    		return s.alive == alive;
    	}
     
    	public boolean equals(Object o) {
    		try {
    			return equals((CellState) o);
    		} catch (ClassCastException e) {
    			return false;
    		}
    	}
     
    	public boolean isAlive()
    	// EFFECTS: Returns true iff this is alive.
    	{
    		return alive;
    	}
    }

    since enumerations is used when trying to make constants, I would no longer need the boolean?

  6. #6
    Junior Member
    Join Date
    Mar 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Conways Game of Life. Long Death option. Please help..

    Someone please help.. I've been working on this for hours for the last few days and I can't figure it out..

  7. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Conways Game of Life. Long Death option. Please help..

    Basically, instead of having a boolean value, you'd have something like a CellState value (whatever you want to call your enumeration type). That type would have three different enum constants: ALIVE, DEAD, and DYING. Hope that helps.

    PS- Bumping a thread is seen as impatient, and can actually decrease your chances of getting help. We understand your frustration, and we've all been there before, but there are hundreds of questions here- all with an urgent, frustrated user behind them.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    Junior Member
    Join Date
    Mar 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Conways Game of Life. Long Death option. Please help..

    okay, so I have

    public enum CellStateValue{
      DEAD,
      ALIVE,
      DYING;}

    instead of the private boolean alive; that was used earlier.
    I understand that these are used keep constant but I still don't understand how I'm suppose to make it useful for the program. would I assign each constant a number and reference that?

    PS, KevinWorkman, thank you for the advice. I will refrain from doing so from now on, Thank you.
    Last edited by byako; March 8th, 2011 at 07:50 PM.

  9. #9
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Conways Game of Life. Long Death option. Please help..

    No, you can directly reference the enum constants, no need for any other values.

    You'll need to store the current state of the cell in a variable. The syntax for that would be something like this:


    CellStateValue cellState = CellStateValue.DEAD;
     
    public void setCellState(CellStateValue newState){
       cellState = newState;
    }

    And to check against the enum constants, it would look something like this:

    if(cellState.equals(CellState.ALIVE)){
       //whatever
    }
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. 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
  2. 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
  3. Is thread the best option?
    By 256mxr in forum Threads
    Replies: 1
    Last Post: May 22nd, 2010, 08:24 PM
  4. help help, ... :) "game of life"
    By sanfor in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 9th, 2009, 12:40 PM
  5. Replies: 1
    Last Post: March 28th, 2009, 07:21 AM