Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Computer Science: Racket Lab10: Lab 10: Location sharing In this lab and in Assignment 10, youll make a universe so that each world can share

Computer Science:

Racket Lab10:

Lab 10: Location sharing

In this lab and in Assignment 10, youll make a universe so that each world can share its location and see others nearby. This lab is about the worlds.

1 Messages

Programs that talk over the network must agree on the format of messages they send to each other. Because a struct is private to each computer and cannot be sent over the network, these messages cannot contain posns. Rather, we use a list that contains exactly two numbers to store the same information as a posn. We call such a list a Place, and make ourselves some tiny helper functions so that it feels like a struct.

; A Place is (list Number Number)
; *Interpretation*: x and y coordinates
; make-place : Number Number -> Place
(define (make-place x y) (list x y))
; place-x : Place -> Number
(define (place-x p) (first p))
; place-y : Place -> Number
(define (place-y p) (second p))

(The function second is built-in, like first and rest.) We call what a world sends to the universe an Upload.

; An Upload is a Place

Exercise 1 Write two examples of Uploads.

As for what the universe sends to a world, we want to associate Names with Places, but again we cannot store the association in a struct. Rather, we use a list that contains exactly a Name and a Place to store the same information. We call such a list a LabeledPlace, and make ourselves some more tiny helper functions so that it feels like a struct.

; A Name is a String
; *Interpretation*: the name of a world
; A LabeledPlace is (list Name Place)
; make-labeled-place : Name Place -> LabeledPlace
(define (make-labeled-place s p) (list s p))
; labeled-place-label : LabeledPlace -> Name
(define (labeled-place-label lp) (first lp))
; labeled-place-place : LabeledPlace -> Place
(define (labeled-place-place lp) (second lp))

We call what the universe sends to a world a Download. Its not a single LabeledPlace but a list of them.

; A Download is [ListOf LabeledPlace]

Exercise 2 Write two examples of Downloads.

Heres how a world and the universe will interact:

When the world moves (because its user clicks the mouse), the world uploads its new Place to the universe.

To stay posted about its neighborhood, a world downloads a [ListOf LabeledPlace] from the universe.

2 Worlds

Once connected to the universe, a world needs to do three things: draw its neighborhood, download its neighborhood, and upload its place.

; A World is a Download

Exercise 3 Design a function place-labeled-place that takes a LabeledPlace and an Image, and returns a new Image by placing the given LabeledPlace on the given Image. Draw the Name of the given LabeledPlace as a piece of text.

Exercise 4 Design a function draw that takes a world and draws it as an image of a reasonable size. Use foldr and place-labeled-place.

Exercise 5 Write a big-bang program that draws a Download in a window. Use one of the example Downloads you wrote in Exercise 2 as the (initial) world.

(big-bang ...
[to-draw draw])

Exercise 6 Expand your big-bang program to connect to the universe. Get the address of the universe (the register and the port) from your lab instructor. Enter your name.

(big-bang empty
[to-draw draw]
[on-receive (lambda (w download) download)]
[register ...]
[port ...]
[name ...])

Exercise 7 What is the signature of (lambda (w download) download) above? What is its purpose?

Exercise 8 Finish designing this on-mouse handler and add it to your big-bang program:

; A WorldResult is one of:
; - World
; - (make-package World Upload)
; mouse : World Number Number MouseEvent -> WorldResult
; Send the place to the universe when the mouse is clicked
(check-expect (mouse empty 50 60 "move") empty)
(check-expect (mouse empty 50 60 "drag")
(make-package empty (make-place 50 60)))

To run many worlds at the same time, either open multiple tabs in DrRacket, or use launch-many-worlds:

(launch-many-worlds
(big-bang ...)
(big-bang ...))

3 To boldly go

The remaining exercises are independent of each other, but finish one before you start on the other.

Exercise 9 Without changing the universe, let the user zoom or pan the display, like in Assignment 9. Youll need to extend what a World is, to store the current view. Remember that the view needs to be taken into account not only for drawing but also for clicking.

Exercise 10 Let each user choose the color they appear as in public. Use this extended data definition for exchanging messages with the universe:

; A Place is one of
; - (list Number Number String)
; - (list Number Number)
; *Interpretation*: x and y coordinates, and a color

Youll probably also need to extend what a World is. Update everything you designed accordingly.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design For Mere Mortals

Authors: Michael J Hernandez

4th Edition

978-0136788041

More Books

Students also viewed these Databases questions