Basic working ugly tic tac toe moving.

This commit is contained in:
Vivianne 2023-02-11 21:37:38 -08:00
parent ecd912e50d
commit 8ba7ea6a40
2 changed files with 80 additions and 19 deletions

View file

@ -1,60 +1,114 @@
(define-module (gib-gab-gob actors)
#:use-module (gib-gab-gob rps)
#:use-module (gib-gab-gob lib) ;; to go into goblins eventually!
#:use-module (gib-gab-gob utils)
#:use-module (goblins)
#:use-module (goblins actor-lib methods)
#:use-module (goblins actor-lib sealers)
#:use-module (oop goops)
#:use-module (ice-9 match)
#:export (^game-lobby ^client-picker))
(define (^game-lobby bcom)
(define pick (pick-rps))
(methods
[(register-opponent name client sealed-pick)
[(register-opponent name peer sealed-pick)
(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)
(on (<- (<- peer 'pick->unsealer pick) sealed-pick)
(lambda (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?))))]))
;; Transition to gobbler/receiver
(let* ((gobbler (spawn ^gib-gab-gobbler won? #f))
(receiver ($ gobbler 'get-receiver)))
($ gobbler 'register-receiver (<- peer 'register-receiver receiver)))))]))
(define (^client-picker bcom)
(define-values (seal-pick unseal-pick my-pick?)
(spawn-sealer-triplet))
(define pick (pick-rps))
(define won? #f)
(methods
[(get-sealed-pick) ($ seal-pick pick)]
[(pick->unsealer peer-pick)
(define won? (rps-winner pick peer-pick))
(set! 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]))
unseal-pick]
[(register-receiver receiver) ($ (spawn ^gib-gab-gobbler won? receiver) 'get-receiver)]))
;; 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 (^gib-gab-gobbler bcom initiator arg-peer)
(format #t "Creating gobbler\n")
(define (^peer-board bcom self)
(define my-board (selfish-spawn ^peer-board (if initiator "x" "o")))
(define peer-board (selfish-spawn ^peer-board (if initiator "o" "x")))
;; Move Receiver actor which receives moves from the peer.
(define (^move-receiver bcom)
(methods
;; Receive the move from the peer.
[(exchange-move peer coords)
(format #t "exchange-move ~s\n" coords)
($ peer-board 'choose! coords)
(move! peer)]))
(define receiver (spawn ^move-receiver))
;; Convert from a prompt to a valid move.
;; TODO: validation
(define (prompt->move)
(format #t "enter move > ")
(map string->number (string-tokenize (read) char-set:digit)))
;; Do our move, and send it to the peer. We then await its response.
(define (move! peer)
(format #t "begin move w ~s\n" peer)
(on (<- peer 'exchange-move receiver
(let ((move (prompt->move)))
($ my-board 'choose! move)
($ peer-board 'display)
move))
;; get exchange
(lambda (peer-move)
;; Actually respond to the move
(format #t "Peer move ~s\n" peer-move)
($ my-board 'display)
($ peer-board 'choose! peer-move))))
;; Initial logic. If the peer is not set we wait until we receive it.
;; If we don't go first we also wait.
(when (and arg-peer initiator)
(move! arg-peer))
(methods
[(get-receiver) receiver]
;; Learn about the receiver from the peer. Then do our move if we are first.
[(register-receiver peer)
(when initiator
(move! peer))]))
;; Board logic
(define (^peer-board bcom self mark-char)
;; Define the array with unspecified values, then fill
(define arr (make-array *unspecified* ggg-size ggg-size))
(array-map! arr (lambda () (spawn ^mark)))
(methods
[(ref x y) (array-ref arr x y)]
[(chosen? x y) ($ ($ self 'ref x y) 'chosen?)]
[(choose! x y)
(if ($ self 'chosen? x y)
(error "coords already chosen:" x y)
(begin
($ ($ self 'ref x y) 'choose!)
($ self 'display)))]
[(ref coords) (match coords ((x y) (array-ref arr x y)))]
[(chosen? coords) ($ ($ self 'ref coords) 'chosen?)]
[(choose! coords)
(if ($ self 'chosen? coords)
(error "coords already chosen:" coords)
(begin
($ ($ self 'ref coords) 'choose!)
($ self 'display)))]
[(display)
(define (print-mark mark)
(if ($ mark 'chosen?) "x" " "))
(if ($ mark 'chosen?) mark-char " "))
;; this is .. probably messy?
(array-slice-for-each-in-order
1

7
gib-gab-gob/utils.scm Normal file
View file

@ -0,0 +1,7 @@
(define-module (gib-gab-gob utils)
#:export (prompt))
;; Prompt for something in the console and convert it to a string.
(define (prompt text)
(format #t text)
(read))