;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; ;; The play-loop procedure takes as its arguments two prisoner's ;; dilemma strategies, and plays an iterated game of approximately ;; one hundred rounds. A strategy is a procedure that takes ;; two arguments: a history of the player's previous plays and ;; a histgory of the other player's previous plays. The procedure ;; returns either a 1 for cooperate or a -1 for defect. ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define (play-loop strat0 strat1) (define (play-loop-iter strat0 strat1 count history0 history1 limit) (cond ((= count limit) (print-out-results history0 history1 limit)) (else (let ((result0 (strat0 history0 history1)) (result1 (strat1 history1 history0))) (play-loop-iter strat0 strat1 (+ count 1) (extend-history result0 history0) (extend-history result1 history1) limit))))) (play-loop-iter strat0 strat1 0 the-empty-history the-empty-history (+ 90 (random 21)))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; ;; The following procedures are used to compute and print ;; out the players' scores at the end of an iterated game ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define (print-out-results history0 history1 number-of-games) (let ((scores (get-scores history0 history1))) (newline) (display "Player 1 Score: ") (display (* 1.0 (/ (car scores) number-of-games))) (newline) (display "Player 2 Score: ") (display (* 1.0 (/ (cadr scores) number-of-games))) (newline))) (define (get-scores history0 history1) (define (get-scores-helper history0 history1 score0 score1) (cond ((empty-history? history0) (list score0 score1)) (else (let ((game (make-play (most-recent-play history0) (most-recent-play history1)))) (get-scores-helper (rest-of-plays history0) (rest-of-plays history1) (+ (get-player-points 0 game) score0) (+ (get-player-points 1 game) score1)))))) (get-scores-helper history0 history1 0 0)) (define (get-player-points num game) (list-ref (get-point-list game) num)) (define *game-association-list* '(((1 1) (3 3)) ((1 -1) (0 5)) ((-1 1) (5 0)) ((-1 -1) (1 1)))) (define (get-point-list game) (cadr (extract-entry game *game-association-list*))); (define make-play list) (define the-empty-history '()) (define extend-history cons) (define empty-history? null?) (define most-recent-play car) (define rest-of-plays cdr) ;; A sampler of strategies (define (meanie my-history other-history) -1) (define (sucker my-history other-history) 1) (define (spastic my-history other-history) (if (= (random 2) 0) 1 -1)) (define (democratic my-history other-history) (define (count-instances-of test hist) (cond ((empty-history? hist) 0) ((= (most-recent-play hist) test) (+ (count-instances-of test (rest-of-plays hist)) 1)) (else (count-instances-of test (rest-of-plays hist))))) (let ((ds (count-instances-of -1 other-history)) (cs (count-instances-of 1 other-history))) (if (> ds cs) -1 1))) (define (eye-for-eye my-history other-history) (if (empty-history? my-history) 1 (most-recent-play other-history))) ;;; other possibly useful procedures (define (get-nth-from-last-choice n history) (list-ref history n)) (define (get-players-action player-no game) (list-ref game player-no)) (define (get-nth-from-last-play n my-history other-history) (make-play (get-nth-from-last-choice n my-history) (get-nth-from-last-choice n other-history)))