From 9a57256f58d648b3f9c18f99846491aaaf75fbac Mon Sep 17 00:00:00 2001 From: Andinus Date: Sat, 1 May 2021 15:22:09 +0530 Subject: Remove limit on number of players --- lib/Antlia/CLI.rakumod | 47 ++++++++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 21 deletions(-) (limited to 'lib') 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.slurp, + paper => %?RESOURCES.slurp, + scissor => %?RESOURCES.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.slurp, - paper => %?RESOURCES.slurp, - scissor => %?RESOURCES.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 = .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)); } } -- cgit 1.4.1-2-gfad0