How to resolve the algorithm Rock-paper-scissors step by step in the Raku programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Rock-paper-scissors step by step in the Raku programming language
Table of Contents
Problem Statement
Implement the classic children's game Rock-paper-scissors, as well as a simple predictive AI (artificial intelligence) player. Rock Paper Scissors is a two player game. Each player chooses one of rock, paper or scissors, without knowing the other player's choice. The winner is decided by a set of rules:
If both players choose the same thing, there is no winner for that round. For this task, the computer will be one of the players. The operator will select Rock, Paper or Scissors and the computer will keep a record of the choice frequency, and use that information to make a weighted random choice in an attempt to defeat its opponent.
Support additional choices additional weapons.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Rock-paper-scissors step by step in the Raku programming language
Source code in the raku programming language
my %vs = (
options => [],
ro => {
ro => [ 2, '' ],
pa => [ 1, 'Paper covers Rock: ' ],
sc => [ 0, 'Rock smashes Scissors: ' ]
},
pa => {
ro => [ 0, 'Paper covers Rock: ' ],
pa => [ 2, '' ],
sc => [ 1, 'Scissors cut Paper: ' ]
},
sc => {
ro => [ 1, 'Rock smashes Scissors: '],
pa => [ 0, 'Scissors cut Paper: ' ],
sc => [ 2, '' ]
}
);
my %choices = %vs.map({; $_.substr(0,2).lc => $_ });
my $keys = %choices.keys.join('|');
my $prompt = %vs.map({$_.subst(/(\w\w)/, -> $/ {"[$0]"})}).join(' ')~"? ";
my %weight = %choices.keys »=>» 1;
my @stats = 0,0,0;
my $round;
while my $player = (prompt "Round {++$round}: " ~ $prompt).lc {
$player.=substr(0,2);
say 'Invalid choice, try again.' and $round-- and next
unless $player.chars == 2 and $player ~~ /<$keys>/;
my $computer = (flat %weight.keys.map( { $_ xx %weight{$_} } )).pick;
%weight{$_.key}++ for %vs{$player}.grep( { $_.value[0] == 1 } );
my $result = %vs{$player}{$computer}[0];
@stats[$result]++;
say "You chose %choices{$player}, Computer chose %choices{$computer}.";
print %vs{$player}{$computer}[1];
print ( 'You win!', 'You Lose!','Tie.' )[$result];
say " - (W:{@stats[0]} L:{@stats[1]} T:{@stats[2]})\n",
};
my %vs = (
options => [],
ro => {
ro => [ 2, '' ],
pa => [ 1, 'Paper covers Rock: ' ],
sc => [ 0, 'Rock smashes Scissors: ' ],
li => [ 0, 'Rock crushes Lizard: ' ],
sp => [ 1, 'Spock vaporizes Rock: ' ]
},
pa => {
ro => [ 0, 'Paper covers Rock: ' ],
pa => [ 2, '' ],
sc => [ 1, 'Scissors cut Paper: ' ],
li => [ 1, 'Lizard eats Paper: ' ],
sp => [ 0, 'Paper disproves Spock: ' ]
},
sc => {
ro => [ 1, 'Rock smashes Scissors: ' ],
pa => [ 0, 'Scissors cut Paper: ' ],
sc => [ 2, '' ],
li => [ 0, 'Scissors decapitate Lizard: '],
sp => [ 1, 'Spock smashes Scissors: ' ]
},
li => {
ro => [ 1, 'Rock crushes Lizard: ' ],
pa => [ 0, 'Lizard eats Paper: ' ],
sc => [ 1, 'Scissors decapitate Lizard: '],
li => [ 2, '' ],
sp => [ 0, 'Lizard poisons Spock: ' ]
},
sp => {
ro => [ 0, 'Spock vaporizes Rock: ' ],
pa => [ 1, 'Paper disproves Spock: ' ],
sc => [ 0, 'Spock smashes Scissors: ' ],
li => [ 1, 'Lizard poisons Spock: ' ],
sp => [ 2, '' ]
}
);
You may also check:How to resolve the algorithm Loops/While step by step in the jq programming language
You may also check:How to resolve the algorithm Four is magic step by step in the Java programming language
You may also check:How to resolve the algorithm Sum multiples of 3 and 5 step by step in the C programming language
You may also check:How to resolve the algorithm Tokenize a string step by step in the Nial programming language
You may also check:How to resolve the algorithm Primality by Wilson's theorem step by step in the C++ programming language