Nim game: Difference between revisions

Content added Content deleted
(added Arturo implementation)
m (syntax highlighting fixup automation)
Line 22: Line 22:
{{trans|Python}}
{{trans|Python}}


<lang 11l>V tokens = 12
<syntaxhighlight lang="11l">V tokens = 12


F getTokens(curTokens) -> N
F getTokens(curTokens) -> N
Line 47: Line 47:
compTurn(tokens)
compTurn(tokens)


print(‘Computer wins!’)</lang>
print(‘Computer wins!’)</syntaxhighlight>


{{out}}
{{out}}
Line 79: Line 79:
It may not be a very interesting game, but it assembles to only 222 bytes.
It may not be a very interesting game, but it assembles to only 222 bytes.


<lang 8080asm>bdos: equ 5 ; CP/M syscalls
<syntaxhighlight lang="8080asm">bdos: equ 5 ; CP/M syscalls
puts: equ 9
puts: equ 9
putch: equ 2
putch: equ 2
Line 167: Line 167:
lose: db 13,10,'You lose!$'
lose: db 13,10,'You lose!$'
nl: db 13,10,'$'
nl: db 13,10,'$'
wronginp: db 8,7,32,8,'$' ; beep and erase choice</lang>
wronginp: db 8,7,32,8,'$' ; beep and erase choice</syntaxhighlight>


=={{header|8086 Assembly}}==
=={{header|8086 Assembly}}==
Line 187: Line 187:




<lang asm> ;; MS-DOS Nim; assembles with nasm.
<syntaxhighlight lang="asm"> ;; MS-DOS Nim; assembles with nasm.
bits 16
bits 16
cpu 8086
cpu 8086
Line 246: Line 246:
tokens: db 13,10,13,10,'Tokens: $'
tokens: db 13,10,13,10,'Tokens: $'
lose: db 13,10,'You lose!$'
lose: db 13,10,'You lose!$'
wronginp: db 8,7,32,8,'$' </lang>
wronginp: db 8,7,32,8,'$' </syntaxhighlight>


=={{header|Action!}}==
=={{header|Action!}}==
<lang Action!>BYTE FUNC PlayerTurn(BYTE tokens)
<syntaxhighlight lang="action!">BYTE FUNC PlayerTurn(BYTE tokens)
BYTE t,max
BYTE t,max


Line 295: Line 295:
player=1-player
player=1-player
OD
OD
RETURN</lang>
RETURN</syntaxhighlight>
{{out}}
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Nim_game.png Screenshot from Atari 8-bit computer]
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Nim_game.png Screenshot from Atari 8-bit computer]
Line 320: Line 320:
{{works with|Ada|Ada|2012}}
{{works with|Ada|Ada|2012}}


<lang Ada>with Ada.Text_IO;
<syntaxhighlight lang="ada">with Ada.Text_IO;


procedure Nim is
procedure Nim is
Line 364: Line 364:


TIO.Put_Line("Computer won!");
TIO.Put_Line("Computer won!");
end Nim;</lang>
end Nim;</syntaxhighlight>
{{out}}
{{out}}
<pre> 12 tokens remain.
<pre> 12 tokens remain.
Line 387: Line 387:


=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
<lang algol68>BEGIN # play Nim #
<syntaxhighlight lang="algol68">BEGIN # play Nim #
# gets a single character answer from standin and returns it #
# gets a single character answer from standin and returns it #
PROC answer = ( STRING prompt )CHAR:
PROC answer = ( STRING prompt )CHAR:
Line 427: Line 427:
DO SKIP OD;
DO SKIP OD;
print( ( "I win!", newline ) )
print( ( "I win!", newline ) )
END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 444: Line 444:


=={{header|ALGOL-M}}==
=={{header|ALGOL-M}}==
<lang algol>
<syntaxhighlight lang="algol">
BEGIN
BEGIN


Line 524: Line 524:
WRITE("THANKS FOR PLAYING!");
WRITE("THANKS FOR PLAYING!");


END </lang>
END </syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 559: Line 559:
=={{header|Applesoft 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.
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.
<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</lang>
<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}}==


