# HG changeset patch # User Mike Pavone # Date 1342319390 25200 # Node ID feb38aecb98fc4d8b7d236a2bd9baf57005e0dd8 # Parent 582fdb18efaf896fd3d5040475bdc58fa42b4092# Parent a224dc43877f2e5beffcf6a512927c575c4f0c4d merge diff -r 582fdb18efaf -r feb38aecb98f src/sim.tp --- a/src/sim.tp Sat Jul 14 18:33:47 2012 -0700 +++ b/src/sim.tp Sat Jul 14 19:29:50 2012 -0700 @@ -1,14 +1,20 @@ { true <- #{ - if:else <- :self trueblock :elseblock { - trueblock: - } + if <- :self trueblock { + trueblock: + } + if:else <- :self trueblock :elseblock { + trueblock: + } } false <- #{ - if:else <- :self trueblock :elseblock { - elseblock: - } + if <- :self trueblock { + self + } + if:else <- :self trueblock :elseblock { + elseblock: + } } eachbyte <- :string action { @@ -21,12 +27,17 @@ } } + debugLog <- :str { + os write: 2 str + } + makeCellTypes <- { allid <- #[] allobj <- #[] - new <- :idstr { + new <- :idStr { ret <- #{ - id <- (idstr byte: 0) + id <- (idStr byte: 0) + str <- idStr isrobot <- { false } } allobj append: ret @@ -40,7 +51,7 @@ while: { if: index < (allid length) { (allid get: index) != id - } else: {false} + } } do: { index <- index + 1 } @@ -61,11 +72,20 @@ newline <- new: "\n" robot <- { #{ - id <- ("R" byte: 0) + id <- ("R" byte: 0) + str <- "R" isrobot <- { true } heldBreath <- 0 - move <- :cmd { - cmd + x <- 0 + y <- 0 + move <- :cmd mine { + writeMove <- :xPrime yPrime { + mine setCell: xPrime yPrime self + mine setCell: x y empty + x <- xPrime + y <- yPrime + } + writeMove: x (y + 1) } } } @@ -90,22 +110,22 @@ nextGrid <- #[] robot <- false endreached <- false - foreach: in_grid :index el{ - nextGrid append: el - if: (el isrobot) { - robot <- el - } else: { true } - } - #{ + ret <- #{ grid <- in_grid width <- in_width height <- in_height - address <- :x y { x + y * width } + calcIndex <- :x y { x + y * width } + calcX <- :index {index % width} + calcY <- :index {index / width} setCell <- :x y val { - grid set: (address: x y) val + grid set: (calcIndex: x y) val } getCell <- :x y { - grid get: (address: x y) + grid get: (calcIndex: x y) + } + updatePos <- :obj Index { + obj x!: (calcX: Index) + obj y!: (calcY: Index) } water <- 0 flooding <- 0 @@ -118,16 +138,30 @@ } advance <- :roboCmd { endreached <- roboCmd = "A" - robot move: roboCmd + robot move: roboCmd self moves <- moves + 1 doUpdate: self } - + printGrid <- { + grid foreach: :index value { + os write: 2 (value str) + if: index % width = width - 1 { + os write: 2 "\n" + } + } + } } + foreach: in_grid :index el{ + nextGrid append: el + if: (el isrobot) { + robot <- el + ret updatePos: robot index + } else: { true } + } + ret } fromStr <- :str { - strLen <- str byte_length: maxCol <- 0 col <- 0 @@ -143,7 +177,7 @@ col <- col + 1 } } - + col <- 0 grid <- #[] eachbyte: str :index element { if: element = nl { @@ -155,7 +189,7 @@ col <- 0 } else: { grid append: (cellTypes find: element) - col = col + 1 + col <- col + 1 } } new: grid maxCol rows @@ -199,12 +233,17 @@ if: (args length) < 2 { print: "usage: sim filename\n" } else: { + verbose <- true text <- readFile: (args get: 1) print: text os close: 1 simState <- state fromStr: text - while: { if: (simState ended) {false} else: {true} } do: { + derp <- simState ended: + while: { if: (simState ended: ) {false} else: {true} } do: { simState advance: (getMove: ) + if: verbose { + simState printGrid + } } } }