Add support for ambiguous 'get', add car keys

This commit is contained in:
Andrew Miner
2018-06-04 22:01:41 -06:00
parent ea93b42eef
commit 151ab29d23
2 changed files with 58 additions and 14 deletions
+40 -9
View File
@@ -13,13 +13,25 @@ configureDefaults = (story)->
"north", "northeast", "east", "southeast", "south", "southwest", "west", "northwest", "up", "down", "in", "out" "north", "northeast", "east", "southeast", "south", "southwest", "west", "northwest", "up", "down", "in", "out"
) )
parser.addFillerWords( 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({ parser.addAliases({
"d": "down", "e": "east", "g": "go", "get": "take", "i": "inventory", "into": "in", "l": "look", "d": "down",
"leave": "exit", "n": "north", "ne": "northeast", "nw": "northwest", "s": "south", "se": "southeast", "e": "east",
"sw": "southwest", "u": "up", "w": "west" "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?") throw new ParseError("I'm not sure what you meant by #{rawWord}... can you re-phrase that?")
@_normalizeSentence(sentence) sentence = @_normalizeSentence(sentence)
@_validateSentence(sentence) @_validateSentence(sentence)
return sentence return sentence
@@ -265,6 +277,13 @@ class Parser
_normalizeSentence: (sentence)-> _normalizeSentence: (sentence)->
if sentence.has(verb: 0, location: 1) if sentence.has(verb: 0, location: 1)
sentence.addWord(new WordToken("go", "verb")) 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 return sentence
@@ -442,9 +461,12 @@ class Player extends Actor
class Sentence class Sentence
constructor: -> constructor: (tokens...)->
@tokens = item: [], location: [], verb: [] @tokens = item: [], location: [], verb: []
for token in tokens
@addWord(token)
# Properties ################################################################################### # Properties ###################################################################################
Object.defineProperties @prototype, Object.defineProperties @prototype,
@@ -460,7 +482,11 @@ class Sentence
# Public Methods ############################################################################### # Public Methods ###############################################################################
addWord: (wordToken)-> 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)-> has: (patternMap)->
patternMap ?= {} patternMap ?= {}
@@ -531,17 +557,22 @@ class Story extends Actor
location.visited = true location.visited = true
endGame: -> endGame: ->
@currentLocation = null
@log.writeln("\n\nThanks for playing! Say: \"restart\" to play again!\n\n\n") @log.writeln("\n\nThanks for playing! Say: \"restart\" to play again!\n\n\n")
interpret: (userInput)-> interpret: (userInput, options={})->
options.silent ?= false
try try
if not options.silent
@log.echoInput(userInput) @log.echoInput(userInput)
@turns += 1 @turns += 1
sentence = @parser.interpret(userInput) sentence = @parser.interpret(userInput)
if @do(sentence) then return if @do(sentence) then return
if @player.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 for item in @currentLocation.inventory.items
if item.do(sentence) then return if item.do(sentence) then return
+14 -1
View File
@@ -10,6 +10,14 @@ window.STORY = new Story "A Walk Through My House", ->
@log.writeln "Oof! It's too heavy." @log.writeln "Oof! It's too heavy."
return false 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 = @addItem "garden hose"
hose.description = "It's a pretty ordinary garden hose about 20' long." 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 = @addLocation "Your Car"
car.description = 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", => car.addVerb "drive", "start", =>
if @player.inventory.has(carKeys)
@log.writeln("Having finished your visit, you drive back home again.") @log.writeln("Having finished your visit, you drive back home again.")
@endGame() @endGame()
else
@log.writeln("You seem to have left your keys somewhere...")
driveway = @addLocation "Driveway" driveway = @addLocation "Driveway"
driveway.description = driveway.description =
@@ -43,6 +55,7 @@ window.STORY = new Story "A Walk Through My House", ->
# Configure Map #################################################################################################### # Configure Map ####################################################################################################
car.addTransition "out", driveway
driveway.addTransition "in", car driveway.addTransition "in", car
driveway.addTransition "southwest", frontPorch driveway.addTransition "southwest", frontPorch
frontPorch.addTransition "north", driveway frontPorch.addTransition "north", driveway