RosettaCodeData/Task/Nim-game/Nim/nim-game.nim
2023-07-01 13:44:08 -04:00

39 lines
909 B
Nim
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import strutils
import terminal
var tokens = 12
styledEcho(styleBright, "Nim in Nim\n")
proc echoTokens() =
styledEcho(styleBright, "Tokens remaining: ", resetStyle, $tokens, "\n")
proc player() =
var take = '0'
styledEcho(styleBright, "- Your turn -")
echo "How many tokens will you take?"
while true:
stdout.styledWrite(styleDim, "Take (13): ", resetStyle)
take = getch()
stdout.write(take, '\n')
if take in {'1'..'3'}:
tokens -= parseInt($take)
break
else:
echo "Please choose a number between 1 and 3."
echoTokens()
proc computer() =
styledEcho(styleBright, "- Computer's turn -")
let take = tokens mod 4
tokens -= take
styledEcho("Computer took ", styleBright, $take, " ",
if take == 1: "token"
else: "tokens")
echoTokens()
while tokens > 0:
player()
computer()
styledEcho(styleBright, "Computer wins!")