Homework 2: Higher Order functions and Datatypes

CIS 194: Homework 2
Due Tuesday, September 13

The general remarks about style and submittion from last week still apply.

You are free to use the code from last week’s lecture and from your own submission of homework 1. Some of the exercises below reference types and functions from there. In particular, if you have nicely drawn tiles, you should use them instead of my ugly ones.

You learned about local definitions and lambda expressions. Use them when appropriate!

Exercise 1: The small guy (or girl) moves

We hope to have a complete game by next week, so let us work towards that.

Create a value player :: Picture that draws your figure.

Create a value exercise1 :: IO () that calls interactionOf with suitable arguments that:

It might yield nicer code to change the type of maze to Coord -> Tile. If you find that, do not hesitate to make that change.

Exercise 2: Look the right way!

This is an extension of exercise 1. We want to figure to look the way it is going. So write a function player2 :: Direction -> Picture that draws the figure in four variants.

Then extend the code from exercise1 so that after the player has tried to move in some direction, it looks that way. If you can re-use functions and types from exercise1, do so, but if you have to change them, rename them, e.g. by appending a 2 to the name.

Hint: Think about types first (e.g of your state), and then about the implementation.

The resulting program should be called exercise2.

Exercise 3: Reset!

It would be nice to be able to start a game from the beginning. This is generally useful functionality, no matter what the game, so let us implement it generally.

You will write a function

resetableInteractionOf ::
    world ->
    (Double -> world -> world) ->
    (Event -> world -> world) ->
    (world -> Picture) ->
    IO ()

which has the same type as interactionOf.

This function will behave almost the same as interactionOf (which it internally uses, of course), but when Esc is pressed, this event is not passed on, but rather the state of the program is reset to the given initial state.

Style hint: An idiomatic definition of resetableInteractionOf does not require any other top-level definitions, but likely local functions and/or lambda expressions.

Let exercise3 be like exercise2, but using resetableInteractionOf instead of interactionOf.

Exercise 4: New levels

Create a new maze. It should

Use this maze in your exercises above, so that the TA does not get bored while grading.

Also, we will be collecting these mazes and provide them to you so that you can build a sokoban game with different levels next week.