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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Pig the dice game step by step in the AWK 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 AWK programming language

Source code in the awk programming language

# syntax: GAWK -f PIG_THE_DICE_GAME.AWK
# converted from LUA
BEGIN {
    players = 2
    p = 1 # start with first player
    srand()
    printf("Enter: Hold or Roll?\n\n")
    while (1) {
      printf("Player %d, your score is %d, with %d temporary points\n",p,scores[p],points)
      getline reply
      reply = toupper(substr(reply,1,1))
      if (reply == "R") {
        roll = int(rand() * 6) + 1 # roll die
        printf("You rolled a %d\n",roll)
        if (roll == 1) {
          printf("Too bad. You lost %d temporary points\n\n",points)
          points = 0
          p = (p % players) + 1
        }
        else {
          points += roll
        }
      }
      else if (reply == "H") {
        scores[p] += points
        points = 0
        if (scores[p] >= 100) {
          printf("Player %d wins with a score of %d\n",p,scores[p])
          break
        }
        printf("Player %d, your new score is %d\n\n",p,scores[p])
        p = (p % players) + 1
      }
      else if (reply == "Q") { # abandon game
        break
      }
    }
    exit(0)
}


  

You may also check:How to resolve the algorithm 15 puzzle game step by step in the Simula programming language
You may also check:How to resolve the algorithm Currying step by step in the Clojure programming language
You may also check:How to resolve the algorithm Faulhaber's formula step by step in the Phix programming language
You may also check:How to resolve the algorithm File size step by step in the J programming language
You may also check:How to resolve the algorithm Inheritance/Single step by step in the Factor programming language