Compare commits

...

2 commits

Author SHA1 Message Date
da663511f7 Cleanup and proper win checking 2023-07-03 01:09:56 -07:00
3ce3051bef lambdaize 2023-07-03 01:09:24 -07:00
2 changed files with 28 additions and 34 deletions

View file

@ -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)))))

View file

@ -33,7 +33,7 @@
[(register-opponent name peer sealed-pick) [(register-opponent name peer sealed-pick)
(format #t "Hey there, ~a! You sent me your pick of rock-paper-scissors; now I will send mine.\n" name) (format #t "Hey there, ~a! You sent me your pick of rock-paper-scissors; now I will send mine.\n" name)
(on (<- (<- peer 'pick->unsealer pick) sealed-pick) (on (<- (<- peer 'pick->unsealer pick) sealed-pick)
(lambda (peer-pick) (λ (peer-pick)
(define won? (rps-winner pick peer-pick)) (define won? (rps-winner pick peer-pick))
(format #t "Opponent ~a has picked ~a (do I win? ~a). Time to be a controller.\n" name peer-pick won?) (format #t "Opponent ~a has picked ~a (do I win? ~a). Time to be a controller.\n" name peer-pick won?)
(bcom (^game-controller bcom won? peer))))])) (bcom (^game-controller bcom won? peer))))]))
@ -51,8 +51,7 @@
(define lobby ($ mycapn 'enliven lobby-sref)) (define lobby ($ mycapn 'enliven lobby-sref))
(define client-picker (spawn ^client-picker lobby ^game-controller)) (define client-picker (spawn ^client-picker lobby ^game-controller))
(on (<- lobby 'register-opponent user-name client-picker ($ client-picker 'get-sealed-pick)) (on (<- lobby 'register-opponent user-name client-picker ($ client-picker 'get-sealed-pick))
(lambda (_) (λ (_) (format #t "~a finished the game.\n" user-name)))))
(format #t "~a finished the game.\n" user-name)))))
(define (^client-picker bcom lobby ^game-controller) (define (^client-picker bcom lobby ^game-controller)
(define-values (seal-pick unseal-pick my-pick?) (define-values (seal-pick unseal-pick my-pick?)