Skip to content

Instantly share code, notes, and snippets.

@draegtun
Created March 30, 2016 18:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save draegtun/2a107ae1d403c9f20303e519fa5bfd69 to your computer and use it in GitHub Desktop.
Save draegtun/2a107ae1d403c9f20303e519fa5bfd69 to your computer and use it in GitHub Desktop.
Door challenge in Rebol
Rebol [
see: https://www.reddit.com/r/dailyprogrammer/comments/4cb7eh/20160328_challenge_260_easy_garage_door_opener/
]
states: [
'CLOSED [click: 'OPENING blocked: '*CLOSED]
'OPENING [click: 'STOPPED_WHILE_OPENING complete: 'OPEN blocked: '*OPENING]
'OPEN [click: 'CLOSING blocked: 'OPEN_BLOCKED]
'CLOSING [click: 'STOPPED_WHILE_CLOSING complete: 'CLOSED blocked: 'EMERGENCY_OPENING]
'STOPPED_WHILE_OPENING [click: 'CLOSING blocked: '*STOPPED_WHILE_OPENING]
'STOPPED_WHILE_CLOSING [click: 'OPENING blocked: '*STOPPED_WHILE_CLOSING]
'EMERGENCY_OPENING [complete: 'OPEN_BLOCKED cleared: 'OPENING]
'OPEN_BLOCKED [cleared: 'OPEN]
'*CLOSED [cleared: 'CLOSED]
'*OPENING [cleared: 'OPENING]
'*STOPPED_WHILE_OPENING [cleared: 'STOPPED_WHILE_OPENING]
'*STOPPED_WHILE_CLOSING [cleared: 'STOPPED_WHILE_CLOSING]
]
make-decision-engine: function [states] [
body: make block! [switch state []]
forskip states 2 [
current-state: states/1
next-state: states/2
append body/3 compose/deep [
(to-word current-state)
[any [select [(next-state)] input (to-lit-word current-state)]]
]
]
func [input] body
]
state: first states
do-action: make-decision-engine states
p: does [print ["Door:" replace to-string state "*" ""]]
button_clicked: does [print "> Button clicked" state: do-action 'click p]
cycle_complete: does [print "> Cycle complete" state: do-action 'complete p]
block_detected: does [print "> Block detected!" state: do-action 'blocked p]
block_cleared: does [print "> Block cleared" state: do-action 'cleared p]
p ;; start!
;; do bonus commands
button_clicked
cycle_complete
button_clicked
block_detected
button_clicked
cycle_complete
button_clicked
block_cleared
button_clicked
cycle_complete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment