Skip to content

Commit

Permalink
snake demo
Browse files Browse the repository at this point in the history
  • Loading branch information
kostmo committed Dec 31, 2023
1 parent 358e60b commit f398d89
Show file tree
Hide file tree
Showing 7 changed files with 404 additions and 1 deletion.
4 changes: 3 additions & 1 deletion data/scenarios/Fun/00-ORDER.txt
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
61 changes: 61 additions & 0 deletions data/scenarios/Fun/_caterpillar/solution.sw
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;
97 changes: 97 additions & 0 deletions data/scenarios/Fun/_snake/program.sw
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;
55 changes: 55 additions & 0 deletions data/scenarios/Fun/_snake/solution.sw
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;
35 changes: 35 additions & 0 deletions data/scenarios/Fun/_snake/spawn.sw
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;
56 changes: 56 additions & 0 deletions data/scenarios/Fun/caterpillar.yaml
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...
Loading

0 comments on commit f398d89

Please sign in to comment.