aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndinus <andinus@nand.sh>2021-04-27 21:20:51 +0530
committerAndinus <andinus@nand.sh>2021-04-27 21:20:51 +0530
commitb8ed926e37e70c0fed538a1532ea34be013f9cb0 (patch)
tree8653dc50218c8266c8a289b9337b40f63d330910
parentd1f7b4e0ff8e9f621a8690c67b5399393c25aa59 (diff)
downloadcaelum-b8ed926e37e70c0fed538a1532ea34be013f9cb0.tar.gz
Initial implementation
-rw-r--r--META6.json24
-rwxr-xr-xbin/caelum3
-rw-r--r--lib/Caelum/CLI.rakumod103
-rw-r--r--lib/Caelum/Player.rakumod8
4 files changed, 138 insertions, 0 deletions
diff --git a/META6.json b/META6.json
new file mode 100644
index 0000000..ea7d30b
--- /dev/null
+++ b/META6.json
@@ -0,0 +1,24 @@
+{
+ "name" : "caelum",
+ "auth" : "github:andinus",
+ "version" : "0.1.0",
+ "description" : "Caelum is a text based Cee-lo game",
+ "authors" : [ "Andinus <andinus@nand.sh>" ],
+ "license" : "ISC",
+ "perl" : "6.d",
+ "provides" : {
+ "Caelum::CLI" : "lib/Caelum/CLI.rakumod",
+ "Caelum::Player" : "lib/Caelum/Player.rakumod",
+ },
+ "depends" : [
+ "Terminal::Table:auth<github:araraloren>"
+ ],
+ "build-depends" : [ ],
+ "test-depends" : [ ],
+ "resources" : [
+ ],
+ "tags": [
+ "Caelum", "Cee-lo", "Chinchirorin", "Chinchiro"
+ ],
+ "source-url" : "https://git.tilde.institute/andinus/caelum"
+}
diff --git a/bin/caelum b/bin/caelum
new file mode 100755
index 0000000..84c96e1
--- /dev/null
+++ b/bin/caelum
@@ -0,0 +1,3 @@
+#!/usr/bin/env raku
+
+use Caelum::CLI;
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 = <Player Points Wallet>,;
+ 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<version>; }
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; }
+}