49 lines
1.6 KiB
Scheme
49 lines
1.6 KiB
Scheme
(define-module (gib-gab-gob game)
|
|
#:use-module (fibers conditions)
|
|
#:use-module (goblins)
|
|
#:use-module (goblins actor-lib methods)
|
|
#:use-module (goblins actor-lib sealers)
|
|
#:use-module (gib-gab-gob board)
|
|
#:export (^ggg-controller))
|
|
|
|
;; Actual Tic Tac Toe game
|
|
(define (^ggg-controller bcom board my-turn+ first? peer)
|
|
(define mark (if first? 'x 'o))
|
|
(define peer-mark (if first? 'o 'x))
|
|
(define %my-turn? first?)
|
|
(define (%state)
|
|
(cond
|
|
((board-winner? board mark) => 'won)
|
|
((board-winner? board peer-mark) => 'lost)
|
|
(else 'play)))
|
|
(define (switch-turn!)
|
|
(set! %my-turn? (not %my-turn?))
|
|
(when %my-turn? (signal-condition! my-turn+)))
|
|
(when first? (signal-condition! my-turn+))
|
|
(methods
|
|
;; The peer is telling us about the turn it took.
|
|
[(peer-turn! x y)
|
|
(if (not %my-turn?)
|
|
(begin
|
|
(board-choose! board peer-mark x y)
|
|
(switch-turn!))
|
|
(error "It's my turn!"))]
|
|
;; TODO: These need to go somewhere else so the peer can't move or init for us!
|
|
[(try-transition) 'playing]
|
|
[(my-turn?) %my-turn?]
|
|
[(my-mark) mark]
|
|
[(state) (%state)]
|
|
[(initialize!)
|
|
(on (<- peer 'try-transition) (λ (status) (format #t "Peer's status: ~a\n" status)) #:promise? #t)]
|
|
[(my-turn! x y)
|
|
(if %my-turn?
|
|
(begin
|
|
(board-assert-vacant board x y)
|
|
(on (<- peer 'peer-turn! x y)
|
|
(λ (_)
|
|
(board-choose! board mark x y)
|
|
(switch-turn!)
|
|
(%state))
|
|
#:promise? #t))
|
|
(error "It's not my turn."))]))
|