Nim game: Difference between revisions

Content added Content deleted
(Dialects of BASIC moved to the BASIC section.)
Line 554: Line 554:
THANKS FOR PLAYING!
THANKS FOR PLAYING!
</pre>
</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}}==
=={{header|Arturo}}==
Line 720: Line 716:
computer wins
computer wins
</pre>
</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|Craft Basic}}===
<syntaxhighlight lang="basic">title "NIM"

bgcolor 0,180,0
cls
wait

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

if h - t < 0 then

print "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

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

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|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|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}}===
{{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|BlooP}}==
=={{header|BlooP}}==
Line 1,163: Line 1,550:
Computer wins!
Computer wins!
</pre>
</pre>

=={{header|Craft Basic}}==
<syntaxhighlight lang="basic">title "NIM"

bgcolor 0,180,0
cls
wait

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

if h - t < 0 then

print "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

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

endif

goto loop</syntaxhighlight>


=={{header|Crystal}}==
=={{header|Crystal}}==
Line 1,343: Line 1,665:
On my turn I will take 2 tokens.
On my turn I will take 2 tokens.
I got the last token. Better luck next time!</pre>
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|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|Go}}==
=={{header|Go}}==
Line 1,516: Line 1,739:
Computer wins!
Computer wins!
</pre>
</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}}==
=={{header|Haskell}}==
Line 1,587: Line 1,793:
Computer Wins!
Computer Wins!
</pre>
</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}}==
=={{header|J}}==
Line 3,644: Line 3,824:


Computer wins!
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>
</pre>


Line 4,051: Line 4,122:
I'll remove 1 tokens.
I'll remove 1 tokens.
I win!</pre>
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}}==
{{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|V (Vlang)}}==
=={{header|V (Vlang)}}==