Created
February 8, 2012 20:09
-
-
Save larsen/1772927 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Data.Map as Map | |
| type Inventory = [String] | |
| data Direction = North | |
| | South | |
| | East | |
| | West | |
| | Up | |
| | Down | |
| deriving (Eq, Show, Ord) | |
| data Location = Location { | |
| exits :: Map Direction Location, | |
| description :: String } | |
| instance Show Location where | |
| show (Location exits description) = "Sei " ++ description | |
| -- Map definition | |
| cabinaPilotaggio = Location { | |
| exits = fromList [( South, corridoioPrua )], | |
| description = "nella cabina di pilotaggio" } | |
| cabinaSecondo = Location { | |
| exits = fromList [( East, corridoioPrua )], | |
| description = "nella cabina del secondo pilota" } | |
| cabinaComandante = Location { | |
| exits = fromList [( East, corridoio )], | |
| description = "nella cabina del comandante" } | |
| corridoioPrua = Location { | |
| exits = fromList [( North, cabinaPilotaggio ), | |
| ( South, corridoio ), | |
| ( East, compartimentoPasseggeri ), | |
| ( West, cabinaSecondo ) ], | |
| description = "ad un'estremità del corridoio" } | |
| corridoioPoppa = Location { | |
| exits = fromList [( North, corridoio ), | |
| ( East, compartimentoPasseggeri ), | |
| ( West, compartimentoStagno ), | |
| ( South, controlloReattore ) ], | |
| description = "ad un'estremità del corridoio" } | |
| compartimentoStagno = Location { | |
| exits = fromList [( West, corridoioPoppa ) ], | |
| description = "ad un'estremità del corridoio" } | |
| corridoio = Location { | |
| exits = fromList [( North, corridoioPrua ), | |
| ( South, corridoioPoppa ), | |
| ( East, compartimentoPasseggeri ), | |
| ( West, cabinaComandante ) ], | |
| description = "nel corridoio" } | |
| compartimentoPasseggeri = Location { | |
| exits = fromList [( West, corridoio ) ], | |
| description = "nel compartimento passeggeri" } | |
| controlloReattore = Location { | |
| exits = fromList [( North, corridoioPoppa )], | |
| description = "nella salata controllo del reattore" } | |
| type PlayerPosition = Location | |
| go :: Location -> Direction -> Location | |
| go position direction = | |
| case newposition of | |
| Just value -> value | |
| Nothing -> position | |
| where newposition = Map.lookup direction ( exits position ) | |
| describe :: Location -> IO () | |
| describe position = putStrLn $ show position | |
| data AdventureState = AdventureState { | |
| playerPosition :: PlayerPosition, -- Where's the player in the map? | |
| playerInventory :: Inventory -- What is the player carrying? | |
| } deriving (Show) | |
| main = do | |
| mainLoop (AdventureState corridoioPrua []) | |
| mainLoop :: AdventureState -> IO () | |
| mainLoop state | |
| = do | |
| describe (playerPosition state) | |
| putStr "?> " | |
| inputString <- getLine | |
| case inputString of | |
| "N" -> (do | |
| mainLoop $ (AdventureState | |
| (go (playerPosition state) North) | |
| (playerInventory state) ) ) | |
| "S" -> (do | |
| mainLoop $ (AdventureState | |
| (go (playerPosition state) South) | |
| (playerInventory state) ) ) | |
| "W" -> (do | |
| mainLoop $ (AdventureState | |
| (go (playerPosition state) West) | |
| (playerInventory state) ) ) | |
| "E" -> (do | |
| mainLoop $ (AdventureState | |
| (go (playerPosition state) East) | |
| (playerInventory state) ) ) | |
| "quit" -> (do | |
| putStrLn "Ciao!") | |
| _ -> (do | |
| putStrLn "Comando non riconosciuto" | |
| mainLoop state) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment