74 lines
2.3 KiB
Scheme
74 lines
2.3 KiB
Scheme
(define-module (gib-gab-gob ui console)
|
|
#:use-module (gib-gab-gob rps)
|
|
#:use-module (gib-gab-gob game)
|
|
#:use-module (gib-gab-gob board)
|
|
#:use-module (ice-9 rdelim)
|
|
#:use-module (ice-9 exceptions)
|
|
#:use-module (srfi srfi-9)
|
|
#:use-module (srfi srfi-1)
|
|
#:use-module (fibers)
|
|
#:use-module (fibers channels)
|
|
#:use-module (fibers conditions)
|
|
#:use-module (goblins)
|
|
#:export (make-initiator))
|
|
|
|
;; Module for simple console-based UI (no curses)
|
|
|
|
;; A single run of the loop for a bespoke REPL for playing the game
|
|
(define (%loop vat b controller my-turn+)
|
|
(wait my-turn+)
|
|
(with-vat vat
|
|
(on (<- controller 'state)
|
|
(λ (state)
|
|
(%print b state)
|
|
(if (eq? state 'play)
|
|
(let ((coords (%read)))
|
|
(if coords (%eval vat b controller coords) #f))
|
|
#f)))))
|
|
|
|
(define (%read)
|
|
(define line (read-line (current-input-port)))
|
|
(if (string-prefix? ",q" line) #f
|
|
(let ((coords (map string->number (string-tokenize line char-set:digit))))
|
|
(if (not (= (length coords) 2))
|
|
(begin (format #t "Must be two numbers, x y; or ,quit to quit. Try again.\n")
|
|
(%prompt) (%read))
|
|
coords))))
|
|
|
|
(define (%eval vat b controller coordinates)
|
|
(format #t "Moving ~a\n" coordinates)
|
|
;; weird?
|
|
(let ((x (first coordinates))
|
|
(y (second coordinates)))
|
|
(on (<- controller 'my-turn! x y)
|
|
(λ (state) (%display b state)))))
|
|
|
|
(define (%print b state)
|
|
(%display b state)
|
|
(%prompt))
|
|
|
|
(define (%prompt) (format #t "Enter your move: "))
|
|
|
|
(define (%display b state)
|
|
(board-display b)
|
|
(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 vat board controller my-turn+))
|
|
(format #t "bye-bye!\n")
|
|
#f)))
|
|
|
|
(define (make-initiator)
|
|
(with-vat
|
|
(spawn-vat #:name "Initiator Game")
|
|
(define my-turn+ (make-condition))
|
|
(define board (make-board))
|
|
(define initiator (spawn ^game-initiator ^ggg-controller board my-turn+))
|
|
(initiator/connect initiator)
|
|
(begin-game-loop board initiator my-turn+)))
|