so continue on from our previous video where we learnt that about the split
method so we know now how to use that to split down the string to individual parts
which is great for you know sort of enabling you to actually pass a string
and to grab each individual elements in that string so what we gonna do now is
we going to comment out this bit of code because we don't need any more, actually I’ll just delete that
now and we'll come back here to this loop because we want to uncomment that because of all this is going to be part of
our challenge so we got the code back in there again that we worked on in the
previous video
so what we want to do is clean it up a little bit and I'll come up here and
type in what the challenge
actually is
ok so here is the challenge
so what I want you to do is to change the program to allow players to type in full
words or phrases and then to move to the correct location based upon that input
so you need to pass that input and grab the key parts so the player should be able to type
commands such as go west or run south, or just type east obviously without
the double quotes and the program will move to the appropriate location
if there is one now as at present an attempt to move in an invalid direction
one that doesn't exist for a given location should print that message about
you can't move in that direction and player should remain in the same location
of that event and the single letter commands north,west,south,east and q
for the first letters of those N,W,S,E,Q they should still be available as well
so thats your challenged go away now and see whether you can get that working based
on the code you see below and when you're ready to come back and check
your solution against mine, comeback and will go through it
ok hopefully
you figure it out and able to get it going so as it turns out there is only two
changes that need to be made for this challenge the first thing we need to
create a map that containing the words that will be recognized and
the locations that will result then add a bit of code to split the players input a bit
more than the single characters entered now after splitting the words in the players
input will each have to be checked to see if they are keyed in the map and if they
are the single value was returned and otherwise no action is taken and then
the next word is checked and if none of the words match then the loop will terminate and
of course the attempt to look at the input in the exits map will fail
so what we need to do is come down here after our locations so after we add the exits
as well and what we want to do is put in some extra commands and equivalent
command is going to be so what we want to do is create a map so its going to be…
and will call this… and now we need put our command so we gonna put
vocabulary… so the word quit gets converted to Q and…
…so thats our vocabulary, the extra words that we're now going to support
that we are going to be looking for and what we need to do then is coming down
here to our code, after we got the direction on line 65
we need to add a checking here so what we need to do then is specifically check
the direction which is the variable that’s accepted the data from the player and see if
the link is greater than one, so if direction…
if its greater that one that means not a single character commands and therefore we need to do
some extra processing and of course if it's not then we just ignore it
all together we just continue on, but in this case it is greater then what we need to
do is we need to split the contents of the input so its going to be…
…we are gonna split on a space so that we can get it into
our words string array and we’re going to loop through those words so…
…and now we do our test, so now we need to do a test here to
see whether a particular word is in the vocabulary so…
so if it does find that word then we just set our direction…
so you can see what we're doing effectively is were ignoring all
the other words , so if I type run south go south or please go south were
ignoring the other words other than the word that is existing in our vocabulary
map in which case it would only be south and if that's the case if it finds that
that word has been typed remember were converting it to upper case
so were going to accept upper or lower case automatically cause its going to be converted
then you can say it's using the get to retrieve the value from the map based
on the key value and thats gonna be the abbreviated single letter command and so
consequently once we break out directions going to contain a single
the single letter command and we should be able to go
to that direction and obviously the direction length wasn't greater one the alls this code gets ignored
and move down to line 76 and of course that still process it as if it was a
single letter command as well so let's give this a go and see whether it works like
first we want to check if our single letter commands still works so S, north
that was good and we should be able to type in north…Your in the forest, south, your standing at the
end of the road, please sir can I go
west immediately and you can see its gone west which is good as we want to
do, and how about north right now, you are in the forest, and I really need to quit this game, and it actually
work so you can see that it's almost like an attempted to for someone who
didn't know better at artificial intelligence they look at that and go from there and say wow from there you
figured out I want to go west but of course the reason that worked was we are
looking for the key west and only for that keyword and ignore everything else and did a look
up in our map and worked out, that if I typed in West that means a W and we already know from up here that a W is
for a particular room that is valid and that's our location are indicated to go in a
particular direction in west in this case so that's it that's the solution I
hope you enjoyed that and got a lot out of it and the next day we gonna move on
to creating immutable classes we are going to talk about why you want to do that
and the advantages and why can be a good technique and we're going to base
this some more on this particular game that we're putting together so see you in the
next video