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 02:52:44 +00:00
|
|
|
(define mark (if initiator? "x" "o"))
|
|
|
|
(define peer-mark (if initiator? "o" "x"))
|
2023-02-12 05:50:40 +00:00
|
|
|
(define board (selfish-spawn ^peer-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
|
|
|
|
($ board 'choose! coords peer-mark)
|
|
|
|
(set! my-turn? (not my-turn?)))
|
|
|
|
(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
|
|
|
|
($ board 'choose! coords mark)
|
|
|
|
(set! my-turn? (not my-turn?)))
|
|
|
|
(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-02-12 05:50:40 +00:00
|
|
|
(define (^peer-board bcom self)
|
2023-02-06 11:12:28 +00:00
|
|
|
;; Define the array with unspecified values, then fill
|
|
|
|
(define arr (make-array *unspecified* ggg-size ggg-size))
|
|
|
|
(array-map! arr (lambda () (spawn ^mark)))
|
|
|
|
|
2023-02-10 09:02:59 +00:00
|
|
|
(methods
|
2023-02-12 06:34:46 +00:00
|
|
|
;; Switch coords for clarity
|
2023-02-18 20:02:19 +00:00
|
|
|
[(ref coords)
|
2023-02-18 21:40:38 +00:00
|
|
|
(match coords ((x y) (array-ref arr y x)))]
|
2023-02-12 05:50:40 +00:00
|
|
|
[(chosen? coords) (not (not ($ ($ self 'ref coords) 'get)))]
|
|
|
|
[(choose! coords mark-char)
|
2023-02-18 21:40:38 +00:00
|
|
|
(let* ((mark ($ self 'ref coords))
|
|
|
|
(char ($ mark 'get)))
|
2023-07-03 05:14:07 +00:00
|
|
|
(if char
|
2023-02-18 21:40:38 +00:00
|
|
|
(error "coords already chosen:" coords)
|
|
|
|
(begin
|
|
|
|
($ mark 'choose! mark-char)
|
|
|
|
($ self 'display))))]
|
2023-02-10 09:02:59 +00:00
|
|
|
[(display)
|
2023-02-06 11:12:28 +00:00
|
|
|
(define (print-mark mark)
|
2023-02-12 05:50:40 +00:00
|
|
|
(let ((mark ($ mark 'get)))
|
|
|
|
(or mark " ")))
|
2023-02-06 11:12:28 +00:00
|
|
|
;; this is .. probably messy?
|
|
|
|
(array-slice-for-each-in-order
|
|
|
|
1
|
|
|
|
(lambda (x)
|
|
|
|
(map (lambda (i) (format #t "[~a]" (print-mark i)))
|
|
|
|
(array->list x))
|
2023-02-10 09:02:59 +00:00
|
|
|
(format #t "\n")) arr)]))
|
2023-07-03 05:14:07 +00:00
|
|
|
;; Do we need to use SRFI 158, or newra library, to do this?
|
|
|
|
;; [(get-winner) ???]
|
2023-02-06 11:12:28 +00:00
|
|
|
|
2023-02-12 05:50:40 +00:00
|
|
|
(define* (^mark bcom #:optional [mark #f])
|
2023-02-06 11:12:28 +00:00
|
|
|
(methods
|
2023-02-12 05:50:40 +00:00
|
|
|
[(get) mark]
|
|
|
|
[(choose! mark-char)
|
|
|
|
(if mark
|
2023-02-06 11:12:28 +00:00
|
|
|
(error "this mark is already chosen")
|
2023-02-12 05:50:40 +00:00
|
|
|
(bcom (^mark bcom mark-char)))]))
|