comparison src/sim.tp @ 5:be946b2a2cbc

initial barf into simulator file
author William Morgan <bill@mrgn.org>
date Fri, 13 Jul 2012 21:10:10 -0700
parents bb29dcd46cbf
children 86cdb799f950
comparison
equal deleted inserted replaced
4:d4ba6138c99e 5:be946b2a2cbc
1 #{ 1 #{
2 true <- #{
3 if:else <- :self trueblock :elseblock {
4 trueblock:
5 }
6 }
7
8 false <- #{
9 if:else <- :self trueblock :elseblock {
10 elseblock:
11 }
12 }
13
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
24 }
25
26 state <- #{
27 new <- : in_grid in_width in_height {
28 dst_grid <- #[]
29 foreach: in_grid :index el{
30 dst_grid append: el
31 }
32 #{
33 grid <- []
34 width <- in_width
35 height <- in_height
36 address <- :x y { x + y * width }
37 setCell <- :x y cell {
38 grid set: (address: x y) cell
39 }
40 getCell <- :x y {
41 grid get: (address: x y)
42 }
43 collected <- 0
44 moves <- 0
45 ended <- false
46 advance <- :roboMove {
47 nextState <- deepCopy: self
48 nextState <- doMove:
49 nextState ended <- ended:
50 nextState moves <- moves + 1
51 }
52
53 }
54 }
55 fromStr <- :str {
56 strLen <- str byte_length:
57 index <- 0
58 maxRow <- 0
59 curRow <- 0
60 while: {index < strLen} do {
61 curByte <- str byte: index
62 if: curByte = (cellType newline) {
63
64 } else: {
65 curRow = curRow + 1
66 }
67 }
68 grid <- #[ ("#" byte: 0) ("#" byte: 0) ("#" byte: 0) ( "#" byte: 0) (" " byte: 0) ("#" byte: 0) ( "#" byte: 0) ("#" byte: 0) ("#" byte: 0)]
69 fresh <- new: grid 3 3
70 }
71 deepCopy <- :oldState {
72 newState <- new:
73 #{
74 // grid <- (use array copy thing)
75 width <- oldState width
76 height <- oldState height
77 address <- oldState address
78 // ... better way?
79 }
80 }
81 }
82
83 testMoves <- {
84 myStep <- 0
85 {
86 print: (string: myStep)
87 myStep <- myStep + 1
88 if: myStep > 5 {"A"} else: {"W"}
89 }
90 }
91
92 ended <- :simState roboMove {
93 if: roboMove = "A" {}
94 }
95
2 main <- { 96 main <- {
3 0 97
4 } 98 }
99
100 /*
101 main <- {
102 testInput <- "derp"
103 simState <- state fromStr: testInput
104 roboMove <- "W"
105 getMove <- testMoves:
106 while: {playing: simState roboMove} do: {
107 print: "step..."
108 roboMove <- getMove:
109 simState advance:
110 }
111 }
112 */
5 } 113 }