Tracks
/
Factor
Factor
/
Exercises
/
Annalyn's Infiltration
Annalyn's Infiltration

Annalyn's Infiltration

Learning Exercise

Introduction

This exercise builds on the data stack and stack-effect notation you met in Cargo Shuffle. Here you will write words that combine boolean values.

Shuffle words

The most common shuffle words live in kernel:

dup    ( x -- x x )
dupd   ( x y -- x x y )
drop   ( x -- )
swap   ( x y -- y x )
swapd  ( x y z -- y x z )
over   ( x y -- x y x )
pick   ( x y z -- x y z x )
rot    ( x y z -- y z x )
-rot   ( x y z -- z x y )
rotd   ( w x y z -- w y z x )
spin   ( x y z -- z y x )
nip    ( x y -- y )
tuck   ( x y -- y x y )

2dup   ( x y -- x y x y )
3dup   ( x y z -- x y z x y z )
4dup   ( w x y z -- w x y z w x y z )
2drop  ( x y -- )
3drop  ( x y z -- )
4drop  ( w x y z -- )
2nip   ( x y z -- z )
2swap  ( x y z w -- z w x y )

Booleans

The two boolean literals are t and f. f is Factor's only falsy value β€” everything else, including 0 and empty collections, is truthy.

t .    ! => t
f .    ! => f

Boolean words

These three boolean words from the kernel vocabulary:

and ( x y -- ? )    ! true when both inputs are truthy
or  ( x y -- ? )    ! true when either input is truthy
not ( x   -- ? )    ! inverts its argument
t t and .    ! => t
t f and .    ! => f
f t or .     ! => t
t not .      ! => f

The ? in each output is the stack-effect convention for "a boolean".

You'll need swap for one of the tasks below.

Instructions

In this exercise, you'll be writing some logic for a video game a friend is developing. The game's main character is Annalyn, a brave girl with a fierce and loyal pet dog. Unfortunately, disaster strikes, as her best friend is kidnapped while searching for berries in the forest. Annalyn will try to find and free her friend, optionally taking her dog with her on this quest.

Annalyn eventually finds the camp in which her friend is imprisoned and it turns out there are two kidnappers: a mighty knight and a cunning archer.

The player is presented with some options for what to do next. For each of the four possible options you need to write a word that tells the game whether it should show that option or not.

1. Check if the 'Fast Attack' option should be shown

If the knight is sleeping, then Annalyn will be able to make a quick attack into the camp before he can wake up properly and get his armour on.

Implement a word named can-do-fast-attack that takes a boolean value indicating whether the knight is awake. It returns t if the 'Fast Attack' action is available, otherwise f:

t can-do-fast-attack .
! => f

2. Check if the 'Spy' option should be shown

The group can be spied upon if at least one of them is awake. Otherwise, spying is a waste of time.

Implement a word named can-spy that takes three boolean values, indicating whether the knight, archer and prisoner, respectively, are awake. It returns t if the 'Spy' action is available, otherwise f:

f t f can-spy .
! => t

3. Check if the 'Signal Prisoner' option should be shown

The prisoner can be signalled using bird sounds if she is awake and the archer is sleeping. If the archer is awake then she can't be safely signaled because the archer is also trained in bird signalling.

Implement a word named can-signal-prisoner that takes two boolean values, indicating whether the archer and prisoner, respectively, are awake. It returns t if the 'Signal Prisoner' action is available, otherwise f:

f t can-signal-prisoner .
! => t

4. Check if the 'Free Prisoner' option should be shown

Annalyn can try sneaking into the camp to free her friend. This is a risky thing to do and can only succeed in one of two ways:

  • If Annalyn has her pet dog with her, she can rescue the prisoner if the archer is asleep. The knight is scared of the dog and the archer will not have time to get ready before Annalyn and her friend can escape.

  • If Annalyn does not have her dog, then she and the prisoner must be very sneaky! Annalyn can free the prisoner if the prisoner is awake and the knight and archer are both sleeping. If the prisoner is sleeping, she can't be rescued: she would be startled by Annalyn's sudden appearance and wake up the knight and archer.

Implement a word named can-free-prisoner that takes four boolean values. The parameters, in order, indicate whether the archer is awake, whether the dog is present, whether the prisoner is awake, and whether the knight is awake. It returns t if the 'Free Prisoner' action is available, otherwise f:

f t t f can-free-prisoner .
! => t      (archer asleep, dog present, prisoner awake, knight asleep)

t t f f can-free-prisoner .
! => f      (archer is awake, so no rescue possible)
Edit via GitHub The link opens in a new window or tab
Factor Exercism

Ready to start Annalyn's Infiltration?

Sign up to Exercism to learn and master Factor with 47 concepts163 exercises, and real human mentoring, all for free.