Nim game: Difference between revisions

17,346 bytes added ,  1 month ago
Added various BASIC dialects (Chipmunk Basic, Minimal BASIC, MSX Basic and Quite BASIC)
(Added various BASIC dialects (Chipmunk Basic, Minimal BASIC, MSX Basic and Quite BASIC))
(24 intermediate revisions by 10 users not shown)
Line 554:
THANKS FOR PLAYING!
</pre>
 
=={{header|Applesoft BASIC}}==
This one-liner demonstrates the 240 character line limit. The program has been crafted to be exactly 244 characters long. The last 4 characters HEAP will be truncated from the last statement in the line. The HEAP variable is optional in the out-most NEXT statement so the program still runs correctly.
<syntaxhighlight lang="applesoftbasic">0ST$(0)="YOU MUST TAKE 1, 2, OR 3 TOKENS.":FORHEAP=12TO1STEP-4:PRINT"THERE ARE "HEAP" TOKENS REMAINING.":FORI=0TO1:INPUT"HOW MANY WOULD YOU LIKE TO TAKE?";T%:I=T%>0ANDT%<4:PRINTST$(I):NEXTI:PRINT"ON MY TURN I WILL TAKE "4-T%" TOKENS.":NEXTHEAP</syntaxhighlight>
 
=={{header|Arturo}}==
Line 720 ⟶ 716:
computer wins
</pre>
 
=={{header|BASIC}}==
==={{header|Applesoft BASIC}}===
This one-liner demonstrates the 240 character line limit. The program has been crafted to be exactly 244 characters long. The last 4 characters HEAP will be truncated from the last statement in the line. The HEAP variable is optional in the out-most NEXT statement so the program still runs correctly.
<syntaxhighlight lang="applesoftbasic">0ST$(0)="YOU MUST TAKE 1, 2, OR 3 TOKENS.":FORHEAP=12TO1STEP-4:PRINT"THERE ARE "HEAP" TOKENS REMAINING.":FORI=0TO1:INPUT"HOW MANY WOULD YOU LIKE TO TAKE?";T%:I=T%>0ANDT%<4:PRINTST$(I):NEXTI:PRINT"ON MY TURN I WILL TAKE "4-T%" TOKENS.":NEXTHEAP</syntaxhighlight>
 
==={{header|BASIC256}}===
{{works with|BASIC256|1.99.99.14+}}
<syntaxhighlight lang="basic">monton = 12
llevar = 0
 
while monton > 0
print "There are "; monton; " tokens remaining. How many would you like to take? ";
input integer llevar
while llevar = 0 or llevar > 3
print "You must take 1, 2, or 3 tokens. How many would you like to take ";
input integer llevar
end while
 
print "On my turn I will take "; 4 - llevar; " token(s)."
monton = monton - 4
end while
 
print
print "I got the last token. I win! Better luck next time."
end</syntaxhighlight>
 
==={{header|Chipmunk Basic}}===
{{trans|FreeBASIC}}
{{works with|Chipmunk Basic|3.6.4}}
<syntaxhighlight lang="vbnet">100 cls
110 monton = 12
120 llevar = 0
130 do while monton > 0
140 print using "There are ## tokens remaining. How many would you like to take";monton;
150 input llevar
160 do while llevar = 0 or llevar > 3
170 input "You must take 1, 2, or 3 tokens. How many would you like to take";llevar
180 loop
190 print "On my turn I will take";4-llevar;" token(s)."
200 monton = monton-4
210 loop
220 print
230 print "I got the last token. I win! Better luck next time."
240 end</syntaxhighlight>
 
==={{header|Craft Basic}}===
<syntaxhighlight lang="basic">let h = 12
 
label loop
 
alert "There are ", h ," tokens remaining."
input "How many would you like to take? ", t
 
if t > 3 or t < 1 then
 
alert "You must take between 1 to 3 tokens."
 
endif
 
if h - t < 0 then
 
alert "You cannot take that many. There's only ", h ," left."
 
endif
 
if t <= 3 and t >= 1 and h - t >= 0 then
 
let h = h - t
 
if h = 0 then
 
alert "Congratulations. You got the last token."
end
 
endif
 
let t = 4 - t
 
if h >= 15 then
 
let t = 3
 
endif
 
if h <= 3 then
 
let t = h
 
endif
 
alert "I will take ", t ," tokens."
let h = h - t
 
if h = 0 then
 
alert "I got the last token. I win. Better luck next time."
end
 
endif
 
endif
 
goto loop</syntaxhighlight>
 
==={{header|FreeBASIC}}===
<syntaxhighlight lang="freebasic">dim as ubyte heap=12, take
 
