gib-gab-gob/gib-gab-gob/game.scm

39 lines
1.3 KiB
Scheme

(define-module (gib-gab-gob game)
#: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 initiator? peer)
(define mark (if initiator? 'x 'o))
(define peer-mark (if initiator? 'o 'x))
(define board (make-board))
(define my-turn? (not initiator?))
(define (i-won?) (board-winner? board mark))
(define (peer-won?) (board-winner? board peer-mark))
(define (display)
(board-display board)
(format #t "-> It is ~a turn\n" (if my-turn? "my" "peer's"))
(when (i-won?) (format #t "*** I won! ***\n"))
(when (peer-won?) (format #t "*** I lost! ***\n")))
(display)
(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)
(set! my-turn? (not my-turn?))
(display))
(error "It's my turn!"))]
;; TODO: This needs to go somewhere else so the peer can't move for us!
[(my-turn! x y)
(if my-turn?
(begin
(board-choose! board mark x y)
(set! my-turn? (not my-turn?))
(display))
(error "It's not my turn."))]))