How to resolve the algorithm Pig the dice game step by step in the Nim programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Pig the dice game step by step in the Nim programming language

Table of Contents

Problem Statement

The   game of Pig   is a multiplayer game played with a single six-sided die.   The object of the game is to reach   100   points or more.   Play is taken in turns.   On each person's turn that person has the option of either:

Create a program to score for, and simulate dice throws for, a two-person game.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Pig the dice game step by step in the Nim programming language

Source code in the nim programming language

import random, strformat, strutils

randomize()

stdout.write "Player 1 - Enter your name : "
let name1 = block:
              let n = stdin.readLine().strip()
              if n.len == 0: "PLAYER 1" else: n.toUpper
stdout.write "Player 2 - Enter your name : "
let name2 = block:
              let n = stdin.readLine().strip()
              if n.len == 0: "PLAYER 2" else: n.toUpper

let names = [name1, name2]
var totals: array[2, Natural]
var player = 0

while true:
  echo &"\n{names[player]}"
  echo &"  Your total score is currently {totals[player]}"
  var score = 0

  while true:
    stdout.write "  Roll or Hold r/h : "
    let rh = stdin.readLine().toLowerAscii()
    case rh

    of "h":
      inc totals[player], score
      echo &"  Your total score is now {totals[player]}"
      if totals[player] >= 100:
        echo &"  So, {names[player]}, YOU'VE WON!"
        quit QuitSuccess
      player = 1 - player
      break

    of "r":
      let dice = rand(1..6)
      echo &"    You have thrown a {dice}"
      if dice == 1:
        echo "    Sorry, your score for this round is now 0"
        echo &"  Your total score remains at {totals[player]}"
        player = 1 - player
        break
      inc score, dice
      echo &"    Your score for the round is now {score}"

    else:
      echo "    Must be 'r' or 'h', try again"


  

You may also check:How to resolve the algorithm Pragmatic directives step by step in the Go programming language
You may also check:How to resolve the algorithm De Bruijn sequences step by step in the Wren programming language
You may also check:How to resolve the algorithm Terminal control/Unicode output step by step in the Mercury programming language
You may also check:How to resolve the algorithm Accumulator factory step by step in the Python programming language
You may also check:How to resolve the algorithm Euler's identity step by step in the F# programming language