while heap > 0
print using "There are ## tokens remaining. How many would you like to take?"; heap
input take
while take=0 orelse take>3
print "You must take 1, 2, or 3 tokens. How many would you like to take?"
input take
wend
 
print using "On my turn I will take ## tokens."; 4-take
heap = heap - 4
wend
 
print "I got the last token. I win! Better luck next time."</syntaxhighlight>
 
==={{header|FTCBASIC}}===
<syntaxhighlight lang="basic">define tokens = 12, take = 0
 
gosub intro
 
do
 
print "There are " \
print tokens \
print " tokens remaining."
crlf
print "How many would you like to take? " \
 
input take
 
if take > 3 or take < 1 then
 
print "You must take between 1 to 3 tokens."
 
endif
 
if tokens - take < 0 then
 
print "You cannot take that many."
 
endif
 
if take <= 3 and take >= 1 and tokens - take >= 0 then
 
let tokens = tokens - take
 
if tokens = 0 then
 
bell
print "Congratulations. You got the last token."
pause
end
 
endif
 
let take = 4 - take
 
if tokens >= 15 then
 
let take = 3
 
endif
 
if tokens <= 3 then
 
let take = tokens
 
endif
 
print "I will take " \
print take \
print " of the tokens."
 
let tokens = tokens - take
 
if tokens = 0 then
 
print "I got the last token. I win. Better luck next time."
pause
end
 
endif
 
endif
 
loop
 
sub intro
 
cls
print "NIM game"
crlf
print "Press any key to play..."
cls
 
return</syntaxhighlight>
 
==={{header|GW-BASIC}}===
<syntaxhighlight lang="gwbasic">10 HEAP = 12
20 WHILE HEAP>0
30 TAKE = 0
40 PRINT "There are ";HEAP;" tokens left."
50 WHILE TAKE < 1 OR TAKE > 3 OR TAKE > HEAP
60 INPUT "How many would you like to take? ", TAKE
70 IF TAKE = HEAP THEN GOTO 140
80 WEND
90 PRINT "I will take ";4-TAKE;" tokens."
100 HEAP = HEAP - 4
110 WEND
120 PRINT "I got the last token. Better luck next time."
130 END
140 PRINT "You got the last token. Congratulations!"
150 END</syntaxhighlight>
 
==={{header|IS-BASIC}}===
<syntaxhighlight lang="is-basic">100 PROGRAM "Nim.bas"
110 RANDOMIZE
120 CLEAR SCREEN
130 LET TOKENS=12
140 PRINT "Starting with";TOKENS;"tokens.":PRINT
150 DO
160 PRINT "How many tokens will you take? (1-3) ";
170 DO
180 LET K=VAL(INKEY$)
190 LOOP UNTIL K>0 AND K<4
200 LET TOKENS=MAX(TOKENS-K,0):LET G=0
210 PRINT K:PRINT TAB(19);TOKENS;"remainig.":PRINT
220 IF TOKENS>0 THEN
230 LET L=MOD(TOKENS,4)
240 IF L=0 THEN LET L=MIN(RND(3)+1,TOKENS)
250 LET TOKENS=TOKENS-L:LET G=-1
260 PRINT "Computer takes";L;"tokens.";TOKENS;"remaining.":PRINT
270 END IF
280 LOOP WHILE TOKENS>0
290 IF G THEN
300 PRINT "Computer wins!"
310 ELSE
320 PRINT "You win!"
330 END IF</syntaxhighlight>
 
==={{header|Minimal BASIC}}===
{{trans|Tiny BASIC}}
{{works with|BASICA}}
{{works with|Chipmunk Basic}}
{{works with|GW-BASIC}}
{{works with|MSX BASIC}}
{{works with|PC-BASIC|any}}
{{works with|QBasic}}
<syntaxhighlight lang="qbasic">10 LET H = 12
20 PRINT "There are"
30 PRINT H
40 PRINT "tokens remaining. How many would you like to take?"
50 INPUT T
60 IF T > 3 THEN 170
70 IF T < 1 THEN 170
80 LET H = H - T
90 IF H = 0 THEN 190
100 LET T = 4 - T
110 PRINT "I will take"
120 PRINT T
130 PRINT "tokens."
140 LET H = H - T
150 IF H = 0 THEN 210
160 GOTO 20
170 PRINT "You must take 1, 2, or 3 tokens."
180 GOTO 50
190 PRINT "Congratulations. You got the last token."
200 GOTO 220
210 PRINT "I got the last token. I win. Better luck next time."
220 END</syntaxhighlight>
 
