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

Published on 12 May 2024 09:40 PM
#Go

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

This Go program simulates a simple dice rolling game with two players. Here's a detailed explanation:

  1. Package and Imports:

    • package main: Declares the main package.
    • import statements import necessary libraries.
  2. Variables:

    • playerScores: Array of integers to store each player's total score.
    • turn: Integer representing the current player's turn (0 or 1).
    • currentScore: Integer to keep track of the current player's score during their turn.
  3. Main Function:

    • main(): Entry point of the program.
  4. Game Loop:

    • for loop continues until one player reaches 100 points or a player quits.
    • player variable represents the index of the current player in playerScores.
    • Player is prompted to choose "H" (hold), "R" (roll), or "Q" (quit) by entering an option.
    • fmt.Scanf("%v", &answer) reads the user's input and stores it in the answer variable.
  5. Input Handling:

    • switch statement handles different input options:
      • "h": Hold - Player's current score is added to their total score, and the turn ends.
      • "r": Roll - A random number between 1 and 6 is generated as the dice roll.
      • If the roll is 1 (bust), the current player's turn ends.
      • Otherwise, the rolled value is added to the current player's score.
      • "q": Quit - Quits the game.
      • Default case handles incorrect inputs.
  6. Turn and Score Updates:

    • After each turn, the current player's total score is updated.
    • If a player reaches 100 or more points, the game ends, and the winning player is declared.
    • Otherwise, the turn passes to the other player, and the current score is reset to 0.
  7. Game Conclusion:

    • The game concludes when one player reaches 100 points or quits.
    • The winning player's index is calculated based on the current turn and the length of playerScores.
    • The winning player is announced.

Source code in the go programming language

package main

import (
	"fmt"
	"math/rand"
	"strings"
	"time"
)

func main() {
	rand.Seed(time.Now().UnixNano()) //Set seed to current time

	playerScores := [...]int{0, 0}
	turn := 0
	currentScore := 0

	for {
		player := turn % len(playerScores)

		fmt.Printf("Player %v [%v, %v], (H)old, (R)oll or (Q)uit: ", player,
			playerScores[player], currentScore)

		var answer string
		fmt.Scanf("%v", &answer)
		switch strings.ToLower(answer) {
		case "h": //Hold
			playerScores[player] += currentScore
			fmt.Printf("    Player %v now has a score of %v.\n\n", player, playerScores[player])

			if playerScores[player] >= 100 {
				fmt.Printf("    Player %v wins!!!\n", player)
				return
			}

			currentScore = 0
			turn += 1
		case "r": //Roll
			roll := rand.Intn(6) + 1

			if roll == 1 {
				fmt.Printf("    Rolled a 1. Bust!\n\n")
				currentScore = 0
				turn += 1
			} else {
				fmt.Printf("    Rolled a %v.\n", roll)
				currentScore += roll
			}
		case "q": //Quit
			return
		default: //Incorrent input
			fmt.Print("  Please enter one of the given inputs.\n")
		}
	}
	fmt.Printf("Player %v wins!!!\n", (turn-1)%len(playerScores))
}


  

You may also check:How to resolve the algorithm Sorting algorithms/Bogosort step by step in the R programming language
You may also check:How to resolve the algorithm Shoelace formula for polygonal area step by step in the ALGOL 60 programming language
You may also check:How to resolve the algorithm Brownian tree step by step in the Scheme programming language
You may also check:How to resolve the algorithm Apply a callback to an array step by step in the SenseTalk programming language
You may also check:How to resolve the algorithm Van der Corput sequence step by step in the Kotlin programming language