How to resolve the algorithm 21 game step by step in the Raku programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm 21 game step by step in the Raku programming language
Table of Contents
Problem Statement
21 is a two player game, the game is played by choosing a number (1, 2, or 3) to be added to the running total. The game is won by the player whose chosen number causes the running total to reach exactly 21. The running total starts at zero. One player will be the computer. Players alternate supplying a number to be added to the running total.
Write a computer program that will:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm 21 game step by step in the Raku programming language
Source code in the raku programming language
say qq :to 'HERE';
The 21 game. Each player chooses to add 1, 2, or 3 to a running total.
The player whose turn it is when the total reaches 21 wins. Enter q to quit.
HERE
my $total = 0;
loop {
say "Running total is: $total";
my ($me,$comp);
loop {
$me = prompt 'What number do you play> ';
last if $me ~~ /^<[123]>$/;
insult $me;
}
$total += $me;
win('Human') if $total >= 21;
say "Computer plays: { $comp = (1,2,3).roll }\n";
$total += $comp;
win('Computer') if $total >= 21;
}
sub win ($player) {
say "$player wins.";
exit;
}
sub insult ($g) {
exit if $g eq 'q';
print ('Yo mama,', 'Jeez,', 'Ummmm,', 'Grow up,', 'Did you even READ the instructions?').roll;
say " $g is not an integer between 1 & 3..."
}
You may also check:How to resolve the algorithm Kernighans large earthquake problem step by step in the Ada programming language
You may also check:How to resolve the algorithm Pseudo-random numbers/Middle-square method step by step in the F# programming language
You may also check:How to resolve the algorithm Yin and yang step by step in the J programming language
You may also check:How to resolve the algorithm Sequence of primes by trial division step by step in the 11l programming language
You may also check:How to resolve the algorithm Number names step by step in the Groovy programming language