In this demo I'm going to show you how a parser object
does its job of turning whatever the player types in
at the programs user interface in to a command object
As you saw in a previous demo
a command object represents a command
with one or two command words
if the first of those words is a question mark
that means that the command is not understood
or doesn't make sense in the context of the program
where the command class is being used.
So lets see what happens when we
create a parser object
I'm going to call the constructor
and I'm prompted for an array of command words
an array of valid command words.
So its not the parsers responsibility
to know what are the valid commands in the game
the parser has to be told what the valid commands are.
I can give the parser object any list of commands I like
and it will use that list in deciding what is and
what isn't a valid command.
So lets give it some commands
how about run
jump
and
stop
that will do fine for now
and the objects created in the object bench.
So lets try parsing some input
I'm going to call the method
getCommand
that opens the terminal window and opens a prompt
so I can type some input, so lets try run
fast that should be a valid command according
to the command list that
I provided for the parser
and that returns a command object
if I inspect that command object
it has two words, a command word and a second word
which are run and fast.
What happens if there are too many words in the input
lets try that lets getCommand again
again I have a command prompt so I can try jump
very
high that should be valid
that its a valid command word
the only problem is there is too many words in the input.
So lets try that
again we get a command object, inspect that
and it only has two words the third word
of the input has been ignored
and a two word command has been created.
what if I enter a one word command
lets try getCommand again
and this time I'm just going to type stop
again its a valid command word
but I'm only typing one word
return there and
re-inspect the command that has returned
the command has the command word stop
and the second word is null.
Finally lets type something that isn't a valid command
call getCommand again this time i'll type
literally some nonsense at the command line
and that does return a command object
if I inspect that object
you can see the command word is a question mark
so thats an unknown or invalid command.
So a parser object if you tell it what
the valid command words are
can take pretty much any input thats typed in
and turn that into a command object
which represents either a valid command
if the input matches one of the valid command words
or an invalid command.
So you've seen in these demos that the command class
doesn't actually have any knowledge of what
are the valid command words for this game
and the parser class doesn't have that knowledge either
so who does know
well thats the players responsibility
lets look at the player class
the player class has a field called
commands which is an array
containing the valid command words
for this game go, quit and help
when a player takes a turn using the takeTurn method
that array of commands is given
to the parser object that is created there
that parser object is then used by the player
to get a command
and the player can then process that command object
to do whatever action needs to be done in that turn.