Created
August 22, 2020 19:24
-
-
Save jackie-scholl/edfe8612a6ff25234f3e64786f562670 to your computer and use it in GitHub Desktop.
This file contains 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
#lang slideshow | |
(require slideshow/code) | |
(require data/maybe) | |
(require print-debug/print-dbg) | |
(require rebellion/streaming/reducer) | |
; 'X and 'O | |
(define empty-board (build-list 9 (lambda (x) (nothing)))) | |
(define (rows board) | |
(list | |
(take board 3) | |
(take (drop board 3) 3) | |
(drop board 6)) | |
) | |
(define (count-player board player) | |
(count | |
(lambda (x) | |
(equal? x (just player))) | |
board | |
) | |
) | |
(define lines-list (list | |
(list 0 1 2) (list 3 4 5) (list 6 7 8) ; horizontal lines | |
(list 0 3 6) (list 1 4 7) (list 2 5 8) ; vertical lines | |
(list 0 4 8) (list 2 4 6) ; diagonal lines | |
)) | |
(define (test-line to-test) (match to-test | |
[(list 'X 'X 'X) 'X] | |
[(list 'O 'O 'O) 'O] | |
[(list a b c) nothing])) | |
(test-line (list 'X 'X 'O)) | |
(define (extract-lines board) | |
(map | |
(lambda | |
(temp-line) | |
(map | |
(lambda (x) | |
(list-ref board x)) | |
temp-line | |
) | |
) | |
lines-list | |
) | |
) | |
(define (get-empties board) (filter (lambda (x) (nothing? (list-ref board x))) (range 9))) | |
(define (count-empties board) (length (get-empties board))) | |
empty-board | |
(define (print-space p) (match p | |
['X "X"] | |
['O "O"] | |
[nothing "_"])) | |
(define (print-row row ) (string-join (map print-space row))) | |
(define (print-board board) (string-join (map print-row (rows board)) "\n")) | |
(display (print-board empty-board)) | |
(display "\n") | |
(display (extract-lines empty-board)) | |
(define (winner-helper board) | |
(filter | |
(negate nothing?) | |
(map test-line (extract-lines board)) | |
) | |
) | |
(define (check-winner board) | |
(let | |
([winner (winner-helper board)] | |
) | |
(if | |
(null? winner) | |
nothing | |
(car winner)) | |
) | |
) | |
(define (get-current-turn board) | |
(if | |
(= (modulo (count-empties board) 2) 0) | |
'O | |
'X | |
) | |
) | |
(get-current-turn empty-board) | |
(define (move board index) | |
(if | |
(just? (list-ref board index)) | |
(display "err") | |
(list-set board index (get-current-turn board))) | |
) | |
(define (score-helper board player) | |
(score | |
(move board (choose-move board)) | |
player | |
) | |
) | |
(define (score board player) | |
(let ([winner (check-winner board)]) | |
(if (nothing? winner) | |
(if (= 0 (count-empties board)) | |
0 | |
(* .9 (score-helper board player))) | |
(if (eq? winner player) 1 -1))) | |
) | |
(define (choose-move board) | |
(car | |
(sort | |
(get-empties board) | |
> | |
#:key (lambda (c) (score (move board c) (get-current-turn board))) | |
#:cache-keys? true) | |
) | |
) | |
(define sample-inputs (range 9)) | |
(define sample-board (reduce-all (make-fold-reducer (lambda (cur-board item) (move cur-board item)) empty-board) sample-inputs)) | |
(define sample-board2 | |
(reduce-all | |
(make-fold-reducer | |
(lambda (cur-board item) | |
(move cur-board item) | |
) | |
empty-board | |
) | |
(range 3) | |
) | |
) | |
(display "\n") | |
(display (print-board sample-board)) | |
(display "\n") | |
(extract-lines sample-board) | |
(check-winner sample-board) | |
(display (print-board sample-board2)) | |
(display "\n") | |
(score sample-board2 'X) | |
(score sample-board2 'O) | |
(choose-move sample-board2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment