Sign in to follow this  
shaun23

Java Game

Recommended Posts

shaun23    122
Hey guys, I am new to programming and I was wondering if someone could give me a hand with a small game that I am developing in Java. Here is my code so far...... Class Session This class maintains information about the session between two players. public class Session { private int sessionId; private String playerName; private Date startTime private String State; } What I would like to do,is define 3 different states for the State variable.I would like to define a WAIT_REPLY,ACCEPT_REJECT and PLAY states.The only problem is that I am not sure how to implement this in Java code.

Share this post


Link to post
Share on other sites
Zodiak    105
Hm, I don't quite undertand what you need... Why not just assign either of these states to your field? Or use an enumeration with these possible states? Can you please make it a bit more clear, then I am sure I'd be able to help!

Share this post


Link to post
Share on other sites
Zahlman    1682
Java 1.4.x and previous do not provide enumerated types, but there are workarounds of various complexity depending on what you need. Some options are considerably more powerful than the usual C++ style concept. However, you may find it easiest (most familiar) to just do something like:


public class Session
{
private int sessionId;
private String playerName;
private Date startTime;
// Use static, final variables to specify the possible values for the State
// variable. These can generally be relied upon to be inlined by the javac
// optimizer - across class boundaries, too, if you make them public - because
// the compiler knows that the values will not change and do not need to be
// created per object.
private static int WAIT_REPLY = 0;
private static int ACCEPT_REJECT = 1;
private static int PLAY = 2;
private int State; // of course, you could use Strings too, and have the string
// text give a friendly name to each state - that'd be the next step up in
// complexity.
}



It has also been brought to my attention that Java 1.5 does provide an enumeration facility, although it's really syntactic sugar for one of the more powerful represent-it-with-a-class idioms.

[Edited by - Zahlman on March 29, 2005 2:56:19 AM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this