From b8ed926e37e70c0fed538a1532ea34be013f9cb0 Mon Sep 17 00:00:00 2001 From: Andinus Date: Tue, 27 Apr 2021 21:20:51 +0530 Subject: Initial implementation --- lib/Caelum/CLI.rakumod | 103 ++++++++++++++++++++++++++++++++++++++++++++++ lib/Caelum/Player.rakumod | 8 ++++ 2 files changed, 111 insertions(+) create mode 100644 lib/Caelum/CLI.rakumod create mode 100644 lib/Caelum/Player.rakumod (limited to 'lib') diff --git a/lib/Caelum/CLI.rakumod b/lib/Caelum/CLI.rakumod new file mode 100644 index 0000000..a7b1e4f --- /dev/null +++ b/lib/Caelum/CLI.rakumod @@ -0,0 +1,103 @@ +use Caelum::Player; +use Terminal::Table; + +multi sub MAIN( + Int :$players where * >= 2 = 2, #= Number of players (default: 2) +) is export { + say "Caelum - text based Cee-lo game"; + say "-------------------------------\n"; + + my Player @players; + + for ^$players { + push @players, Player.new(name => prompt("[Player $_] Name: ").trim); + } + + my Int $round = 1; + my Int $pot = 0; + + loop { + print "\n"; + say "[Round $round]"; + say "- " x 40; + + my Int $bet; + for @players -> $player { + $bet += prompt "[{$player.name}] Enter bet amount: "; + } + $bet = $bet div @players.elems; + say "\n==> Bet amount: $bet"; + + player: for @players -> $player { + print "\n"; + say "[{$player.name}]"; + say ". " x 20; + + # Reset points. + $player.points = 0; + + unless $player.wallet > $bet { + say "==> Cannot place bet. All amount goes to pot."; + $pot += $player.wallet; + $player.wallet = 0; + + next player; + } + + $player.bet($bet); + $pot += $bet; + + say "==> Placing bet. Updated wallet: {$player.wallet}."; + + roll: loop { + print "++> Rolling dices.. "; + + my Int @dices; + push @dices, (1 .. 6).pick(1)[0] for ^3; + + say @dices; + + given set(@dices).elems { + when 3 { + given @dices.sort { + when * ~~ (4, 5, 6) { $player.points = Inf; } + when * ~~ (1, 2, 3) { $player.points = -Inf; } + } + } + when 2 { + $player.points = @dices.Bag.list.grep( + *.value == 1 + )>>.key[0]; + } + when 1 { $player.points = @dices[0] * 100; } + } + + if $player.points == 0 { + say "--> Invalid Roll."; + next roll; + } else { + last roll; + } + } + } + + with @players.sort(*.wallet)[0] -> $player { + say "{$player.name} wins $pot!"; + $player.wallet += $pot; + } + $pot = 0; + + # Print scorecard. + my @scorecard = ,; + for @players -> $player { + push @scorecard, [$player.name, $player.points.Str, $player.wallet.Str]; + } + print-table(@scorecard, style => "single"); + + $round++; + } +} + +multi sub MAIN( + Bool :$version #= print version +) is export { say "Caelum v" ~ $?DISTRIBUTION.meta; } diff --git a/lib/Caelum/Player.rakumod b/lib/Caelum/Player.rakumod new file mode 100644 index 0000000..ea14421 --- /dev/null +++ b/lib/Caelum/Player.rakumod @@ -0,0 +1,8 @@ +class Player is export { + has Str $.name; + has $.points is rw; + has Int $.wallet is rw = 1000; + + # Place a bet. + method bet(Int $bet) { $!wallet -= $bet; } +} -- cgit 1.4.1-2-gfad0