<lang rebol>total: 12
<syntaxhighlight lang="rebol">total: 12
while ø [
while ø [
tk: 0
tk: 0
Line 579: Line 579:
break
break
]
]
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}
Line 610: Line 610:


=={{header|AsciiDots}}==
=={{header|AsciiDots}}==
<syntaxhighlight lang="asciidots">
<lang AsciiDots>
%$LMRTX
%$LMRTX
.-$"Nim Dots"-$""-
.-$"Nim Dots"-$""-
Line 632: Line 632:
L-------------*---*>$_#-$" dots remaining."-$""
L-------------*---*>$_#-$" dots remaining."-$""
T
T
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 663: Line 663:


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">Play:
<lang AutoHotkey>Play:
tokens := 12
tokens := 12
while tokens {
while tokens {
Line 679: Line 679:
else
else
ExitApp
ExitApp
return</lang>
return</syntaxhighlight>


=={{header|AWK}}==
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f NIM_GAME.AWK
# syntax: GAWK -f NIM_GAME.AWK
BEGIN {
BEGIN {
Line 707: Line 707:
printf("%s takes %d token%s; there are %d remaining\n",who,ans,(ans==1)?"":"s",tokens)
printf("%s takes %d token%s; there are %d remaining\n",who,ans,(ans==1)?"":"s",tokens)
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 725: Line 725:
=={{header|BlooP}}==
=={{header|BlooP}}==
Bloop has no input capabilites, so the game is defined as a procedure, called with 3 numbers (since the game will last only 3 rounds anyhow). The procedure can be called with more numbers - extra parameters are ignored in most implementations I have found. Since there is no easy way to get more inputs, any incorrect values are converted to correct ones.
Bloop has no input capabilites, so the game is defined as a procedure, called with 3 numbers (since the game will last only 3 rounds anyhow). The procedure can be called with more numbers - extra parameters are ignored in most implementations I have found. Since there is no easy way to get more inputs, any incorrect values are converted to correct ones.
<syntaxhighlight lang="bloop">
<lang BlooP>
DEFINE PROCEDURE ''DIVIDE'' [A,B]:
DEFINE PROCEDURE ''DIVIDE'' [A,B]:
BLOCK 0: BEGIN
BLOCK 0: BEGIN
Line 817: Line 817:


PLAY_GAME [1,4,3];
PLAY_GAME [1,4,3];
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 846: Line 846:


=={{header|C}}==
=={{header|C}}==
<syntaxhighlight lang="c">
<lang c>
#include <stdio.h>
#include <stdio.h>


Line 906: Line 906:
return remainingTokens;
return remainingTokens;
}
}
</syntaxhighlight>
</lang>


{{Out}}
{{Out}}
Line 942: Line 942:
=={{header|C++}}==
=={{header|C++}}==
{{trans|Go}}
{{trans|Go}}
<lang cpp>#include <iostream>
<syntaxhighlight lang="cpp">#include <iostream>
#include <limits>
#include <limits>


Line 976: Line 976:
}
}
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,014: Line 1,014:


=={{header|C#}}==
=={{header|C#}}==
<lang csharp>
<syntaxhighlight lang="csharp">
using System;
using System;


Line 1,052: Line 1,052:
}
}
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
Sample game:
Sample game:
Line 1,077: Line 1,077:


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
<lang lisp>
<syntaxhighlight lang="lisp">
(defun pturn (curTokens)
(defun pturn (curTokens)
(write-string "How many tokens would you like to take?: ")
(write-string "How many tokens would you like to take?: ")
Line 1,105: Line 1,105:
(return)))
(return)))
(write-string "Computer wins!")
(write-string "Computer wins!")
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 1,136: Line 1,136:


=={{header|Crystal}}==
=={{header|Crystal}}==
<lang Ruby>tokens = 12
<syntaxhighlight lang="ruby">tokens = 12


until tokens <= 0
until tokens <= 0
Line 1,147: Line 1,147:
end
end


puts "Computer wins."</lang>
puts "Computer wins."</syntaxhighlight>


=={{header|Dyalect}}==
=={{header|Dyalect}}==
<lang dyalect>print("There are twelve tokens.")
<syntaxhighlight lang="dyalect">print("There are twelve tokens.")
print("You can take 1, 2, or 3 on your turn.")
print("You can take 1, 2, or 3 on your turn.")
print("Whoever takes the last token wins.\n")
print("Whoever takes the last token wins.\n")
Line 1,169: Line 1,169:
}
}


