view code/grid.lm @ 46:d631e68a45d5

separated out grid functions for reuse inside gameState.lm
author William Morgan <billjunk@mrgn.org>
date Sun, 27 Jul 2014 01:36:39 -0700
parents
children 57a4bddadd46
line wrap: on
line source

#{
	import: [
		length
		reverse
		split:at
		map
		fold:with
		filter
		flatten
	] from: (module: "ll.lm")
		
	import: [
		makeTree:size
		makeTree
		get:fromTree:size
		get:fromTree
		treeMap:size
		treeMap
		tree:size:update:with
		tree:update:with
		tree:set:to
	] from: (module: "tree.lm")

	grid:get <- :grid :pos {
		x <- pos value
		y <- pos tail
		get: x fromTree: (get: y fromTree: grid)
	}
	
	grid:update:with <- :grid :pos :fun {
		x <- pos value
		y <- pos tail
		tree: grid update: y with: :row {
			tree: row update: x with: fun
		}
	}
	
	grid:set:to <- :grid :pos :val {
		grid: grid update: pos with: :el { val }
	}

	grid:inBounds? <- :grid :pos {
		x <- pos value
		y <- pos tail
		maxY <- grid value
		maxX <- (get: 0 fromTree: grid) value
		((x >= 0) + (y >= 0) + (x < maxX) + (y < maxY)) > 0
	}

	calcPos <- :move from {
		x <- from value
		y <- from tail
		if: move {
			if: move = 1 {
				x <- x + 1
			} else: {
				if: move = 2 {
					y <- y + 1
				} else: {
					x <- x - 1
				}
			}
		} else: {
			y <- y - 1
		}
		#[x y]
	}
}