How to resolve the algorithm Pig the dice game step by step in the uBasic/4tH programming language

Published on 12 May 2024 09:40 PM

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

Source code in the ubasic/4th programming language

n = 2
m = 100
Dim @s(n)

p = 0
s = 0

Do
  r = Ask(Join ("Player ", Str(p+1)," rolling? (Y/N) "))

  If OR(Peek(r, 0), 32) = Ord("y") Then
    r = Rnd(6) + 1
    Print "Player ";p+1;" rolled ";r

    If r = 1 Then
      Print "Bust! you lose player ";p+1;" but still keep your previous score of ";@s(p)
      Print : p = (p + 1) % n : s = 0
    Else
      s = s + r
    EndIf

  Else
    @s(p) = @s(p) + s

    If (@s(p) < m) = 0 Then
      Print "Player ";p+1;" wins with a score of ";@s(p)
      Break
    EndIf

    Print : p = (p + 1) % n : s = 0
  EndIf
Loop


  

You may also check:How to resolve the algorithm Rendezvous step by step in the Erlang programming language
You may also check:How to resolve the algorithm Hex words step by step in the J programming language
You may also check:How to resolve the algorithm One-dimensional cellular automata step by step in the Bracmat programming language
You may also check:How to resolve the algorithm Ordered partitions step by step in the Racket programming language
You may also check:How to resolve the algorithm Input loop step by step in the Vala programming language