Hacker Public Radio

HPR3582: Rolling a new character


Listen Later

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
...more
View all episodesView all episodes
Download on the App Store

Hacker Public RadioBy Hacker Public Radio

  • 4.2
  • 4.2
  • 4.2
  • 4.2
  • 4.2

4.2

34 ratings


More shows like Hacker Public Radio

View all
The Changelog: Software Development, Open Source by Changelog Media

The Changelog: Software Development, Open Source

290 Listeners

Defensive Security Podcast - Malware, Hacking, Cyber Security & Infosec by Jerry Bell and Andrew Kalat

Defensive Security Podcast - Malware, Hacking, Cyber Security & Infosec

372 Listeners

LINUX Unplugged by Jupiter Broadcasting

LINUX Unplugged

268 Listeners

SANS Internet Stormcenter Daily Cyber Security Podcast (Stormcast) by Johannes B. Ullrich

SANS Internet Stormcenter Daily Cyber Security Podcast (Stormcast)

651 Listeners

Curious Cases by BBC Radio 4

Curious Cases

822 Listeners

The Strong Towns Podcast by Strong Towns

The Strong Towns Podcast

423 Listeners

Late Night Linux by The Late Night Linux Family

Late Night Linux

164 Listeners

Darknet Diaries by Jack Rhysider

Darknet Diaries

8,055 Listeners

Cybersecurity Today by Jim Love

Cybersecurity Today

180 Listeners

CISO Series Podcast by David Spark, Mike Johnson, and Andy Ellis

CISO Series Podcast

189 Listeners

TechCrunch Daily Crunch by TechCrunch

TechCrunch Daily Crunch

42 Listeners

Strict Scrutiny by Crooked Media

Strict Scrutiny

5,799 Listeners

2.5 Admins by The Late Night Linux Family

2.5 Admins

98 Listeners

Cyber Security Headlines by CISO Series

Cyber Security Headlines

139 Listeners

What the Hack? by DeleteMe

What the Hack?

228 Listeners