1 Attachment(s)
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
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.
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..
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)
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.
Code Java:
// 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?
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..
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.
Re: Conways Game of Life. Long Death option. Please help..
okay, so I have
Code Java:
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.
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:
Code java:
CellStateValue cellState = CellStateValue.DEAD;
public void setCellState(CellStateValue newState){
cellState = newState;
}
And to check against the enum constants, it would look something like this:
Code java:
if(cellState.equals(CellState.ALIVE)){
//whatever
}