Skip to content

Instantly share code, notes, and snippets.

@tomcha tomcha/cardsim.pl
Created Jan 27, 2019

Embed
What would you like to do?
card_simulator
#!/usr/bin/env perl
use strict;
use warnings;
use feature 'say';
my @player_a = (1, 5, 10);
my @player_b = (2, 2, 2);
my %winer = (
pa => 0,
pb => 0,
draw => 0,
);
print "How many times try it ? >>>";
chomp(my $times = <STDIN>);
for (1..$times){
for (1..3){
my $num = int(rand(40)) + 1;
@player_a = matching($num, \@player_a);
@player_b = matching($num, \@player_b);
}
if (!@player_a){
$winer{pa} += 1;
}
elsif (!@player_b){
$winer{pb} += 1;
}
else{
$winer{draw} += 1;
}
}
say "player_a win:$winer{pa}, player_b win: $winer{pb}, draw: $winer{draw}";
sub matching{
my $n = shift;
my $player_cards = shift;
my @result;
for my $card (@$player_cards){
if ($card != $n){
push(@result, $card);
}
}
return @result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.