Nim game: Difference between revisions

Content deleted Content added
m added whitespace before the TOC.
→‎{{header|REXX}}: added the REXX computer programming language for this task.
Line 396: Line 396:


Computer wins!
Computer wins!
</pre>

=={{header|REXX}}==
<lang rexx>/*REXX programm plays the NIM game with a human opponent; the pot size can be specified.*/
pad= copies('─', 8) /*literal used as an eyecatcher in msgs*/
parse arg pot _ . 1 __ /*obtain optional argument from the CL.*/
if pot=='' | pot=="," then pot= 12 /*Not specified? Then use the default.*/
if _\=='' then do; call ser "Too many arguments entered: " __; exit 13; end
if \isNum(pot) then do; call ser "argument isn't numeric: " pot; exit 13; end
if \isInt(pot) then do; call ser "argument isn't an integer: " pot; exit 13; end
if pot<4 then do; call ser "The pot number is too small: " pot; exit 13; end
if pot>100 then do; call ser "The pot number is too large: " pot; exit 13; end
pot= pot/1 /*normalize the pot (number). */

do forever; call show pot
do until ok; ok=1; say
say pad "How many tokens do you want to take away (1, 2, or 3) (or QUIT)?"
parse pull t _ . 1 q 1 __; upper q; say
if abbrev('QUIT',q,1) then do; say pad 'Quitting.'; exit 1; end
if t='' then call ser "No arguments entered."
if _\=='' then call ser "Too many arguments entered: " __
if \isNum(t) then call ser "Argument isn't numeric: " t
if \isInt(t) then call ser "Argument isn't an integer: " t
if t<1 then call ser "Argument can't be less than 1: " t
if t>3 then call ser "Argument can't be greater than 3: " t
end /*while*/
t= t/1 /*Normalize the number: 001 2. +3 */
#= 4-t /*calculate the computer's take─away. */
say pad "The computer takes " # " token"s(#).
pot= pot - t - # /*calculate the number of tokens in pot*/
if pot==0 then do; say pad 'No tokens left.' /*No tokens left in the pot? */
say pad "The computer wins!" /*Display a braggart message.*/
exit /*exit this computer program.*/
end
end /*forever*/ /*keep looping until there's a winner. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
isNum: return datatype( arg(1), 'N') /*verify that the arg is a number. */
isInt: return datatype( arg(1), 'W') /* " " " " " an integer. */
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</lang>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
──────── Tokens remaining: 12 ────────


──────── How many tokens do you want to take away (1, 2, or 3) (or QUIT)?
2 ◄■■■■■■■■■■■ user input

──────── The computer takes 2 tokens.

──────── Tokens remaining: 8 ────────


──────── How many tokens do you want to take away (1, 2, or 3) (or QUIT)?
3 ◄■■■■■■■■■■■ user input

──────── The computer takes 1 token.

──────── Tokens remaining: 4 ────────


──────── How many tokens do you want to take away (1, 2, or 3) (or QUIT)?
1 ◄■■■■■■■■■■■ user input

──────── The computer takes 3 tokens.
──────── No tokens left.
──────── The computer wins!
</pre>
</pre>