Last active
December 29, 2015 20:59
-
-
Save mavant/7727316 to your computer and use it in GitHub Desktop.
So I was reading The Princess Bride today...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define (generic-agent put drink survive) (lambda (arg) | |
(cond ((null? arg) put) ;; output a glass to put poison into | |
((boolean? arg) (survive arg)) ;; output whether you survive given poison | |
((procedure? arg) (drink arg)) ;; output a glass to drink given the other player's source | |
(else (error "Input type must be one of: Null, boolean, or procedure"))))) | |
(define (contains? list element) (if (member element list) #t #f)) | |
(define (battle-of-wits p1 p2) | |
(define p1lives (not (p1 (contains? (p1 '()) ;;contains returns whether the glass chosen has poison in it | |
(cond ((= (p2 p1) 1 ) 2) ;; this cond chooses whichever glass the other player didn't | |
((= (p2 p1) 2) 1) | |
(else (error "...he drank WHAT?"))))))) | |
(define p2lives (not (p2 (contains? (p1 '()) (p2 p1))))) | |
(cond ((and p1lives p2lives) "They both lived! Some game of death that was...") | |
(p1lives "Player one lives! True love prevails, maybe?") | |
(p2lives "Player 2 lives! Presumably, the Princess will not.") | |
(else "Everybody dies. Hamlet."))) | |
(define fezzik (generic-agent | |
(list 1) | |
(lambda (otherplayer) 1) | |
(lambda (given-poison) given-poison))) | |
(define aristotle (generic-agent | |
(list 2) | |
(lambda (otherplayer) 2) | |
(lambda (given-poison) given-poison))) | |
(define vizzini (generic-agent | |
(list (+ (random 2) 1)) ;; chooses at random to avoid prediction | |
(lambda (otherplayer) | |
(cond ((contains? (otherplayer '()) 1) 2) | |
((contains? (otherplayer '()) 2) 1) | |
(else (error "INCONCEIVABLE!")))) | |
(lambda (given-poison) given-poison))) | |
(define dread-pirate-roberts (generic-agent | |
(list 1 2) | |
(lambda (otherplayer) (+ (random 2) 1)) | |
(lambda (given-poison) #f))) | |
(battle-of-wits dread-pirate-roberts vizzini) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment