summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEkaitz Zarraga <ekaitz@elenq.tech>2022-11-04 20:13:29 +0100
committerEkaitz Zarraga <ekaitz@elenq.tech>2022-11-04 20:13:29 +0100
commit30fdfca72ca1277c127c42e86bba2481246db0f7 (patch)
tree9d8bd10966d926ca34a23ac44da12b717e144171
parent2a2f1ab792e62cf504c0c7ad38c44299ab1793c6 (diff)
Add state and its representation in states
-rw-r--r--depre/main.scm37
1 files changed, 33 insertions, 4 deletions
diff --git a/depre/main.scm b/depre/main.scm
index 37a3b35..81bdb03 100644
--- a/depre/main.scm
+++ b/depre/main.scm
@@ -1,8 +1,10 @@
; https://xn--rpa.cc/irl/term.html
(define-module (depre main))
-(define %depression 50)
-(define %tiredness 50)
+;; Code makes heavy use of functional code and parameters for state.
+(define max-100 (lambda (x) (if (> x 100) 100 x)))
+(define %depression (make-parameter 50 max-100))
+(define %tiredness (make-parameter 50 max-100))
(define (nth n lst)
(if (> n (length lst))
@@ -13,6 +15,8 @@
(car lst)
(loop (cdr lst) (- n 1))))))
+
+;; Function combinations: they define the state transitions
(define (combine . functions)
"Calls given functions in given order."
(lambda ()
@@ -21,13 +25,36 @@
((car fs))
(loop (cdr fs))))))
+;; Simple I/O
(define (answer message)
(lambda ()
(display message)
(newline)))
-(define (state heading menu)
+(define (bar percent)
+ "A simple 12 character bar for life-like variable representation"
+ (define len 22)
+ (string-tabulate
+ (lambda (x)
+ (cond
+ ((= x 0) #\[)
+ ((= x (- len 1)) #\])
+ ((>= (/ percent (/ 100 len)) x) #\=)
+ (else #\space)))
+ len))
+
+
+;; Basic state representation and creation: A state is a function that knows
+;; how to jump to other states through a menu.
+(define* (state heading menu #:optional (hide-status #f))
(lambda ()
+ (when (not hide-status)
+ (newline)
+ (display "Depression:\t")
+ (display (bar (%depression)))
+ (newline)
+ (display "Tiredness:\t")
+ (display (bar (%tiredness))))
(newline)
(display heading)
(newline)
@@ -43,6 +70,7 @@
(display "Wrong answer. Try again > ")
(loop (read)))))))
+;; States:
(define start
(state "Hi, game starts"
(list (cons "Start game"
@@ -52,6 +80,7 @@
(list (cons "hola" (lambda () (display "HOLA")))
(cons "adios" (lambda () (display "adios")))))))
(cons "Quit game"
- (answer "Good bye!")))))
+ (answer "Good bye!")))
+ #:hide-status))
(start)