print("I win again.")</lang>
print("I win again.")</syntaxhighlight>


=={{header|Factor}}==
=={{header|Factor}}==
<lang factor>USING: interpolate io kernel math math.parser sequences ;
<syntaxhighlight lang="factor">USING: interpolate io kernel math math.parser sequences ;
IN: rosetta-code.nim-game
IN: rosetta-code.nim-game


Line 1,194: Line 1,194:
: nim-game ( -- ) 12 round drop "Computer wins!" print ;
: nim-game ( -- ) 12 round drop "Computer wins!" print ;


MAIN: nim-game</lang>
MAIN: nim-game</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,224: Line 1,224:


=={{header|Fermat}}==
=={{header|Fermat}}==
<lang fermat>heap:=12;
<syntaxhighlight lang="fermat">heap:=12;
while heap>0 do
while heap>0 do
!!('There are ',heap,' tokens left. How many do you want to take?');
!!('There are ',heap,' tokens left. How many do you want to take?');
Line 1,236: Line 1,236:
od;
od;


!!('I got the last token. Better luck next time!');</lang>
!!('I got the last token. Better luck next time!');</syntaxhighlight>
{{out}}<pre>There are 12 tokens left. How many do you want to take?
{{out}}<pre>There are 12 tokens left. How many do you want to take?
>take := 4
>take := 4
Line 1,251: Line 1,251:


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<lang freebasic>dim as ubyte heap=12, take
<syntaxhighlight lang="freebasic">dim as ubyte heap=12, take


while heap > 0
while heap > 0
Line 1,265: Line 1,265:
wend
wend


print "I got the last token. I win! Better luck next time."</lang>
print "I got the last token. I win! Better luck next time."</syntaxhighlight>


=={{header|Go}}==
=={{header|Go}}==
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 1,310: Line 1,310:
}
}
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,342: Line 1,342:


=={{header|GW-BASIC}}==
=={{header|GW-BASIC}}==
<lang gwbasic>10 HEAP = 12
<syntaxhighlight lang="gwbasic">10 HEAP = 12
20 WHILE HEAP>0
20 WHILE HEAP>0
30 TAKE = 0
30 TAKE = 0
Line 1,356: Line 1,356:
130 END
130 END
140 PRINT "You got the last token. Congratulations!"
140 PRINT "You got the last token. Congratulations!"
150 END</lang>
150 END</syntaxhighlight>


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang haskell>import Data.Char (isDigit, digitToInt)
<syntaxhighlight lang="haskell">import Data.Char (isDigit, digitToInt)
import System.IO
import System.IO


Line 1,393: Line 1,393:


main :: IO ()
main :: IO ()
main = play 12</lang>
main = play 12</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,413: Line 1,413:


=={{header|IS-BASIC}}==
=={{header|IS-BASIC}}==
<lang IS-BASIC>100 PROGRAM "Nim.bas"
<syntaxhighlight lang="is-basic">100 PROGRAM "Nim.bas"
110 RANDOMIZE
110 RANDOMIZE
120 CLEAR SCREEN
120 CLEAR SCREEN
Line 1,436: Line 1,436:
310 ELSE
310 ELSE
320 PRINT "You win!"
320 PRINT "You win!"
330 END IF</lang>
330 END IF</syntaxhighlight>


=={{header|J}}==
=={{header|J}}==
Line 1,442: Line 1,442:
Implementation:
Implementation:


<lang J>nim=: {{
<syntaxhighlight lang="j">nim=: {{
prompt tokens=: 12
prompt tokens=: 12
}}
}}
Line 1,461: Line 1,461:
echo 'I take ',(":t=. 4-y),' tokens'
echo 'I take ',(":t=. 4-y),' tokens'
prompt tokens=:tokens-t
prompt tokens=:tokens-t
}}</lang>
}}</syntaxhighlight>


Sample session:
Sample session:


<lang J> nim''
<syntaxhighlight lang="j"> nim''
tokens: 12
tokens: 12
take 1, 2 or 3 tokens
take 1, 2 or 3 tokens
Line 1,482: Line 1,482:
I take 2 tokens
I take 2 tokens
tokens: 0
tokens: 0
game over</lang>
game over</syntaxhighlight>


=={{header|Java}}==
=={{header|Java}}==
<lang java>
<syntaxhighlight lang="java">
import java.util.Scanner;
import java.util.Scanner;


Line 1,540: Line 1,540:


}
}
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 1,578: Line 1,578:
=== Browser Version ===
=== Browser Version ===
This is the easy but dirty way - with prompt for input, and console.log for output. The Nim class was structured so that input and output could be customized, for example to use HTML DOM elements for in and out, instead of the terminal.
This is the easy but dirty way - with prompt for input, and console.log for output. The Nim class was structured so that input and output could be customized, for example to use HTML DOM elements for in and out, instead of the terminal.
<syntaxhighlight lang="javascript">
<lang Javascript>
class Nim {
class Nim {
constructor(tokens, printFun) {
constructor(tokens, printFun) {
Line 1,630: Line 1,630:
}
}
}
}
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 1,667: Line 1,667:
=={{header|Julia}}==
=={{header|Julia}}==
{{trans|Raku}}
{{trans|Raku}}
<lang julia>function nimgame()
<syntaxhighlight lang="julia">function nimgame()
tcount = 12
tcount = 12
takenum = 0
takenum = 0
Line 1,689: Line 1,689:


nimgame()
nimgame()
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
12 tokens remain.
12 tokens remain.
Line 1,708: Line 1,708:
=={{header|Kotlin}}==
=={{header|Kotlin}}==
{{trans|Go}}
{{trans|Go}}
<lang scala>// Version 1.3.21
<syntaxhighlight lang="scala">// Version 1.3.21


fun showTokens(tokens: Int) {
fun showTokens(tokens: Int) {
Line 1,734: Line 1,734:
}
}
}
}
}</lang>
}</syntaxhighlight>


{{output}}
{{output}}
Line 1,766: Line 1,766:


=={{header|Lua}}==
=={{header|Lua}}==
<syntaxhighlight lang="lua">
<lang Lua>
tokens = 12
tokens = 12


Line 1,806: Line 1,806:


print ("Computer wins.")
print ("Computer wins.")
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 1,844: Line 1,844:


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<lang Mathematica>n = 12;
<syntaxhighlight lang="mathematica">n = 12;
While[n > 0,
While[n > 0,
c = ChoiceDialog["Current amount = " <> ToString[n] <> "\nHow many do you want to pick?", {1 -> 1, 2 -> 2, 3 -> 3}];
c = ChoiceDialog["Current amount = " <> ToString[n] <> "\nHow many do you want to pick?", {1 -> 1, 2 -> 2, 3 -> 3}];
Line 1,851: Line 1,851:
n -= (4 - c);
n -= (4 - c);
]
]
ChoiceDialog["Current amount = " <> ToString[n] <> "\n You lost!"]</lang>
ChoiceDialog["Current amount = " <> ToString[n] <> "\n You lost!"]</syntaxhighlight>


=={{header|MiniScript}}==
=={{header|MiniScript}}==
{{trans|Lua}}
{{trans|Lua}}
<lang MiniScript>tokens = 12
<syntaxhighlight lang="miniscript">tokens = 12
print "Nim Game"
print "Nim Game"
Line 1,894: Line 1,894:
end while
end while
print "Computer wins."</lang>
print "Computer wins."</syntaxhighlight>
{{out}}
{{out}}
<pre>Nim Game
<pre>Nim Game
Line 1,926: Line 1,926:
=={{header|Nim}}==
=={{header|Nim}}==


<lang nim>import strutils
<syntaxhighlight lang="nim">import strutils
import terminal
import terminal


Line 1,964: Line 1,964:
computer()
computer()


styledEcho(styleBright, "Computer wins!")</lang>
styledEcho(styleBright, "Computer wins!")</syntaxhighlight>


