Jump to content

Nim game: Difference between revisions

m
syntax highlighting fixup automation
(added Arturo implementation)
m (syntax highlighting fixup automation)
Line 22:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">V tokens = 12
 
F getTokens(curTokens) -> N
Line 47:
compTurn(tokens)
 
print(‘Computer wins!’)</langsyntaxhighlight>
 
{{out}}
Line 79:
It may not be a very interesting game, but it assembles to only 222 bytes.
 
<langsyntaxhighlight lang="8080asm">bdos: equ 5 ; CP/M syscalls
puts: equ 9
putch: equ 2
Line 167:
lose: db 13,10,'You lose!$'
nl: db 13,10,'$'
wronginp: db 8,7,32,8,'$' ; beep and erase choice</langsyntaxhighlight>
 
=={{header|8086 Assembly}}==
Line 187:
 
 
<langsyntaxhighlight lang="asm"> ;; MS-DOS Nim; assembles with nasm.
bits 16
cpu 8086
Line 246:
tokens: db 13,10,13,10,'Tokens: $'
lose: db 13,10,'You lose!$'
wronginp: db 8,7,32,8,'$' </langsyntaxhighlight>
 
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">BYTE FUNC PlayerTurn(BYTE tokens)
BYTE t,max
 
Line 295:
player=1-player
OD
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Nim_game.png Screenshot from Atari 8-bit computer]
Line 320:
{{works with|Ada|Ada|2012}}
 
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO;
 
procedure Nim is
Line 364:
 
TIO.Put_Line("Computer won!");
end Nim;</langsyntaxhighlight>
{{out}}
<pre> 12 tokens remain.
Line 387:
 
=={{header|ALGOL 68}}==
<langsyntaxhighlight lang="algol68">BEGIN # play Nim #
# gets a single character answer from standin and returns it #
PROC answer = ( STRING prompt )CHAR:
Line 427:
DO SKIP OD;
print( ( "I win!", newline ) )
END</langsyntaxhighlight>
{{out}}
<pre>
Line 444:
 
=={{header|ALGOL-M}}==
<langsyntaxhighlight lang="algol">
BEGIN
 
Line 524:
WRITE("THANKS FOR PLAYING!");
 
