so in the last video we add the code to actually read in the directions the
exits in other words but their is a fair amount of code here still and I asked the
question you know what's the advantage of what we have done here and with the
small number of locations that we have at the moment the advantage of this code
compared to just sort of hard coding the location is not really obvious but
theirs actually a downloaded file called gamedata.zip that is in the Resources
section of this video so download that and we are going to use those files instead and you
see that it gets a little easier when we got lots of locations so let's actually
have a look at that so I'm going to swing over to a folder where I already got this
downloading so again you can get this from the Resources section of this video and
unzip it and you can see in the folder now there is going to be 2 files directions_big
and locations_big so what I'm gonna do is go back to my project
and I'm going to right click it
reveal in finder and I'm going to go into my input output folder which is the project
that project name that I have assigned for this one and I'm going to just copy these
files these are the ones that are basically from unzip data file and I'm going to paste them
into their and I'm going to go back to the project and they should appear as they have in the left hand side now what
were going to do is instead of renaming the existing files I'm going to actually
change the references on lines 43 and 64 to use the new names
so its going to be directions_big and locations_big so change that
names and use these files...
and when I have done that and lets have a look at some of them so see what locations_big you
can see we've actually got a lot locations now and this is basically the
entire locations for the entire game so it has quite a few their
and likewise for directions_big theirs now going to be a lot more entries
significant amount of entries so lets just closed some of these files down
and we give this a running again to make sure it still working and you can our input seems to
work because we've got a lot of location data on the screen there and a lot of
imported locations significant amount their so just scroll down to the bottom and
we will just try running a few locations so these are getting hard to read it because of these
locations so lets actually just change the run Windows change it so its down the bottom
so gonna move that to move to bottom we will be able to see it a little bit
better now and just to check lets go south go north North that seems to be working ok now this is
again the locations from the original colossal cave adventure game and then actually
had a random elements so that you don't end up stuck in the forests which is
actually quite easy to do with this version and then we also haven't got any
items such as the key so you can't actually get into the caves at the moment
we can change cheat a bit thought by setting the starting location to say location 64 for
example we can actually sort of moved to a different areas so let's look at doing that
so we go back to main close the run window so what we can do here is on line 29 instead of the location being one lets actually comment that out
and temporary put it to locations 64 which is an entirely different location and that should start us off in a more
complex junctions so lets do that to see that it's actually working
you are now at a complex junction own hands and knees passage from the north joins a
high crawl from the east etc so you should be able to now just do a bit of wondering so go east
...east east again here at wits end go east again....
and that's a good way to get lost anyway it seems to be working that's
good
so if we open up the big locations and directions fiels again so directions_big
locations_big I'll just close this run window down you imagine actually you know typing all that code into
and initialize this manually into your calls and instead of actually reading into a file
so you can see that this is obviously going to be far more efficient when you
got lots of data to read that data in using some code rather than try the type
it directly into our java files that we did initially before we got into this input output
section and these files are actually extracted from the FORTRAN source from the
original colossal cave game you might be wondering why these descriptions are all
in uppercase well that's because most of the terminals at the time and we are
talking the early nineteen seventies then they couldn't display lower case
characters believe it or not but as I mentioned earlier theirs actually a random element to
the game and we haven't implemented that yet and if you get to a secret Canyon
the description does not necessarily mean that you're in the same...
each time in other words the available exits can change but getting back to our
code we have now seen a simple way to write data to a file and to read it back
again and in the process we looked at the finally block to ensure that the
cleanup code is executed even if an exception is thrown and will also
chain objects together we will just go back and have a look at some these code
while I am talking about it you can see a reference of chaining there on line 64 so we also chain these
objects together as I mentioned to allow scanner to read from a BufferedReader that
uses a string to get us data and I covered the trial with resources method
that became available with Java 7 and that resulted in much cleaner code and
we saw an example of that you now I haven't used that in the example that reads that data
back in because it's important to understand how to perform IO with older
Java versions when you have to maintain legacy code that will happen
quite a bit but also the second reason we are doing that secret evil plan was it makes for a
good challenge for you to do it so the code to read the data back is a bit cluttered
catching exceptions and
we don't actually do anything with those exceptions but unfortunately our options are
limited here now when we have code in our main method we can just specify that the
method throws the exceptions and remove the catch block now that approach can't be
taken with a static initialization block and the reason is that the static
initialization block
they're actually executed when the classes loaded so in other words there is no way
for any of our code anywhere to catch any exceptions that are thrown now you can throw
unchecked exceptions in a initialization block but not checked ones unchecked
exceptions if you remembered don't have to be caught but checked exceptions must be
that said we can rewrite the code to use the Java 7 trial resources to make
it a bit tidier and in the next video I am gonna set you in a bit of a challenge to get you to do
just that so stick around and I'll see you in the next video and will have a
challenge for you to undertake for this code see you their