gib-gab-gob/gib-gab-gob/ui/console.scm

56 lines
1.6 KiB
Scheme

(define-module (gib-gab-gob ui console)
#:use-module (gib-gab-gob board)
#:use-module (ice-9 rdelim)
#:use-module (ice-9 exceptions)
#:use-module (srfi srfi-9)
#:use-module (fibers)
#:use-module (fibers channels)
#:use-module (fibers conditions)
#:use-module (goblins)
#:export (begin-game-loop))
;; 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+)
(wait my-turn+)
(let ((state) ($ controller 'state))
(%print b state)
(if (eq? state 'play)
(let ((coords (%read)))
(if coords (%eval move-ch 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 move-ch coordinates)
(format #t "Moving ~a\n" coordinates)
(put-message move-ch coordinates))
(define (%print b state)
(%display b state)
(%prompt))
(define (%prompt) (format #t "Enter your move: "))
(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"))))
(define (begin-game-loop board controller my-turn+)
(spawn-fiber
(λ ()
(while (%loop board controller my-turn+))
(format #t "bye-bye!\n")
#f)))