DEV Community

Cover image for Use Nim language to implement Nim Game

Posted on

Use Nim language to implement Nim Game

Nim is a mathematical game of strategy in which two players take turns removing (or "nimming") objects from distinct heaps or piles. On each turn, a player must remove at least one object, and may remove any number of objects provided they all come from the same heap or pile. Depending on the version being played, the goal of the game is either to avoid taking the last object, or to take the last object.

From wiki:

Nim language is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula.

The code

We write a simple program based on command line. It mostly uses terminal module to format output, namely styledEcho.

We use parameters paramStr to get the total numbers of objects. Use readline to continue to get the out output of user. Use random ranging from 1 ~ 3 to emulate operations that computers execute.

import random, terminal, strutils, os

const TOTAL = 12

proc tryParseInt(s: string): int =
    result = parseInt(s)
  except ValueError:

proc succeed() =
  styledEcho(styleBright, fgRed, repeat('#', 50))
  styledEcho(styleDim, fgBlue, "Congratulations!")
  styledEcho(styleBright, fgRed, repeat('#', 50))

proc fail() =
  styledEcho(styleBright, fgGreen, repeat('#', 50))
  styledEcho(styleDim, fgBlue, "Don't lose heart. Come again next time:-)")

proc nimgame(total: int = TOTAL) =
    total = total
    number: int
  while total > 1:
    styledEcho(styleDim, fgBlue, $total & " left" & "\n")
    styledEcho(styleBright, fgRed, "It's your turn")
    styledEcho(styleItalic, fgYellow, "fetch your stones(1 ~ 3): ")
    number = tryParseInt(readLine(stdin))
    while number notin {1, 2, 3}:
      styledEcho(styleItalic, fgYellow, "fetch your stones(1 ~ 3): ")
      number = tryParseInt(readLine(stdin))
    styledEcho(styleDim, fgBlue, "\nYou fetches: " & $number)
    dec(total, number)
    if total == 1:
    if total > 1:
      let choice = rand(1 .. min(3, total))
      styledEcho(styleDim, fgBlue, "Computer fetches: " & $choice)
      dec(total, choice)
      if total == 0:
  styledEcho(styleDim, fgBlue, $total & " left" & "\n")

when isMainModule:
  if paramCount() < 1:
    echo "Please input the total numbers of stones!"
  if paramCount() == 1:
    var total = tryParseInt(paramStr(1))
    if total == 0:
      total = 21
Enter fullscreen mode Exit fullscreen mode

Now play in windows.

Alt Text

Top comments (2)

ringabout profile image

I love Nim's indentation grammar, static type and efficiency.Before I use Nim, I am familiar with Python.
There exists an introduction so it's simple for me to learn Nim. Nim language is very interesting.

ringabout profile image

It's just my taste.Because I think Nim is more elegant. 😄