Given a stack with 10 natural numbers and one string ("#"), the game is that
a player tries to find the position number on the stack of the sharp number.
If the player guess wrong the natural number is zapped from the stack, and If
he finds the sharp number his score increases by the number of elements
remainded in the stack.
>>> given this stack [3 4 2 #1 0 3 5 2 1 9 2]
// the user do not see the stack
choose between 1 and 11? 4
=> failed // pop 4th from stack [3 4 2 "#" 0 3 5 1 9 2]
choose between 1 and 10? 6
=> failed // pop 6th from stack [3 4 2 "#" 3 5 1 9 2]
choose between 1 and 9? 2
=> failed // pop 2nd from stack [3 4 2 "#" 3 5 1 2]
choose between 1 and 8? 6
=> failed // pop 6th from stack [3 4 "#" 3 5 1 2]
choose between 1 and 7? 5
=> Good! your score is 7 // stack [3 4 "1" 3 5 1 2]
Do you want to continue? Yes
>>> given this stack [0 0 0 0 0 0 0 0 #1 0 0]
// the user do not see the stack
choose between 1 and 11? 1
=> failed // pop 1st from stack [0 0 0 0 0 0 0 0 "#" 0 ]
choose between 1 and 10? 8
=> failed // pop 8th from stack [0 0 0 0 0 0 0 "#" 0 ]
choose between 1 and 9? 2
=> Good! your score is 16 // stack [0 0 0 0 0 0 0 "#" 0 ]
Now how do you write this game in Joy?
The following is the program implementation using #> and <# primitives:
DEFINE
sharp? == "#" = ;
choose == "choose between 1 and " putchars stack size 2 - put get;
good == "Good! your score is " putchars dup put ;
score == stack uncons size +; // score always on top of stack
genlist == rand 11 rem 1 +
[1 2 3 4 5 6 7 8 9 10 11]
[[=] ["#"] [rand 11 rem 1 +] ifte]
map;
game == choose dup # <#
[not sharp?]
[#> pop game]
[good score genlist dip game]
ifte ;
play == 0 genlist dip game.
do you see the how the sharp primitives work (see message 2692 for definitions)
?
Taoufik Dachraoui