diff src/lifter.tp @ 44:0c09730c173e

Robot works on simple maps now
author Mike Pavone <pavone@retrodev.com>
date Sun, 15 Jul 2012 17:21:27 -0700
parents 9bccdb3ac979
children 9f1ca5ba2684
line wrap: on
line diff
--- a/src/lifter.tp	Sun Jul 15 14:41:28 2012 -0700
+++ b/src/lifter.tp	Sun Jul 15 17:21:27 2012 -0700
@@ -52,20 +52,71 @@
 		(abs: sx - dx) + (abs: sy - dy)
 	}
 	
+	moveFinder <- :field {
+		#{
+			curbest <- (field clone) advance: "A"
+			playfield <- field
+			bestMove:withMaxSteps <- :self :max{
+				n <- 0
+				states <- #[playfield]
+				while: { if: (states length) > 0 { if: n < max { not: (curbest succeeded) } } } do: {
+					nextstates <- #[]
+					foreach: states :idx curstate {
+						me <-curstate getRobot
+						candidates <- curstate validMoves: (me x) (me y)
+						foreach: candidates :idx move {
+							curfield <- curstate clone
+							curfield advance: (move cmd)
+							if: (curfield ended) {
+								if: (curfield score) > (curbest score) {
+									curbest <- curfield
+								}
+							} else: {
+								nextstates append: curfield
+							}
+						}
+					}
+					states <- nextstates
+					n <- n + 1
+				}
+				if: (curbest succeeded) {
+					false
+				} else: {
+					if: (states length) > 0 {
+						bestofcur <- states get: 0
+						n <- 1
+						while: { n < (states length) } do: {
+							curstate <- states get: n
+							if: ((curstate score) > (bestofcur score)) {
+								bestofcur <- curstate
+							}
+							n <- n + 1
+						}
+						playfield <- bestofcur
+						true
+					}
+				}
+			}
+		}
+	}
+	
 	main <- {
 		text <- sim readFd: 0
-		playfield <- (sim state) fromStr: text
+		initial <- (sim state) fromStr: text
 		os write: 2 text
-		os write: 2 "width: " . (string: (playfield width)) . "\n"
-		os write: 2 "height: " . (string: (playfield height)) . "\n"
-		me <-playfield getRobot
-		os write: 2 "robot x: " . (me x) . " y: " . (me y) . "\n"
-		neighbors <- playfield validMoves: (me x) (me y)
-		foreach: neighbors :idx move {
-			os write: 2 "move: " . move . "\n"
-			curfield <- playfield clone
-			curfield advance: (move cmd)
-			curfield printGrid
+		os write: 2 "width: " . (string: (initial width)) . "\n"
+		os write: 2 "height: " . (string: (initial height)) . "\n"
+		
+		finder <- moveFinder: initial
+		while: { bestMove: finder withMaxSteps: 5 } do: {
+			os write: 2 "--------iteration results-------\n"
+			os write: 2 "Best:\n"
+			(finder curbest) printGrid
+			os write: 2 "Current:\n"
+			(finder playfield) printGrid
 		}
+		os write: 2 "---------------\n"
+		os write: 2 "End Best:\n"
+		(finder curbest) printGrid
 	}
 }