From 598ac34998588d087004f6a9b2d93b9de35001db Mon Sep 17 00:00:00 2001 From: Vivianne Langdon Date: Mon, 6 Feb 2023 03:12:28 -0800 Subject: [PATCH] Experiments with doing tic tac toe! So far the board can be written to and displayed. Probably a bit hacky and could be better? --- gib-gab-gob/actors.scm | 54 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 2 deletions(-) diff --git a/gib-gab-gob/actors.scm b/gib-gab-gob/actors.scm index d60643a..bb93e43 100644 --- a/gib-gab-gob/actors.scm +++ b/gib-gab-gob/actors.scm @@ -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)))]))