More tweaks to comms for ui

This commit is contained in:
Vivianne 2023-07-09 20:23:00 -07:00
parent b2444ace11
commit 748bbfdb3f
2 changed files with 22 additions and 16 deletions

View File

@ -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."))]))

View File

@ -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)))