==={{header|MSX Basic}}===
{{works with|MSX BASIC|any}}
{{works with|Applesoft BASIC}}
{{works with|BASICA}}
{{works with|Chipmunk Basic}}
{{works with|GW-BASIC}}
{{works with|PC-BASIC|any}}
{{works with|QBasic}}
<syntaxhighlight lang="qbasic">100 CLS : rem 100 HOME for Applesoft BASIC
110 LET M = 12
120 LET L = 0
130 IF M <= 0 THEN GOTO 220
140 PRINT "There are "; M; " tokens remaining. How many would you like to take";
150 INPUT L
160 IF L <> 0 AND L <= 3 THEN GOTO 190
170 PRINT "You must take 1, 2, or 3 tokens. How many would you like to take";
180 GOTO 150
190 PRINT "On my turn I will take "; 4 - L; " token(s)."
200 LET M = M - 4
210 GOTO 130
220 PRINT
230 PRINT "I got the last token. I win! Better luck next time."
240 END</syntaxhighlight>
 
==={{header|PureBasic}}===
<syntaxhighlight lang="PureBasic">OpenConsole()
Define monton.i = 12, llevar.i
 
While monton > 0
Print("There are " + Str(monton) + " tokens remaining. How many would you like to take? ")
llevar = Val(Input())
While llevar = 0 Or llevar > 3
Print("You must take 1, 2, or 3 tokens. How many would you like to take ")
llevar = Val(Input())
Wend
PrintN("On my turn I will take " + Str(4 - llevar) + " token(s).")
monton = monton - 4
Wend
 
PrintN("I got the last token. I win! Better luck next time.")
 
