Mercurial > repos > icfp2012
annotate src/sim.tp @ 8:5941e6b3684c
Read map file in simulator
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Sat, 14 Jul 2012 01:42:05 -0700 |
parents | 86cdb799f950 |
children | 66ea6fdd3fcb |
rev | line source |
---|---|
3
bb29dcd46cbf
Put dummy code in placeholder source files. Create makefile.
Mike Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
1 #{ |
5
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
2 true <- #{ |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
3 if:else <- :self trueblock :elseblock { |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
4 trueblock: |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
5 } |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
6 } |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
7 |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
8 false <- #{ |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
9 if:else <- :self trueblock :elseblock { |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
10 elseblock: |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
11 } |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
12 } |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
13 |
6 | 14 cellType <- #{ |
15 robot <- ("R" byte: 0) | |
16 wall <- ("#" byte: 0) | |
17 rock <- ("*" byte: 0) | |
18 lambda <- ("\\" byte: 0) | |
19 closedlift <- ("L" byte: 0) | |
20 openlift <- ("O" byte: 0) | |
21 earth <- ("." byte: 0) | |
22 empty <- (" " byte: 0) | |
23 newline <- ("\n" byte: 0) | |
5
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
24 } |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
25 |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
26 state <- #{ |
6 | 27 new <- :in_grid in_width in_height { |
28 next_grid <- #[] | |
5
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
29 foreach: in_grid :index el{ |
8 | 30 next_grid append: el |
5
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
31 } |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
32 #{ |
8 | 33 grid <- in_grid |
5
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
34 width <- in_width |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
35 height <- in_height |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
36 address <- :x y { x + y * width } |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
37 setCell <- :x y cell { |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
38 grid set: (address: x y) cell |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
39 } |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
40 getCell <- :x y { |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
41 grid get: (address: x y) |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
42 } |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
43 collected <- 0 |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
44 moves <- 0 |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
45 ended <- false |
6 | 46 doMove <- :roboMove { |
47 ended <- roboMove = "A" | |
48 } | |
5
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
49 advance <- :roboMove { |
6 | 50 doMove: roboMove |
51 moves <- moves + 1 | |
52 self | |
5
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
53 } |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
54 |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
55 } |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
56 } |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
57 fromStr <- :str { |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
58 strLen <- str byte_length: |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
59 index <- 0 |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
60 maxRow <- 0 |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
61 curRow <- 0 |
8 | 62 while: {index < strLen} do: { |
5
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
63 curByte <- str byte: index |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
64 if: curByte = (cellType newline) { |
6 | 65 maxRow <- if: curRow > maxRow {curRow} else: {maxRow} |
5
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
66 } else: { |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
67 curRow = curRow + 1 |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
68 } |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
69 } |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
70 grid <- #[ ("#" byte: 0) ("#" byte: 0) ("#" byte: 0) ( "#" byte: 0) (" " byte: 0) ("#" byte: 0) ( "#" byte: 0) ("#" byte: 0) ("#" byte: 0)] |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
71 fresh <- new: grid 3 3 |
8 | 72 fresh |
5
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
73 } |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
74 } |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
75 |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
76 testMoves <- { |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
77 myStep <- 0 |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
78 { |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
79 print: (string: myStep) |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
80 myStep <- myStep + 1 |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
81 if: myStep > 5 {"A"} else: {"W"} |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
82 } |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
83 } |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
84 |
8 | 85 readFile <- :path { |
86 fd <- os open: path (os O_RDONLY) | |
87 print: "fd: " . fd . "\n" | |
88 if: fd < 0 { "" } else: { | |
89 cur <- "" | |
90 part <- "" | |
91 while: { | |
92 part <- os read: fd 128 | |
93 print: "read: " . part . "\n" | |
94 part != "" | |
95 } do: { | |
96 cur <- cur . part | |
97 } | |
98 os close: fd | |
99 cur | |
100 } | |
101 } | |
102 | |
103 getMove <- { | |
104 os read: 0 1 | |
3
bb29dcd46cbf
Put dummy code in placeholder source files. Create makefile.
Mike Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
105 } |
5
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
106 |
8 | 107 main <- :args { |
108 if: (args length) < 2 { | |
109 print: "usage: sim filename\n" | |
110 } else: { | |
111 print: (args get: 1) . "\n" | |
112 text <- readFile: (args get: 1) | |
113 simState <- state fromStr: text | |
114 while: { if: (simState ended) {false} else: {true} } do: { | |
115 print: "step...\n" | |
116 simState advance: (getMove: ) | |
117 } | |
5
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
118 } |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
119 } |
8 | 120 |
3
bb29dcd46cbf
Put dummy code in placeholder source files. Create makefile.
Mike Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
121 } |