WEBVTT 1 00:00:00.224 --> 00:00:03.119 In this demo I'm going to show you how a parser object 2 00:00:03.220 --> 00:00:06.119 does its job of turning whatever the player types in 3 00:00:06.221 --> 00:00:10.114 at the programs user interface in to a command object 4 00:00:11.002 --> 00:00:12.220 As you saw in a previous demo 5 00:00:12.224 --> 00:00:14.226 a command object represents a command 6 00:00:14.227 --> 00:00:16.222 with one or two command words 7 00:00:17.002 --> 00:00:19.117 if the first of those words is a question mark 8 00:00:19.229 --> 00:00:22.111 that means that the command is not understood 9 00:00:22.112 --> 00:00:25.000 or doesn't make sense in the context of the program 10 00:00:25.003 --> 00:00:27.000 where the command class is being used. 11 00:00:28.006 --> 00:00:29.220 So lets see what happens when we 12 00:00:29.221 --> 00:00:31.110 create a parser object 13 00:00:31.223 --> 00:00:35.003 I'm going to call the constructor 14 00:00:35.115 --> 00:00:39.002 and I'm prompted for an array of command words 15 00:00:39.004 --> 00:00:41.221 an array of valid command words. 16 00:00:42.222 --> 00:00:45.003 So its not the parsers responsibility 17 00:00:45.005 --> 00:00:47.228 to know what are the valid commands in the game 18 00:00:48.007 --> 00:00:51.222 the parser has to be told what the valid commands are. 19 00:00:53.003 --> 00:00:56.001 I can give the parser object any list of commands I like 20 00:00:56.004 --> 00:00:58.113 and it will use that list in deciding what is and 21 00:00:58.114 --> 00:01:00.115 what isn't a valid command. 22 00:01:01.002 --> 00:01:02.229 So lets give it some commands 23 00:01:03.009 --> 00:01:05.227 how about run 24 00:01:08.005 --> 00:01:09.007 jump 25 00:01:09.227 --> 00:01:11.004 and 26 00:01:12.229 --> 00:01:14.008 stop 27 00:01:14.227 --> 00:01:16.225 that will do fine for now 28 00:01:17.117 --> 00:01:20.004 and the objects created in the object bench. 29 00:01:20.220 --> 00:01:22.229 So lets try parsing some input 30 00:01:23.000 --> 00:01:24.119 I'm going to call the method 31 00:01:24.227 --> 00:01:25.229 getCommand 32 00:01:27.004 --> 00:01:29.225 that opens the terminal window and opens a prompt 33 00:01:29.226 --> 00:01:34.112 so I can type some input, so lets try run 34 00:01:34.225 --> 00:01:37.224 fast that should be a valid command according 35 00:01:38.001 --> 00:01:39.119 to the command list that 36 00:01:39.226 --> 00:01:42.004 I provided for the parser 37 00:01:43.114 --> 00:01:46.110 and that returns a command object 38 00:01:46.113 --> 00:01:48.220 if I inspect that command object 39 00:01:48.223 --> 00:01:52.000 it has two words, a command word and a second word 40 00:01:52.009 --> 00:01:54.221 which are run and fast. 41 00:01:59.002 --> 00:02:01.225 What happens if there are too many words in the input 42 00:02:02.004 --> 00:02:05.118 lets try that lets getCommand again 43 00:02:05.226 --> 00:02:10.119 again I have a command prompt so I can try jump 44 00:02:11.114 --> 00:02:12.112 very 45 00:02:13.002 --> 00:02:14.227 high that should be valid 46 00:02:15.005 --> 00:02:16.223 that its a valid command word 47 00:02:17.006 --> 00:02:20.110 the only problem is there is too many words in the input. 48 00:02:20.117 --> 00:02:22.110 So lets try that 49 00:02:22.226 --> 00:02:26.007 again we get a command object, inspect that 50 00:02:26.223 --> 00:02:28.227 and it only has two words the third word 51 00:02:28.228 --> 00:02:30.220 of the input has been ignored 52 00:02:30.223 --> 00:02:33.112 and a two word command has been created. 53 00:02:37.004 --> 00:02:40.004 what if I enter a one word command 54 00:02:40.008 --> 00:02:42.114 lets try getCommand again 55 00:02:42.117 --> 00:02:45.225 and this time I'm just going to type stop 56 00:02:46.001 --> 00:02:47.228 again its a valid command word 57 00:02:48.006 --> 00:02:50.111 but I'm only typing one word 58 00:02:52.003 --> 00:02:54.000 return there and 59 00:02:54.008 --> 00:02:56.228 re-inspect the command that has returned 60 00:02:57.229 --> 00:03:00.115 the command has the command word stop 61 00:03:00.220 --> 00:03:02.228 and the second word is null. 62 00:03:08.000 --> 00:03:11.007 Finally lets type something that isn't a valid command 63 00:03:12.110 --> 00:03:16.006 call getCommand again this time i'll type 64 00:03:16.225 --> 00:03:19.224 literally some nonsense at the command line 65 00:03:20.226 --> 00:03:23.003 and that does return a command object 66 00:03:23.007 --> 00:03:24.226 if I inspect that object 67 00:03:24.228 --> 00:03:27.114 you can see the command word is a question mark 68 00:03:27.116 --> 00:03:30.220 so thats an unknown or invalid command. 69 00:03:34.112 --> 00:03:36.119 So a parser object if you tell it what 70 00:03:36.220 --> 00:03:38.007 the valid command words are 71 00:03:38.117 --> 00:03:41.223 can take pretty much any input thats typed in 72 00:03:42.000 --> 00:03:44.110 and turn that into a command object 73 00:03:44.114 --> 00:03:46.229 which represents either a valid command 74 00:03:47.006 --> 00:03:50.001 if the input matches one of the valid command words 75 00:03:50.006 --> 00:03:52.004 or an invalid command. 76 00:03:57.223 --> 00:04:00.111 So you've seen in these demos that the command class 77 00:04:00.113 --> 00:04:02.111 doesn't actually have any knowledge of what 78 00:04:02.111 --> 00:04:05.002 are the valid command words for this game 79 00:04:05.222 --> 00:04:08.002 and the parser class doesn't have that knowledge either 80 00:04:08.009 --> 00:04:09.220 so who does know 81 00:04:10.110 --> 00:04:11.225 well thats the players responsibility 82 00:04:11.226 --> 00:04:14.007 lets look at the player class 83 00:04:15.006 --> 00:04:18.117 the player class has a field called 84 00:04:18.220 --> 00:04:20.009 commands which is an array 85 00:04:20.110 --> 00:04:22.006 containing the valid command words 86 00:04:22.009 --> 00:04:25.221 for this game go, quit and help 87 00:04:26.220 --> 00:04:30.002 when a player takes a turn using the takeTurn method 88 00:04:30.228 --> 00:04:33.004 that array of commands is given 89 00:04:33.006 --> 00:04:36.007 to the parser object that is created there 90 00:04:37.009 --> 00:04:40.002 that parser object is then used by the player 91 00:04:40.005 --> 00:04:42.008 to get a command 92 00:04:42.112 --> 00:04:45.116 and the player can then process that command object 93 00:04:45.220 --> 00:04:48.224 to do whatever action needs to be done in that turn.