END </langsyntaxhighlight>
{{out}}
<pre>
Line 559:
=={{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.
<langsyntaxhighlight ApplesoftBasiclang="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</langsyntaxhighlight>
 
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">total: 12
while ø [
tk: 0
Line 579:
break
]
]</langsyntaxhighlight>
 
{{out}}
Line 610:
 
=={{header|AsciiDots}}==
<syntaxhighlight lang="asciidots">
<lang AsciiDots>
%$LMRTX
.-$"Nim Dots"-$""-
Line 632:
L-------------*---*>$_#-$" dots remaining."-$""
T
</syntaxhighlight>
</lang>
 
{{out}}
Line 663:
 
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">Play:
<lang AutoHotkey>Play:
tokens := 12
while tokens {
Line 679:
else
ExitApp
return</langsyntaxhighlight>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f NIM_GAME.AWK
BEGIN {
Line 707:
printf("%s takes %d token%s; there are %d remaining\n",who,ans,(ans==1)?"":"s",tokens)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 725:
=={{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.
<syntaxhighlight lang="bloop">
<lang BlooP>
DEFINE PROCEDURE ''DIVIDE'' [A,B]:
BLOCK 0: BEGIN
Line 817:
 
PLAY_GAME [1,4,3];
</syntaxhighlight>
</lang>
 
{{out}}
Line 846:
 
=={{header|C}}==
<syntaxhighlight lang="c">
<lang c>
#include <stdio.h>
 
Line 906:
return remainingTokens;
}
</syntaxhighlight>
</lang>
 
{{Out}}
Line 942:
=={{header|C++}}==
{{trans|Go}}
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <limits>
 
Line 976:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,014:
 
=={{header|C#}}==
<langsyntaxhighlight lang="csharp">
using System;
 
Line 1,052:
}
}
</syntaxhighlight>
</lang>
{{out}}
Sample game:
Line 1,077:
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">
(defun pturn (curTokens)
(write-string "How many tokens would you like to take?: ")
Line 1,105:
(return)))
(write-string "Computer wins!")
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,136:
 
=={{header|Crystal}}==
<langsyntaxhighlight Rubylang="ruby">tokens = 12
 
until tokens <= 0
Line 1,147:
end
 
puts "Computer wins."</langsyntaxhighlight>
 
=={{header|Dyalect}}==
<langsyntaxhighlight lang="dyalect">print("There are twelve tokens.")
print("You can take 1, 2, or 3 on your turn.")
print("Whoever takes the last token wins.\n")
Line 1,169:
}
 
print("I win again.")</langsyntaxhighlight>
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: interpolate io kernel math math.parser sequences ;
IN: rosetta-code.nim-game
 
Line 1,194:
: nim-game ( -- ) 12 round drop "Computer wins!" print ;
 
MAIN: nim-game</langsyntaxhighlight>
{{out}}
<pre>
Line 1,224:
 
=={{header|Fermat}}==
<langsyntaxhighlight lang="fermat">heap:=12;
while heap>0 do
!!('There are ',heap,' tokens left. How many do you want to take?');
Line 1,236:
od;
 
!!('I got the last token. Better luck next time!');</langsyntaxhighlight>
{{out}}<pre>There are 12 tokens left. How many do you want to take?
>take := 4
Line 1,251:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">dim as ubyte heap=12, take
 
while heap > 0
Line 1,265:
wend
 
print "I got the last token. I win! Better luck next time."</langsyntaxhighlight>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,310:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,342:
 
=={{header|GW-BASIC}}==
<langsyntaxhighlight lang="gwbasic">10 HEAP = 12
20 WHILE HEAP>0
30 TAKE = 0
Line 1,356:
130 END
140 PRINT "You got the last token. Congratulations!"
150 END</langsyntaxhighlight>
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.Char (isDigit, digitToInt)
import System.IO
 
Line 1,393:
 
main :: IO ()
main = play 12</langsyntaxhighlight>
{{out}}
<pre>
Line 1,413:
 
=={{header|IS-BASIC}}==
<langsyntaxhighlight ISlang="is-BASICbasic">100 PROGRAM "Nim.bas"
110 RANDOMIZE
120 CLEAR SCREEN
Line 1,436:
310 ELSE
320 PRINT "You win!"
330 END IF</langsyntaxhighlight>
 
=={{header|J}}==
Line 1,442:
Implementation:
 
<langsyntaxhighlight Jlang="j">nim=: {{
prompt tokens=: 12
}}
Line 1,461:
echo 'I take ',(":t=. 4-y),' tokens'
prompt tokens=:tokens-t
}}</langsyntaxhighlight>
 
Sample session:
 
<langsyntaxhighlight Jlang="j"> nim''
tokens: 12
take 1, 2 or 3 tokens
Line 1,482:
I take 2 tokens
tokens: 0
game over</langsyntaxhighlight>
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">
import java.util.Scanner;
 
Line 1,540:
 
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,578:
=== 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.
<syntaxhighlight lang="javascript">
<lang Javascript>
class Nim {
constructor(tokens, printFun) {
Line 1,630:
}
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,667:
=={{header|Julia}}==
{{trans|Raku}}
<langsyntaxhighlight lang="julia">function nimgame()
tcount = 12
takenum = 0
Line 1,689:
 
nimgame()
</langsyntaxhighlight>{{out}}
<pre>
12 tokens remain.
Line 1,708:
=={{header|Kotlin}}==
{{trans|Go}}
<langsyntaxhighlight lang="scala">// Version 1.3.21
 
fun showTokens(tokens: Int) {
Line 1,734:
}
}
}</langsyntaxhighlight>
 