{{out}}
{{out}}
Line 1,998: Line 1,998:
=={{header|OCaml}}==
=={{header|OCaml}}==
{{trans|Python|with plurals added, loops turned to recursion and string input handled}}
{{trans|Python|with plurals added, loops turned to recursion and string input handled}}
<lang ocaml>let rec player_turn () =
<syntaxhighlight lang="ocaml">let rec player_turn () =
print_string "How many tokens would you like to take? ";
print_string "How many tokens would you like to take? ";
let n = read_int_opt () |> Option.value ~default:0 in
let n = read_int_opt () |> Option.value ~default:0 in
Line 2,023: Line 2,023:
if tokens = 0 then print_endline "Computer wins!" else play_game tokens
if tokens = 0 then print_endline "Computer wins!" else play_game tokens


let () = play_game 12</lang>
let () = play_game 12</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,046: Line 2,046:
=={{header|Perl}}==
=={{header|Perl}}==
{{trans|Raku}}
{{trans|Raku}}
<lang perl>use strict;
<syntaxhighlight lang="perl">use strict;
use warnings;
use warnings;
use feature 'say';
use feature 'say';
Line 2,062: Line 2,062:
say "Computer wins." and last
say "Computer wins." and last
if $tokens <= 0;
if $tokens <= 0;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>12 tokens remaining.
<pre>12 tokens remaining.
Line 2,085: Line 2,085:
=={{header|Phix}}==
=={{header|Phix}}==
{{trans|Raku}}
{{trans|Raku}}
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- getc</span>
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- getc</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">tokens</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">12</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">player</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">tokens</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">12</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">player</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
Line 2,097: Line 2,097:
<span style="color: #000000;">tokens</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">;</span> <span style="color: #000000;">player</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #000000;">tokens</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">;</span> <span style="color: #000000;">player</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 2,109: Line 2,109:
{{trans|Python}}
{{trans|Python}}


<lang Pike>int tokens = 12;
<syntaxhighlight lang="pike">int tokens = 12;


void get_tokens(int cur_tokens) {
void get_tokens(int cur_tokens) {
Line 2,141: Line 2,141:
write("Computer wins!\n");
write("Computer wins!\n");
return 0;
return 0;
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 2,175: Line 2,175:


=={{header|Plain English}}==
=={{header|Plain English}}==
<lang plainenglish>To run:
<syntaxhighlight lang="plainenglish">To run:
Start up.
Start up.
Play the game of Nim.
Play the game of Nim.
Line 2,207: Line 2,207:
Format the count and "piece" or "pieces" into a string.
Format the count and "piece" or "pieces" into a string.
Write "The computer took " then the string then "." on the console.
Write "The computer took " then the string then "." on the console.
Put the count into the pieces.</lang>
Put the count into the pieces.</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,228: Line 2,228:


=={{header|Prolog}}==
=={{header|Prolog}}==
<lang prolog>nim :- next_turn(12), !.
<syntaxhighlight lang="prolog">nim :- next_turn(12), !.


next_turn(N) :-
next_turn(N) :-
Line 2,247: Line 2,247:
-> format('Computer wins!')
-> format('Computer wins!')
; next_turn(N2)
; next_turn(N2)
).</lang>
).</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,279: Line 2,279:
=={{header|Python}}==
=={{header|Python}}==
Works on Python 3
Works on Python 3
<syntaxhighlight lang="python">
<lang Python>
print("Py Nim\n")
print("Py Nim\n")


Line 2,312: Line 2,312:


print("Computer wins!")
print("Computer wins!")
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 2,343: Line 2,343:


=={{header|R}}==
=={{header|R}}==
<lang R>## Nim game
<syntaxhighlight lang="r">## Nim game
##
##


Line 2,363: Line 2,363:


}
}
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 2,397: Line 2,397:


=={{header|Racket}}==
=={{header|Racket}}==
<lang racket>
<syntaxhighlight lang="racket">
#lang racket
#lang racket


Line 2,431: Line 2,431:


(pturn 12)
(pturn 12)
</syntaxhighlight>
</lang>


=={{header|Raku}}==
=={{header|Raku}}==
Line 2,437: Line 2,437:
{{works with|Rakudo|2019.03}}
{{works with|Rakudo|2019.03}}


<lang perl6>say my $tokens = 12, " tokens remaining.\n";
<syntaxhighlight lang="raku" line>say my $tokens = 12, " tokens remaining.\n";


