Nim game: Difference between revisions

Line 3,430:
<br><br>
[https://youtu.be/b4mP6TrARKI Nim Game - video]
 
=={{header|RPL}}==
This task is an opportunity to showcase the use of both persistent variables and local variables, the latter vanishing once the program executed.
The computer has been named "HAL" rather than "Player 2" or "Computer" to keep the message length under 16 characters, which is the width of the HP-28 display.
To play, initialize the game by entering the word <code>NUNIM</code>, then input your moves followed by the word <code>PLNIM</code> until you will lose. The case for a human win is nevertheless coded to appear as a a fair game.
{{works with|Halcyon Calc|4.2.7}}
≪ "Tokens = " tokens →STR +
"1..3 PLNIM?"
'DispN' STO
 
≪ CLEAR 12 'tokens' STO // emptying the stack and creating a persistent variable
DispN
'NUNIM' STO
 
≪ → move // creating a local variable, with value = content of stack level 1:
≪ IF { 1 2 3 } move POS
THEN
"You take " move →STR +
'tokens' move STO- // substracting the value of a local variable from a persistent variable directly
IF tokens 0 ≤
THEN "You win!"
ELSE
4 move - tokens MIN "HAL takes " OVER →STR + 'tokens' ROT STO-
IF tokens 0 ≤ THEN DROP "HAL wins!" ELSE DispN END
END
ELSE move →STR " is illegal" + DispN
END
≫ ≫
'PLNIM' STO // 67 words, 4% stack
 
NUNIM
3 PLNIM
4 PLNIM
2 PLNIM
2 PLNIM
{{out}}
<pre>
15: "Tokens = 12"
14: "1..3 PLNIM?"
13: "You take 3"
12: "HAL takes 1"
11: "Tokens = 8"
10: "1..3 PLNIM?"
9: "4 is illegal"
8: "Tokens = 8"
7: "1..3 PLNIM?"
6: "You take 2"
5: "HAL takes 2"
4: "Tokens = 4"
3: "1..3 PLNIM?"
2: "You take 2"
1: "HAL wins!"
</pre>
 
=={{header|Ruby}}==
1,150

edits