diff options
author | Andinus <andinus@nand.sh> | 2021-05-01 16:50:38 +0530 |
---|---|---|
committer | Andinus <andinus@nand.sh> | 2021-05-01 16:50:38 +0530 |
commit | 63ca7a1de1dacd319abea01f23948a95ec6c2598 (patch) | |
tree | 3aac940dfeff70389be18301d445c6ce4163c08a /lib/Antlia/CLI.rakumod | |
parent | 9a57256f58d648b3f9c18f99846491aaaf75fbac (diff) | |
download | antlia-63ca7a1de1dacd319abea01f23948a95ec6c2598.tar.gz |
Print scorecard & end gracefully
Diffstat (limited to 'lib/Antlia/CLI.rakumod')
-rw-r--r-- | lib/Antlia/CLI.rakumod | 22 |
1 files changed, 19 insertions, 3 deletions
diff --git a/lib/Antlia/CLI.rakumod b/lib/Antlia/CLI.rakumod index 3bcdfd9..8261e57 100644 --- a/lib/Antlia/CLI.rakumod +++ b/lib/Antlia/CLI.rakumod @@ -1,4 +1,5 @@ use Terminal::Boxer; +use Terminal::ANSIColor; class Player is export { has Str $.name; @@ -16,6 +17,9 @@ class Player is export { } } +my Bool $end-loop = False; +signal(SIGINT).tap({$end-loop = True;}); + #| text based Rock paper scissors game multi sub MAIN( Int :$players where * >= 2 = 2, #= Number of players (default: 2) @@ -27,6 +31,7 @@ multi sub MAIN( for 1 .. $players { push @players, Player.new(name => prompt("[Player $_] Name: ").trim); } + print "\n"; my %score-against = ( rock => "scissor", @@ -36,19 +41,30 @@ multi sub MAIN( my Int $round = 0; loop { - say "[Round {++$round}]"; - for @players -> $player { $player.throw = <rock scissor paper>.pick[0]; } for @players -> $player { for @players -> $player-against { - $player.score += 1 if $player-against.throw eq %score-against{$player.throw}; + $player.score += 1 if $player-against.throw + eq %score-against{$player.throw}; } } + say "[Round {++$round}]"; say ss-box(:4col, :20cw, @players.map(*.throw-art)); + + last if $end-loop; + } + + with @players.sort(*.score).reverse -> @players-sorted { + my @scorecard = <Name Score>; + for @players-sorted -> $player { + push @scorecard, $player.name, $player.score.Str; + } + say ss-box(:2col, :40cw, @scorecard); + say colored(@players-sorted[0].name, 'cyan') ~ " wins!"; } } |