comparison 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
comparison
equal deleted inserted replaced
6:86cdb799f950 8:5941e6b3684c
25 25
26 state <- #{ 26 state <- #{
27 new <- :in_grid in_width in_height { 27 new <- :in_grid in_width in_height {
28 next_grid <- #[] 28 next_grid <- #[]
29 foreach: in_grid :index el{ 29 foreach: in_grid :index el{
30 dst_grid append: el 30 next_grid append: el
31 } 31 }
32 #{ 32 #{
33 grid <- [] 33 grid <- in_grid
34 width <- in_width 34 width <- in_width
35 height <- in_height 35 height <- in_height
36 address <- :x y { x + y * width } 36 address <- :x y { x + y * width }
37 setCell <- :x y cell { 37 setCell <- :x y cell {
38 grid set: (address: x y) cell 38 grid set: (address: x y) cell
57 fromStr <- :str { 57 fromStr <- :str {
58 strLen <- str byte_length: 58 strLen <- str byte_length:
59 index <- 0 59 index <- 0
60 maxRow <- 0 60 maxRow <- 0
61 curRow <- 0 61 curRow <- 0
62 while: {index < strLen} do { 62 while: {index < strLen} do: {
63 curByte <- str byte: index 63 curByte <- str byte: index
64 if: curByte = (cellType newline) { 64 if: curByte = (cellType newline) {
65 maxRow <- if: curRow > maxRow {curRow} else: {maxRow} 65 maxRow <- if: curRow > maxRow {curRow} else: {maxRow}
66 } else: { 66 } else: {
67 curRow = curRow + 1 67 curRow = curRow + 1
68 } 68 }
69 } 69 }
70 grid <- #[ ("#" byte: 0) ("#" byte: 0) ("#" byte: 0) ( "#" byte: 0) (" " byte: 0) ("#" byte: 0) ( "#" byte: 0) ("#" byte: 0) ("#" byte: 0)] 70 grid <- #[ ("#" byte: 0) ("#" byte: 0) ("#" byte: 0) ( "#" byte: 0) (" " byte: 0) ("#" byte: 0) ( "#" byte: 0) ("#" byte: 0) ("#" byte: 0)]
71 fresh <- new: grid 3 3 71 fresh <- new: grid 3 3
72 } 72 fresh
73 deepCopy <- :oldState {
74 newState <- new:
75 #{
76 // grid <- (use array copy thing)
77 width <- oldState width
78 height <- oldState height
79 address <- oldState address
80 // ... better way?
81 }
82 } 73 }
83 } 74 }
84 75
85 testMoves <- { 76 testMoves <- {
86 myStep <- 0 77 myStep <- 0
89 myStep <- myStep + 1 80 myStep <- myStep + 1
90 if: myStep > 5 {"A"} else: {"W"} 81 if: myStep > 5 {"A"} else: {"W"}
91 } 82 }
92 } 83 }
93 84
94 main <- { 85 readFile <- :path {
95 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
96 } 105 }
97 106
98 /* 107 main <- :args {
99 main <- { 108 if: (args length) < 2 {
100 testInput <- "derp" 109 print: "usage: sim filename\n"
101 simState <- state fromStr: testInput 110 } else: {
102 roboMove <- "W" 111 print: (args get: 1) . "\n"
103 getMove <- testMoves: 112 text <- readFile: (args get: 1)
104 while: {playing: simState roboMove} do: { 113 simState <- state fromStr: text
105 print: "step..." 114 while: { if: (simState ended) {false} else: {true} } do: {
106 roboMove <- getMove: 115 print: "step...\n"
107 simState advance: 116 simState advance: (getMove: )
117 }
108 } 118 }
109 } 119 }
110 */ 120
111 } 121 }