What is the most appropriate type of variable to use when coding a game's location? Boolean/String/Some other? (java) -
first post hope appropriate type question site. if not i'd appreciate if direct me more appropriate place. i'm extremely new @ programming. did bit in high school , have decided relearn starting making text-based survival game in java7 using eclipse.
right i'm coding location superclass. particular function need this: needs able keep track of of 9 regions user "is in" (which used in large number of other classes many various purposes. location class includes functionality accepting user input move new region, among various other things.) way started making boolean variable each region , whenever transition should occur variable set true. i'm wondering if efficient way this. have take string inputs, run method standardize various acceptable answers one, , run through switch statement makes corresponding boolean variable true?
would simpler keep track of location single string variable gets set whatever region player in? or more cause errors or complications when coding? array better suit need? edit: (i want thank guys people such open , helpful community. appreciated.)
big edit: wanted further elaborate on regions do. in each region there handful of places user can go generic small number of places unique each location. other major superclasses altered depending on region user in (example: "encounters" superclass have variables dictate how encounters happen (i.e. chance hostile attack) , these variables altered depending on region) other instances (the "time" superclass keep track of day , time of day effect variables in "encounters".) current plan make class each generic place (i.e. walmart, technology store, grocery, public park, etc.) contain different properties depending on region , effect classes "encounters". going have properties defined if/else & switch statements depending on region user in. i'm realizing make more sense define properties when create object.
while lot of people steering me enums, suggesting make classes each region, (and hearing interfaces.) if go 2nd route have 3 questions: (a) if region classes subclasses "location", wouldn't have problem creating objects generic places inside region classes (i.e. walmarts) because walmart class can belong 1 superclass? (if not difference between object being created in class , actual relationship between superclass , subclasses) (b) if initialized each region object instead of recording variable, how achieve original task of remembering region user in (for functions simple printing region out making alterations variables in classes "encounters"). wouldn't still need have sort of variable identify region? , if so, practical purpose creating classes region accomplish? (i can see might still let me make code cleaner housing variables interact "encounters" instead of having use if/else/switch statements inside "encounters" class (also in case how make variables in region classes interact variables in "encounters" since neither belong each other) else?) (c) make more sense create classes every region or single region class gets defined differently when initialized , why?
finally, know may have asked many questions please explain me different utilities found in enums , interfaces (i'm interested in hearing enums) , know little bit more, should using enums, interfaces, or sort of classes regions? thank guys much!
enum recommended, stated vasily liaskovsky. using int great way well. example:
int currentregion; static final int region1 = 0; static final int region2 = 1; static final int region3 = 2; etc...
make sure region1 etc stated final, ids cannot changed afterwards, static reference save memory if you're using multiple location superclass objects, easier accessible outside class. way check if you're in region, use if statement:
if(currentregion == region1) {}
to set it:
currentregion = region1;
simple that
Comments
Post a Comment