loop {
loop {
Line 2,446: Line 2,446:
say "Computer takes {4 - $player}.\n$tokens tokens remaining.\n";
say "Computer takes {4 - $player}.\n$tokens tokens remaining.\n";
say "Computer wins." and last if $tokens <= 0;
say "Computer wins." and last if $tokens <= 0;
}</lang>
}</syntaxhighlight>
{{out|Sample output}}
{{out|Sample output}}
<pre>12 tokens remaining.
<pre>12 tokens remaining.
Line 2,471: Line 2,471:


=={{header|Red}}==
=={{header|Red}}==
<lang rebol>Red [
<syntaxhighlight lang="rebol">Red [
date: 2021-10-24
date: 2021-10-24
version: 0.6.4
version: 0.6.4
Line 2,501: Line 2,501:
tokens: subtract tokens subtract 4 n
tokens: subtract tokens subtract 4 n
]
]
print "Computer wins!"</lang>
print "Computer wins!"</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,525: Line 2,525:
=={{header|REXX}}==
=={{header|REXX}}==
Programming notes: &nbsp; extra error checking was done with specific informative error messages. &nbsp; Also included was a method of quitting the game. &nbsp; The number of (starting) tokens &nbsp; (the ''pot'') &nbsp; can be specified on the command line, &nbsp; the default is &nbsp; '''12'''.
Programming notes: &nbsp; extra error checking was done with specific informative error messages. &nbsp; Also included was a method of quitting the game. &nbsp; The number of (starting) tokens &nbsp; (the ''pot'') &nbsp; can be specified on the command line, &nbsp; the default is &nbsp; '''12'''.
<lang rexx>/*REXX program plays the NIM game with a human opponent; the pot size can be specified. */
<syntaxhighlight lang="rexx">/*REXX program plays the NIM game with a human opponent; the pot size can be specified. */
parse arg pot _ . 1 __ /*obtain optional argument from the CL.*/
parse arg pot _ . 1 __ /*obtain optional argument from the CL.*/
if pot=='' | pot=="," then pot= 12 /*Not specified? Then use the default.*/
if pot=='' | pot=="," then pot= 12 /*Not specified? Then use the default.*/
Line 2,567: Line 2,567:
show: say; say pad "Tokens remaining: " arg(1)' ' pad; say; return
show: say; say pad "Tokens remaining: " arg(1)' ' pad; say; return
s: if arg(1)==1 then return arg(3); return word(arg(2) 's',1)
s: if arg(1)==1 then return arg(3); return word(arg(2) 's',1)
ser: if ok then say pad '***error***' arg(1); ok= 0; return</lang>
ser: if ok then say pad '***error***' arg(1); ok= 0; return</syntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
{{out|output|text=&nbsp; when using the default input:}}
<pre>
<pre>
Line 2,598: Line 2,598:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
# Project : CalmoSoft Nim Game
# Project : CalmoSoft Nim Game
# Date : 16/04/2020-13:27:07
# Date : 16/04/2020-13:27:07
Line 2,974: Line 2,974:
win.close()
win.close()
app.Quit()
app.Quit()
</syntaxhighlight>
</lang>
Necessary image files:
Necessary image files:
[http://www.mediafire.com/file/8z9tus0owwkv92q/Images.zip/file Images file]
[http://www.mediafire.com/file/8z9tus0owwkv92q/Images.zip/file Images file]
Line 2,983: Line 2,983:


=={{header|Ruby}}==
=={{header|Ruby}}==
<lang ruby>[12, 8, 4].each do |remaining|
<syntaxhighlight lang="ruby">[12, 8, 4].each do |remaining|
puts "There are #{remaining} dots.\nHow many dots would you like to take? "
puts "There are #{remaining} dots.\nHow many dots would you like to take? "
unless (num=gets.to_i).between?(1, 3)
unless (num=gets.to_i).between?(1, 3)
Line 2,993: Line 2,993:


puts "Computer took the last and wins."
puts "Computer took the last and wins."
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>There are 12 dots.
<pre>There are 12 dots.
Line 3,021: Line 3,021:


=={{header|Rust}}==
=={{header|Rust}}==
<lang rust>
<syntaxhighlight lang="rust">
fn main() {
fn main() {
let mut tokens = 12;
let mut tokens = 12;
Line 3,080: Line 3,080:
println!("");
println!("");
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
sample game:
sample game:
Line 3,120: Line 3,120:


=={{header|S-Basic}}==
=={{header|S-Basic}}==
<lang basic>
<syntaxhighlight lang="basic">
$constant maxtokens = 12
$constant maxtokens = 12
$constant machine = 0
$constant machine = 0
Line 3,195: Line 3,195:
print "Thanks for playing!"
print "Thanks for playing!"


end </lang>
end </syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 3,225: Line 3,225:


=={{header|Scala}}==
=={{header|Scala}}==
<lang scala>
<syntaxhighlight lang="scala">
var tokens = 12
var tokens = 12


Line 3,257: Line 3,257:
println("Computer wins!")
println("Computer wins!")
}
}
</syntaxhighlight>
</lang>


=={{header|Smalltalk}}==
=={{header|Smalltalk}}==
{{works with|GNU Smalltalk}}
{{works with|GNU Smalltalk}}
<lang smalltalk>
<syntaxhighlight lang="smalltalk">
Object subclass: Nim [
Object subclass: Nim [
| tokens |
| tokens |
Line 3,322: Line 3,322:
g := Nim new.
g := Nim new.
g mainLoop.
g mainLoop.
</syntaxhighlight>
</lang>
{{out}}
{{out}}
sample game:
sample game:
Line 3,358: Line 3,358:
=={{header|SNOBOL4}}==
=={{header|SNOBOL4}}==
{{works with|CSNOBOL4}}
{{works with|CSNOBOL4}}
<lang snobol4> &TRIM = 1
<syntaxhighlight lang="snobol4"> &TRIM = 1
&DUMP = 1
&DUMP = 1


Line 3,445: Line 3,445:
&DUMP = 0
&DUMP = 0


END</lang>
END</syntaxhighlight>


<pre>The mysterious game of Nim!
<pre>The mysterious game of Nim!
Line 3,478: Line 3,478:
=={{header|Swift}}==
=={{header|Swift}}==


<lang swift>var tokens = 12
<syntaxhighlight lang="swift">var tokens = 12


while tokens != 0 {
while tokens != 0 {
Line 3,504: Line 3,504:


print()
print()
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 3,522: Line 3,522:


=={{Header|Tiny BASIC}}==
=={{Header|Tiny BASIC}}==
<lang Tiny BASIC>10 LET H = 12
<syntaxhighlight lang="tiny basic">10 LET H = 12
20 PRINT "There are"
20 PRINT "There are"
30 PRINT H
30 PRINT H
Line 3,543: Line 3,543:
200 END
200 END
210 PRINT "I got the last token. I win. Better luck next time."
210 PRINT "I got the last token. I win. Better luck next time."
220 END</lang>
220 END</syntaxhighlight>




=={{header|True BASIC}}==
=={{header|True BASIC}}==
<lang qbasic>LET monton = 12
<syntaxhighlight lang="qbasic">LET monton = 12
LET llevar = 0
LET llevar = 0


Line 3,565: Line 3,565:
SET COLOR 2
SET COLOR 2
PRINT "Obtuve la última ficha. ¡Gané! Mejor suerte la próxima vez."
PRINT "Obtuve la última ficha. ¡Gané! Mejor suerte la próxima vez."
END</lang>
END</syntaxhighlight>




=={{header|Vlang}}==
=={{header|Vlang}}==
{{trans|Go}}
{{trans|Go}}
<lang ecmascript>import os { input }
<syntaxhighlight lang="ecmascript">import os { input }


fn show_tokens(tokens int) {
fn show_tokens(tokens int) {
Line 3,598: Line 3,598:
}
}
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 3,632: Line 3,632:
=={{header|Wren}}==
=={{header|Wren}}==
{{trans|Go}}
{{trans|Go}}
<lang ecmascript>import "io" for Stdin, Stdout
<syntaxhighlight lang="ecmascript">import "io" for Stdin, Stdout


var showTokens = Fn.new { |tokens| System.print("Tokens remaining %(tokens)\n") }
var showTokens = Fn.new { |tokens| System.print("Tokens remaining %(tokens)\n") }
Line 3,655: Line 3,655:
break
break
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}