blackjack_game 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/Makefile +2 -0
- data/README.md +41 -0
- data/Rakefile +6 -0
- data/bin/blackjack +6 -0
- data/blackjack_game.gemspec +29 -0
- data/lib/blackjack.rb +11 -0
- data/lib/blackjack/card.rb +79 -0
- data/lib/blackjack/cli.rb +6 -0
- data/lib/blackjack/cli/app.rb +109 -0
- data/lib/blackjack/deck.rb +18 -0
- data/lib/blackjack/game.rb +119 -0
- data/lib/blackjack/hand.rb +8 -0
- data/lib/blackjack/hand/base_hand.rb +72 -0
- data/lib/blackjack/hand/dealer_hand.rb +10 -0
- data/lib/blackjack/hand/player_hand.rb +77 -0
- data/lib/blackjack/player.rb +22 -0
- data/lib/blackjack/shoe.rb +15 -0
- data/lib/blackjack/version.rb +3 -0
- metadata +125 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7893aedcb9b86b83481d0ff70c0562cf749fccbd
|
4
|
+
data.tar.gz: a5c78fe7d5196708e61e4ed533378f2c1b5d75b0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 544d4e17e4e0c2fc6e85506a88916c2192320191e84eb507eb79e8c7a0e08e0c46b5bdc3f68dce8172bb1d0951c75978d1a75b70b0a6e8a40ab2c21dd37533eb
|
7
|
+
data.tar.gz: 5d42c98c47ba1e6b3195fdc864b8e2ac0cf652b95d50f308d666b4f28e25b58404be7931fdb27dcb8e117ce123eae95b1c06d95a789a7c8a7db97d1ece015ee9
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at cyril.sadovnik@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Cyril Sadovnik
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/Makefile
ADDED
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# Blackjack
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/blackjack`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'blackjack'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install blackjack
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/blackjack. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
36
|
+
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
41
|
+
|
data/Rakefile
ADDED
data/bin/blackjack
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'blackjack/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'blackjack_game'
|
8
|
+
spec.version = Blackjack::VERSION
|
9
|
+
spec.authors = ['Cyril Sadovnik']
|
10
|
+
spec.email = ['cyril.sadovnik@gmail.com']
|
11
|
+
|
12
|
+
spec.summary = 'Simple CLI Blackjack'
|
13
|
+
spec.homepage = 'https://github.com/sadovnik/blackjack'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
18
|
+
end
|
19
|
+
|
20
|
+
spec.bindir = 'bin'
|
21
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ['lib']
|
23
|
+
|
24
|
+
spec.add_dependency 'tty-prompt', '~> 0.12'
|
25
|
+
|
26
|
+
spec.add_development_dependency 'bundler', '~> 1.14'
|
27
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
28
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
29
|
+
end
|
data/lib/blackjack.rb
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
module Blackjack
|
2
|
+
class Card
|
3
|
+
SUITS = [ :heart, :spade, :diamond, :club ]
|
4
|
+
|
5
|
+
SUIT_SYMBOLS = {
|
6
|
+
heart: '♥',
|
7
|
+
spade: '♠',
|
8
|
+
diamond: '♦',
|
9
|
+
club: '♣'
|
10
|
+
}
|
11
|
+
|
12
|
+
NUMERICAL_RANKS = (2..10).to_a
|
13
|
+
FACE_RANKS = [ :jack, :queen, :king ]
|
14
|
+
RANKS = NUMERICAL_RANKS + FACE_RANKS + [ :ace ]
|
15
|
+
|
16
|
+
attr_reader :suit, :rank
|
17
|
+
|
18
|
+
def initialize(rank, suit, is_hidden = false)
|
19
|
+
raise ArgumentError.new('Invalid rank') unless valid_rank?(rank)
|
20
|
+
raise ArgumentError.new('Invalid suit') unless valid_suit?(suit)
|
21
|
+
|
22
|
+
@rank = rank
|
23
|
+
@suit = suit
|
24
|
+
@is_hidden = is_hidden
|
25
|
+
end
|
26
|
+
|
27
|
+
def numeric?
|
28
|
+
NUMERICAL_RANKS.include?(@rank)
|
29
|
+
end
|
30
|
+
|
31
|
+
def face?
|
32
|
+
FACE_RANKS.include?(@rank)
|
33
|
+
end
|
34
|
+
|
35
|
+
def ace?
|
36
|
+
@rank == :ace
|
37
|
+
end
|
38
|
+
|
39
|
+
def hidden?
|
40
|
+
@is_hidden
|
41
|
+
end
|
42
|
+
|
43
|
+
def equal_by_rank?(other)
|
44
|
+
self.rank == other.rank ||
|
45
|
+
self.face? && other.face? ||
|
46
|
+
self.rank == 10 && other.face? ||
|
47
|
+
self.face? && other.rank == 10 ||
|
48
|
+
self.ace? && other.ace?
|
49
|
+
end
|
50
|
+
|
51
|
+
def to_s
|
52
|
+
if hidden?
|
53
|
+
"[ Hidden ]"
|
54
|
+
else
|
55
|
+
"[ #{ @rank.to_s.capitalize } of #{ SUIT_SYMBOLS[@suit] } ]"
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def hide
|
60
|
+
@is_hidden = true
|
61
|
+
self
|
62
|
+
end
|
63
|
+
|
64
|
+
def open
|
65
|
+
@is_hidden = false
|
66
|
+
self
|
67
|
+
end
|
68
|
+
|
69
|
+
private
|
70
|
+
|
71
|
+
def valid_suit?(suit)
|
72
|
+
SUITS.include?(suit)
|
73
|
+
end
|
74
|
+
|
75
|
+
def valid_rank?(rank)
|
76
|
+
RANKS.include?(rank)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,109 @@
|
|
1
|
+
require 'tty-prompt'
|
2
|
+
|
3
|
+
module Blackjack
|
4
|
+
module CLI
|
5
|
+
class App
|
6
|
+
def initialize
|
7
|
+
@prompt = TTY::Prompt.new
|
8
|
+
|
9
|
+
configure_prompt
|
10
|
+
end
|
11
|
+
|
12
|
+
def run
|
13
|
+
name = @prompt.ask('What is your name?', default: ENV['USER'], convert: :string)
|
14
|
+
bankroll = @prompt.ask('What is your bankroll?', default: 100, convert: :int)
|
15
|
+
|
16
|
+
player = Player.new(name, bankroll)
|
17
|
+
|
18
|
+
game_number = 1
|
19
|
+
|
20
|
+
while player.can_afford?(Game::MINIMUM_BET_SIZE)
|
21
|
+
puts "Game №#{ game_number }"
|
22
|
+
bet = @prompt.ask('What is your bet?', default: Game::MINIMUM_BET_SIZE, convert: :int)
|
23
|
+
|
24
|
+
game = Game.new(player, bet)
|
25
|
+
|
26
|
+
puts "Dealer's hand:"
|
27
|
+
puts game.dealer_hand
|
28
|
+
puts
|
29
|
+
|
30
|
+
puts "Your initial hand:"
|
31
|
+
puts game.player_hands.first
|
32
|
+
puts
|
33
|
+
|
34
|
+
until game.over?
|
35
|
+
game.player_hands.each_with_index do |hand, hand_index|
|
36
|
+
next if hand.options.empty?
|
37
|
+
|
38
|
+
hand_number = hand_index + 1
|
39
|
+
|
40
|
+
message_hand_count_part = game.player_hands.count > 1 ? " (hand №#{ hand_number })" : ''
|
41
|
+
message = "What to do next#{ message_hand_count_part }?"
|
42
|
+
|
43
|
+
choice = @prompt.select(message, hand.options)
|
44
|
+
|
45
|
+
game.act(choice, hand_index)
|
46
|
+
|
47
|
+
break if choice == :split
|
48
|
+
end
|
49
|
+
|
50
|
+
game.player_hands.each_with_index do |hand, hand_index|
|
51
|
+
hand_number = hand_index + 1
|
52
|
+
|
53
|
+
puts game.player_hands.count > 1 ? "Hand №#{ hand_number }:" : 'Your hand:'
|
54
|
+
puts hand
|
55
|
+
puts
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
unless game.player_hands.count == 1 && game.player_hands.first.blackjack?
|
60
|
+
puts "Dealer opens his hand and takes cards until 17:"
|
61
|
+
puts game.dealer_hand
|
62
|
+
puts
|
63
|
+
end
|
64
|
+
|
65
|
+
game.player_hands.each_with_index do |hand, hand_index|
|
66
|
+
hand_number = hand_index + 1
|
67
|
+
|
68
|
+
if game.player_hands.count > 1
|
69
|
+
puts "Hand №#{ hand_number }"
|
70
|
+
end
|
71
|
+
|
72
|
+
if hand.blackjack?
|
73
|
+
puts 'Blackjack!'
|
74
|
+
else
|
75
|
+
puts 'Win!' if hand > game.dealer_hand
|
76
|
+
end
|
77
|
+
|
78
|
+
puts 'Loose!' if hand < game.dealer_hand
|
79
|
+
puts 'Push!' if hand == game.dealer_hand
|
80
|
+
puts
|
81
|
+
end
|
82
|
+
|
83
|
+
puts "Game is over. Your bankroll: #{ player.bankroll }"
|
84
|
+
puts
|
85
|
+
|
86
|
+
game_number += 1
|
87
|
+
end
|
88
|
+
|
89
|
+
puts 'Your bankroll is less than the minimum bet size.'
|
90
|
+
rescue TTY::Prompt::Reader::InputInterrupt
|
91
|
+
puts
|
92
|
+
puts 'Bye!'
|
93
|
+
end
|
94
|
+
|
95
|
+
private
|
96
|
+
|
97
|
+
def configure_prompt
|
98
|
+
@prompt.on(:keypress) do |event|
|
99
|
+
case event.value
|
100
|
+
when 'j'
|
101
|
+
@prompt.trigger(:keydown)
|
102
|
+
when 'k'
|
103
|
+
@prompt.trigger(:keyup)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Blackjack
|
2
|
+
class Deck
|
3
|
+
attr_reader :cards
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@cards = Card::SUITS.reduce([]) do |product, suit|
|
7
|
+
product + Card::RANKS.map do |rank|
|
8
|
+
Card.new(rank, suit)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def shuffle
|
14
|
+
@cards.shuffle!
|
15
|
+
self
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,119 @@
|
|
1
|
+
module Blackjack
|
2
|
+
class Game
|
3
|
+
module Errors
|
4
|
+
class UnableToActionError < StandardError; end
|
5
|
+
end
|
6
|
+
|
7
|
+
MINIMUM_BET_SIZE = 10
|
8
|
+
|
9
|
+
attr_reader :player_hands, :dealer_hand
|
10
|
+
|
11
|
+
def initialize(player, bet, shoe = Shoe.new)
|
12
|
+
@player = player
|
13
|
+
@shoe = shoe
|
14
|
+
|
15
|
+
@player.withdraw(bet)
|
16
|
+
|
17
|
+
@player_hands = [ Hand::PlayerHand.new(player, bet, [ @shoe.take, @shoe.take ]) ]
|
18
|
+
@dealer_hand = Hand::DealerHand.new([ @shoe.take, @shoe.take(open: false) ])
|
19
|
+
|
20
|
+
@state = :initial
|
21
|
+
|
22
|
+
check_on_blackjack
|
23
|
+
end
|
24
|
+
|
25
|
+
def over?
|
26
|
+
@state == :over
|
27
|
+
end
|
28
|
+
|
29
|
+
def act(action, hand_index)
|
30
|
+
raise Errors::UnableToAction.new('Game is over') if over?
|
31
|
+
raise Errors::UnableToAction.new('Invalid action') unless valid_action?(action)
|
32
|
+
|
33
|
+
hand = @player_hands[hand_index]
|
34
|
+
|
35
|
+
raise Errors::UnableToAction.new('Unknown hand') if hand.nil?
|
36
|
+
|
37
|
+
self.send(action, hand)
|
38
|
+
|
39
|
+
if ready_to_finish?
|
40
|
+
@state = :over
|
41
|
+
|
42
|
+
unless all_hands_busted?
|
43
|
+
@dealer_hand.open_last_card
|
44
|
+
|
45
|
+
fill_dealer_hand_until_17
|
46
|
+
|
47
|
+
@player_hands.each do |hand|
|
48
|
+
if hand > @dealer_hand
|
49
|
+
@player.reward(hand.bet * 2)
|
50
|
+
else hand == @dealer_hand
|
51
|
+
@player.reward(hand.bet)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
def all_hands_busted?
|
61
|
+
@player_hands
|
62
|
+
.map(&:busted?)
|
63
|
+
.reduce { |product, is_busted| product && is_busted }
|
64
|
+
end
|
65
|
+
|
66
|
+
def fill_dealer_hand_until_17
|
67
|
+
until @dealer_hand.value >= 17
|
68
|
+
@dealer_hand << @shoe.take
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def ready_to_finish?
|
73
|
+
!@player_hands
|
74
|
+
.map(&:playable?)
|
75
|
+
.reduce { |product, is_playable| product || is_playable }
|
76
|
+
end
|
77
|
+
|
78
|
+
def stand(hand)
|
79
|
+
fail unless hand.can_stand?
|
80
|
+
|
81
|
+
hand.stand
|
82
|
+
end
|
83
|
+
|
84
|
+
def hit(hand)
|
85
|
+
fail unless hand.can_hit?
|
86
|
+
|
87
|
+
hand.append(@shoe.take)
|
88
|
+
end
|
89
|
+
|
90
|
+
def double(hand)
|
91
|
+
fail unless hand.can_double?
|
92
|
+
|
93
|
+
@player.withdraw(hand.bet)
|
94
|
+
hand.double_bet
|
95
|
+
hand.append(@shoe.take)
|
96
|
+
hand.stand unless hand.busted?
|
97
|
+
end
|
98
|
+
|
99
|
+
def split(hand)
|
100
|
+
fail unless hand.can_split?
|
101
|
+
|
102
|
+
@player_hands << hand.split(@shoe.take, @shoe.take)
|
103
|
+
@player.withdraw(hand.bet)
|
104
|
+
end
|
105
|
+
|
106
|
+
def check_on_blackjack
|
107
|
+
first_hand = @player_hands.first
|
108
|
+
|
109
|
+
if first_hand.blackjack?
|
110
|
+
@state = :over
|
111
|
+
@player.reward(first_hand.bet * 1.5)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
def valid_action?(action)
|
116
|
+
[ :hit, :stand, :double, :split ].include?(action)
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
module Blackjack
|
2
|
+
module Hand
|
3
|
+
class BaseHand
|
4
|
+
include Comparable
|
5
|
+
|
6
|
+
module Errors
|
7
|
+
class HandError < StandardError; end
|
8
|
+
end
|
9
|
+
|
10
|
+
attr_reader :cards, :state
|
11
|
+
|
12
|
+
def initialize(cards = [])
|
13
|
+
@cards = cards
|
14
|
+
@state = :initial
|
15
|
+
end
|
16
|
+
|
17
|
+
def value
|
18
|
+
open_cards = @cards.reject(&:hidden?)
|
19
|
+
|
20
|
+
aces = open_cards.select(&:ace?)
|
21
|
+
|
22
|
+
value_without_aces = open_cards.reject(&:ace?).reduce(0) do |product, card|
|
23
|
+
product + (card.numeric? ? card.rank : 10)
|
24
|
+
end
|
25
|
+
|
26
|
+
value_of_aces = aces.reduce(0) do |product, ace|
|
27
|
+
product + (value_without_aces + product > 10 ? 1 : 11)
|
28
|
+
end
|
29
|
+
|
30
|
+
value_without_aces + value_of_aces
|
31
|
+
end
|
32
|
+
|
33
|
+
def <<(card)
|
34
|
+
append(card)
|
35
|
+
end
|
36
|
+
|
37
|
+
def append(card)
|
38
|
+
raise Errors::HandError.new('Unable to append: hand is busted') if busted?
|
39
|
+
|
40
|
+
@cards.push(card)
|
41
|
+
|
42
|
+
@state = :pushed
|
43
|
+
|
44
|
+
self
|
45
|
+
end
|
46
|
+
|
47
|
+
def busted?
|
48
|
+
value > 21
|
49
|
+
end
|
50
|
+
|
51
|
+
def blackjack?
|
52
|
+
@cards.count == 2 && value == 21
|
53
|
+
end
|
54
|
+
|
55
|
+
def to_s
|
56
|
+
"Cards: #{ @cards.map(&:to_s).join(', ') }. Value: #{ value }."
|
57
|
+
end
|
58
|
+
|
59
|
+
def <=>(other)
|
60
|
+
if self.busted? && other.busted?
|
61
|
+
0
|
62
|
+
elsif self.busted?
|
63
|
+
-1
|
64
|
+
elsif other.busted?
|
65
|
+
1
|
66
|
+
else
|
67
|
+
self.value <=> other.value
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
module Blackjack
|
2
|
+
module Hand
|
3
|
+
class PlayerHand < BaseHand
|
4
|
+
attr_reader :bet
|
5
|
+
|
6
|
+
def initialize(player, bet, cards)
|
7
|
+
@player = player
|
8
|
+
@bet = bet
|
9
|
+
|
10
|
+
super(cards)
|
11
|
+
end
|
12
|
+
|
13
|
+
def split(first_card, second_card)
|
14
|
+
raise Errors::HandError.new('Unable to split') unless can_split?
|
15
|
+
|
16
|
+
last_card = @cards.pop
|
17
|
+
@cards.push(first_card)
|
18
|
+
PlayerHand.new(@player, @bet, [ last_card, second_card ])
|
19
|
+
end
|
20
|
+
|
21
|
+
def stand
|
22
|
+
raise Errors::HandError.new('Unable to stand') unless can_stand?
|
23
|
+
|
24
|
+
@state = :stand
|
25
|
+
|
26
|
+
self
|
27
|
+
end
|
28
|
+
|
29
|
+
def stand?
|
30
|
+
@state == :stand
|
31
|
+
end
|
32
|
+
|
33
|
+
def append(card)
|
34
|
+
raise Errors::HandError.new('Unable to append: hand is marked as stand') if stand?
|
35
|
+
|
36
|
+
super(card)
|
37
|
+
end
|
38
|
+
|
39
|
+
def playable?
|
40
|
+
!(stand? || busted?) && value < 21
|
41
|
+
end
|
42
|
+
|
43
|
+
def can_stand?
|
44
|
+
!(stand? || busted?)
|
45
|
+
end
|
46
|
+
|
47
|
+
def can_hit?
|
48
|
+
can_stand?
|
49
|
+
end
|
50
|
+
|
51
|
+
def can_double?
|
52
|
+
can_hit? && @player.can_afford?(bet)
|
53
|
+
end
|
54
|
+
|
55
|
+
def can_split?
|
56
|
+
can_double? &&
|
57
|
+
state == :initial &&
|
58
|
+
@cards.first.equal_by_rank?(@cards.last)
|
59
|
+
end
|
60
|
+
|
61
|
+
def options
|
62
|
+
return [] if busted? || stand?
|
63
|
+
|
64
|
+
[].tap do |options|
|
65
|
+
options << :hit if can_hit?
|
66
|
+
options << :stand if can_stand?
|
67
|
+
options << :double if can_double?
|
68
|
+
options << :split if can_split?
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def double_bet
|
73
|
+
@bet *= 2
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Blackjack
|
2
|
+
class Player
|
3
|
+
attr_reader :name, :bankroll
|
4
|
+
|
5
|
+
def initialize(name, bankroll)
|
6
|
+
@name = name
|
7
|
+
@bankroll = bankroll
|
8
|
+
end
|
9
|
+
|
10
|
+
def can_afford?(amount)
|
11
|
+
@bankroll >= amount
|
12
|
+
end
|
13
|
+
|
14
|
+
def withdraw(amount)
|
15
|
+
@bankroll -= amount
|
16
|
+
end
|
17
|
+
|
18
|
+
def reward(amount)
|
19
|
+
@bankroll += amount
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Blackjack
|
2
|
+
class Shoe
|
3
|
+
def initialize(deck_count = 2)
|
4
|
+
decks = (1..deck_count).map { Deck.new.shuffle }
|
5
|
+
@cards = decks
|
6
|
+
.reduce([]) { |product, deck| product + deck.cards }
|
7
|
+
.map(&:hide)
|
8
|
+
end
|
9
|
+
|
10
|
+
def take(open: true)
|
11
|
+
card = @cards.pop
|
12
|
+
open ? card.open : card
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
metadata
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: blackjack_game
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Cyril Sadovnik
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-05-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: tty-prompt
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.12'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.12'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.14'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.14'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- cyril.sadovnik@gmail.com
|
72
|
+
executables:
|
73
|
+
- blackjack
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- ".gitignore"
|
78
|
+
- ".rspec"
|
79
|
+
- ".travis.yml"
|
80
|
+
- CODE_OF_CONDUCT.md
|
81
|
+
- Gemfile
|
82
|
+
- LICENSE.txt
|
83
|
+
- Makefile
|
84
|
+
- README.md
|
85
|
+
- Rakefile
|
86
|
+
- bin/blackjack
|
87
|
+
- blackjack_game.gemspec
|
88
|
+
- lib/blackjack.rb
|
89
|
+
- lib/blackjack/card.rb
|
90
|
+
- lib/blackjack/cli.rb
|
91
|
+
- lib/blackjack/cli/app.rb
|
92
|
+
- lib/blackjack/deck.rb
|
93
|
+
- lib/blackjack/game.rb
|
94
|
+
- lib/blackjack/hand.rb
|
95
|
+
- lib/blackjack/hand/base_hand.rb
|
96
|
+
- lib/blackjack/hand/dealer_hand.rb
|
97
|
+
- lib/blackjack/hand/player_hand.rb
|
98
|
+
- lib/blackjack/player.rb
|
99
|
+
- lib/blackjack/shoe.rb
|
100
|
+
- lib/blackjack/version.rb
|
101
|
+
homepage: https://github.com/sadovnik/blackjack
|
102
|
+
licenses:
|
103
|
+
- MIT
|
104
|
+
metadata: {}
|
105
|
+
post_install_message:
|
106
|
+
rdoc_options: []
|
107
|
+
require_paths:
|
108
|
+
- lib
|
109
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
110
|
+
requirements:
|
111
|
+
- - ">="
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
requirements: []
|
120
|
+
rubyforge_project:
|
121
|
+
rubygems_version: 2.5.1
|
122
|
+
signing_key:
|
123
|
+
specification_version: 4
|
124
|
+
summary: Simple CLI Blackjack
|
125
|
+
test_files: []
|