{{output}}
Line 1,766:
 
=={{header|Lua}}==
<syntaxhighlight lang="lua">
<lang Lua>
tokens = 12
 
Line 1,806:
 
print ("Computer wins.")
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,844:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">n = 12;
While[n > 0,
c = ChoiceDialog["Current amount = " <> ToString[n] <> "\nHow many do you want to pick?", {1 -> 1, 2 -> 2, 3 -> 3}];
Line 1,851:
n -= (4 - c);
]
ChoiceDialog["Current amount = " <> ToString[n] <> "\n You lost!"]</langsyntaxhighlight>
 
=={{header|MiniScript}}==
{{trans|Lua}}
<langsyntaxhighlight MiniScriptlang="miniscript">tokens = 12
print "Nim Game"
Line 1,894:
end while
print "Computer wins."</langsyntaxhighlight>
{{out}}
<pre>Nim Game
Line 1,926:
=={{header|Nim}}==
 
<langsyntaxhighlight lang="nim">import strutils
import terminal
 
Line 1,964:
computer()
 
styledEcho(styleBright, "Computer wins!")</langsyntaxhighlight>
 
{{out}}
Line 1,998:
=={{header|OCaml}}==
{{trans|Python|with plurals added, loops turned to recursion and string input handled}}
<langsyntaxhighlight lang="ocaml">let rec player_turn () =
print_string "How many tokens would you like to take? ";
let n = read_int_opt () |> Option.value ~default:0 in
Line 2,023:
if tokens = 0 then print_endline "Computer wins!" else play_game tokens
 
let () = play_game 12</langsyntaxhighlight>
{{out}}
<pre>
Line 2,046:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 2,062:
say "Computer wins." and last
if $tokens <= 0;
}</langsyntaxhighlight>
{{out}}
<pre>12 tokens remaining.
Line 2,085:
=={{header|Phix}}==
{{trans|Raku}}
<!--<langsyntaxhighlight Phixlang="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: #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:
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 2,109:
{{trans|Python}}
 
<langsyntaxhighlight Pikelang="pike">int tokens = 12;
 
void get_tokens(int cur_tokens) {
Line 2,141:
write("Computer wins!\n");
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 2,175:
 
=={{header|Plain English}}==
<langsyntaxhighlight lang="plainenglish">To run:
Start up.
Play the game of Nim.
Line 2,207:
Format the count and "piece" or "pieces" into a string.
Write "The computer took " then the string then "." on the console.
Put the count into the pieces.</langsyntaxhighlight>
{{out}}
<pre>
Line 2,228:
 
=={{header|Prolog}}==
<langsyntaxhighlight lang="prolog">nim :- next_turn(12), !.
 
next_turn(N) :-
Line 2,247:
-> format('Computer wins!')
; next_turn(N2)
).</langsyntaxhighlight>
{{out}}
<pre>
Line 2,279:
=={{header|Python}}==
Works on Python 3
<syntaxhighlight lang="python">
<lang Python>
print("Py Nim\n")
 
Line 2,312:
 
print("Computer wins!")
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,343:
 
=={{header|R}}==
<langsyntaxhighlight Rlang="r">## Nim game
##
 
Line 2,363:
 
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,397:
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">
#lang racket
 
Line 2,431:
 
(pturn 12)
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
Line 2,437:
{{works with|Rakudo|2019.03}}
 
<syntaxhighlight lang="raku" perl6line>say my $tokens = 12, " tokens remaining.\n";
 
loop {
Line 2,446:
say "Computer takes {4 - $player}.\n$tokens tokens remaining.\n";
say "Computer wins." and last if $tokens <= 0;
}</langsyntaxhighlight>
{{out|Sample output}}
<pre>12 tokens remaining.
Line 2,471:
 
