Quick peek at some places in code
Main.hs has our Main module definition. It was generated by Stack when we started. In the end of the main function, it calls run function, which is defined in Run.hs file. This is the place where we can see overall flow of the program in one glance.
run :: RIO App ()
run = do
choice <- showMainMenu
case choice of
StarNewGame -> do
logDebug "New game starting..."
logDebug "Rolling new character..."
player <- liftIO $ evalRandIO rollNewCharacter
displayNewCharacter player
logDebug "Selecting starting gear..."
gear <- selectStartingGear $ playerGear player
logDebug "Preparing game..."
game <- liftIO $ evalRandIO $ startGame player gear
logDebug "Dealing first card..."
finishedGame <- playGame game
logDebug "Displaying game over..."
displayGameOver finishedGame
ExitGame ->
return ()
Another interesting module is Types. Here you can find how player, items, monsters and such are represented.
Third and biggest module is UserInterface, which contains functions to display game status to player and ask their input.
So, what does our run function do? Lets have a look:
choice <- showMainMenu
show main menu and ask for player input
case choice of
depending on the choice, continue with game logic or exit the function
player <- liftIO $ evalRandIO rollNewCharacter
roll a new character
evalRandIO indicates we're dealing with random numbers
displayNewCharacter player
display the new character on screen
gear <- selectStartingGear $ playerGear player
select starting gear
game <- liftIO $ evalRandIO $ startGame player gear
shuffle the deck and set up the game
again using random numbers here
finishedGame <- playGame game
play game until we're done
displayGameOver finishedGame
display game over screen
Word about input and output
One of the features of Haskell I like is the ability to show which functions are pure (always returning same output with given set of inputs and not having any side effects). In our program, every function that returns RIO a b has access to input and output. In addition to that, it also has access to system wide configuration (which we don't use much here) and logging functions.
To write on the screen, we use putStrLn and reading a user input readLine. Since they're designed to work with IO instead of RIO a b, we have to use liftIO. But all that is technical details that we won't worry now.
App is our configuration. We aren't directly using it, so it's safe to ignore for now.
Showing main menu
showMainMenu function will print out the menu and then call mainMenuInput. mainMenuInput will read input, validate that it's either 1 or 2 and return respectively StarNewGame or ExitGame. In case user enters something else, mainMenuInput will recurse until user enters valid input.
-- | Display main menu
showMainMenu :: RIO App MainMenuChoice
showMainMenu = do
logDebug "Displaying main menu"
liftIO $ putStrLn "nn