finish following scala code based on comment instructions Here is the code: /* Sample…
- User
finish following scala code based on comment instructions Here is the code: /* Sample code to get adjective def getAdjectives: List[String] = word.flatMap(g => getWordList(g.desc).init).distinct.sorted */
object CommandAnalyzer {
case class GameObj(desc: String, kind: String)
val grammar: List[String] = List( "look", "inventory", "go {direction}", "climb {object}", "take {object}", "drop {object}", "examine {object}", "search {object}", "sit on {object}", "lie on {object}", "open {container}", "close {container}", "lock {container}", "unlock {container}", "put {item} in {container}", "put {item} on {supporter}", "wear {clothing}", "take off {clothing}", "tie {item} to {object}", "talk to {person}")
val world: List[GameObj] = List( GameObj("north", "direction"), GameObj("south", "direction"), GameObj("east", "direction"), GameObj("west", "direction"), GameObj("comfy chair", "object"), GameObj("shabby twin bed", "object"), GameObj("elegant carpet", "object"), GameObj("soccer ball", "item"), GameObj("beach ball", "item"), GameObj("small green frog", "item"), GameObj("small tree frog", "item"), GameObj("large wooden box", "container"), GameObj("flimsy cardboard box", "container"), GameObj("solid wooden table", "supporter"), GameObj("glass side stand", "supporter"), GameObj("purple hoodie", "clothing"), GameObj("leather jacket", "clothing"), GameObj("very old man", "person"), GameObj("very young woman", "person"))
// ========================================================== // helper functions // ==========================================================
// turn a string of words separated by spaces into a list of those words def getWordList(words: String): List[String] = { // ========================================================== // functions on world // ==========================================================
// Note: All lists of string that are returned from the following methods should be distinct (no duplicates) and they // should be in sorted order. // returns a list of all the adjectives (all but last words in desc) in world // example: cardboard, flimsy, very [ but not 'box' ] words.split(" ").dropRight(1).sorted.reverse.toList} def getAdjectives: List[String] = { // returns a list of nouns (last words in desc) from world // example: north, bed, man }
def getNouns: List[String] =
// returns a list of game-object kinds from world // example: direction, item, object def getGameObjectKinds: List[String] =
// returns a list of game-objects associated with a particular noun (no need to be sorted) // example: getGameObjects("frog") => // List(GameObj("small green frog", "item"), GameObj("small tree frog", "item")) def getGameObjects(noun: String): List[GameObj] =
// ========================================================== // functions on grammar // ==========================================================
// Note: All lists of string that are returned from the following methods should be distinct (no duplicates) and they // should be in sorted order.
- Assistant