Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
War Games: A Nuclear Simulation
;;; Rishi Goutam; War Games
; Global variables
; Cities - get name by car and population by cdr of (assoc 'name cities)
(define cities '((Shanghai . 14) (Mumbai . 14) (Karachi . 13) (Delhi . 13) (Istanbul . 13) (Moscow. 13) (Seoul . 12) (Beijing . 10) (Jakarta . 10) (Tokyo . 9) (Kinshasa . 9) (Lagos . 8) (Lima . 8) (London . 8) (Bogota . 7) (Tehran . 7) (Bangkok . 7) (Dhaka . 7) (Cairo . 7) (Hanoi . 7) (Lahore . 6) (Chongqing . 5) (Bangalore . 5) (Tianjin . 5) (Baghdad . 5) (Kolkata . 5) (Singapore . 5) (Santiago . 5) (Riyadh . 5) (Surat . 5) (Chicago . 3) (Seattle . 1) (Boston . 1)))
; Invalid targets - should not annihilate American cities
(define american-cities '((Seattle . 1) (Boston . 1) (Chicago . 3)))
; presidential password
(define passwd 'JOSHUA)
; logged in
(define logged-in #f)
; starting number of bombs
(define bombs 3)
; the main program loop. run this to simulate a nuclear scenario
(define (wargames)
(define (repl)
(newline)
(begin
; user interface changes whether logged in
(if logged-in
(display "Input a command: BOMB-CITY, LOGOUT, BOMBS, CITIES, SHOW-MAP ")
(display "Input a command: LOGIN, CITIES, SHOW-MAP "))
(let ((command (read)))
; assign each command to its helper
(begin
(cond ((eq? 'LOGIN command) (process-login))
((eq? 'BOMB-CITY command) (process-bomb-city))
((eq? 'LOGOUT command) (process-logout))
((eq? 'SHOW-MAP command) (process-show-map)) ; always accessible
((eq? 'CITIES command) (display cities) (newline)) ; always accessible
((eq? 'BOMBS command) (process-bombs))
(else
(display "Not a valid command.")))
(repl))))); REPL wargames
(display "Would you like to play a game? ")
(let ((play-game (read)))
(if (or (eq? 'y play-game) (eq? 'yes play-game))
(begin
(newline) ; RULES
(display "War Operation Plan Response allows you to target cities\n")
(display "but prevents you from making an invalid selection to\n")
(display "Each missile dropped on a city reduces the population by 3 million\n")
(repl))
; Quits the REPL loop.
(begin
(display "A strange game. \n")
(display "The only winning move is\n")
(display "not to play.\n")))))
; COMMAND helpers
; logs the president out
(define (process-logout)
(if logged-in
(begin
(display "Goodbye, Mr. President.\n")
(set! logged-in #f))
(display "You are not logged in. Please do so.\n")))
; logs the president in
(define (process-login)
(if logged-in
(display "Silly President. You are already logged in!")
(begin
(display "Type in the password, Mr. President: ")
(begin
(let ((user-password (read)))
(if (eq? user-password passwd)
(begin
(set! logged-in #t)
(display "Welcome, Mr. President. You are now logged in."))
(display "Incorrect password.")))
(newline)))))
; bombs a city that the user inputs. must be logged in as the president
(define (process-bomb-city)
(if logged-in
(begin
(display "Enter a city to bomb from CITIES.\n")
(let ((city (read)))
(if (assoc city cities)
(if (assoc city american-cities)
(display "War Operation Plan Response will not bomb American cities.\n")
(let* ((pair (assoc city cities))
(population (cdr pair)))
(cond ((= 0 population)
(display "This city has no inhabitants. No bomb has been dropped.\n"))
((= 0 bombs)
(display "You do not possess any bombs. No bomb has been dropped.\n"))
(else
(begin
(set! bombs (- bombs 1))
(if (< population 3)
(set! cities (cons (cons city 0) cities))
(set! cities (cons (cons city (- population 3)) cities)))
(display "You have bombed the city and killed 3 million inhabitants."))))))
(display "This city exists only in your imagination.\n"))))
(display "You are not logged in. Please do so.\n")))
; shows number of bombs remaining
(define (process-bombs)
(if logged-in
(begin (display bombs) (newline))
(display "You are not logged in. Please do so.")))
; display an ASCII world map
(define (process-show-map)
(display "``````````````````~?????=+?????=~````````=```````+?``````?``````````````````````\n")
(display "`````````````?++`?~?~`???????????```````?~````````````````??````````````````````\n")
(display "````````````??==????`````????????````````````````?``````?????=`````?~?``````````\n")
(display "``````````?~??~`+`???+```???????~`````````~``````?``??????????????????~=````?```\n")
(display "`???????????~`?++?~=???```????``````````+?????``?????????????????????????????=``\n")
(display "?`????????????????=+~`=```??```````````??`??????????????????????????????????````\n")
(display "``??```??????????```??`~````````````~`?`?`??????????????????????????````````````\n")
(display "`~``````=??????????`????+``````````?```????????????777??????`???????```?````````\n")
(display "``````````????????????=?`?```````````??????????777777777????????????``~`````````\n")
(display "``````````????????+~+?~``````````````???~???`~?=77=7777???????????+`?```````````\n")
(display "``````````???????????``````````````??~```~~????=`7777??????????`?``?````````````\n")
(display "```````````+????????```````````````IIII?`````=====7777?????????``?``````````````\n")
(display "````````````????```?``````````````?IIIIIIIII=======77777I7?????~````````````````\n")
(display "``````````````??```?``````````````IIIIIIIIIII`====``77777?????``````````````````\n")
(display "````````````````??````````````````IIIIIIIIIIII`==````77```??+``?````````````````\n")
(display "`````````````````````+~=``````````IIIIIIIIIIIIII``````?`````=```````````````````\n")
(display "````````````````````++++++`````````?``IIIIIIIIII``````````??``?`````````````````\n")
(display "````````````````````++++++=~```````````IIIII`I`````````````?`?+?+~``````````````\n")
(display "````````````````````++++++++++``````````IIIIII``````````````??``~``??``~````````\n")
(display "`````````````````````++++++++```````````IIIIII```````````````````??`````````````\n")
(display "``````````````````````+++++++```````````IIIII``I```````````````??????```````````\n")
(display "``````````````````````+++++`````````````IIII=`````````````````????????~`````````\n")
(display "``````````````````````++++```````````````III``````````````````????????~`````````\n")
(display "`````````````````````++++```````````````````````````````````````````??``````````\n")
(display "`````````````````````++```````````````````````````````````````````````````?`````\n")
(display "`````````````````````++`````````````````````````````````````````````````````````\n")
(display "`````````````````````+``````````````````````````````````````````````````````````\n")
(display "````````````````````````````````````````````````````````````````````````````````\n"))
; starts an instance of wargames
(wargames)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment