Cleanup and proper win checking
This commit is contained in:
parent
3ce3051bef
commit
da663511f7
1 changed files with 26 additions and 31 deletions
|
@ -20,18 +20,18 @@
|
||||||
|
|
||||||
(methods
|
(methods
|
||||||
;; The peer is telling us about the turn it took.
|
;; The peer is telling us about the turn it took.
|
||||||
[(peer-turn! coords)
|
[(peer-turn! x y)
|
||||||
(if (not my-turn?)
|
(if (not my-turn?)
|
||||||
(begin
|
(begin
|
||||||
(board-choose! board peer-mark coords)
|
(board-choose! board peer-mark x y)
|
||||||
(set! my-turn? (not my-turn?))
|
(set! my-turn? (not my-turn?))
|
||||||
(board-display board))
|
(board-display board))
|
||||||
(error "It's my turn!"))]
|
(error "It's my turn!"))]
|
||||||
;; TODO: This needs to go somewhere else so the peer can't move for us!
|
;; TODO: This needs to go somewhere else so the peer can't move for us!
|
||||||
[(my-turn! coords)
|
[(my-turn! x y)
|
||||||
(if my-turn?
|
(if my-turn?
|
||||||
(begin
|
(begin
|
||||||
(board-choose! board mark coords)
|
(board-choose! board mark x y)
|
||||||
(set! my-turn? (not my-turn?))
|
(set! my-turn? (not my-turn?))
|
||||||
(board-display board))
|
(board-display board))
|
||||||
(error "It's not my turn."))]))
|
(error "It's not my turn."))]))
|
||||||
|
@ -41,43 +41,38 @@
|
||||||
(define (make-board)
|
(define (make-board)
|
||||||
(make-array #f ggg-size ggg-size))
|
(make-array #f ggg-size ggg-size))
|
||||||
|
|
||||||
(define (board-ref board coords)
|
(define (board-ref board x y)
|
||||||
(match coords ((x y) (array-ref board y x))))
|
(array-ref board y x))
|
||||||
|
|
||||||
(define (board-choose! board val coords)
|
(define (board-choose! board val x y)
|
||||||
(match coords
|
(define ref (board-ref board x y))
|
||||||
((x y)
|
|
||||||
(define ref (board-ref board coords))
|
|
||||||
(if ref
|
(if ref
|
||||||
(error "That space is already occupied with:" ref)
|
(error "That space is already occupied with:" ref)
|
||||||
(array-set! board val y x)))))
|
(array-set! board val y x)))
|
||||||
|
|
||||||
(define (board-display board)
|
(define (board-display board)
|
||||||
(define (print m) (or m " "))
|
(define (print m) (or m " "))
|
||||||
|
|
||||||
;; this is .. probably messy?
|
;; this is .. probably messy?
|
||||||
(array-slice-for-each-in-order
|
(array-slice-for-each-in-order
|
||||||
1
|
1
|
||||||
(lambda (x)
|
(λ (x)
|
||||||
(map (lambda (i) (format #t "[~a]" (print i)))
|
(map (λ (i) (format #t "[~a]" (print i)))
|
||||||
(array->list x))
|
(array->list x))
|
||||||
(format #t "\n"))
|
(format #t "\n"))
|
||||||
board))
|
board))
|
||||||
|
|
||||||
(define (board-winner? board mark)
|
(define (board-winner? board mark)
|
||||||
|
;; e.g. '(0 1 2)
|
||||||
(define idxs (iota ggg-size))
|
(define idxs (iota ggg-size))
|
||||||
(define (row-winner? y)
|
;; true, if any item in list is non-false
|
||||||
(apply eq? mark (map (lambda (x) (board-ref board (list x y))) idxs)))
|
(define (any? l) (and (any identity l) #t))
|
||||||
(define (col-winner? x)
|
;; Iterate through iota calling fn and check if all are true
|
||||||
(apply eq? mark (map (lambda (y) (board-ref board (list x y))) idxs)))
|
(define (iter-all? fn) (apply eq? mark (map fn idxs)))
|
||||||
(define (diag-winner?)
|
;; Iterate through the rows and see if any are winners
|
||||||
(or
|
(define (row-winner? b)
|
||||||
(apply eq? mark (map (lambda (x) (board-ref board (list x x))) idxs))
|
(any? (map (λ (y) (iter-all? (λ (x) (board-ref b x y)))) idxs)))
|
||||||
(apply eq? mark (map (lambda (x) (board-ref board (list x x))) idxs))))
|
(or (row-winner? board)
|
||||||
|
(row-winner? (transpose-array board 1 0))
|
||||||
(any (lambda (x) (eq? #t x))
|
;; check the two diagonals
|
||||||
(cons
|
(iter-all? (λ (x) (board-ref board x x)))
|
||||||
(diag-winner?)
|
(iter-all? (λ (x) (board-ref board x (- ggg-size x 1))))))
|
||||||
(append
|
|
||||||
(map row-winner? idxs)
|
|
||||||
(map col-winner? idxs)))))
|
|
||||||
|
|
Loading…
Reference in a new issue