aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-03-22 12:38:15 +0000
committerGitHub <noreply@github.com>2020-03-22 12:38:15 +0000
commit4545582b4ec38d82d7c9eda067926a539060e39e (patch)
tree58b688332e673e308f018697ed95711dc371900e
parent6099bbb8d150ba71334ac39c070c440617a7e360 (diff)
parentf181136309d715e510729d06211297841b4d87db (diff)
downloadperlweeklychallenge-club-4545582b4ec38d82d7c9eda067926a539060e39e.tar.gz
perlweeklychallenge-club-4545582b4ec38d82d7c9eda067926a539060e39e.tar.bz2
perlweeklychallenge-club-4545582b4ec38d82d7c9eda067926a539060e39e.zip
Merge pull request #1439 from holli-holzer/master
#2
-rw-r--r--challenge-052/markus-holzer/raku/ch-2.p676
1 files changed, 76 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..3d2ef06e14
--- /dev/null
+++ b/challenge-052/markus-holzer/raku/ch-2.p6
@@ -0,0 +1,76 @@
+enum Player <Computer Human>;
+subset Difficulty of Int where * ~~ 1|2|3;
+
+sub USAGE {
+ say q:to/USAGE/;
+ Usage:
+ ch-2.p6 <difficulty>
+
+ Possible Difficulties: 1, 2, 3
+ USAGE
+}
+
+sub MAIN( Difficulty $difficulty )
+{
+ 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 %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;
+} \ No newline at end of file