From 151ab29d237e60806fa92f2a07a192e3e5635903 Mon Sep 17 00:00:00 2001 From: Andrew Miner Date: Mon, 4 Jun 2018 22:01:41 -0600 Subject: [PATCH] Add support for ambiguous 'get', add car keys --- game_engine.coffee | 53 ++++++++++++++++++++++++++++++++++++---------- story.coffee | 19 ++++++++++++++--- 2 files changed, 58 insertions(+), 14 deletions(-) diff --git a/game_engine.coffee b/game_engine.coffee index 7207bc1..408a5c0 100644 --- a/game_engine.coffee +++ b/game_engine.coffee @@ -13,13 +13,25 @@ configureDefaults = (story)-> "north", "northeast", "east", "southeast", "south", "southwest", "west", "northwest", "up", "down", "in", "out" ) parser.addFillerWords( - "a", "all", "an", "around", "at", "everything", "of", "it", "the", "to", "thing" + "a", "all", "an", "around", "at", "everything", "my", "of", "it", "the", "to", "thing" ) parser.addAliases({ - "d": "down", "e": "east", "g": "go", "get": "take", "i": "inventory", "into": "in", "l": "look", - "leave": "exit", "n": "north", "ne": "northeast", "nw": "northwest", "s": "south", "se": "southeast", - "sw": "southwest", "u": "up", "w": "west" + "d": "down", + "e": "east", + "g": "go", + "i": "inventory", + "into": "in", + "l": "look", + "n": "north", + "ne": "northeast", + "nw": "northwest", + "s": "south", + "se": "southeast", + "sw": "southwest", + "u": "up", + "w": "west" }) + parser.addVerb("get") ######################################################################################################################## @@ -250,7 +262,7 @@ class Parser throw new ParseError("I'm not sure what you meant by #{rawWord}... can you re-phrase that?") - @_normalizeSentence(sentence) + sentence = @_normalizeSentence(sentence) @_validateSentence(sentence) return sentence @@ -265,6 +277,13 @@ class Parser _normalizeSentence: (sentence)-> if sentence.has(verb: 0, location: 1) sentence.addWord(new WordToken("go", "verb")) + else if sentence.has(verb: 0, item: 1) + sentence.addWord(new WordToken("take", "verb")) + else if sentence.verb is "get" + if sentence.has(location: 1) + sentence = new Sentence(new WordToken("go", "verb"), sentence.tokens.location[0]) + if sentence.has(item: 1) + sentence = new Sentence(new WordToken("take", "verb"), sentence.tokens.item[0]) return sentence @@ -442,9 +461,12 @@ class Player extends Actor class Sentence - constructor: -> + constructor: (tokens...)-> @tokens = item: [], location: [], verb: [] + for token in tokens + @addWord(token) + # Properties ################################################################################### Object.defineProperties @prototype, @@ -460,7 +482,11 @@ class Sentence # Public Methods ############################################################################### addWord: (wordToken)-> - @tokens[wordToken.type].push(wordToken) + tokenList = @tokens[wordToken.type] + for existingToken in tokenList + if existingToken.referant and existingToken.referant is wordToken.referant + return + tokenList.push(wordToken) has: (patternMap)-> patternMap ?= {} @@ -531,17 +557,22 @@ class Story extends Actor location.visited = true endGame: -> + @currentLocation = null @log.writeln("\n\nThanks for playing! Say: \"restart\" to play again!\n\n\n") - interpret: (userInput)-> + interpret: (userInput, options={})-> + options.silent ?= false + try - @log.echoInput(userInput) - @turns += 1 + if not options.silent + @log.echoInput(userInput) + @turns += 1 + sentence = @parser.interpret(userInput) if @do(sentence) then return if @player.do(sentence) then return - if @currentLocation.do(sentence) then return + if @currentLocation and @currentLocation.do(sentence) then return for item in @currentLocation.inventory.items if item.do(sentence) then return diff --git a/story.coffee b/story.coffee index 6a2a2b3..511e8b3 100644 --- a/story.coffee +++ b/story.coffee @@ -10,6 +10,14 @@ window.STORY = new Story "A Walk Through My House", -> @log.writeln "Oof! It's too heavy." return false + carKeys = @addItem "your car keys" + carKeys.description = + "Your keychain has your car key, your house key, and a few beat-up kick-knacks which are scarcely even + recognizable anymore." + carKeys.addVerb "use", "insert", => + if @currentLocation is car + @interpret "drive", silent:true + hose = @addItem "garden hose" hose.description = "It's a pretty ordinary garden hose about 20' long." @@ -17,10 +25,14 @@ window.STORY = new Story "A Walk Through My House", -> car = @addLocation "Your Car" car.description = - "Your car isn't anything special, but it does get you around." + "Your car isn't anything special, but it does get you around. If you're ready to end your visit, just drive + away. If you'd like to continue playing, hop out of the car." car.addVerb "drive", "start", => - @log.writeln("Having finished your visit, you drive back home again.") - @endGame() + if @player.inventory.has(carKeys) + @log.writeln("Having finished your visit, you drive back home again.") + @endGame() + else + @log.writeln("You seem to have left your keys somewhere...") driveway = @addLocation "Driveway" driveway.description = @@ -43,6 +55,7 @@ window.STORY = new Story "A Walk Through My House", -> # Configure Map #################################################################################################### + car.addTransition "out", driveway driveway.addTransition "in", car driveway.addTransition "southwest", frontPorch frontPorch.addTransition "north", driveway