=={{header|Red}}==
<langsyntaxhighlight lang="rebol">Red [
date: 2021-10-24
version: 0.6.4
Line 2,501:
tokens: subtract tokens subtract 4 n
]
print "Computer wins!"</langsyntaxhighlight>
{{out}}
<pre>
Line 2,525:
=={{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'''.
<langsyntaxhighlight 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.*/
if pot=='' | pot=="," then pot= 12 /*Not specified? Then use the default.*/
Line 2,567:
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)
ser: if ok then say pad '***error***' arg(1); ok= 0; return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
Line 2,598:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : CalmoSoft Nim Game
# Date : 16/04/2020-13:27:07
Line 2,974:
win.close()
app.Quit()
</syntaxhighlight>
</lang>
Necessary image files:
[http://www.mediafire.com/file/8z9tus0owwkv92q/Images.zip/file Images file]
Line 2,983:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">[12, 8, 4].each do |remaining|
puts "There are #{remaining} dots.\nHow many dots would you like to take? "
unless (num=gets.to_i).between?(1, 3)
Line 2,993:
 
puts "Computer took the last and wins."
</syntaxhighlight>
</lang>
{{out}}
<pre>There are 12 dots.
Line 3,021:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">
fn main() {
let mut tokens = 12;
Line 3,080:
println!("");
}
</syntaxhighlight>
</lang>
{{out}}
sample game:
Line 3,120:
 
=={{header|S-Basic}}==
<langsyntaxhighlight lang="basic">
$constant maxtokens = 12
$constant machine = 0
Line 3,195:
print "Thanks for playing!"
 
end </langsyntaxhighlight>
{{out}}
<pre>
Line 3,225:
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">
var tokens = 12
 
Line 3,257:
println("Computer wins!")
}
</syntaxhighlight>
</lang>
 
=={{header|Smalltalk}}==
{{works with|GNU Smalltalk}}
<langsyntaxhighlight lang="smalltalk">
Object subclass: Nim [
| tokens |
Line 3,322:
g := Nim new.
g mainLoop.
</syntaxhighlight>
</lang>
{{out}}
sample game:
Line 3,358:
=={{header|SNOBOL4}}==
{{works with|CSNOBOL4}}
<langsyntaxhighlight lang="snobol4"> &TRIM = 1
&DUMP = 1
 
Line 3,445:
&DUMP = 0
 
END</langsyntaxhighlight>
 
<pre>The mysterious game of Nim!
Line 3,478:
=={{header|Swift}}==
 
<langsyntaxhighlight lang="swift">var tokens = 12
 
while tokens != 0 {
Line 3,504:
 
print()
}</langsyntaxhighlight>
 
{{out}}
Line 3,522:
 
=={{Header|Tiny BASIC}}==
<langsyntaxhighlight Tinylang="tiny BASICbasic">10 LET H = 12
20 PRINT "There are"
30 PRINT H
Line 3,543:
200 END
210 PRINT "I got the last token. I win. Better luck next time."
220 END</langsyntaxhighlight>
 
 
=={{header|True BASIC}}==
<langsyntaxhighlight lang="qbasic">LET monton = 12
LET llevar = 0
 
Line 3,565:
SET COLOR 2
PRINT "Obtuve la última ficha. ¡Gané! Mejor suerte la próxima vez."
END</langsyntaxhighlight>
 
 
=={{header|Vlang}}==
{{trans|Go}}
<langsyntaxhighlight lang="ecmascript">import os { input }
 
fn show_tokens(tokens int) {
Line 3,598:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 3,632:
=={{header|Wren}}==
{{trans|Go}}
<langsyntaxhighlight lang="ecmascript">import "io" for Stdin, Stdout
 
var showTokens = Fn.new { |tokens| System.print("Tokens remaining %(tokens)\n") }
Line 3,655:
break
}
}</langsyntaxhighlight>
 
{{out}}
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.