Non-Homework 6: IO

CIS 194: Homework 6
This homework is optional (due to the fall brake). If you do it, you can still send your solution to the TA for feedback.

The general remarks about style and submission from the first week still apply.

Exercise 1: ASCII-Sokoban

The goal of this homework is to take your Sokoban game from Homework 4, and repace the CodeWorld environment with your own, text based environment.

You can make the go function detect presses of q to quit the game. In this case, simply do not call go again.

Handling arrow keys is a bit tricky, as pressing the “→” key actually yields the sequence "\ESC[C"?. (Simply run getLine in ghci, press a key and then enter to see that.), but pressing the escape key produces simply "\ESC". That is tricky!

Here is one way of handling it:

  1. Create a function getAllInput :: IO [Char] that uses getChar to read a character, then uses hReady stdin from System.IO to check if there is another character to read, and so on, until all characters that are currently available are read.

  2. Pattern match on the initial segment of that list to detect '\ESC':'[':'C':rest, or '\ESC':rest, or simply c:rest, and act accordingly.

  3. Pass the unmatched portion of the input on to the next iteration of go, and prepend it to what you read from readAllInput before matching on it, so that no key presses are lost.

If you are still bored, you can make it colorful, using ANSI escape sequences!

Enjoy your own sokoban.


  1. If you like group theory: it’s a conjugate!