2023-07-03 02:56:06 +00:00
|
|
|
(define-module (gib-gab-gob game)
|
2023-02-03 13:18:17 +00:00
|
|
|
#:use-module (goblins)
|
2023-02-18 20:02:19 +00:00
|
|
|
#:use-module (goblins vat)
|
2023-02-03 13:18:17 +00:00
|
|
|
#:use-module (goblins actor-lib methods)
|
|
|
|
#:use-module (goblins actor-lib sealers)
|
2023-07-02 07:08:23 +00:00
|
|
|
#:use-module (goblins actor-lib selfish-spawn)
|
2023-02-12 05:37:38 +00:00
|
|
|
#:use-module (ice-9 match)
|
2023-02-12 08:42:07 +00:00
|
|
|
#:use-module (ice-9 rdelim)
|
2023-07-03 02:52:44 +00:00
|
|
|
#:export (^ggg-controller))
|
2023-02-06 11:12:28 +00:00
|
|
|
|
|
|
|
;; Actual Tic Tac Toe game
|
|
|
|
(define ggg-size 3) ;; tic tac toe with more than 3x3 grid?
|
|
|
|
|
2023-07-03 05:14:07 +00:00
|
|
|
(define (^ggg-controller bcom initiator? peer)
|
2023-07-03 05:57:40 +00:00
|
|
|
(define mark (if initiator? 'x 'o))
|
|
|
|
(define peer-mark (if initiator? 'o 'x))
|
|
|
|
(define board (make-board))
|
2023-07-03 05:14:07 +00:00
|
|
|
(define my-turn? (not initiator?))
|
2023-02-12 05:37:38 +00:00
|
|
|
|
2023-02-06 11:12:28 +00:00
|
|
|
(methods
|
2023-07-03 05:14:07 +00:00
|
|
|
;; The peer is telling us about the turn it took.
|
|
|
|
[(peer-turn! coords)
|
|
|
|
(if (not my-turn?)
|
|
|
|
(begin
|
2023-07-03 05:57:40 +00:00
|
|
|
(board-choose! board peer-mark coords)
|
|
|
|
(set! my-turn? (not my-turn?))
|
|
|
|
(board-display board))
|
2023-07-03 05:14:07 +00:00
|
|
|
(error "It's my turn!"))]
|
|
|
|
;; TODO: This needs to go somewhere else so the peer can't move for us!
|
|
|
|
[(my-turn! coords)
|
|
|
|
(if my-turn?
|
|
|
|
(begin
|
2023-07-03 05:57:40 +00:00
|
|
|
(board-choose! board mark coords)
|
|
|
|
(set! my-turn? (not my-turn?))
|
|
|
|
(board-display board))
|
2023-07-03 05:14:07 +00:00
|
|
|
(error "It's not my turn."))]))
|
2023-02-12 05:37:38 +00:00
|
|
|
|
|
|
|
;; Board logic
|
2023-02-06 11:12:28 +00:00
|
|
|
|
2023-07-03 05:57:40 +00:00
|
|
|
(define (make-board)
|
|
|
|
(make-array #f ggg-size ggg-size))
|
2023-02-06 11:12:28 +00:00
|
|
|
|
2023-07-03 05:57:40 +00:00
|
|
|
(define (board-ref board coords)
|
|
|
|
(match coords ((x y) (array-ref board y x))))
|
2023-02-06 11:12:28 +00:00
|
|
|
|
2023-07-03 05:57:40 +00:00
|
|
|
(define (board-choose! board val coords)
|
|
|
|
(match coords
|
|
|
|
((x y)
|
|
|
|
(define ref (board-ref board coords))
|
|
|
|
(if ref
|
|
|
|
(error "That space is already occupied with:" ref)
|
|
|
|
(array-set! board val y x)))))
|
|
|
|
|
|
|
|
(define (board-display board)
|
2023-07-03 05:59:01 +00:00
|
|
|
(define (print m) (or m " "))
|
2023-07-03 05:57:40 +00:00
|
|
|
|
|
|
|
;; this is .. probably messy?
|
|
|
|
(array-slice-for-each-in-order
|
|
|
|
1
|
|
|
|
(lambda (x)
|
2023-07-03 05:59:01 +00:00
|
|
|
(map (lambda (i) (format #t "[~a]" (print i)))
|
2023-07-03 05:57:40 +00:00
|
|
|
(array->list x))
|
|
|
|
(format #t "\n"))
|
|
|
|
board))
|