The Command class allows an object
to be created that represents the action
that the player wants to take during a turn.
In this demo I'm going to create a couple
of Command objects and see what they look like.
So let's create a Command object.
In the game a command can consist of two words
known as the commandWord and the secondWord
all one word.
And the Command object can represent either situation.
I'm going to start by
creating a command that has two words.
So I need to provide two parameters
for the constructor representing those two words.
It doesn't actually matter what those words are,
at the moment, the command itself
doesn't have any sense of what is or isn't
a valid command that makes sense in the game.
Checking for valid commands is done somewhere else.
So let's just choose a couple of words
and ok to that.
And the object should be created in the object bench
and there it is.
Let's inspect the object
and I see that it's failed by simply the two words
that I entered when the object was created.
Let's look at the methods.
It has getAMethods for the two fields
and it has a a couple of other methods.
hasSecondWord is going to return true
if the object has a second word
and the one that I created did have two words
so that should return true, which it does.
The last method is called isUnknown
and I want to look at the code for that
before I try calling the method.
So here's the method and that simply returns true
if the commandWord, the first word,
equals the question mark
it will return false if the commandWord
is anything other than a question mark.
So that should for this command that should return
false because the commandWord
wasn't a question mark and indeed it does.
I said when I created the Command object
that the Command class itself
doesn't have a way of checking
whether the words that I entered
make sense as valid commands in the game.
However, when you create a Command object
it is possible to store in that object
the fact that it is an invalid command
and have this reported using the isUnknown method.
Let's create an invalid command.
All I have to do to do that is create a command
with a question mark commandWord
and any other command word we want.
So this time our command if we
call isUnknown should return true.
It may seem a bit strange at the moment
that a command object can't check when it's created,
whether or not it's valid
but it can store the fact of being invalid.
This should make more sense shortly when you see
how commands are actually created in the game.
For now though I'm going to finish this demo
by creating one more Command object
in this case the command with just one word.
So, I need to supply the commandWord as before
but I don't want it to have a secondWord
so I'm just going to put null
in here so there's no second word.
And if I Inspect the object you can see
that the second word field is indeed null
and now if I call the hasSecondWord method
that returns false.
So the Command class is very simple
but it does allow us to represent
commands that have either one word or two words
and are either valid or invalid commands.
However, the Command class itself
doesn't have knowledge
of the valid command words for this game.
That knowledge, as you'll see shortly,
is within another class in the programme.