7.0

Week 10 Set b

home work!

Programming Language ISL+

Due Date Thu at 9:00pm (Week 10)

Purpose To work on multiplayer minicraft and further practice recursion

Finger Exercises

Exercise 1 From HTDP, 347 348

Graded Exercises

Minicraft, All Together Now

Your helpful silent partner has finished her part and has a running minicraft server at "minicraft.ccs.neu.edu", listening on port 10001. She tells you that if you add the following to your big-bang invocation, it will make your client connect to that server:

(register "minicraft.ccs.neu.edu")
(port 10001)

e.g., your call might look like

(big-bang empty-world
    (to-draw render-world)
    (on-key handle-key)
    (on-tick tick-handler 1)
    (on-receive receive-msg)
    (register "minicraft.ccs.neu.edu")
    (port 10001))

All that remains for you to do is extend your client to communicate with the server. You may even have this out before Thanksgiving!

(NOTE: The earlier protocol had a mistake in the 'join message – it said it sent an ID, but actually sent a serialized player, with is a (list ID dir). Week 9 Set a has been updated to reflect this.)

Exercise 2 Add support for the 'map and 'move messages. Please consult Week 9 Set a for the protocol specification, both in terms of what you need to handle the server sending to you (in your on-receive handler) and what you need to send to the server (as part of your on-key handler).

Once you have added this support, you should be able to connect, receive a map (which will be a random tile in 10x10 grid), and move around. When you move off the edge of the grid, you will move onto a new tile, as the server will send you a new 'map message. If you spawn in a place where you cannot move to the edge, you can close the window and reconnect, as each time you will be placed in a random location.

Note that since you have not yet added support for 'join and 'left, you may run into bugs because you are not aware of other players that are on the same tile. If you move to an adjacent tile and return, however, the map that the server gives you will have an accurate representation of everyone who is on the tile.

(You are welcome, of course, to implement the full protocols – the server supports them all, and eventually you will have to.)

Algebraic Simplification

We can define algebraic expressions like 1 + (2 * x) + 3 with the following data definition (we could add division and subtraction, but aren’t for these exercises):

; An Expression is one of:
; - Number
; - Symbol
(define-struct add [left right])
; - (make-add Expression Expression)
(define-struct mul [left right])
; - (make-mul Expression Expression

We can translate expressions in the form you are used to from math class into ISL+ as follows:

; x
'x
; 1 + 2
(make-add 1 2)
; x * (x + 2) * z
(make-mul 'x (make-mul (make-add 'x 2) 'z))
; x + 2 * 3
(make-add 'x (make-mul 2 3))

Now that we have these expressions, we notice that it’s easy to write silly expressions:

(make-add 0 'x) ; same as just 'x
(make-add (make-add 1 2) 0) ; same as just (make-add 1 2)
(make-mul 1 'y) ; same as just 'y
(make-mul (make-add 1 0) 3) ; same as just 3

In particular, we notice that adding 0 to anything and multiplying 1 to anything results in the same as the input.

Exercise 3 Design a function simplify that, given an Expression, returns an expression that has been simplified according to the strategy described above. Note that you should simplify as much as you can, according to the rules described (without doing arithmetic or reordering expressions).

In addition to adding 0 and multiplying by 1, it’s also not necessary to have add and mul when both operands are constants, as we can make equivalent expressions where the expression has been replaced with the result. e.g.,

(make-mul 2 3) ; same as 6
(make-add (make-add 1 2) 0) ; same 3

Exercise 4 Design a function compact that, given a Expression, replaces all constant add and mul expressions with the number that they evaluate to. Note that you should do this as much as possible, but don’t need to re-order expressions in order to do this (i.e., (make-add 2 (make-add 3 'x)) need not be compacted, even thought the equivalent (make-add (make-add 2 3) 'x) would be).