changeset 25:a224dc43877f

major progress! ...robot always moves down though
author William Morgan <bill@mrgn.org>
date Sat, 14 Jul 2012 19:05:56 -0700
parents 8435bf0360f8
children feb38aecb98f
files src/sim.tp
diffstat 1 files changed, 38 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/src/sim.tp	Sat Jul 14 14:38:50 2012 -0700
+++ b/src/sim.tp	Sat Jul 14 19:05:56 2012 -0700
@@ -27,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
@@ -68,6 +73,7 @@
 			robot       <- {
 				#{
 					id <- ("R"  byte: 0)
+					str <- "R"
 					isrobot <- { true }
 					heldBreath <- 0
 					x <- 0
@@ -76,10 +82,10 @@
 						writeMove <- :xPrime yPrime {
 							mine setCell: xPrime yPrime self
 							mine setCell: x y empty
-							x!: xPrime
-							y!: yPrime
+							x <- xPrime
+							y <- yPrime
 						}
-						writeMove: x (y - 1)
+						writeMove: x (y + 1)
 					}
 				}
 			}
@@ -104,14 +110,7 @@
 			nextGrid <- #[]
 			robot <- false
 			endreached <- false
-			foreach: in_grid :index el{
-				nextGrid append: el
-				if: (el isrobot) { 
-					robot <- el
-					updatePos: robot index
-				} else: { true }
-			}
-			#{
+			ret <- #{
 				grid <- in_grid
 				width <- in_width
 				height <- in_height
@@ -125,8 +124,8 @@
 					grid get: (calcIndex: x y)
 				}
 				updatePos <- :obj Index {
-					obj x! (calcX: Index)
-					obj y! (calcY: Index)
+					obj x!: (calcX: Index)
+					obj y!: (calcY: Index)
 				}
 				water <- 0
 				flooding <- 0
@@ -142,18 +141,27 @@
 					robot move: roboCmd self
 					moves <- moves + 1
 					doUpdate:
-					if: debugToStdErr {
-						grid foreach: :index value {
-							os write: 2 (string: (value id))
+					self
+				}
+				printGrid <- {
+					grid foreach: :index value {
+						os write: 2 (value str)
+						if: index % width = width - 1 {
+							os write: 2 "\n"
 						}
 					}
-					self
-				}
-				
+				}				
 			}
+			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
@@ -169,7 +177,7 @@
 					col <- col + 1
 				}
 			}
-			
+			col <- 0
 			grid <- #[]
 			eachbyte: str :index element {
 				if: element = nl {
@@ -181,7 +189,7 @@
 					col <- 0
 				} else: {
 					grid append: (cellTypes find: element)
-					col = col + 1
+					col <- col + 1
 				}
 			}
 			new: grid maxCol rows
@@ -221,17 +229,21 @@
 		os read: 0 1
 	}
 
-	debugToStdErr <- true
 	main <- :args {
 		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
+				}
 			}
 		}
 	}