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

Published on 12 May 2024 09:40 PM

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

Source code in the zkl programming language

const WIN=100, PLAYERS=2;
players,safeScores:=Walker.cycle([0..PLAYERS-1]), PLAYERS.pump(List(),0);
rollDie:=(1).random.fp(7);
yes,player,score,S:=T("","y"),players.next(),0,0;
tally:='wrap(player,score){ w:=safeScores[player]+=score; (w>=WIN) };

while(True){
   print("Player %d: (%d, %d). Rolling? (y/n) ".fmt(player+1,S,score));
   if(yes.holds(ask().strip().toLower())){
      rolled:=rollDie(); println(" Rolled a %d".fmt(rolled));
      if(rolled==1){
	 println(" Bust! You lose %d but keep %d\n".fmt(score,S));
      }else{
	 score+=rolled; 
	 if(score + S>=WIN){ tally(player,score); break; }
	 continue; 
      }
   }else{
      if(tally(player,score)) break;
      println(" Sticking with %d\n".fmt(safeScores[player]));
   }
   player,score,S=players.next(),0,safeScores[player];
}
println("\n\nPlayer %d wins with a score of %d".fmt(player+1, safeScores[player]));

  

You may also check:How to resolve the algorithm Truth table step by step in the Clojure programming language
You may also check:How to resolve the algorithm Bitmap/Bézier curves/Cubic step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Guess the number/With feedback (player) step by step in the Euphoria programming language
You may also check:How to resolve the algorithm Include a file step by step in the Processing programming language
You may also check:How to resolve the algorithm Doubly-linked list/Definition step by step in the Ada programming language