From 748bbfdb3fe327469100da64a33a65eddac68f0f Mon Sep 17 00:00:00 2001 From: Vivianne Langdon Date: Sun, 9 Jul 2023 20:23:00 -0700 Subject: [PATCH] More tweaks to comms for ui --- gib-gab-gob/game.scm | 15 +++++++++------ gib-gab-gob/ui/console.scm | 23 +++++++++++++---------- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/gib-gab-gob/game.scm b/gib-gab-gob/game.scm index 39eea66..23c86b7 100644 --- a/gib-gab-gob/game.scm +++ b/gib-gab-gob/game.scm @@ -12,6 +12,11 @@ (define peer-mark (if first? 'o 'x)) (define %my-turn? first?) (define my-turn+ (make-condition)) + (define (%state) + (cond + ((board-winner? board mark) => 'won) + ((board-winner? board peer-mark) => 'lost) + (else 'play))) (define (switch-turn!) (set! %my-turn? (not %my-turn?)) (when %my-turn? (signal-condition! my-turn+))) @@ -28,11 +33,7 @@ [(try-transition) 'playing] [(my-turn?) %my-turn?] [(my-mark) mark] - [(state) - (cond - ((board-winner? board mark) => 'won) - ((board-winner? board peer-mark) => 'lost) - (else 'play))] + [(state) (%state)] [(initialize!) (on (<- peer 'try-transition) (λ (status) (format #t "Peer's status: ~a\n" status)) #:promise? #t)] [(my-turn! x y) @@ -42,5 +43,7 @@ (on (<- peer 'peer-turn! x y) (λ (_) (board-choose! board mark x y) - (switch-turn!)))) + (switch-turn!) + (%state)) + #:promise? #t)) (error "It's not my turn."))])) diff --git a/gib-gab-gob/ui/console.scm b/gib-gab-gob/ui/console.scm index 013d1d0..e6f1f24 100644 --- a/gib-gab-gob/ui/console.scm +++ b/gib-gab-gob/ui/console.scm @@ -12,13 +12,13 @@ ;; Module for simple console-based UI (no curses) ;; A single run of the loop for a bespoke REPL for playing the game -(define (%loop board controller my-turn+) +(define (%loop vat b controller my-turn+) (wait my-turn+) - (let ((state) ($ controller 'state)) + (let ((state ($ controller 'state))) (%print b state) (if (eq? state 'play) (let ((coords (%read))) - (if coords (%eval move-ch coords) #f)) + (if coords (%eval vat controller coords) #f)) #f))) (define (%read) @@ -30,9 +30,12 @@ (%prompt) (%read)) coords)))) -(define (%eval move-ch coordinates) +(define (%eval vat controller coordinates) (format #t "Moving ~a\n" coordinates) - (put-message move-ch coordinates)) + (with-vat + vat + (on (<- controller 'my-turn! coordinates) + (λ (state) (%display b state))))) (define (%print b state) (%display b state) @@ -42,14 +45,14 @@ (define (%display b state) (board-display b) - (format #t "-> It is ~a turn!\n" (if %my-turn? "my" "peer's")) - (cond state - ('won => (format #t "*** I won! ***\n")) - ('lost => (format #t "*** I lost! ***\n")))) + (cond + ((eq? state 'won) => (format #t "*** I won! ***\n")) + ((eq? state 'lost) => (format #t "*** I lost! ***\n")))) (define (begin-game-loop board controller my-turn+) + (define vat (spawn-vat #:name "UI")) (spawn-fiber (λ () - (while (%loop board controller my-turn+)) + (while (%loop vat board controller my-turn+)) (format #t "bye-bye!\n") #f)))