-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
404 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
GoL.yaml | ||
logo-burst.yaml | ||
horton.yaml | ||
horton.yaml | ||
snake.yaml | ||
caterpillar.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
def bind = \f. \x. case x inL f end | ||
def fmap = \f. \x. case x inL (\y. inR $ f y) end | ||
|
||
def nil = \_. \x. x end | ||
def cons = \x. \xs. \f. \b. f x $ xs f b end | ||
|
||
// Accumulator functions | ||
|
||
def offsetCoords : (int * int) -> (int * int) -> (int * int) = \coord. \offset. | ||
(fst coord + fst offset, snd coord + snd offset) | ||
end | ||
|
||
def getLength = \list. | ||
list (\_. \y. 1 + y) 0 | ||
end | ||
|
||
def getLast = \list. | ||
list (\x. \b. case b (\_. inR x) inR) $ inL () | ||
end | ||
|
||
def getInit = \list. | ||
list (\x. \b. case b (\_. inR nil) (\y. inR $ cons x y)) $ inL () | ||
end | ||
|
||
// "pop" from the tail; | ||
// returns the last element of the list and the truncated list. | ||
def shift = \list. | ||
list (\x. \b. case b (\_. inR (x, nil)) (\y. inR (fst y, cons x $ snd y))) $ inL () | ||
end | ||
|
||
def makeList = \i. \currentList. | ||
if (i > 0) { | ||
newThing <- makeList (i - 1) $ cons (0, i) currentList; | ||
return newThing; | ||
} { | ||
return currentList; | ||
}; | ||
end | ||
|
||
def go = | ||
d <- makeList 5 $ nil; | ||
|
||
let w = d offsetCoords (0, 0) in | ||
let x = getLength d in | ||
let y = getLast d in | ||
let z = bind getLast $ getInit d in | ||
|
||
let shifted = shift d in | ||
|
||
log "======"; | ||
log $ format w; | ||
log $ format x; | ||
log $ format y; | ||
log $ format z; | ||
|
||
log "-------"; | ||
log $ format $ fmap fst shifted; | ||
log $ format $ fmap fst $ bind shift $ fmap snd shifted; | ||
end; | ||
|
||
go; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
def bind = \f. \x. case x inL f end | ||
def fmap = \f. \x. case x inL (\y. inR $ f y) end | ||
|
||
def nil = \_. \x. x end | ||
def cons = \x. \xs. \f. \b. f x $ xs f b end | ||
|
||
// "pop" from the tail; | ||
// returns the last element of the list and the truncated list. | ||
def shift = \list. | ||
list (\x. \b. case b (\_. inR (x, nil)) (\y. inR (fst y, cons x $ snd y))) $ inL () | ||
end | ||
|
||
def abs = \n. if (n < 0) {-n} {n} end | ||
|
||
// modulus function (%) | ||
def mod : int -> int -> int = \i.\m. | ||
i - m * (i / m) | ||
end | ||
|
||
def getDir = \currentLoc. \dest. | ||
/* | ||
let xDist = fst currentLoc - fst dest in | ||
let yDist = snd currentLoc - snd dest in | ||
if (abs xDist > abs yDist) { | ||
if (xDist > 0) {west} {east}; | ||
} { | ||
if (yDist > 0) {south} {north}; | ||
} | ||
*/ | ||
path (inL ()) (inL dest); | ||
end; | ||
|
||
def doAtLoc = \currLoc. \targetLoc. \func. | ||
teleport self targetLoc; | ||
func; | ||
teleport self currLoc; | ||
end; | ||
|
||
def moveTail = \tailList. | ||
|
||
let maybeShifted = shift tailList in | ||
case maybeShifted (\_. | ||
// Nothing to pick up or replace | ||
return tailList; | ||
|
||
) (\newPair. | ||
let farthestTail = fst newPair in | ||
let newInit = snd newPair in | ||
|
||
newLoc <- whereami; | ||
doAtLoc newLoc farthestTail grab; | ||
|
||
// FIXME: Should be: | ||
// return $ cons newLoc newInit; | ||
|
||
return $ cons newLoc tailList; | ||
); | ||
end; | ||
|
||
// Invariant: No tail tail pieces shall ever be moved underneath the | ||
// snake robot, unless an apple was just picked up. | ||
def moveToApple = \tailList. | ||
|
||
myLoc <- whereami; | ||
|
||
appleHere <- ishere "apple"; | ||
if appleHere { | ||
appleCount <- count "apple"; | ||
|
||
if (mod appleCount 3 == 0) { | ||
swap "tail"; | ||
return $ cons myLoc tailList; | ||
} { | ||
grab; | ||
return tailList; | ||
}; | ||
} { | ||
// This robot will always be sitting atop the apple | ||
r <- robotnamed "spawn"; | ||
targetLoc <- as r {whereami}; | ||
|
||
maybeD <- getDir myLoc targetLoc; | ||
case maybeD (\_. say "Dead!"; return nil) (\d. | ||
turn d; | ||
move; | ||
|
||
moveTail tailList; | ||
); | ||
} | ||
end; | ||
|
||
def go = \tailList. | ||
moveToApple tailList; | ||
go tailList; | ||
end; | ||
|
||
go nil; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
def bind = \f. \x. case x inL f end | ||
def fmap = \f. \x. case x inL (\y. inR $ f y) end | ||
|
||
def nil = \_. \x. x end | ||
def cons = \x. \xs. \f. \b. f x $ xs f b end | ||
|
||
// Accumulator functions | ||
|
||
def offsetCoords : (int * int) -> (int * int) -> (int * int) = \coord. \offset. | ||
(fst coord + fst offset, snd coord + snd offset) | ||
end | ||
|
||
def getLength = \list. | ||
list (\_. \y. 1 + y) 0 | ||
end | ||
|
||
def getLast = \list. | ||
list (\x. \b. case b (\_. inR x) inR) $ inL () | ||
end | ||
|
||
def getInit = \list. | ||
list (\x. \b. case b (\_. inR nil) (\y. inR $ cons x y)) $ inL () | ||
end | ||
|
||
// "pop" from the tail; | ||
// returns the last element of the list and the truncated list. | ||
def shift = \list. | ||
list (\x. \b. case b (\_. inR (x, nil)) (\y. inR (fst y, cons x $ snd y))) $ inL () | ||
end | ||
|
||
def go = | ||
let a = cons (3, 0) $ nil in | ||
let b = cons (7, 1) $ a in | ||
let c = cons (4, 2) $ b in | ||
let d = cons (9, 3) $ c in | ||
|
||
let w = d offsetCoords (2, 4) in | ||
let x = getLength d in | ||
let y = getLast d in | ||
let z = bind getLast $ getInit d in | ||
|
||
let shifted = shift d in | ||
|
||
log "======"; | ||
log $ format w; | ||
log $ format x; | ||
log $ format y; | ||
log $ format z; | ||
|
||
log "-------"; | ||
log $ format $ fmap fst shifted; | ||
log $ format $ fmap fst $ bind shift $ fmap snd shifted; | ||
end; | ||
|
||
go; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
def waitForConsumption = | ||
watch down; | ||
wait 2000; | ||
appleHere <- ishere "apple"; | ||
if appleHere { | ||
waitForConsumption; | ||
} {} | ||
end; | ||
|
||
// TODO Ensure we don't spawn inside a coil of the tail | ||
def placeAtOpenLocation = \range. | ||
randX <- random range; | ||
randY <- random range; | ||
let x = range/2 - randX in | ||
let y = range/2 - randY in | ||
teleport self (x, y); | ||
emptyHere <- isempty; | ||
if emptyHere { | ||
place "apple"; | ||
} { | ||
placeAtOpenLocation range; | ||
} | ||
end; | ||
|
||
def repeatedlyPlaceApple = \range. | ||
placeAtOpenLocation range; | ||
waitForConsumption; | ||
repeatedlyPlaceApple range; | ||
end; | ||
|
||
def go = | ||
repeatedlyPlaceApple 24; | ||
end; | ||
|
||
go; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
version: 1 | ||
name: Caterpillar | ||
author: Karl Ostmo | ||
seed: 1 | ||
description: | | ||
Crawl along | ||
creative: false | ||
objectives: | ||
- goal: | ||
- | | ||
Eat 30 apples | ||
condition: | | ||
return false | ||
robots: | ||
- name: base | ||
dir: north | ||
devices: | ||
- ADT calculator | ||
- branch predictor | ||
- clock | ||
- comparator | ||
- compass | ||
- dictionary | ||
- grabber | ||
- hourglass | ||
- hearing aid | ||
- lambda | ||
- logger | ||
- net | ||
- scanner | ||
- strange loop | ||
- string | ||
- treads | ||
inventory: | ||
- [5, tail] | ||
solution: | | ||
run "scenarios/Fun/_caterpillar/solution.sw" | ||
entities: | ||
- name: tail | ||
display: | ||
char: '@' | ||
attr: green | ||
description: | ||
- | | ||
Segment of snake's tail | ||
properties: [known, unwalkable] | ||
known: [water, boulder, flower] | ||
world: | ||
dsl: | | ||
{grass} | ||
upperleft: [0, 0] | ||
palette: | ||
'B': [grass, erase, base] | ||
'.': [grass, erase] | ||
map: | | ||
B... |
Oops, something went wrong.