From 23bb2f3b9c2ed0faea1856dea23728245a80c532 Mon Sep 17 00:00:00 2001 From: Greg Donald Date: Thu, 8 Aug 2019 19:38:04 -0500 Subject: [PATCH] Add deck options --- README.md | 22 ++++-- bin/console-blackjack | 6 ++ lib/Console/Blackjack.pm6 | 105 ++++++++++++++++++++++++--- lib/Console/Blackjack/Card.pm6 | 2 +- lib/Console/Blackjack/PlayerHand.pm6 | 12 +-- lib/Console/Blackjack/Shoe.pm6 | 13 ++-- 6 files changed, 127 insertions(+), 33 deletions(-) create mode 100755 bin/console-blackjack diff --git a/README.md b/README.md index 3c8d3c4..f92db65 100644 --- a/README.md +++ b/README.md @@ -4,33 +4,41 @@ Console Blackjack written in Perl6 ### Get Perl6 -[https://perl6.org/downloads/](https://perl6.org/downloads/) +#### [https://perl6.org/](https://perl6.org/) + +Console Blackjack does not work with Perl5 ☹ ### Install Blackjack with zef: ``` -zef install Console::Blackjack +$ zef update +$ zef install Console::Blackjack ``` ### Run -##### Start perl6 and run: +``` +$ console-blackjack +``` + +#### or ``` -perl6 +$ perl6 perl6> use Console::Blackjack perl6> Console::Blackjack::Game.new ``` -##### Increase your terminal font size for a better view: +  +#### Increase your terminal font size for a better view: ![Blackjack](https://raw.githubusercontent.com/gdonald/Console-Blackjack/master/bj.png) ### Report Bugs -If you find any bugs or have other issues please [report them here](https://github.com/gdonald/Console-Blackjack/issues). +#### If you find any bugs or have other issues please [report them here](https://github.com/gdonald/Console-Blackjack/issues). ### License -Console-Blackjack is released under the [Artistic License 2.0](https://opensource.org/licenses/Artistic-2.0). +#### Console-Blackjack is released under the [Artistic License 2.0](https://opensource.org/licenses/Artistic-2.0). diff --git a/bin/console-blackjack b/bin/console-blackjack new file mode 100755 index 0000000..9b066c0 --- /dev/null +++ b/bin/console-blackjack @@ -0,0 +1,6 @@ +#!perl6 + +use v6.d; +use Console::Blackjack; + +Console::Blackjack::Game.new; diff --git a/lib/Console/Blackjack.pm6 b/lib/Console/Blackjack.pm6 index d4344d4..5bc6ddc 100644 --- a/lib/Console/Blackjack.pm6 +++ b/lib/Console/Blackjack.pm6 @@ -198,10 +198,8 @@ class Game is export { $c = self.read-one-char; given $c { - when 'y' { $br = True; - self.insure-hand; } - when 'n' { $br = True; - self.no-insurance; } + when 'y' { $br = True; self.insure-hand; } + when 'n' { $br = True; self.no-insurance; } default { $br = True; self.clear; @@ -278,7 +276,7 @@ class Game is export { } method draw-player-bet-options { - say ' (D) Deal Hand (B) Change Bet (Q) Quit'; + say ' (D) Deal Hand (B) Change Bet (O) Options (Q) Quit'; my Bool $br = False; my Str $c; @@ -287,12 +285,10 @@ class Game is export { $c = self.read-one-char; given $c { - when 'd' { $br = True; - self.deal-new-hand; } - when 'b' { $br = True; - self.get-new-bet; } - when 'q' { $br = True; - self.clear; } + when 'd' { $br = True; self.deal-new-hand; } + when 'b' { $br = True; self.get-new-bet; } + when 'o' { $br = True; self.game-options; } + when 'q' { $br = True; self.clear; } default { $br = True; self.clear; @@ -305,6 +301,93 @@ class Game is export { } } + method game-options { + self.clear; + self.draw-hands; + + say ' (N) Number of Decks (T) Deck Type (B) Back'; + + my Bool $br = False; + my Str $c; + + loop { + $c = self.read-one-char; + + given $c { + when 'n' { $br = True; self.get-new-num-decks; } + when 't' { $br = True; self.get-new-deck-type; } + when 'b' { + $br = True; + self.clear; + self.draw-hands; + self.draw-player-bet-options; + } + default { + $br = True; + self.clear; + self.draw-hands; + self.game-options; + } + } + + last if $br + } + } + + method get-new-deck-type { + self.clear; + self.draw-hands; + + say ' (1) Regular (2) Aces (3) Jacks (4) Aces & Jacks (5) Sevens (6) Eights'; + + my Bool $br = False; + my Str $c; + + loop { + $c = self.read-one-char; + + given $c { + when '1' { $br = True; self.shoe.new-regular; } + when '2' { $br = True; self.shoe.new-aces; } + when '3' { $br = True; self.shoe.new-jacks; } + when '4' { $br = True; self.shoe.new-aces-jacks; } + when '5' { $br = True; self.shoe.new-sevens; } + when '6' { $br = True; self.shoe.new-eights; } + default { + $br = True; + self.clear; + self.draw-hands; + self.get-new-deck-type; + } + } + + if $br { + self.clear; + self.draw-hands; + self.draw-player-bet-options; + last; + } + } + } + + method get-new-num-decks { + self.clear; + self.draw-hands; + + my Str $opts = ' Number Of Decks: '; + $opts ~= $!num-decks; + $opts ~= "\n"; + $opts ~= ' Enter New Number Of Decks: '; + + my Int $tmp = prompt $opts; + + $tmp = 1 if $tmp < 1; + $tmp = 8 if $tmp > 8; + + $!num-decks = $tmp; + self.game-options; + } + method get-new-bet { self.clear; self.draw-hands; diff --git a/lib/Console/Blackjack/Card.pm6 b/lib/Console/Blackjack/Card.pm6 index e3d3594..c1908be 100644 --- a/lib/Console/Blackjack/Card.pm6 +++ b/lib/Console/Blackjack/Card.pm6 @@ -28,7 +28,7 @@ class Card is export { } method is-ten(--> Bool) { - $!value > 9; + $!value > 8; } method draw(--> Str) { diff --git a/lib/Console/Blackjack/PlayerHand.pm6 b/lib/Console/Blackjack/PlayerHand.pm6 index 94b0b1d..8e1feab 100644 --- a/lib/Console/Blackjack/PlayerHand.pm6 +++ b/lib/Console/Blackjack/PlayerHand.pm6 @@ -33,14 +33,10 @@ class PlayerHand is Hand is export { $c = $!game.read-one-char; given $c { - when 'h' { if self.can-hit() { $br = True; - self.hit; } } - when 's' { if self.can-stand() { $br = True; - self.stand; } } - when 'p' { if self.can-split() { $br = True; - $!game.split-current-hand; } } - when 'd' { if self.can-dbl() { $br = True; - self.dbl; } } + when 'h' { if self.can-hit() { $br = True; self.hit; } } + when 's' { if self.can-stand() { $br = True; self.stand; } } + when 'p' { if self.can-split() { $br = True; $!game.split-current-hand; } } + when 'd' { if self.can-dbl() { $br = True; self.dbl; } } default { $br = True; $!game.clear; diff --git a/lib/Console/Blackjack/Shoe.pm6 b/lib/Console/Blackjack/Shoe.pm6 index 0bef945..d55f791 100644 --- a/lib/Console/Blackjack/Shoe.pm6 +++ b/lib/Console/Blackjack/Shoe.pm6 @@ -10,6 +10,7 @@ class Shoe is export { submethod BUILD(:$!num-decks) { @!shuffle-specs = (80 => 1), (81 => 2), (82 => 3), (84 => 4), (86 => 5), (89 => 6), (92 => 7), (95 => 8); + self.new-regular; self.shuffle; } @@ -34,37 +35,37 @@ class Shoe is export { } method shuffle { - # self.new-sevens; - # self.new-eights; - # self.new-aces; - # self.new-jacks; - # self.new-aces-jacks; - self.new-regular; for 0..6 { @!cards = @!cards.pick: *; } } method new-aces-jacks { self.new-irregular([0, 10]); + self.shuffle; } method new-jacks { self.new-irregular([10]); + self.shuffle; } method new-aces { self.new-irregular([0]); + self.shuffle; } method new-eights { self.new-irregular([7]); + self.shuffle; } method new-sevens { self.new-irregular([6]); + self.shuffle; } method new-regular { self.new-irregular([0..12]); + self.shuffle; } method new-irregular(@values) {