PrintN(#CRLF$ + "--- terminado, pulsa RETURN---"): Input()
CloseConsole()</syntaxhighlight>
 
==={{header|QBasic}}===
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
<syntaxhighlight lang="qbasic">monton = 12
llevar = 0
 
DO WHILE monton > 0
PRINT USING "There are ## tokens remaining. How many would you like to take"; monton;
INPUT llevar
DO WHILE llevar = 0 OR llevar > 3
INPUT "You must take 1, 2, or 3 tokens. How many would you like to take"; llevar
LOOP
PRINT "On my turn I will take"; 4 - llevar; " token(s)."
monton = monton - 4
LOOP
 
PRINT
PRINT "I got the last token. I win! Better luck next time."
END</syntaxhighlight>
 
==={{header|Quite BASIC}}===
{{trans|Minimal BASIC}}
{{works with|BASICA}}
{{works with|Chipmunk Basic}}
{{works with|GW-BASIC}}
{{works with|MSX BASIC}}
{{works with|PC-BASIC|any}}
{{works with|QBasic}}
<syntaxhighlight lang="qbasic">10 LET H = 12
20 PRINT "There are"
30 PRINT H
40 PRINT "tokens remaining. How many would you like to take?"
50 INPUT ""; T
60 IF T > 3 THEN 170
70 IF T < 1 THEN 170
80 LET H = H - T
90 IF H = 0 THEN 190
100 LET T = 4 - T
110 PRINT "I will take"
120 PRINT T
130 PRINT "tokens."
140 LET H = H - T
150 IF H = 0 THEN 210
160 GOTO 20
170 PRINT "You must take 1, 2, or 3 tokens."
180 GOTO 50
190 PRINT "Congratulations. You got the last token."
200 GOTO 220
210 PRINT "I got the last token. I win. Better luck next time."
220 END</syntaxhighlight>
 
==={{header|Run BASIC}}===
{{works with|Just BASIC}}
{{works with|Liberty BASIC}}
<syntaxhighlight lang="vb">monton = 12
llevar = 0
 
while monton > 0
input "There are "; monton; " tokens remaining. How many would you like to take? "; llevar
while llevar = 0 or llevar > 3
input "You must take 1, 2, or 3 tokens. How many would you like to take "; llevar
wend
 
print "On my turn I will take "; 4 - llevar; " token(s)."
monton = monton - 4
wend
 
print
print "I got the last token. I win! Better luck next time."
end</syntaxhighlight>
 
==={{header|S-Basic}}===
<syntaxhighlight lang="basic">
$constant maxtokens = 12
$constant machine = 0
$constant human = 1
$constant false = 0
$constant true = 0FFFFH
 
procedure welcome
print "Welcome to the Game of Nim."
print "We begin with";maxtokens;" tokens. On each turn, a player"
print "may take between 1 and 3 tokens. The player who takes the"
print "last token wins."
print
end
 
procedure show(n = integer)
var i = integer
print "Available tokens:";n;" ";
rem - provide a visual display
for i = 1 to n
print "o ";
next i
print
end
 
function getnum(lowlim, toplim = integer) = integer
var ok, n = integer
repeat
begin
input "You take:";n
if n < lowlim or n > toplim then
begin
print "Must take between";lowlim;" and";toplim
print "Try again."
ok = false
end
else
ok = true
end
until ok
end = n
 
function play(player, tokens, taken = integer) = integer
if player = human then
taken = getnum(1,3)
else
begin
if tokens <= 3 then
taken = tokens
else
taken = 4 - taken
end
end = taken
 
procedure report(player = integer)
if player = human then
print "You took the last one. You win. Congratulations!"
else
print "I took the last one, so I win. Sorry about that."
end
 
var player, tokens, taken = integer
 
welcome
tokens = maxtokens
taken = 0
player = human
print "You go first."
repeat
begin
show tokens
taken = play(player, tokens, taken)
if player = machine then print "I took:";taken
tokens = tokens - taken
if tokens > 0 then player = 1 - player
end
until tokens <= 0
report player
print "Thanks for playing!"
 
end </syntaxhighlight>
{{out}}
<pre>
Welcome to the Game of Nim.
We begin with 12 tokens. On each turn, a
player may take between 1 and 3 tokens. The player
who takes the last token wins.
 
You go first.
Available tokens: 12 o o o o o o o o o o o o
You take:? 4
Must take between 1 and 3
Try again.
You take:? 3
Available tokens: 9 o o o o o o o o o
I took: 1
Available tokens: 8 o o o o o o o o
You take:? 2
Available tokens: 6 o o o o o o
I took: 2
Available tokens: 4 o o o o
You take:? 1
Available tokens: 3 o o o
I took: 3
I took the last one, so I win. Sorry about that.
Thanks for playing!
</pre>
 
==={{Header|Tiny BASIC}}===
{{works with|TinyBasic}}
<syntaxhighlight lang="basic">10 LET H = 12
20 PRINT "There are"
30 PRINT H
40 PRINT "tokens remaining. How many would you like to take?"
50 INPUT T
60 IF T > 3 THEN GOTO 170
70 IF T < 1 THEN GOTO 170
80 LET H = H - T
90 IF H = 0 THEN GOTO 190
100 LET T = 4 - T
110 PRINT "I will take"
120 PRINT T
130 PRINT "tokens."
140 LET H = H - T
150 IF H = 0 THEN GOTO 210
160 GOTO 20
170 PRINT "You must take 1, 2, or 3 tokens."
180 GOTO 50
190 PRINT "Congratulations. You got the last token."
200 END
210 PRINT "I got the last token. I win. Better luck next time."
220 END</syntaxhighlight>
 
==={{header|True BASIC}}===
{{works with|QBasic}}
{{works with|Just BASIC}}
{{works with|Liberty BASIC}}
<syntaxhighlight lang="qbasic">LET monton = 12
LET llevar = 0
 
DO WHILE monton > 0
PRINT "Quedan"; monton; "fichas. ¿Cuántas te gustaría tomar";
INPUT llevar
DO WHILE llevar = 0 Or llevar > 3
PRINT "Debes tomar 1, 2, o 3 fichas. ¿Cuántas te gustaría tomar";
INPUT llevar
LOOP
 
PRINT "Es mi turno, tomaré"; 4-llevar; "ficha(s)."
LET monton = monton - 4
LOOP
 
PRINT
PRINT "Obtuve la última ficha. ¡Gané! Mejor suerte la próxima vez."
END</syntaxhighlight>
 
==={{header|Yabasic}}===
<syntaxhighlight lang="basic">monton = 12
llevar = 0
 
while monton > 0
print "There are ", monton, " tokens remaining. How many would you like to take? ";
input "" llevar
while llevar = 0 or llevar > 3
input "You must take 1, 2, or 3 tokens. How many would you like to take? " llevar
wend
print "On my turn I will take ", 4 - llevar, " token(s)."
monton = monton - 4
wend
 
print "\nI got the last token. I win! Better luck next time."
end</syntaxhighlight>
 
=={{header|BlooP}}==
Line 1,071 ⟶ 1,636:
I take 2.
I win again.
</pre>
 
 
=={{header|Clojure}}==
<syntaxhighlight lang="clojure">(loop [n 12]
(print (format "%s remain, take how many?\n> " n)) (flush)
(let [v (try (Long. (clojure.string/trim (read-line)))
(catch Exception _ 0))]
(if (#{1 2 3} v)
(do (println (format "You took %s, leaving %s, computer takes %s..."
v (- n v) (- 4 v)))
(if (= 4 n)
(println "Computer wins. 😐")
(recur (- n 4))))
(do (println "Please enter 1, 2, or 3...")
(recur n)))))
</syntaxhighlight>
 
{{out}}
Sample game:
<pre>harold@freeside:~/src/clj-nim$ rlwrap clj -M nim.clj
12 remain, take how many?
3
You took 3, leaving 9, computer takes 1...
8 remain, take how many?
2
You took 2, leaving 6, computer takes 2...
4 remain, take how many?
1
You took 1, leaving 3, computer takes 3...
Computer wins. 😐
</pre>
 
Line 1,132 ⟶ 1,728:
Computer wins!
</pre>
 
=={{header|Craft Basic}}==
<syntaxhighlight lang="basic">title "NIM"
 
let h = 12
 
label loop
 
print "There are ", h ," tokens remaining."
input "How many would you like to take? ", t
 
if t > 3 or t < 1 then
 
print "You must take between 1 to 3 tokens."
 
endif
 
let h = h - t
 
if h = 0 then
 
alert "Congratulations. You got the last token."
end
 
endif
 
let t = 4 - t
 
print "I will take ", t ," tokens."
 
let h = h - t
 
if h = 0 then
 
alert "I got the last token. I win. Better luck next time."
end
 
endif
 
goto loop</syntaxhighlight>
 
=={{header|Crystal}}==
Line 1,208 ⟶ 1,764:
 
print("I win again.")</syntaxhighlight>
 
=={{header|EasyLang}}==
[Run it]
{{Trans|BASIC256}}
<syntaxhighlight lang="easylang">
token = 12
while token > 0
repeat
print "There are " & token & " tokens remaining."
write "How many would you like to take? (1,2 or 3) "
h = number input
print h
until h >= 1 and h <= 3
.
sleep 1
print "On my turn I will take " & 4 - h & " token(s)."
print ""
token -= 4
.
print "I got the last token. I win! Better luck next time."
</syntaxhighlight>
 
=={{header|Factor}}==
Line 1,287 ⟶ 1,864:
On my turn I will take 2 tokens.
I got the last token. Better luck next time!</pre>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">dim as ubyte heap=12, take
 
while heap > 0
print using "There are ## tokens remaining. How many would you like to take?"; heap
input take
while take=0 orelse take>3
print "You must take 1, 2, or 3 tokens. How many would you like to take?"
input take
wend
 
print using "On my turn I will take ## tokens."; 4-take
heap = heap - 4
wend
 
print "I got the last token. I win! Better luck next time."</syntaxhighlight>
 
=={{header|Go}}==
Line 1,378 ⟶ 1,938:
Computer wins!
</pre>
 
=={{header|GW-BASIC}}==
<syntaxhighlight lang="gwbasic">10 HEAP = 12
20 WHILE HEAP>0
30 TAKE = 0
40 PRINT "There are ";HEAP;" tokens left."
50 WHILE TAKE < 1 OR TAKE > 3 OR TAKE > HEAP
60 INPUT "How many would you like to take? ", TAKE
70 IF TAKE = HEAP THEN GOTO 140
80 WEND
90 PRINT "I will take ";4-TAKE;" tokens."
100 HEAP = HEAP - 4
110 WEND
120 PRINT "I got the last token. Better luck next time."
130 END
140 PRINT "You got the last token. Congratulations!"
150 END</syntaxhighlight>
 
=={{header|Haskell}}==
Line 1,449 ⟶ 1,992:
Computer Wins!
</pre>
 
=={{header|IS-BASIC}}==
<syntaxhighlight lang="is-basic">100 PROGRAM "Nim.bas"
110 RANDOMIZE
120 CLEAR SCREEN
130 LET TOKENS=12
140 PRINT "Starting with";TOKENS;"tokens.":PRINT
150 DO
160 PRINT "How many tokens will you take? (1-3) ";
170 DO
180 LET K=VAL(INKEY$)
190 LOOP UNTIL K>0 AND K<4
200 LET TOKENS=MAX(TOKENS-K,0):LET G=0
210 PRINT K:PRINT TAB(19);TOKENS;"remainig.":PRINT
220 IF TOKENS>0 THEN
230 LET L=MOD(TOKENS,4)
240 IF L=0 THEN LET L=MIN(RND(3)+1,TOKENS)
250 LET TOKENS=TOKENS-L:LET G=-1
260 PRINT "Computer takes";L;"tokens.";TOKENS;"remaining.":PRINT
270 END IF
280 LOOP WHILE TOKENS>0
290 IF G THEN
300 PRINT "Computer wins!"
310 ELSE
320 PRINT "You win!"
330 END IF</syntaxhighlight>
 
=={{header|J}}==
Line 1,701 ⟶ 2,218:
 
Computer wins.
</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Also works with gojq, the Go implementation of jq.'''
<syntaxhighlight lang=jq>
def play($tokens):
label $out
| "There are \($tokens) tokens. Take at most \([$tokens,3]|min) or enter q to quit.",
( foreach inputs as $in ( {$tokens};
if $in == "q" then break $out
else .in = $in
| if $in | test("^[0-9]+$") then .in |= tonumber else .in = null end
| if .in and .in > 0 and .in < 4
then (4 - .in) as $ct
| (if $ct == 1 then "" else "s" end) as $s
| .emit = " Computer takes \($ct) token\($s);"
| .tokens += -4
else .emit = "Please enter a number from 1 to \([3, .tokens]|min) inclusive."
end
end;
.emit,
if .tokens == 0
then "\nComputer wins!", break $out
elif .tokens < 0 then "\nCongratulations!", break $out
else "\(.tokens) tokens remain. How many tokens will you take?"
end )) ;
 
play(12)
</syntaxhighlight>
<pre>
$ jq -nRr -f nim-game.jq
There are 12 tokens. Take at most 3 or enter q to quit.
2
Computer takes 2 tokens;
8 tokens remain. How many tokens will you take?
1
Computer takes 3 tokens;
4 tokens remain. How many tokens will you take?
4
Please enter a number from 1 to 3 inclusive.
4 tokens remain. How many tokens will you take?
3
Computer takes 1 token;
 
Computer wins!
</pre>
 
Line 2,203 ⟶ 2,767:
Thanks for playing!
</pre>
 
=={{header|Pebble}}==
<syntaxhighlight lang="pebble">
;NIM game example program for x86 DOS
;Compiles with Pebble to com file under 1kb.
 
program examples\nim
 
data
 
int tokens[12]
int take[0]
 
begin
 
call intro
 
label gameloop
 
echo "There are"
echo [tokens]
echo " tokens remaining."
crlf
echo "How many would you like to take? "
input [take]
 
if [take] > 3 | [take] < 1 then
 
echo "You must take between 1 to 3 tokens."
 
endif
 
if [take] <= 3 & [take] >= 1 then
 
[tokens] = [tokens] - [take]
 
if [tokens] = 0 then
 
bell
echo "Congratulations. You got the last token."
pause
kill
 
endif
 
[take] = 4 - [take]
 
echo "I will take"
echo [take]
echo " tokens.
[tokens] = [tokens] - [take]
 
if [tokens] = 0 then
 
echo "I got the last token. I win. Better luck next time."
pause
kill
 
endif
 
endif
 
goto gameloop
 
end
 
sub intro
 
cls
echo "NIM game"
crlf
 
ret
 
</syntaxhighlight>
 
=={{header|Perl}}==
Line 2,683 ⟶ 3,322:
 
=={{header|Red}}==
<syntaxhighlight lang="rebolred">Red [
date: 2021-10-24
version: 0.6.4
Line 3,193 ⟶ 3,832:
<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}}==
Line 3,329 ⟶ 4,023:
 
Computer wins!
</pre>
 
=={{header|S-Basic}}==
<syntaxhighlight lang="basic">
$constant maxtokens = 12
$constant machine = 0
$constant human = 1
$constant false = 0
$constant true = 0FFFFH
 
procedure welcome
print "Welcome to the Game of Nim."
print "We begin with";maxtokens;" tokens. On each turn, a player"
print "may take between 1 and 3 tokens. The player who takes the"
print "last token wins."
print
end
 
procedure show(n = integer)
var i = integer
print "Available tokens:";n;" ";
rem - provide a visual display
for i = 1 to n
print "o ";
next i
print
end
 
function getnum(lowlim, toplim = integer) = integer
var ok, n = integer
repeat
begin
input "You take:";n
if n < lowlim or n > toplim then
begin
print "Must take between";lowlim;" and";toplim
print "Try again."
ok = false
end
else
ok = true
end
until ok
end = n
 
function play(player, tokens, taken = integer) = integer
if player = human then
taken = getnum(1,3)
else
begin
if tokens <= 3 then
taken = tokens
else
taken = 4 - taken
end
end = taken
 
procedure report(player = integer)
if player = human then
print "You took the last one. You win. Congratulations!"
else
print "I took the last one, so I win. Sorry about that."
end
 
var player, tokens, taken = integer
 
welcome
tokens = maxtokens
taken = 0
player = human
print "You go first."
repeat
begin
show tokens
taken = play(player, tokens, taken)
if player = machine then print "I took:";taken
tokens = tokens - taken
if tokens > 0 then player = 1 - player
end
until tokens <= 0
report player
print "Thanks for playing!"
 
end </syntaxhighlight>
{{out}}
<pre>
Welcome to the Game of Nim.
We begin with 12 tokens. On each turn, a
player may take between 1 and 3 tokens. The player
who takes the last token wins.
 
You go first.
Available tokens: 12 o o o o o o o o o o o o
You take:? 4
Must take between 1 and 3
Try again.
You take:? 3
Available tokens: 9 o o o o o o o o o
I took: 1
Available tokens: 8 o o o o o o o o
You take:? 2
Available tokens: 6 o o o o o o
I took: 2
Available tokens: 4 o o o o
You take:? 1
Available tokens: 3 o o o
I took: 3
I took the last one, so I win. Sorry about that.
Thanks for playing!
</pre>
 
Line 3,736 ⟶ 4,321:
I'll remove 1 tokens.
I win!</pre>
 
=={{Header|Tiny BASIC}}==
{{works with|TinyBasic}}
<syntaxhighlight lang="basic">10 LET H = 12
20 PRINT "There are"
30 PRINT H
40 PRINT "tokens remaining. How many would you like to take?"
50 INPUT T
60 IF T > 3 THEN GOTO 170
70 IF T < 1 THEN GOTO 170
80 LET H = H - T
90 IF H = 0 THEN GOTO 190
100 LET T = 4 - T
110 PRINT "I will take"
120 PRINT T
130 PRINT "tokens."
140 LET H = H - T
150 IF H = 0 THEN GOTO 210
160 GOTO 20
170 PRINT "You must take 1, 2, or 3 tokens."
180 GOTO 50
190 PRINT "Congratulations. You got the last token."
200 END
210 PRINT "I got the last token. I win. Better luck next time."
220 END</syntaxhighlight>
 
=={{header|Tiny Craft Basic}}==
<syntaxhighlight lang="basic">5 cls
10 print "NIM
20 let h = 12
30 print "There are ", h ," tokens remaining."
40 input "How many would you like to take? ", t
50 if t > 3 then 140
60 if t < 1 then 140
70 let h = h - t
80 if h = 0 then 160
90 let t = 4 - t
100 print "I will take ", t ," tokens."
110 let h = h - t
120 if h = 0 then 180
130 goto 30
140 print "You must take between 1 to 3 tokens."
150 goto 40
160 print "Congratulations. You got the last token."
170 goto 190
180 print "I got the last token. I win. Better luck next time."
190 shell "pause"</syntaxhighlight>
 
=={{header|True BASIC}}==
<syntaxhighlight lang="qbasic">LET monton = 12
LET llevar = 0
 
DO WHILE monton > 0
PRINT "Quedan"; monton; "fichas. ¿Cuántas te gustaría tomar";
INPUT llevar
DO WHILE llevar = 0 Or llevar > 3
PRINT "Debes tomar 1, 2, o 3 fichas. ¿Cuántas te gustaría tomar";
INPUT llevar
LOOP
 
PRINT "Es mi turno, tomaré"; 4-llevar; "ficha(s)."
LET monton = monton - 4
LOOP
 
PRINT
SET COLOR 2
PRINT "Obtuve la última ficha. ¡Gané! Mejor suerte la próxima vez."
END</syntaxhighlight>
 
 
=={{header|V (Vlang)}}==
Line 3,869 ⟶ 4,385:
=={{header|Wren}}==
{{trans|Go}}
<syntaxhighlight lang="ecmascriptwren">import "io" for Stdin, Stdout
 
var showTokens = Fn.new { |tokens| System.print("Tokens remaining %(tokens)\n") }
Line 3,921 ⟶ 4,437:
 
Computer wins!
</pre>
 
=={{header|XPL0}}==
{{trans|Go}}
<syntaxhighlight lang "XPL0">func ShowTokens(Tokens);
int Tokens;
[Text(0, "Tokens remaining: ");
IntOut(0, Tokens);
CrLf(0); CrLf(0);
];
 
int Tokens, T, CT;
[Tokens:= 12;
loop [ShowTokens(Tokens);
Text(0, " How many Tokens (1, 2 or 3)? ");
T:= IntIn(0);
if T < 1 or T > 3 then
Text(0, "^m^jMust be a number between 1 and 3. Try again.^m^j^m^j")
else [CT:= 4 - T;
Text(0, " Computer takes ");
IntOut(0, CT);
Text(0, " token");
if CT # 1 then ChOut(0, ^s);
Text(0, ".^m^j^m^j");
Tokens:= Tokens - 4;
];
if Tokens = 0 then
[ShowTokens(0);
Text(0, " Computer wins!^m^j");
return;
];
];
]</syntaxhighlight>
{{out}}
<pre>
Tokens remaining: 12
 
How many Tokens (1, 2 or 3)? 2
Computer takes 2 tokens.
 
Tokens remaining: 8
 
How many Tokens (1, 2 or 3)? 4
 
Must be a number between 1 and 3. Try again.
 
Tokens remaining: 8
 
How many Tokens (1, 2 or 3)? 1
Computer takes 3 tokens.
 
Tokens remaining: 4
 
How many Tokens (1, 2 or 3)? 3
Computer takes 1 token.
 
Tokens remaining: 0
 
Computer wins!
</pre>
 
=={{header|Z80 Assembly}}==
{{works with|CP/M 3.1|YAZE-AG-2.51.2 Z80 emulator}}
{{works with|ZSM4 macro assembler|YAZE-AG-2.51.2 Z80 emulator}}
Use the /S8 switch on the ZSM4 assembler for 8 significant characters for labels and names<br><br>
This assembles to 99 bytes of instructions plus 98 bytes of static data for the texts and buffer, accounting for a total of 197 bytes. Not sure whether the 8080 and 8086 versions counted only instruction size or total program size, but the Z80 provides some powerful looping instructions that help to reduce program size.<br><br>
Please note that I did not bother with conversion from input characters to integers and using comparisons to validate them, nor did I use arithmetic to calculate the computer's move. Instead, I used fixed strings and the cpdr instruction to look up the user's input as well as the computer's response. The CP/M BDOS call ensures that the user can enter a single character only.<br>
<syntaxhighlight lang="z80">
;
; Nim game using Z80 assembly language
;
; Runs under CP/M 3.1 on YAZE-AG-2.51.2 Z80 emulator
; Assembled with zsm4 on same emulator/OS, uses macro capabilities of said assembler
; Created with vim under Windows
;
; 2023-04-28 Xorph
;
 
;
; Useful definitions
;
 
bdos equ 05h ; Call to CP/M BDOS function
readstr equ 0ah ; Read string from console
wrtstr equ 09h ; Write string to console
 
cr equ 0dh ; ASCII control characters
lf equ 0ah
 
buflen equ 01h ; Length of input buffer
maxtok equ 12 ; Starting number of tokens
 
;
; Macro for BDOS calls
;
 
readln macro buf ; Read a line from input
push bc
ld c,readstr
ld de,buf
call bdos
pop bc
endm
 
 
;
; =====================
; Start of main program
; =====================
;
 
cseg
 
ld de,nim ; Print title and initialize
call print
ld c,maxtok ; Register c keeps track of remaining tokens
 
loop:
ld de,tokens ; Print the remaining tokens
call print
ld b,c ; Use b for loop to print remaining tokens
printtk:
ld de,token
call print
djnz printtk
ld de,prompt ; Prompt user for input
call print
readln inputbuf
 
ld a,(bufcont) ; Now check input for validity and compute response
ld hl,validinp+2 ; Start from end of valid string, so bc gets set to numeric equivalent
push bc ; Save token counter, use bc for cpdr
ld bc,3
cpdr ; Use automatic search function
jr nz,printerr ; If input character not found, print error
 
ld hl,mymoves ; Get character for response into a
add hl,bc ; bc contains index into check string as well as response string
pop bc
ld a,(hl)
ld (outbuf),a ; Put it in output buffer
 
ld de,response ; Print the computer's move
call print
 
ld a,c ; Subtract 4 tokens from counter
sub 4
ld c,a
 
jr nz,loop ; If not finished, repeat
ld de,lose ; Otherwise, player lost
call print
 
ret ; Return to CP/M
 
printerr: ; Print error message and try again
pop bc
ld de,wronginp
call print
jp loop
 
print: ; Use subroutine instead of macro for smaller code
push bc
ld c,wrtstr
call bdos
pop bc
ret
 
;
; ===================
; End of main program
; ===================
;
 
;
; ================
; Data definitions
; ================
;
 
dseg
 
inputbuf: ; Input buffer
defb buflen ; Maximum possible length
defb 00h ; Returned length of actual input
bufcont:
defs buflen ; Actual input area
nim:
defb 'Nim$' ; Dialog texts
prompt:
defb cr,lf,'How many will you take (1-3)? $'
response:
defb cr,lf,'I take ' ; No $ here! Saves one print command
outbuf:
defb ' $' ; For printing response
tokens:
defb cr,lf,cr,lf,'Tokens: $'
token:
defb '|$'
lose:
defb cr,lf,'You lose!$'
wronginp:
defb cr,lf,'Wrong input$'
validinp:
defb '123' ; Valid input
mymoves:
defb '321' ; Computer's response
</syntaxhighlight>
 
{{out}}
<pre>
E>nim
Nim
 
Tokens: ||||||||||||
How many will you take (1-3)? 2
I take 2
 
Tokens: ||||||||
How many will you take (1-3)? 4
Wrong input
 
Tokens: ||||||||
How many will you take (1-3)? 3
I take 1
 
Tokens: ||||
How many will you take (1-3)? 1
I take 3
You lose!
E>
</pre>
2,122

edits