Experiments with doing tic tac toe! So far the board can be written to and displayed. Probably a bit hacky and could be better?

This commit is contained in:
Vivianne 2023-02-06 03:12:28 -08:00
parent aa89fc61a9
commit 598ac34998

View file

@ -13,7 +13,9 @@
(format #t "Hey there, ~a! You sent me your pick of rock-paper-scissors; now I will send mine.\n" name)
(on (<- (<- client 'pick->unsealer pick) sealed-pick)
(lambda (peer-pick)
(format #t "Opponent ~a has picked ~a (do I win? ~a)\n" name peer-pick (rps-winner pick peer-pick))))]))
(define won? (rps-winner pick peer-pick))
(format #t "Opponent ~a has picked ~a (do I win? ~a)\n" name peer-pick won?)
(bcom (^gib-gab-gobbler bcom won?))))]))
(define (^client-picker bcom)
(define-values (seal-pick unseal-pick my-pick?)
@ -22,5 +24,53 @@
(methods
[(get-sealed-pick) ($ seal-pick pick)]
[(pick->unsealer peer-pick)
(format #t "Peer picked ~a... a bold choice (do I win? ~a), I will send my unsealer\n" peer-pick (rps-winner pick peer-pick))
(define won? (rps-winner pick peer-pick))
(format #t "Peer picked ~a... a bold choice (do I win? ~a), I will send my unsealer\n" peer-pick won?)
(bcom (^gib-gab-gobbler bcom won?))
unseal-pick]))
;; Actual Tic Tac Toe game
(define ggg-size 3) ;; tic tac toe with more than 3x3 grid?
(define (^gib-gab-gobbler bcom going-first?)
(methods
[(do-move coords) #f]))
(define (^peer-board bcom)
;; Define the array with unspecified values, then fill
(define arr (make-array *unspecified* ggg-size ggg-size))
(array-map! arr (lambda () (spawn ^mark)))
(define (_ref x y) (array-ref arr x y))
(define (_chosen? x y) ($ (_ref x y) 'chosen?))
(define (_display)
(define (print-mark mark)
(if ($ mark 'chosen?) "x" " "))
;; 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))
(format #t "\n")) arr))
(methods
[(ref x y) (_ref x y)]
[(chosen? x y) (_chosen? x y)]
[(choose! x y)
(if (_chosen? x y)
(error "coords already chosen:" x y)
(begin
($ (_ref x y) 'choose!)
(_display)))]
[(display) (_display)]))
(define* (^mark bcom #:optional [chosen #f])
(methods
[(chosen?) chosen]
[(choose!)
(if chosen
(error "this mark is already chosen")
(bcom (^mark bcom #t)))]))