πŸ•Ή Game loop state and dictionary

Keep track of game state

There are a few final features to complete our Wordle clone. We need to limit the user to six guesses and read a file representing the dictionary.

First try to write a function,stubs.gameLoop,which does the following:

  • Gets the user's guess from the console
  • Renders the result of the guess with the user's previous guesses
  • Lets the user know if they have guessed successfully or run out of attempts.

Load the dictionary file

Thus far we've been assuming that a bunch of five letter words represented by aSetofTextwill be provided as an argument to our functions. We still need to write the function which generates this data. To do that we'll read in a file.

Try to write a function,stubs.loadDict,which returns the words in thewords file.Each five letter word is separated by a newline character"\n"For the sake of convenience you might want to place it in the same folder where you're calling the UCM from.

Write themainfunction

Yourmainfunction is what the ucmruncommand will execute. It should do the following:

  • Create the target word (hard-coding it is fine for now)
  • Read the dictionary file
  • Kick off the game loop 🍾

When you're done post your wordle in the#beginner-friendly slack channelso the Unison crew and others canprocrastinateplay it! 😁

✨See a breakdown of each of the arguments instubs.gameLoop
✨See a breakdown of each of the arguments instubs.gameLoop

Imagine we callstubs.gameLoopwith the following named arguments:stubs.gameLoop turns target results.

  • turnsis aNatrepresenting the number of guesses a user has had
  • targetis the word of the day
  • resultsis the history of the user's guesses, for rendering purposes
  • TheAskability requirement can be thought of as an environment which provides the dictionary

Think about which of these are tracking state which should change based on the user's input.

πŸ“šHow to read files using theIOability
πŸ“šHow to read files using theIOability

To perform a file read we'll need thefileExists,openFile,getBytes,andcloseFilefunctions frombase,in roughly that order.

  • FilePathis the type representing directories and files.
  • FileModedescribes the possible interactions we can have with the fileβ€”here we'll probably just need theReadmode.
    • When you open a file, the reference that is created for you is aHandle(file handle).

To write thestubs.loadDictfunction, you'll want to pay special attention to what happens when we callgetByteson the same file handle multiple times. Additionally, consider what happens to the file handle if reading the file fails.

✨UnderstandgetByteswith a simple test! πŸ§ͺ
✨UnderstandgetByteswith a simple test! πŸ§ͺ

Create a file called "test" in your working directory. "test" should contain one ten letter word. (I like to use "strawberry" πŸ“)

ThefileTestfunction calls getBytes exactly twice, each time taking 5 bytes (half the length of the word).

fileTest : '{IO, Exception} ()
fileTest _ =
  filePath = FilePath "test"
  fileHandle = openFile filePath Read
  performRead fileHandle =
    first = getBytes fileHandle 5
    second = getBytes fileHandle 5
    text =
      use Text ++
      first |> fromUtf8 ++ (second |> fromUtf8)
    printLine text
  bracket 'fileHandle closeFile performRead

runthefileTestfunction to see what gets printed to the console.

πŸ“šHandling theAskability
πŸ“šHandling theAskability

The handler forAskis calledprovide.

provide : a -> '{g, Ask a} r ->{g} r

When functions callaskfor a value in the environment, it makes sense that the value they should get is supplied byprovide.

The second argument toprovideis a delayed computation.

Note thatprovide 42 '(myAskFunction a b)is different fromprovide 42 'myAskFunction a b.Read more about this difference here.

πŸ“šPackaging and sharing Unison code
πŸ“šPackaging and sharing Unison code

To quickly share your Unison source code, you can use thegistcommand.Gist pushes everything in a given namespace to a remote repository and returns a pull''command representing that namespace to share with other people.

.> cd my.desired.namespace
my.desired.namespace> gist remoteRepo

πŸŽ₯Gist command walk through

You can also create a standalone executable file from your main function for other folks to run with thecompilecommand in the UCM.Read more about standalone binaries here

Resources

Solutions

πŸ”‘basic.gameLoopimplementation
πŸ”‘basic.gameLoopimplementation

basic.gameLoopis a recursive function which determines if the user has won, keeps track of the state of the user's turn, and prints the user's past guesses.

basic.gameLoop :
  Nat
  -> basic.Target
  -> [[basic.Result]]
  ->{IO, Exception, Ask (Set Text)} ()
basic.gameLoop turn target pastGuesses =
  use Nat >=
  if turn >= 6 then printLine "Alas! Better luck next time! 🀑"
  else
    guess = !basic.getUserInput
    result = basic.Guess.score guess target
    results = pastGuesses :+ result
    printLine (basic.renderGuesses results)
    if basic.isVictory result then printLine "πŸŽ‰ Yay! 🎊"
    else
      use Nat +
      basic.gameLoop (turn + 1) target results
πŸ”‘basic.loadDictimplementation
πŸ”‘basic.loadDictimplementation

stubs.loadDictloads theTextfrom the dictionary file as raw text, then splits it on the new line character.

basic.loadDict : FilePath -> '{IO, Exception} Set Text
basic.loadDict filePath _ =
  if fileExists filePath then
    printLine "πŸ“š Dictionary found! This may take a second... ⏰"
    text = ((<|) basic.readFile filePath ())
    t = Text.split ?\n text |> Set.fromList
    printLine "πŸ“— Dictionary created."
    t
  else raise (Generic.failure "Dictionary file not found" filePath)

basic.readFileis the function which handles the file reading as described above. Insidebasic.readFileis a recursive function callinggetBytesin 4096 byte chunks at a time (the size of a page of memory in most OS architectures).

basic.readFile : FilePath -> '{IO, Exception} Text
basic.readFile filePath =
  read : Handle ->{IO, Exception} Bytes
  read fileHandle =
    go acc =
      use Bytes ++
      use Nat <
      bs = getBytes fileHandle 4096
      if Bytes.size bs < 4096 then acc ++ bs else go (acc ++ bs)
    go Bytes.empty
  fileHandle : '{IO, Exception} Handle
  fileHandle _ = openFile filePath Read
  '(bracket fileHandle closeFile (file -> read file |> fromUtf8))
πŸ”‘basic.mainfunction implementation
πŸ”‘basic.mainfunction implementation

basic.mainis our "edge of the world πŸ—Ί" function. We've hard-coded the file path to a dictionary file, and hard-coded the target word for this iteration of the code.

It's here that we provide a handler for theAskability. The handler forAskisprovide,which provides the set of five letter words as its first argument to thebasic.gameLoopfunction which requires it.

basic.main : '{IO, Exception} ()
basic.main _ =
  printLine "πŸ₯³ Welcome to WORDLE!"
  filePath = FilePath "dict"
  dict = !(basic.loadDict filePath)
  target = "party"
  if not (Text.size target === 5) then
    raise
      (Generic.failure
        "The word of the day must be 5 letters long" target)
  else
    printLine "Guess a five letter word"
    provide
      dict '(basic.gameLoop 0 (basic.Target.fromText target) [[]])

Next steps

πŸ‘‰Check out the challenge task for this lab

πŸ‘‰Explore other ways to extend this code