Big cleanup, removing much of the non-repl code.
- No longer functioning outside of repl
This commit is contained in:
parent
3830cdbed4
commit
fbdf8129f5
1 changed files with 19 additions and 55 deletions
|
@ -11,65 +11,27 @@
|
|||
;; Actual Tic Tac Toe game
|
||||
(define ggg-size 3) ;; tic tac toe with more than 3x3 grid?
|
||||
|
||||
(define (^ggg-controller bcom initiator? arg-peer)
|
||||
(format #t "Creating gobbler\n")
|
||||
|
||||
(define (^ggg-controller bcom initiator? peer)
|
||||
(define mark (if initiator? "x" "o"))
|
||||
(define peer-mark (if initiator? "o" "x"))
|
||||
|
||||
(define board (selfish-spawn ^peer-board))
|
||||
|
||||
;; 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)
|
||||
($ board 'choose! coords peer-mark)
|
||||
(move!)]))
|
||||
|
||||
(define receiver (spawn ^move-receiver))
|
||||
|
||||
;; Convert from a prompt to a valid move.
|
||||
;; TODO: validation
|
||||
;; Careful! This does not play nice with the REPL!
|
||||
(define (prompt->move)
|
||||
(format #t "enter move? > ")
|
||||
(map string->number (string-tokenize (read-line (current-input-port)) char-set:digit)))
|
||||
|
||||
;; Do our move.
|
||||
(define (move!)
|
||||
(on (spawn-fibrous-vow (lambda () (prompt->move)))
|
||||
(lambda (move)
|
||||
(format #t "move is ~s\n" move)
|
||||
($ board 'choose! move mark)
|
||||
move)
|
||||
#:promise? #t))
|
||||
|
||||
(define (loop-move peer)
|
||||
(format #t "begin move w ~s\n" peer)
|
||||
(on (move!)
|
||||
(lambda (move)
|
||||
(on (<- peer 'exchange-move receiver move)
|
||||
;; get exchange
|
||||
(lambda (peer-move)
|
||||
;; Actually respond to the move
|
||||
(format #t "Peer move ~s\n" peer-move)
|
||||
($ board 'choose! peer-move peer-mark)
|
||||
;; And now we repeat!
|
||||
(loop-move peer))))))
|
||||
|
||||
;; 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?)
|
||||
(loop-move arg-peer))
|
||||
(define my-turn? (not initiator?))
|
||||
|
||||
(methods
|
||||
[(get-receiver) receiver]
|
||||
;; Learn about the receiver from the peer. Then do our move if we are first.
|
||||
[(register-receiver peer)
|
||||
(when initiator?
|
||||
(loop-move peer))]))
|
||||
;; 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."))]))
|
||||
|
||||
;; Board logic
|
||||
|
||||
|
@ -86,7 +48,7 @@
|
|||
[(choose! coords mark-char)
|
||||
(let* ((mark ($ self 'ref coords))
|
||||
(char ($ mark 'get)))
|
||||
(if (not (not char))
|
||||
(if char
|
||||
(error "coords already chosen:" coords)
|
||||
(begin
|
||||
($ mark 'choose! mark-char)
|
||||
|
@ -102,6 +64,8 @@
|
|||
(map (lambda (i) (format #t "[~a]" (print-mark i)))
|
||||
(array->list x))
|
||||
(format #t "\n")) arr)]))
|
||||
;; Do we need to use SRFI 158, or newra library, to do this?
|
||||
;; [(get-winner) ???]
|
||||
|
||||
(define* (^mark bcom #:optional [mark #f])
|
||||
(methods
|
||||
|
|
Loading…
Reference in a new issue