Recap - Command Parser
Part of the Logic & Flow section of Coddy's GDScript journey. Lesson 11 of 78.
Challenge
EasyWrite the command parser of a text adventure. The program reads one command per line, until the command quit. Clean each line first: remove the spaces at both ends, convert it to lowercase and split it into words at the spaces, leaving out empty pieces.
Answer each command with one line, using match on the array of words:
- No words:
Type a command. help:Commands: go, take, inventory, quitgoand a direction fromDIRECTIONS:You go DIRECTION.goand any other word:You can't go WORD.takeand an item already ininventory:You already have the ITEM.takeand any other item: add it toinventoryand printYou take the ITEM.inventory:Inventory:and the items separated by a comma and a space, orInventory: emptyquit:Goodbye!, and the program stops reading- Anything else:
Unknown command.
For the input below
help
Go North
go up
take lamp
inventory
quitthe output is:
Commands: go, take, inventory, quit
You go north.
You can't go up.
You take the lamp.
Inventory: lamp
Goodbye!Try it yourself
extends Node
func _ready():
const DIRECTIONS = ["north", "south", "east", "west"]
var inventory: Array[String] = []
# Read and answer commands until the player types quit
All lessons in Logic & Flow
1Strings In Depth
String MethodsSearching in StringsReplace and TrimSplit and JoinRecap - Username Check4Dictionaries Part 2
Nested DictionariesArrays of DictionariesMerging DictionariesCopying DictionariesRecap - Frequency Counter2Advanced Decision Making
Match With VariablesMatch GuardsArray PatternsDictionary PatternsMembership ChecksRecap - Command Parser5Contact Book Application
Display MenuAdd Contact8Student Records Manager
Project OverviewAdd Student3Multi-dimensional Arrays
2D Arrays BasicsAccessing 2D Array ElementsNested Loops with 2D ArraysRecap - 2D ArraysJagged ArraysCommon 2D Array PatternsRecap - Game Board6Sets with Dictionaries
What is a Set?Dictionary as a SetAdding and RemovingUnion and IntersectionSet DifferenceRecap - Remove Duplicates9Advanced Data Aggregation
Summing ValuesFinding Minimum and MaximumCustom SortingRecap - Dictionary SorterPractice on your own: Online GDScript compiler