#lang htdp/isl ;; Herding Cats, a Start (require 2htdp/image) (require 2htdp/universe) ;; --- DATA DEFINITIONS -------------------------------------------------------- ;; [[ from the How to Design Programs/2e: ]] ;; N is one of: ;; -- 0 ;; -- (add1 N) ;; INTERPRETATION the natural (counting) numbers ;; Use sub1 as selector and zero? and positive? as predicates. ;; (define MOVEMENTS '(straight turn-left turn-right)) ;; A Movement is a member of MOVEMENTS. ;; (define DIRECTIONS '(left right up down)) ;; A Direction is a member of DIRECTIONS. ;; (define-struct cat [dir loc steps]) ;; A Cat is a (make-cat Direction Posn [Listof Movement]) ;; INTERPRETATION ;; - dir is the direction the cat is facing ;; - loc is the location of the cat ;; - steps is the list of steps the cat will follow ;; EXERCISE 1: Develop data examples using quasiquote and unquote. ;; EXERCISE 2: Develop a template for functions that process N and [Listof Cat] ;; (separately). ;; --- CONSTANTS --------------------------------------------------------------- (define WIDTH 500) (define HEIGHT 500) (define BACKGROUND (empty-scene WIDTH HEIGHT)) (define CAT-IMG (circle 20 "solid" "orange")) ;; <-- insert your favorite cat picture here, then delete this comment (define CENTER (make-posn (/ WIDTH 2) (/ HEIGHT 2))) ;; --- MAIN ------------------------------------------------------------------- ;; N N -> [Listof Cat] ;; Simulate this many cats, ;; each following a random pattern of the given # of steps (define (main cats# steps#) (big-bang (all-the-cats-in-the-world cats# steps#) [to-draw draw-all-cats] [on-tick all-cats-move] [stop-when is-any-cat-out-of-steps?])) ;; EXERCISE 3 ;; N -> [Listof Movement] ;; creates a list of n Movements (define (create-steps n) '()) ;; EXERCISE 4 ;; [Listof Cat] -> [Listof Cat] ;; Every cat in the list follows the next step in its movement pattern (define (all-cats-move loc) ;; FIX ME I'M A DUMMY HEADER loc) ;; EXERCISE 5 ;; [Listof Cat] -> Boolean ;; Are any of the cats out of steps to take? (define (is-any-cat-out-of-steps? t) #f) ;; --- PROVIDED FUNCTIONS ------------------------------------------------------ ;; [Listof X] -> X ;; Picks a random item from lox (define (random-choice lox) (list-ref lox (random (length lox)))) ;; --- COMPLETED FUNCTIONS ----------------------------------------------------- ;; ***************************************************************************** ;; The functions below use concepts that we will start covering TOMORROW. ;; Do not attempt to follow these examples at home just yet. ;; ***************************************************************************** ;; N N -> [Listof Cat] ;; Produces a list of cats# cats, each with steps# steps to follow (define (all-the-cats-in-the-world cats# steps#) (local (;; Any -> Cat (define (create-cat _) (make-cat (random-choice DIRECTIONS) (make-posn (random WIDTH) (random HEIGHT)) (create-steps steps#)))) (build-list cats# create-cat))) ;; ----------------------------------------------------------------------------- ;; [Listof Cat] -> Image ;; Draw all the cats onto the background (define (draw-all-cats loc) (add-cats-to loc BACKGROUND)) ;; [Listof Cat] Image -> Image ;; Adds the cats to the given image (define (add-cats-to loc img) (local (;; Cat Image -> Image (define (add-one-cat-to cat img) (place-cat-at (cat-loc cat) img)) ;; Posn Image -> Image (define (place-cat-at p img) (place-image CAT-IMG (posn-x p) (posn-y p) img))) (foldr add-one-cat-to img loc)))