Nim game: Difference between revisions
m
syntax highlighting fixup automation
Drkameleon (talk | contribs) (added Arturo implementation) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 22:
{{trans|Python}}
<
F getTokens(curTokens) -> N
Line 47:
compTurn(tokens)
print(‘Computer wins!’)</
{{out}}
Line 79:
It may not be a very interesting game, but it assembles to only 222 bytes.
<
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</
=={{header|8086 Assembly}}==
Line 187:
<
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,'$' </
=={{header|Action!}}==
<
BYTE t,max
Line 295:
player=1-player
OD
RETURN</
{{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}}
<
procedure Nim is
Line 364:
TIO.Put_Line("Computer won!");
end Nim;</
{{out}}
<pre> 12 tokens remain.
Line 387:
=={{header|ALGOL 68}}==
<
# 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</
{{out}}
<pre>
Line 444:
=={{header|ALGOL-M}}==
<
BEGIN
Line 524:
WRITE("THANKS FOR PLAYING!");
END </
{{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.
<
=={{header|Arturo}}==
<
while ø [
tk: 0
Line 579:
break
]
]</
{{out}}
Line 610:
=={{header|AsciiDots}}==
<syntaxhighlight lang="asciidots">
%$LMRTX
.-$"Nim Dots"-$""-
Line 632:
L-------------*---*>$_#-$" dots remaining."-$""
T
</syntaxhighlight>
{{out}}
Line 663:
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">Play:
tokens := 12
while tokens {
Line 679:
else
ExitApp
return</
=={{header|AWK}}==
<syntaxhighlight 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>
{{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">
DEFINE PROCEDURE ''DIVIDE'' [A,B]:
BLOCK 0: BEGIN
Line 817:
PLAY_GAME [1,4,3];
</syntaxhighlight>
{{out}}
Line 846:
=={{header|C}}==
<syntaxhighlight lang="c">
#include <stdio.h>
Line 906:
return remainingTokens;
}
</syntaxhighlight>
{{Out}}
Line 942:
=={{header|C++}}==
{{trans|Go}}
<
#include <limits>
Line 976:
}
}
}</
{{out}}
Line 1,014:
=={{header|C#}}==
<
using System;
Line 1,052:
}
}
</syntaxhighlight>
{{out}}
Sample game:
Line 1,077:
=={{header|Common Lisp}}==
<
(defun pturn (curTokens)
(write-string "How many tokens would you like to take?: ")
Line 1,105:
(return)))
(write-string "Computer wins!")
</syntaxhighlight>
{{out}}
Line 1,136:
=={{header|Crystal}}==
<
until tokens <= 0
Line 1,147:
end
puts "Computer wins."</
=={{header|Dyalect}}==
<
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.")</
=={{header|Factor}}==
<
IN: rosetta-code.nim-game
Line 1,194:
: nim-game ( -- ) 12 round drop "Computer wins!" print ;
MAIN: nim-game</
{{out}}
<pre>
Line 1,224:
=={{header|Fermat}}==
<
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!');</
{{out}}<pre>There are 12 tokens left. How many do you want to take?
>take := 4
Line 1,251:
=={{header|FreeBASIC}}==
<
while heap > 0
Line 1,265:
wend
print "I got the last token. I win! Better luck next time."</
=={{header|Go}}==
<
import (
Line 1,310:
}
}
}</
{{out}}
Line 1,342:
=={{header|GW-BASIC}}==
<
20 WHILE HEAP>0
30 TAKE = 0
Line 1,356:
130 END
140 PRINT "You got the last token. Congratulations!"
150 END</
=={{header|Haskell}}==
<
import System.IO
Line 1,393:
main :: IO ()
main = play 12</
{{out}}
<pre>
Line 1,413:
=={{header|IS-BASIC}}==
<
110 RANDOMIZE
120 CLEAR SCREEN
Line 1,436:
310 ELSE
320 PRINT "You win!"
330 END IF</
=={{header|J}}==
Line 1,442:
Implementation:
<
prompt tokens=: 12
}}
Line 1,461:
echo 'I take ',(":t=. 4-y),' tokens'
prompt tokens=:tokens-t
}}</
Sample session:
<
tokens: 12
take 1, 2 or 3 tokens
Line 1,482:
I take 2 tokens
tokens: 0
game over</
=={{header|Java}}==
<
import java.util.Scanner;
Line 1,540:
}
</syntaxhighlight>
{{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">
class Nim {
constructor(tokens, printFun) {
Line 1,630:
}
}
</syntaxhighlight>
{{out}}
Line 1,667:
=={{header|Julia}}==
{{trans|Raku}}
<
tcount = 12
takenum = 0
Line 1,689:
nimgame()
</
<pre>
12 tokens remain.
Line 1,708:
=={{header|Kotlin}}==
{{trans|Go}}
<
fun showTokens(tokens: Int) {
Line 1,734:
}
}
}</
{{output}}
Line 1,766:
=={{header|Lua}}==
<syntaxhighlight lang="lua">
tokens = 12
Line 1,806:
print ("Computer wins.")
</syntaxhighlight>
{{out}}
Line 1,844:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
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!"]</
=={{header|MiniScript}}==
{{trans|Lua}}
<
print "Nim Game"
Line 1,894:
end while
print "Computer wins."</
{{out}}
<pre>Nim Game
Line 1,926:
=={{header|Nim}}==
<
import terminal
Line 1,964:
computer()
styledEcho(styleBright, "Computer wins!")</
{{out}}
Line 1,998:
=={{header|OCaml}}==
{{trans|Python|with plurals added, loops turned to recursion and string input handled}}
<
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</
{{out}}
<pre>
Line 2,046:
=={{header|Perl}}==
{{trans|Raku}}
<
use warnings;
use feature 'say';
Line 2,062:
say "Computer wins." and last
if $tokens <= 0;
}</
{{out}}
<pre>12 tokens remaining.
Line 2,085:
=={{header|Phix}}==
{{trans|Raku}}
<!--<
<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>
<!--</
{{out}}
<pre>
Line 2,109:
{{trans|Python}}
<
void get_tokens(int cur_tokens) {
Line 2,141:
write("Computer wins!\n");
return 0;
}</
{{out}}
Line 2,175:
=={{header|Plain English}}==
<
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.</
{{out}}
<pre>
Line 2,228:
=={{header|Prolog}}==
<
next_turn(N) :-
Line 2,247:
-> format('Computer wins!')
; next_turn(N2)
).</
{{out}}
<pre>
Line 2,279:
=={{header|Python}}==
Works on Python 3
<syntaxhighlight lang="python">
print("Py Nim\n")
Line 2,312:
print("Computer wins!")
</syntaxhighlight>
{{out}}
Line 2,343:
=={{header|R}}==
<
##
Line 2,363:
}
</syntaxhighlight>
{{out}}
Line 2,397:
=={{header|Racket}}==
<
#lang racket
Line 2,431:
(pturn 12)
</syntaxhighlight>
=={{header|Raku}}==
Line 2,437:
{{works with|Rakudo|2019.03}}
<syntaxhighlight lang="raku"
loop {
Line 2,446:
say "Computer takes {4 - $player}.\n$tokens tokens remaining.\n";
say "Computer wins." and last if $tokens <= 0;
}</
{{out|Sample output}}
<pre>12 tokens remaining.
Line 2,471:
=={{header|Red}}==
<
date: 2021-10-24
version: 0.6.4
Line 2,501:
tokens: subtract tokens subtract 4 n
]
print "Computer wins!"</
{{out}}
<pre>
Line 2,525:
=={{header|REXX}}==
Programming notes: extra error checking was done with specific informative error messages. Also included was a method of quitting the game. The number of (starting) tokens (the ''pot'') can be specified on the command line, the default is '''12'''.
<
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</
{{out|output|text= when using the default input:}}
<pre>
Line 2,598:
=={{header|Ring}}==
<
# Project : CalmoSoft Nim Game
# Date : 16/04/2020-13:27:07
Line 2,974:
win.close()
app.Quit()
</syntaxhighlight>
Necessary image files:
[http://www.mediafire.com/file/8z9tus0owwkv92q/Images.zip/file Images file]
Line 2,983:
=={{header|Ruby}}==
<
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>
{{out}}
<pre>There are 12 dots.
Line 3,021:
=={{header|Rust}}==
<
fn main() {
let mut tokens = 12;
Line 3,080:
println!("");
}
</syntaxhighlight>
{{out}}
sample game:
Line 3,120:
=={{header|S-Basic}}==
<
$constant maxtokens = 12
$constant machine = 0
Line 3,195:
print "Thanks for playing!"
end </
{{out}}
<pre>
Line 3,225:
=={{header|Scala}}==
<
var tokens = 12
Line 3,257:
println("Computer wins!")
}
</syntaxhighlight>
=={{header|Smalltalk}}==
{{works with|GNU Smalltalk}}
<
Object subclass: Nim [
| tokens |
Line 3,322:
g := Nim new.
g mainLoop.
</syntaxhighlight>
{{out}}
sample game:
Line 3,358:
=={{header|SNOBOL4}}==
{{works with|CSNOBOL4}}
<
&DUMP = 1
Line 3,445:
&DUMP = 0
END</
<pre>The mysterious game of Nim!
Line 3,478:
=={{header|Swift}}==
<
while tokens != 0 {
Line 3,504:
print()
}</
{{out}}
Line 3,522:
=={{Header|Tiny BASIC}}==
<
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</
=={{header|True BASIC}}==
<
LET llevar = 0
Line 3,565:
SET COLOR 2
PRINT "Obtuve la última ficha. ¡Gané! Mejor suerte la próxima vez."
END</
=={{header|Vlang}}==
{{trans|Go}}
<
fn show_tokens(tokens int) {
Line 3,598:
}
}
}</
{{out}}
Line 3,632:
=={{header|Wren}}==
{{trans|Go}}
<
var showTokens = Fn.new { |tokens| System.print("Tokens remaining %(tokens)\n") }
Line 3,655:
break
}
}</
{{out}}
|