diff options
| author | Markus "Holli" Holzer <holli.holzer@gmail.com> | 2020-03-22 13:07:17 +0100 |
|---|---|---|
| committer | Markus "Holli" Holzer <holli.holzer@gmail.com> | 2020-03-22 13:07:17 +0100 |
| commit | 22d333a2156f8da12481999a44061efa2dd5ccad (patch) | |
| tree | 208df7fa4798259914f9f37917e0b8612f49dbbd /challenge-052 | |
| parent | 199f3ad7cd01da2fa57742dc916c4ed7d5a0fcdd (diff) | |
| download | perlweeklychallenge-club-22d333a2156f8da12481999a44061efa2dd5ccad.tar.gz perlweeklychallenge-club-22d333a2156f8da12481999a44061efa2dd5ccad.tar.bz2 perlweeklychallenge-club-22d333a2156f8da12481999a44061efa2dd5ccad.zip | |
#2
Diffstat (limited to 'challenge-052')
| -rw-r--r-- | challenge-052/markus-holzer/raku/ch-2.p6 | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/challenge-052/markus-holzer/raku/ch-2.p6 b/challenge-052/markus-holzer/raku/ch-2.p6 new file mode 100644 index 0000000000..9d1c9da833 --- /dev/null +++ b/challenge-052/markus-holzer/raku/ch-2.p6 @@ -0,0 +1,67 @@ +enum Player <Computer Human>; + +sub MAIN( $difficulty where * ~~ 1|2|3 ) +{ + my @moneyz = (0.01, 0.02, 0.05, 0.1, 0.2, 0.5, 1, 2).pick(8); + my @player = (Computer, Human).pick(2); + my $winner = Player; + my %score; + + while @moneyz.elems + { + say "@player[0] Pick: {@moneyz}"; + %score{ @player[0] } += pick-best( @player[0], $difficulty, @moneyz ); + @player = @player.rotate(1); + } + + say %score{Computer} > %score{Human} + ?? "The machine wins with { %score{Computer} } by { %score{Computer} - %score{Human} }" + !! "Good job, you beat the machine with { %score{Human} } by { %score{Human} - %score{Computer} }"; +} + + +multi sub pick-best( Human, $, @moneyz ) +{ + my $side = prompt "Your turn (l/r): "; + + $side ~~ 'l'|'r' + ?? pick( @moneyz, $side ~~ 'l' ) + !! pick-best( Human, $, @moneyz ); +} + +multi sub pick-best( Computer, 1, @moneyz ) +{ + pick( @moneyz, (True, False).pick ); +} + +multi sub pick-best( Computer, 2, @moneyz ) +{ + pick( @moneyz, @moneyz[0] > @moneyz[*-1] ); +} + +multi sub pick-best( Computer, 3, @moneyz ) +{ + my $left = @moneyz.first( * == 2, :k); + + # 2 not in the list anymore + return pick-best( Computer, 2, @moneyz ) + unless $left.defined; + + my $right = @moneyz.elems - $left - 1; + + return pick(@moneyz, True) if $left == 0; + return pick(@moneyz, False) if $right == 0; + + my $l = $left %% 2; + my $r = $right %% 2; + + return pick(@moneyz, True) if $l && !$r; + return pick(@moneyz, False) if $r && !$l; + + return pick(@moneyz, $left > $right); +} + +multi sub pick( @moneyz, Bool $left ) +{ + $left ?? @moneyz.shift !! @moneyz.pop; +} |
