view src/lifter.tp @ 39:9bccdb3ac979

Add priority queue implementation to lifter. Add methods for cloning playfield and determining valid moves.
author Mike Pavone <pavone@retrodev.com>
date Sun, 15 Jul 2012 14:27:21 -0700
parents f7a1daaec925
children 0c09730c173e
line wrap: on
line source

#{
	pqueue <- {
		normalnode <- :pri val {
			#{
				priority <- pri
				value <- val
				next <- false
				higherPriority? <- :other {
					priority > (other priority)
				}
				if:else <- :self trueblock :elseblock {
					trueblock:
				}
			}
		}
		head <- #{
			higherPriority? <- :other {false}
			next <- { self }
			value <- { false }
		}
		#{
			take <- {
				cur <- head
				head <- cur next
				cur value
			}
			insert:atPriority <- :val pri {
				node <- normalnode: pri val
				cur <- head
				last <- false
				while: {cur higherPriority?: node} do: {
					last <- cur
					cur <- cur next
				}
				if: last {
					node next!: (last next)
					last next!: node
				} else: {
					node next!: head
					head <- node
				}
				self
			}
		}
	}
	
	abs <- :val {
		if: val < 0 { 0 - val } else: { val }
	}
	
	distanceFrom:to <- :sx sy :dx dy {
		(abs: sx - dx) + (abs: sy - dy)
	}
	
	main <- {
		text <- sim readFd: 0
		playfield <- (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
		}
	}
}