scg/ch14/core/GameWithMapOnly

From FANG

Jump to: navigation, search

001 package scg.ch14.core;
002 
003 import java.io.File;
004 import java.io.FileNotFoundException;
005 import java.io.FileReader;
006 import java.util.ArrayList;
007 import java.util.Scanner;
008 
009 import scg.ch14.dictionary.Dictionary;
010 
011 import scg.ch14.gamestuff.Location;
012 
013 import scg.ch14.io.Keyboard;
014 import scg.ch14.io.SansCommentFilterReader;
015 
016 /**
017  * Text adventure game phase with just the map. Reads the map and
018  * permits moving around the map, seeing the "description" of each
019  * location. At this point the description is the string representation
020  * of the {@link Location} objects.
021  */
022 public class GameWithMapOnly {
023   /**
024    * Main program for the game with just a map. Create a game object (it
025    * takes no parameters as the file names are hard wired) and then call
026    * play. The "Game Begins" and "Game Ends" lines are there for
027    * debugging; make sure things actually happen.
028    *
029    @param  args  command-line arguments to the program; ignored by
030    *               this program
031    */
032   public static void main(String[] args{
033     GameWithMapOnly gmo = new GameWithMapOnly();
034     System.out.println("Game Begins");
035     gmo.play();
036     System.out.println("Game Over");
037   }
038 
039   /** command alias dictionary */
040   private final Dictionary aliases;
041 
042   /** game over flag */
043   private boolean gameOver;
044 
045   /** the map of the world */
046   private final ArrayList<Location> map;
047 
048   /** the uuid where the player "is" */
049   private String player;
050 
051   /**
052    * Construct a new game (with just a map). The name of the Location
053    * file and the dictionary are hard coded here. They probably belong
054    * in a configuration file.
055    */
056   public GameWithMapOnly() {
057     aliases = new Dictionary(openFileForInput("school.aliases"));
058     map = readMapFile("school.map");
059     if (map != null{
060       player = map.get(0).getUUID();
061     }
062   }
063 
064   /**
065    * The main game loop. Play until done.
066    */
067   public void play() {
068     gameOver = false;
069     while (!gameOver{
070       // show state
071       System.out.println(locationByUUID(player));
072       // get user input
073       String command = Keyboard.next();
074       // update state
075       processCommand(command);
076     }
077   }
078 
079   /**
080    * Move player East if possible.
081    *
082    @return  true if player moved; false otherwise
083    */
084   private boolean doEast() {
085     Location here = locationByUUID(player);
086     boolean canMove = (here.getEast() != Location.NO_SUCH_LOCATION);
087     if (canMove{
088       player = here.getEast();
089     }
090     return canMove;
091   }
092 
093   /**
094    * Move player North if possible.
095    *
096    @return  true if player moved; false otherwise
097    */
098   private boolean doNorth() {
099     Location here = locationByUUID(player);
100     boolean canMove = (here.getNorth() != Location.NO_SUCH_LOCATION);
101     if (canMove{
102       player = here.getNorth();
103     }
104     return canMove;
105   }
106 
107   /**
108    * Move player South if possible.
109    *
110    @return  true if player moved; false otherwise
111    */
112   private boolean doSouth() {
113     Location here = locationByUUID(player);
114     boolean canMove = (here.getSouth() != Location.NO_SUCH_LOCATION);
115     if (canMove{
116       player = here.getSouth();
117     }
118     return canMove;
119   }
120 
121   /**
122    * Move player West if possible.
123    *
124    @return  true if player moved; false otherwise
125    */
126   private boolean doWest() {
127     Location here = locationByUUID(player);
128     boolean canMove = (here.getWest() != Location.NO_SUCH_LOCATION);
129     if (canMove{
130       player = here.getWest();
131     }
132     return canMove;
133   }
134 
135   /**
136    * Find the location that goes with the given UUID. Uses sequential
137    * search of the {@link #map}.
138    *
139    @param   uuid  the id to search for
140    *
141    @return  null if not found; reference to the given {@link Location}
142    *          otherwise.
143    */
144   private Location locationByUUID(String uuid{
145     for (int = 0; i != map.size()++i{
146       if (map.get(i).getUUID().equalsIgnoreCase(uuid)) {
147         return map.get(i);
148       }
149     }
150     return null;
151   }
152 
153   /**
154    * Open the named file for input, removing any comments from the file
155    * as it is read.
156    *
157    @param   fname  path name (relative or absolute) to file to open
158    *
159    @return  {@link Scanner} attached to the given file or null if
160    *          there was a problem opening the named file
161    */
162   private Scanner openFileForInput(String fname{
163     Scanner opened = null;
164     try {
165       opened = new Scanner(new SansCommentFilterReader(
166             new FileReader(new File(fname))));
167     } catch (FileNotFoundException e{
168       // do nothing (null will be returned which is what we want)
169     }
170     return opened;
171   }
172 
173   /**
174    * Given a command, as entered by the user, process it. Translate the
175    * command through the alias dictionary and then look it up. This is
176    * just a dispatch routine; the real work is done in the handler
177    * commands.
178    *
179    @param  command  the word entered by the user.
180    */
181   private void processCommand(String command{
182     String normalizedCommand = aliases.get(command);
183     if (normalizedCommand.equalsIgnoreCase("north")) {
184       doNorth();
185     } else if (normalizedCommand.equalsIgnoreCase("south")) {
186       doSouth();
187     } else if (normalizedCommand.equalsIgnoreCase("east")) {
188       doEast();
189     } else if (normalizedCommand.equalsIgnoreCase("west")) {
190       doWest();
191     } else if (normalizedCommand.equalsIgnoreCase("exit")) {
192       gameOver = true;
193     } else {
194       unknownCommand(command, normalizedCommand);
195     }
196   }
197 
198   /**
199    * Read the map file. Open the named file and read it as locations
200    * into the array list returned by the method.
201    *
202    @param   fname  name of location file
203    *
204    @return  ArrayList<Location> conatining all of the read locations.
205    */
206   private ArrayList<Location> readMapFile(String fname{
207     ArrayList<Location> returnMap = null;
208     Scanner mapFile = openFileForInput(fname);
209     if (mapFile != null{
210       returnMap = new ArrayList<Location>();
211       while (mapFile.hasNext()) {
212         Location location = Location.readObjectFromFile(mapFile);
213         if (location != null{
214           returnMap.add(location);
215         }
216       }
217     }
218     return returnMap;
219   }
220 
221   /**
222    * Tell the user that their command was not understood. The parameters
223    * are the pre and post translation versions of the command. That way,
224    if one wants, one can show the internal command alias that was
225    * found.
226    *
227    @param  playerProvided  just what the player typed
228    @param  translated      the internal alias
229    */
230   private void unknownCommand(String playerProvided,
231     String translated{
232     System.out.println("Unable to understand comamnd " +
233       playerProvided);
234   }
235 }
236 
237 //Uploaded on Mon Mar 29 21:39:00 EDT 2010


Download/View scg/ch14/core/GameWithMapOnly.java





Views
Personal tools
Add to 
del.icio.usAdd to 
diggAdd to 
FacebookAdd to 
favoritesAdd to 
GoogleAdd to 
MySpaceAdd to 
PrintAdd to 
SlashdotAdd to 
StumbleUponAdd to 
Twitter

Games
Games