-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathadventure.tao
85 lines (75 loc) · 2.06 KB
/
adventure.tao
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import "../lib/main.tao"
data Location =
| Beach
| Forest
| Village
\ Mountain
fn describe_loc =
| Beach => "You are standing on a gravel beach"
| Forest => "You are in a dark, spooky forest"
| Village => "You are standing in the centre of sleepy village"
\ Mountain => "You are on the side of a desolate mountain"
fn directions =
| Beach => [("uphill", Forest)]
| Forest => [("downhill", Beach), ("east", Village)]
| Village => [("west", Forest), ("climb", Mountain)]
\ Mountain => [("descend", Village)]
data State = {
loc: Location,
}
def default_state = State {
loc: Beach,
}
fn describe : State -> io ~ () = state => do {
print(state.loc->describe_loc)!;
print("You can go...")!;
state.loc
-> directions
-> map(fn dir => print("- " ++ dir.0)!)!;
}
data Event =
| Grab
\ MoveTo Location
fn apply_event : Event -> State -> io ~ State =
| Grab, state => do {
print("There was nothing to grab!")!;
state
}
\ MoveTo loc, state => do {
print("You moved.")!;
print(loc->describe_loc)!;
state with { loc }
}
fn game_loop : State -> io ~ () = state => do {
let cmd = input!;
let res = when cmd is
| "quit" => Done
| "help" => do {
print("Commands: 'help', 'quit', 'look', 'grab'")!;
Next None
}
| "look" => do {
state->describe!;
Next None
}
| "grab" => Next Just Grab
\ other => when state.loc
-> directions
-> find_first(fn (name, _) => name = cmd)
is
| Just (_, loc) => Next Just MoveTo loc
\ None => do {
print("Unknown command '" ++ cmd ++ "'")!;
Next None
};
when res is
| Next Just event => game_loop(state->apply_event(event)!)!
| Next None => game_loop(state)!
\ Done => ()
}
def main : io ~ () = do {
print("Welcome to the adventure game!")!;
print("Type 'help' to get started.")!;
game_loop(default_state)!;
print("Goodbye!")!;
}