aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRuben Westerberg <drclaw@mac.com>2020-03-23 06:40:09 +1000
committerRuben Westerberg <drclaw@mac.com>2020-03-23 06:40:09 +1000
commit0e9517fbad52d8a57e502342993d80b5c9ad6a53 (patch)
tree3dfedee1ef88214e51151b4d6f7b09e5735d6b3a
parent5b91fa3318668faa615c3d65cb10589b6d8737ef (diff)
downloadperlweeklychallenge-club-0e9517fbad52d8a57e502342993d80b5c9ad6a53.tar.gz
perlweeklychallenge-club-0e9517fbad52d8a57e502342993d80b5c9ad6a53.tar.bz2
perlweeklychallenge-club-0e9517fbad52d8a57e502342993d80b5c9ad6a53.zip
Added ch-2 perl and raku
-rw-r--r--challenge-052/ruben-westerberg/README5
-rwxr-xr-xchallenge-052/ruben-westerberg/perl/ch-2.pl76
-rwxr-xr-xchallenge-052/ruben-westerberg/raku/ch-2.raku56
3 files changed, 135 insertions, 2 deletions
diff --git a/challenge-052/ruben-westerberg/README b/challenge-052/ruben-westerberg/README
index 41a53334d1..84ce79a25d 100644
--- a/challenge-052/ruben-westerberg/README
+++ b/challenge-052/ruben-westerberg/README
@@ -2,8 +2,9 @@ Solution by Ruben Westerberg
ch-1.pl and ch-1.raku
===================
-3 Sum. Run the program to display triplets of values which which add to a target, and which have increasing components
+Stepping numbers
+Run the the program with two arguments between 100 and 999. Displays stepping numbers.
ch-2.pl and ch-2.raku
===================
-Run the program to display all 3 digit colourful numbers
+Coin picking game. Use the < and > keys to select the end of the coin list. Plays against the computer. Most money wins
diff --git a/challenge-052/ruben-westerberg/perl/ch-2.pl b/challenge-052/ruben-westerberg/perl/ch-2.pl
new file mode 100755
index 0000000000..488821db86
--- /dev/null
+++ b/challenge-052/ruben-westerberg/perl/ch-2.pl
@@ -0,0 +1,76 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use List::Util;
+my %coins;
+@coins{qw<£1 50p 1p 10p 5p 20p £2 2p>}=(1,0.5,0.01,0.1,0.05,0.2,2,0.02);
+
+
+my @game= pickSome([keys %coins], 8);
+my @player;
+my @computer;
+my ($computerTurnFlag)=pickSome([0,1],1);
+
+
+print "NEW GAME\n";
+print "Select left or right ends of list with the < or > keys\n\n";
+while (@game) {
+ print join ", ",@game;
+ print "\n\n";
+ my $pick;
+ my $currentPlayer;
+ if($computerTurnFlag) {
+ ($pick)=pickSome(["<",">"],1);
+ print "Computer picks $pick\n";
+ $currentPlayer=\@computer;
+ }
+ else {
+ $currentPlayer=\@player;
+ until ($pick) {
+
+ print "Select Left or Right\n";
+ $pick= <STDIN>;
+ chomp $pick;
+ if (($pick ne "<" )and( $pick ne ">")) {
+ $pick=undef;
+ next;
+ }
+ last;
+ }
+ print "Player picks $pick\n";
+ }
+ print "\n";
+ print "PICK IS $pick\n";
+ my $val;
+ $val=pop @game if $pick eq ">";
+ $val=shift @game if $pick eq "<";
+ push @$currentPlayer, $val;
+ $computerTurnFlag++;
+ $computerTurnFlag%=2;
+
+}
+
+my $playerTotal=List::Util::sum @coins{@player};
+my $computerTotal=List::Util::sum @coins{@computer};
+
+my $winner="DRAW";
+$winner="PLAYER" if $playerTotal>$computerTotal;
+$winner="COMPUTER" if $playerTotal < $computerTotal;
+print "Player total: $playerTotal\n";
+print "Computer Total: $computerTotal\n";
+print "Winner is $winner\n";
+
+
+sub pickSome {
+ my ($in,$count)=@_;
+ my @out;
+ my @data=@$in;
+ # print "Data is: ",join ", ", @data;
+ #print "\n";
+ for (0..$count-1) {
+ push @out,splice @data,int(rand(@data)),1;
+ }
+ #print "OUT is: ", join ", ", @out;
+ #print "\n";
+ @out;
+}
diff --git a/challenge-052/ruben-westerberg/raku/ch-2.raku b/challenge-052/ruben-westerberg/raku/ch-2.raku
new file mode 100755
index 0000000000..e159184c94
--- /dev/null
+++ b/challenge-052/ruben-westerberg/raku/ch-2.raku
@@ -0,0 +1,56 @@
+#!/usr/bin/env raku
+
+my %coins;
+#= ("£1"=>1, "50p"=>0.5, "1p"=>0.01, "10p"=>0.1, "5p"=>0.05, "20p"=>0.2, "£2"=>2, "2p"=>0.02);
+
+%coins{qw<£1 50p 1p 10p 5p 20p £2 2p>}=(1,0.5,0.01,0.1,0.05,0.2,2,0.02);
+my @game= %coins.keys.pick: 8;
+
+my @player;
+my @computer;
+my $computerTurnFlag=(True,False).pick;
+
+
+put "NEW GAME";
+put "Select left or right ends of list with the < or > keys";
+put "";
+while @game {
+ put @game;
+ put "";
+ my $pick;
+ my $currentPlayer;
+ if $computerTurnFlag {
+ $pick=("<",">").pick;
+ put "Computer picks $pick";
+ $currentPlayer=@computer;
+ }
+ else {
+ $currentPlayer=@player;
+ while $pick=prompt "Select Left or right\n" {
+
+ next if $pick eq none("<",">");
+ last;
+ }
+ put "Player picks $pick";
+ }
+ put "";
+ my $val;
+ $val=@game.pop if $pick eq ">";
+ $val=@game.shift if $pick eq "<";
+ $currentPlayer.push: $val;
+ $computerTurnFlag=!$computerTurnFlag;
+
+}
+
+my $playerTotal=%coins{@player}.sum;
+my $computerTotal=%coins{@computer}.sum;
+
+my $winner="DRAW";
+$winner="PLAYER" if $playerTotal>$computerTotal;
+$winner="COMPUTER" if $playerTotal < $computerTotal;
+put "Player total: $playerTotal";
+put "Computer Total: $computerTotal";
+put "Winner is $winner";
+
+
+