;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; ;; Nim program, not including computer-move and human-move ;; (define play-with-turns (lambda (game-state player) (display-game-state game-state) (cond ((over? game-state) (announce-winner player)) ((equal? player 'human) (play-with-turns (human-move game-state) 'computer)) ((equal? player 'computer) (play-with-turns (computer-move game-state) 'human)) (else (error "player wasn't human or computer:" player))))) (define prompt (lambda (prompt-string) (newline) (display prompt-string) (newline) (read))) (define over? (lambda (game-state) (= (total-size game-state) 0))) (define announce-winner (lambda (player) (if (equal? player 'human) (display "You lose. Better luck next time." ) (display "You win. Congratulations.")))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; ;; Game state ADT implementation ;; (define make-game-state (lambda (i j) (cons i j))) (define size-of-pile (lambda (game-state pile-number) (if (= pile-number 1) (car game-state) (cdr game-state)))) (define remove-coins-from-pile (lambda (game-state num-coins pile-number) (if (= pile-number 1) (make-game-state (- (size-of-pile game-state 1) num-coins) (size-of-pile game-state 2)) (make-game-state (size-of-pile game-state 1) (- (size-of-pile game-state 2) num-coins))))) (define display-game-state (lambda (game-state) (newline) (newline) (display " Pile 1: ") (display (size-of-pile game-state 1)) (newline) (display " Pile 2: ") (display (size-of-pile game-state 2)) (newline) (newline))) (define total-size (lambda (game-state) (+ (size-of-pile game-state 1) (size-of-pile game-state 2)))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; ;; Solution to problem 6.13 (c) ;; (define next-game-state (lambda (game-state move-instruction) (remove-coins-from-pile game-state (num-coins move-instruction) (pile-number move-instruction)))) (define computer-move (lambda (game-state) (let ((pile (if (> (size-of-pile game-state 1) 0) 1 2))) (display "I take 1 coin from pile ") (display pile) (newline) (next-game-state game-state (make-move-instruction 1 pile))))) (define human-move (lambda (game-state) (let ((p (prompt "Which pile will you remove from?"))) (let ((n (prompt "How many coins do you want to remove?"))) (next-game-state game-state (make-move-instruction n p)))))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; ;; Stubs for problem 6.13 (b) ;; (define make-move-instruction (lambda (how-many-coins which-pile) (error "Make-move-instruction hasn't been correctly written yet."))) (define num-coins (lambda (move-instruction) (error "Num-coins hasn't been correctly written yet."))) (define pile-number (lambda (move-instruction) (error "Pile-number hasn't been correctly written yet."))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; ;; Simple strategy of taking one from first non-empty pile ;; (define simple-strategy (lambda (game-state) (if (> (size-of-pile game-state 1) 0) (make-move-instruction 1 1) (make-move-instruction 1 2))))