comparison 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
comparison
equal deleted inserted replaced
41:20327ae2120b 44:0c09730c173e
50 50
51 distanceFrom:to <- :sx sy :dx dy { 51 distanceFrom:to <- :sx sy :dx dy {
52 (abs: sx - dx) + (abs: sy - dy) 52 (abs: sx - dx) + (abs: sy - dy)
53 } 53 }
54 54
55 moveFinder <- :field {
56 #{
57 curbest <- (field clone) advance: "A"
58 playfield <- field
59 bestMove:withMaxSteps <- :self :max{
60 n <- 0
61 states <- #[playfield]
62 while: { if: (states length) > 0 { if: n < max { not: (curbest succeeded) } } } do: {
63 nextstates <- #[]
64 foreach: states :idx curstate {
65 me <-curstate getRobot
66 candidates <- curstate validMoves: (me x) (me y)
67 foreach: candidates :idx move {
68 curfield <- curstate clone
69 curfield advance: (move cmd)
70 if: (curfield ended) {
71 if: (curfield score) > (curbest score) {
72 curbest <- curfield
73 }
74 } else: {
75 nextstates append: curfield
76 }
77 }
78 }
79 states <- nextstates
80 n <- n + 1
81 }
82 if: (curbest succeeded) {
83 false
84 } else: {
85 if: (states length) > 0 {
86 bestofcur <- states get: 0
87 n <- 1
88 while: { n < (states length) } do: {
89 curstate <- states get: n
90 if: ((curstate score) > (bestofcur score)) {
91 bestofcur <- curstate
92 }
93 n <- n + 1
94 }
95 playfield <- bestofcur
96 true
97 }
98 }
99 }
100 }
101 }
102
55 main <- { 103 main <- {
56 text <- sim readFd: 0 104 text <- sim readFd: 0
57 playfield <- (sim state) fromStr: text 105 initial <- (sim state) fromStr: text
58 os write: 2 text 106 os write: 2 text
59 os write: 2 "width: " . (string: (playfield width)) . "\n" 107 os write: 2 "width: " . (string: (initial width)) . "\n"
60 os write: 2 "height: " . (string: (playfield height)) . "\n" 108 os write: 2 "height: " . (string: (initial height)) . "\n"
61 me <-playfield getRobot 109
62 os write: 2 "robot x: " . (me x) . " y: " . (me y) . "\n" 110 finder <- moveFinder: initial
63 neighbors <- playfield validMoves: (me x) (me y) 111 while: { bestMove: finder withMaxSteps: 5 } do: {
64 foreach: neighbors :idx move { 112 os write: 2 "--------iteration results-------\n"
65 os write: 2 "move: " . move . "\n" 113 os write: 2 "Best:\n"
66 curfield <- playfield clone 114 (finder curbest) printGrid
67 curfield advance: (move cmd) 115 os write: 2 "Current:\n"
68 curfield printGrid 116 (finder playfield) printGrid
69 } 117 }
118 os write: 2 "---------------\n"
119 os write: 2 "End Best:\n"
120 (finder curbest) printGrid
70 } 121 }
71 } 122 }