in the previous video we started off on our adventure game and we had a very
simple mechanism that when we ran it were able to type in a number corresponding to a
location and it would actually the program would print out the location and
description by retrieving it from our map which we defined on line 8 and of course
entering in zero would break out of the loop and ultimately quit the program
and the other thing we had some very basic validation in there
on line 28, on line 27 we accepted the input for the particular location on line
28 we did a check to see whether that key existed in our
map which the key was just the number of this case at this stage for
our location and we get an error message saying you cannot go in that direction
if we couldn't that location in the map
but we have a problem and this what happens
If we run it, and clear a bit ofe space to the left side, like so, will open up the
project window if we need to and you can see that was got our start here now, we can enter a valid
location, 2, thats working ok, but if we enter something like 25 we actually
get an exception so it kinda does quite helpful telling us that we can't go in that
direction then what happens is that it loops back and tries to retrieve that location
and obviously gets itself into difficulty on line 22 when trying to
print out the location based on our get for that particular location now we
won't fix that bug and the reason for that is we are going to extend the program
anyway and if you look back at the locations classic again, remember that
on line 12 we definded an exits field and that's also a map and
that also contains a string for the key and integer value, so sorts of other way round
so getting back to the main of course we had integer and location, but in location here
for the map its string and an integer so the idea here is that the keys is going to
be the direction, where we will start by using the initial letters north-south
N,S,E,W to signify north, south, east and west and q to quit
and the value will be the number of the location where we will get to if we go in that
direction which will be sort of the location ID, and just to help you
understand what we're trying to do here, I’m going to open up an image on the screen
here’s an image on the screen here now and you can see that the various like locations
are indicated as you can see on the screen and there's also the valid directions
the valid exits in other words you can go to, from each particular location so this is
a map but an adventure map not a Java map of the locations that are going to be valid for our game
and just to confirm starting from location one which is the road you can
see that from there we can go in on all 4 compass directions and the arrow show
the locations linked as you can see there, so some links only go 1 way for example
from the valley you can only go one way going west to the hill you can't go back
directly from the hill back to the valley thats the way this mapping works in our adventure
game and so consequently what we're going to do is for example with Valley
what will do is for our map will add an entry W
that standing for west and in the room number in this case is 2 so that would indicate
that room location 4, it's
only exit in this case is to the west which is going to go to location 2, so lets go ahead
and put some code in there now and we go back to main to do that and what we're going to do
here is just below the initial set of locations you can see that we've added
the locations but what we wanna do now is add the exits for each location and
unfortunately map literals is still not available in Java which is a bit
annoying there was talk of including them in Java 8 but it didn't happen
so as a result we really have to use repeated calls to add exit for each location
which is a little painful but it does get the job done
so type locations…so we are getting room one… and locations one, if we go
back here, location 1 is the road so now we are going to map out the available exits and
you can see that we've got
west, north, east, and south, as valid directions from location 1 so what we
gonna do is add exits… and add the key W
and what I’ll do is just copy the following line, and paste it 3 times, so on the west we went to room 2
east and of course we went to room 3, South was room 4…and north to room 5
north…and we need one more and thats quit… Q is our quit keywords, so
just to refresh that so you can understand what we're doing here so from room one
west 2 on line 20 where west goes to room 2, and on line 21 east goes to room 3 south goes
to room 4 and finally north goes to room 5 and of course q is our quit
which goes too location zero which is the special location too indicate that we are finishing the game
so that’s what we're mapping each room, so let's go ahead now and map the rest of the rooms
out and we can continue on, to the next one be for room 2, and for that one
the two options were to go north to room 5 then the only other one was to quit… and the
next one is three and 3 and had a West which went to room one…
and we have to add a quit as well
qui which goes to room 0, next for room 4 we got a north and west option so 4, and that goesto north which goes to room
one which happens to be correct and the next one is still room 4 which goes to
west, we’re going to go to room 2 and of course we need a quit as always which goes to
room 0 ok so were getting through them now, we just got room 5 left, and room 5 has got… and two more for location 5, west is going
to go to room 2… and quit is going to go to room 0… so that should be it so very briefly going back
and looking at our image again, for example location for room 4 which is the
value down here you can see north goes up to
location 1 correctly which is Road and West for locations for on line 33 goes to
hill in location 2 and from the hills
perspective you can see that location two, there are two exits from one to four but
there's no way to go back to them
we’ve got these arrows going multiple ways if there is a case to go back that way so in
other words you can only get to the hill from the road you can't go back from the
hill to the road and likewise you can’t go back from the hill to the valley and
that's why for location two the only exit is to go north to the forest ok so we've
map our exits now for each location and to get this working so that we
can actually used those indicators those directions for our program, don’t need a
lot of changes so what we do is instead of reading and integer, were going to
read a character which we then convert to uppercase to match the keys
in the maps and will also print out the keys on our locations exits map using the key set, a set
that we saw earlier, and we need to do is look up the new location matching the chosen
direction and set the lock
if the direction exists in the map, if the direction isn't a key in the exits then the
locations not changed and consequently its not going to be bugged, and thats why we didn’t bother
fixing this bug that came up previously because we are going to fix it in this version of
the code anyway so the code we need to change, where going to leave the code here after the while that prints out
the description that still valid and the if location equals to 0 to exit if
that's been entered thats still correct
where we are going change it though, is just after that we are going to indicate the directions, so were gonna
put our map in there so its map…
so we are getting the exit that are valid for this particular location and then we are
going to print out… and I’ll get red of that print line for that… and were going to use a for loop
using the keys as I mentioned to go through all the keys which indicate the
directions that are valid for this particular location and we are gonna use exits…
we are gonna point exit…so we can show the
different exits if there is multiple ones and after all of that we need to print line
and there should have been a print line because we want to print that one the same line
so printing line afterwards heart goes to a new line, so what we need to do is we need to
change our scanner and we need to grab a character instead of a numbers so were going to do
string…
…so that you can type
different directions in upper or lower case and then we're going to do we’re
still gonna update the lock variable but we're going to put a test in there first so if
exits…
…then and only then we set the
lock variable and that's no longer going to be a scanner anymore
what that's going to be is exit…so were getting the integer
corresponding to the valid directions that's been hinted based on the key that
has been typed in then we put an else in there…
… and just to confirm on line 47 we’re using our
get access method to retrieve the map of valid exits from the current
location and as get exits returns a copy if we did make any changes to that map it wouldn't
effect the map field in the location instance so now if we run this we should be able to
do walk around a little bit
so there we are we started off in the end of a road with a small brick building
and thats this location so we're in location 1 so valid exits are to quit obviously the south, east,
north, west we can to any of them so let's go north which would take us to the forest, type in
north, in the forest, and just to confirm the forest now, available options quit or
we can go south back to the road or we can go west back to the hill so let's just go back
west first, take us to the hill, and the top of the hill, and the available exits for the
hill are obviously back north again and north should take us back to the forest so
we will go back to the forest again and obviously if we entered a location east
we cannot go to that direction, we're on top of a hill again, so we go back north and if I go back south It should take
us back to the road again and then lets go east we are inside a building, a well house for small spring
ok and just to confirm that
work there's our building again and maybe just to make sure we've done
all the locations will go back west now and then south, so west and south, south takes us to a
value we can go north or west from their and that equates to north back to the road or west back to the
hills so let's go back to the hill again, west, and q to quit, so you can see that our
basic game is working and obviously it's got very little functionality but we
were able to navigate and successfully add the valid exits for each location
and it didn't really take much code as you saw there the majority of the code here was
the initialization aspect
setting up the locations and setting up the valid exits for each
location as well now one observation that i’ll make here
is that the entry q and 0 in other words on line 24, line 27 and line 30 etc.
appears in every locations exit map, so what we could do is we can remove it from main and add it to the
location constructor and that would probably be better way of doing it so you
go back here and just remove this, and ill comment those out, because if we know if there is going to be a location
added each time its best not to put it in there, because its redundant, so I’m commenting all of this out and we'll go back now
to the location and because the location class has got a constructor and we defined
the new HashMap for the particular exits that are valid for this location
we can actually put that in there so we could type in this…
… and its a good idea to do that because that means, for each location thats added
we automatically adding the ability to quit out of the program and that saves a bit of
code obviously, those five lines that were there now have been replaced
with one in the location constructor just to confirm that it still works, we still got our
quit their
we can go south, and we can quit still, so obviously the quit is still working, lets
fix that typo, ok so we are using some of the same map methods that we discussed earlier in the
previous video the main difference now is that the locations.getMethod
returns an instance of the location class rather than string so in other words just
to be clear when using locations.get this method here that's what returning the
actual location object that has been set up that we have define here
when we created the new class for each one of our map entries so that's the beauty
there we got a key now which is the location ID and the value for our locations map is
a location class and from there we can then get the appropriate information
which you can see there on line 47 we are using .exits which gets a copy of the
map of the available exits for that particular location now we got a pretty basic interface, at the moment
you can type end and just like single abbreviations, we can make an
improvement to the user interface to allow the player to type the full words
like north, south, east, and west if they wish and short phrases such as go west an
interesting enough here in our interface that lets you to type go west and have
the computer understand was literally state of the art back in 1976 when the
colossal cave adventure game which is based on was actually written initially
the only thing you need over and above what we've been discussing in order to
implement such an interface as i’ve just mentioned is the String class split method and if that
sounds like this task is going to be your next challenge you are spot on but
first let's just briefly talked about the split method and how to use it and then we
get moving to the next video and start talking about the challenge so we're
going to do is temporarily comment the entire while loop, so I’m just going to comment out this entire while loop code, comment that out
I want to demonstrate the split method so that you get an understanding, I’m gonna copy
and paste the code in to save a bit of time as this video is getting long, I’m pasting the code there and
closing the run window so you can see it, so the first use of split on line 64 creates a
string with 14 items containing each of the 14 words in the locations string so
the split
method breaks up the string into individual items separated by the string
we pass parameters as a split which of course is this text here probably easier if I run this rather
that explain it, so you can see what happened there, on the first code on line 64
it’s gone through and split up each word based on a delimiter which was the space
so if I just close that again and you can see there we've got a little space
character so essentially that’s used as a delimiter so each word that is separated
by space has been added and available to you now as a string in the array on line 64
and the second example it does something very similar but it uses the comma
followed by space and as a consequence we end up with something
containing only two entries this time you're inside a house and a well
house for a small spring and that's because we look up here, we look at the
delimiter that using that to limit which is a comma and a space
so you can see that the split method is very useful for splitting down
a large string into smaller pieces
so I’m going to end the video here in the next video we are going to go through a
challenge to enhance our application so will talk about that challenge in
the next video