;; The first three lines of this file were inserted by DrRacket. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-advanced-reader.ss" "lang")((modname |Final_Project_1(1)|) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #t #t none #f () #f))) ; Authors: Alden, Darryl ; Assitance from: Jose and Marco ; MD: Final Project 1 ; Cisc: Cisc 106_48853 ;As a user, I can see a window with a gray background, so I can see where the simulation is taking place. (require 2htdp/image) (require 2htdp/universe) ;------------------------------------------------------------------------------------------------------------------------ ; Purpose: Create a gray background for the simulator to run on ; Signature: magic -> image ; Check-expect: ; image? ; code: (define background (square 500 "solid" "gray") ) ;------------------------------------------------------------------------------------------------------------------------ ; Purpose: count infected ; Signature: list-> number ; examples: (check-expect (count-infected (list)) 0) (check-expect (count-infected (list (make-individual "infected" 10 100 100) (make-individual "susceptible" 1 160 100) (make-individual "recovered" 6 200 200))) 1) (check-expect (count-infected (list (make-individual "susceptible" 1 160 100) (make-individual "recovered" 6 200 200))) 0) ; Code: (define (count-infected lst) (cond ((empty? lst) 0) ((string=? (individual-status (first lst)) "infected") (+ 1 (count-infected (rest lst)))) (else (count-infected (rest lst))) ) ) ;------------------------------------------------------------------------------------------------------------------------ ; Purpose: count recovered ; Signature: list-> number ; examples: (check-expect (count-recovered (list)) 0) (check-expect (count-recovered (list (make-individual "infected" 10 100 100) (make-individual "susceptible" 1 160 100) (make-individual "recovered" 6 200 200))) 1) (check-expect (count-recovered (list (make-individual "susceptible" 1 160 100) (make-individual "infected" 6 200 200))) 0) ; Code: (define (count-recovered lst) (cond ((empty? lst) 0) ((string=? (individual-status (first lst)) "recovered") (+ 1 (count-recovered (rest lst)))) (else (count-recovered (rest lst))) ) ) ;------------------------------------------------------ ;Purpose to take a list of individuals and a background images, display individuals on background. ;signature list image -> image ;examples (check-expect (show-blobs (list) background) background) (check-expect (show-blobs (list (make-individual "infected" 12 30 100)) background) (place-image infected-blob 30 100 background)) (check-expect (show-blobs (list (make-individual "recovered" 12 30 100)) background) (place-image recovered-blob 30 100 background)) (check-expect (show-blobs (list (make-individual "susceptible" 12 30 100)) background) (place-image susceptible-blob 30 100 background)) ; code: (define (show-blobs lst augmented-background) (cond ((empty? lst) augmented-background) (else (place-image (cond ((string=? "infected" (individual-status (first lst))) infected-blob) ((string=? "recovered" (individual-status (first lst))) recovered-blob) (else susceptible-blob) ) (individual-x (first lst)) (individual-y (first lst)) (show-blobs (rest lst) augmented-background) ) ) ) ) ;------------------------------------------------------------------------------------------------------------------------ ; Purpose: Create a funciton that shows scene ; Signature: world-status -> Image (check-expect (show-scene (make-world-status 17 (list))) (overlay/align "left" "top" (text "17" 50 "black") (overlay/align "right" "top" (text (string-append "0" ; We have to turn our number from our count-infected/recovered functions into a string " " ; Space between count-infected and count-recovered "0") 50 "black") background) ) ) (check-expect (show-scene (make-world-status 17 (list (make-individual "susceptible" 12 200 300)))) (place-image susceptible-blob 200 300 (overlay/align "left" "top" (text "17" 50 "black") (overlay/align "right" "top" (text (string-append "0" ; We have to turn our number from our count-infected/recovered functions into a string " " ; Space between count-infected and count-recovered "0") 50 "black") background) ) ) ) ; Code: (define (show-scene status) (show-blobs (world-status-populace status) (overlay/align "right" "top" (text (string-append (number->string (count-infected (world-status-populace status))) ; We have to turn our number from our count-infected/recovered functions into a string " " ; Space between count-infected and count-recovered (number->string (count-recovered (world-status-populace status)))) 50 "black") (overlay/align "left" "top" (text (number->string (world-status-counter-steps status)) 50 "black") background) ) ) ) ; --------------------------------------------- ; Purpose: Write a function that takes a list of individuals, that have moved one pixel in one direction ; Signature: List -> List ; Examples: (check-expect (move-blobs (list)) (list)) ;(check-expect (move-blobs (list (make-individual "infected" 0 50 100))) ; (list (make-individual "infected" 0 60 100))) ; Code: (define (move-blobs lst) (cond ((empty? lst) lst) (else (append (list (make-individual (individual-status (first lst)) (individual-status-duration (first lst)) (- (+ (individual-x (first lst)) (random 2)) (random 2)) (- (+ (individual-y (first lst)) (random 2)) (random 2)) ) ) (move-blobs (rest lst)) ) ) ) ) ;------------------------------------------------------------------------------------------------------------------------ ; Purpose: Create tick-handler for the counter ; Signature: world-status -> world-status ;Example: (check-expect (counter (make-world-status 0 (list ))) (make-world-status 1 (list ))) ; Code: (define (counter status) (make-world-status (+ 1 (world-status-counter-steps status)) (move-blobs (world-status-populace status))) ) (define infected-blob (circle 25 "solid" "red")) (define susceptible-blob (circle 20 "solid" "yellow")) (define recovered-blob (circle 15 "solid" "green")) (define-struct individual (status status-duration x y)) ; String number number number (define-struct world-status (counter-steps populace)) ; number list-of-individuals ; Days past in world list of individuals ;------------------------------------------------------------------------------------------------------------------------ ;Purpose: Stop the simulation!!!! ;Signture: World-status->last-world ;Example: (check-expect (all-infected? (list (make-individual "infected" 10 100 100) (make-individual "infected" 1 200 200) (make-individual "infected" 6 400 400) (make-individual "infected" 4 300 300))) #true) (check-expect (all-infected? (list (make-individual "infected" 10 100 100) (make-individual "susceptible" 1 200 200) (make-individual "susceptible" 6 400 400) (make-individual "recovered" 4 300 300))) #false) ; Code: ;(define (all-infected? lst) ;define all-infected, takes a lst ; (cond ; ((empty? lst) #true) ;if the lst is empty turn true ; ((string=? "recovered" (individual-status (first lst))) #false) ;if the string = recovered of the 1st element of the lst return false ; ((string=? "susceptible" (individual-status (first lst))) #false) ;if the string = susceptible of the 1st element of the lst return false ; (else (all-infected? (rest lst))) ;Otherwise look at the rest of the given list ; ) ; ) ;------------------------------------------------------------------------------------------------------------------------ ; Purpose: Create a window to display the background ; Signature: -> Window ; Code: (big-bang (make-world-status 0 ;Starting the world at 0 (list ;The list of individuals on the screen (make-individual "infected" 10 100 100) (make-individual "susceptible" 1 160 100) (make-individual "recovered" 6 200 200) (make-individual "recovered" 6 300 300) ) ) (to-draw show-scene) ;Showing the scene on each tick (on-tick counter) ;moving the counter on each tick ; (stop-when all-infected?) ) ;As a user, I can see a counter in the top left corner; it counts steps in the simulation, so I can see time passing in the simulation. ; Purpose: Overlay 'timer' over background ;------------------------------------------------------------------------------------------------------------------------ ;Purpose: check if blobs are touching ;Signature: Status->Boolean ;Examples: (check-expect (blobs-touching? (list)) #false) (check-expect (blobs-touching? (list (make-individual "infected" 10 100 100) (make-individual "susceptible" 1 200 200))) #false) (check-expect (blobs-touching? (list (make-individual "infected" 10 200 200) (make-individual "susceptible" 1 200 200))) #true) ; Code (define (blobs-touching? lst) ;Blobs-touching? takes a lst (cond ((empty? lst) #false) ;if the list if empty return false ((= 1 (length lst)) #false) ((and (= (individual-x (first lst)) (individual-x (first (rest lst)))) (= (individual-y (first lst)) (individual-y (first (rest lst))))) #true) (else (blobs-touching? (rest lst))) ) ) ;------------------------------------------------------------------------------------------------------------------------ ;Purpose: Change color of yellow blob to red if they touch ;Signature: Status -> Status ;examples: ;(check-expect (transmission (make-individual "infected" 10 100 100) (make-individual "susceptible" 1 200 200)) (make-individual "susceptible" 1 200 200)) ;(check-expect (transmission (make-individual "infected" 10 100 100) (make-individual "susceptible" 1 200 200)) (make-individual "susceptible" 10 200 200)) ;Code: (define (transmission lst) (cond ((empty? lst) lst) ((blobs-touching?) (append (list (make-individual (individual-status (make-individual "infected")) (individual-status-duration (first lst)) (individual-x (first lst)) (individual-y (first lst)))) (transmission (rest lst)) ) ) ) )
Write, Run & Share Racket code online using OneCompiler's Racket online compiler for free. It's one of the robust, feature-rich online compilers for Racket language, running on the latest version 6.8. Getting started with the OneCompiler's Racket compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as Racket
and start coding.
OneCompiler's Racket online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample Racket program which takes name as input and print your name with hello.
#lang racket/base
(define name (read))
(printf "Hello ~a.\n" name)
Racket is a general-purpose programming language based on the Scheme dialect of Lisp. It is also used for scripting, computer science education, and research related applications.
Item | Decsription |
---|---|
; | To comment a single line |
;; | to mark important comments |
#; | to comment the following s-expression |
Data-type | Decsription |
---|---|
Numbers | represents integers, float and complex numbers |
Boolean | #t and #f are the two boolean literals |
Strings | To represent sequence of characters and double quotes("") are used to represent strings |
let and define are used to declare variables
(let ([id value-expression] ...) body ...+)
(let proc-id ([id init-expression] ...) body ...+)
define id expression
> (let ([x 10]) x)
10
If, If-else are used when you want to perform a certain set of operations based on conditional expressions.
(if cond-expr then-expr)
(if cond-expr then-expr else-expr)
For loop is used to iterate a set of statements based on a condition.
(for (for-clause ...) body-or-break ... body)
where
for-clause = [id seq-expr] | [(id ...) seq-expr] | #:when guard-expr | #:unless guard-expr | break-clause
break-clause = #:break guard-expr | #:final guard-expr
body-or-break = body | break-clause
seq-expr : sequence?
A lambda expression is used to create a function.
(lambda (argument-id ...)
body ...+)