about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAndinus <andinus@nand.sh>2021-05-01 15:22:09 +0530
committerAndinus <andinus@nand.sh>2021-05-01 15:22:09 +0530
commit9a57256f58d648b3f9c18f99846491aaaf75fbac (patch)
tree78e0d990455663dd38423f309392c5e8902c0968
parent7f1bf49ead4e1095edb4e8f1443f80704c8c1af5 (diff)
downloadantlia-9a57256f58d648b3f9c18f99846491aaaf75fbac.tar.gz
Remove limit on number of players
-rw-r--r--lib/Antlia/CLI.rakumod47
1 files changed, 26 insertions, 21 deletions
diff --git a/lib/Antlia/CLI.rakumod b/lib/Antlia/CLI.rakumod
index 581bfe8..3bcdfd9 100644
--- a/lib/Antlia/CLI.rakumod
+++ b/lib/Antlia/CLI.rakumod
@@ -4,23 +4,29 @@ class Player is export {
     has Str $.name;
     has $.throw is rw;
     has $.score is rw = 0;
+
+    my %ascii-art = (
+        rock => %?RESOURCES<rock/2>.slurp,
+        paper => %?RESOURCES<paper/2>.slurp,
+        scissor => %?RESOURCES<scissor/2>.slurp,
+    );
+
+    method throw-art() {
+        return %ascii-art{$!throw} ~ "\n$!name ($!score)";
+    }
 }
 
 #| text based Rock paper scissors game
 multi sub MAIN(
-    Int :$players where * == 2 = 2, #= Number of players (default: 2)
+    Int :$players where * >= 2 = 2, #= Number of players (default: 2)
 ) is export {
     say "Antlia - text based Rock paper scissors game";
     say "--------------------------------------------\n";
 
-    my Player $player1 = Player.new(name => prompt("[Player 1] Name: ").trim);
-    my Player $player2 = Player.new(name => prompt("[Player 2] Name: ").trim);
-
-    my %ascii-art = (
-        rock => %?RESOURCES<rock/2>.slurp,
-        paper => %?RESOURCES<paper/2>.slurp,
-        scissor => %?RESOURCES<scissor/2>.slurp,
-    );
+    my Player @players;
+    for 1 .. $players {
+        push @players, Player.new(name => prompt("[Player $_] Name: ").trim);
+    }
 
     my %score-against = (
         rock => "scissor",
@@ -28,22 +34,21 @@ multi sub MAIN(
         scissor => "paper"
     );
 
+    my Int $round = 0;
     loop {
-        print "\n";
-        say "- " x 40;
-
-        $player1.throw = %ascii-art.pick[0].key;
-        $player2.throw = %ascii-art.pick[0].key;
+        say "[Round {++$round}]";
 
-        $player1.score += 1 if $player2.throw eq %score-against{$player1.throw};
-        $player2.score += 1 if $player1.throw eq %score-against{$player2.throw};
+        for @players -> $player {
+            $player.throw = <rock scissor paper>.pick[0];
+        }
 
-        say ss-box(:40cw, %ascii-art{$player1.throw},
-                   %ascii-art{$player2.throw});
-        say ss-box(:40cw, "{$player1.name} ({$player1.score})",
-                   "{$player2.name} ({$player2.score})");
+        for @players -> $player {
+            for @players -> $player-against {
+                $player.score += 1 if $player-against.throw eq %score-against{$player.throw};
+            }
+        }
 
-        sink prompt "";
+        say ss-box(:4col, :20cw, @players.map(*.throw-art));
     }
 }