How to resolve the algorithm Pig the dice game step by step in the Objeck programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Pig the dice game step by step in the Objeck 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 Objeck programming language
Source code in the objeck programming language
class Pig {
function : Main(args : String[]) ~ Nil {
player_count := 2;
max_score := 100;
safe_score := Int->New[player_count];
player := 0; score := 0;
while(true) {
safe := safe_score[player];
" Player {$player}: ({$safe}, {$score}) Rolling? (y/n) "->PrintLine();
rolling := IO.Console->ReadString();
if(safe_score[player] + score < max_score & (rolling->Equals("y") | rolling->Equals("yes"))) {
rolled := ((Float->Random() * 100.0)->As(Int) % 6) + 1;
" Rolled {$rolled}"->PrintLine();
if(rolled = 1) {
safe := safe_score[player];
" Bust! you lose {$score} but still keep your previous {$safe}\n"->PrintLine();
score := 0;
player := (player + 1) % player_count;
}
else {
score += rolled;
};
}
else {
safe_score[player] += score;
if(safe_score[player] >= max_score) {
break;
};
safe := safe_score[player];
" Sticking with {$safe}\n"->PrintLine();
score := 0;
player := (player + 1) % player_count;
};
};
safe := safe_score[player];
"\n\nPlayer {$player} wins with a score of {$safe}"->PrintLine();
}
}
You may also check:How to resolve the algorithm World Cup group stage step by step in the C# programming language
You may also check:How to resolve the algorithm Symmetric difference step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Rot-13 step by step in the CLU programming language
You may also check:How to resolve the algorithm Zero to the zero power step by step in the Oforth programming language
You may also check:How to resolve the algorithm Longest common subsequence step by step in the Liberty BASIC programming language