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

Published on 12 May 2024 09:40 PM

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

Source code in the wren programming language

import "/ioutil" for Input
import "/str" for Str
import "random" for Random

var name1 = Input.text("Player 1 - Enter your name : ").trim()
name1 = (name1 == "") ? "PLAYER1" : Str.upper(name1)
var name2 = Input.text("Player 2 - Enter your name : ").trim()
name2 = (name2 == "") ? "PLAYER2" : Str.upper(name2)
var names = [name1, name2]
var r = Random.new()
var totals = [0, 0]
var player = 0
while (true) {
    System.print("\n%(names[player])")
    System.print("  Your total score is currently %(totals[player])")
    var score = 0
    while (true) {
        var rh = Str.lower(Input.option("  Roll or Hold r/h : ", "rhRH"))
        if (rh == "h") {
            totals[player] = totals[player] + score
            System.print("  Your total score is now %(totals[player])")
            if (totals[player] >= 100) {
                System.print("  So, %(names[player]), YOU'VE WON!")
                return
            }
            player = (player == 0) ? 1 : 0
            break
        }
        var dice = r.int(1, 7)
        System.print("    You have thrown a %(dice)")
        if (dice == 1) {
            System.print("    Sorry, your score for this round is now 0")
            System.print("  Your total score remains at %(totals[player])")
            player = (player == 0) ? 1 : 0
            break
        }
        score = score + dice
        System.print("    Your score for the round is now %(score)")
    }
}

  

You may also check:How to resolve the algorithm Solve a Hidato puzzle step by step in the Go programming language
You may also check:How to resolve the algorithm Cumulative standard deviation step by step in the Racket programming language
You may also check:How to resolve the algorithm Factorial step by step in the VHDL programming language
You may also check:How to resolve the algorithm Palindrome detection step by step in the Ol programming language
You may also check:How to resolve the algorithm Queue/Definition step by step in the CoffeeScript programming language