blackjack-ken 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/blackjack/card.rb +32 -0
- data/lib/blackjack/deck.rb +22 -0
- data/lib/blackjack/game.rb +42 -0
- data/lib/blackjack/hand.rb +25 -0
- data/lib/blackjack/player.rb +44 -0
- data/lib/blackjack/turn.rb +61 -0
- data/lib/blackjack.rb +14 -0
- metadata +50 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: c2611b49ea1ffad532943748618778a3f55c683d
|
|
4
|
+
data.tar.gz: 010926bac58aa4fe52114efef37c69dfec1d4f64
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: bba3e0da13d4b206324aaa395742ea379c91b816d628a0f39cb6ada1388aaf470c00ce2cced1f5fcec760f2be5998a8f052c9a1ec295dd440c5311e4d3b69e66
|
|
7
|
+
data.tar.gz: 527103287e550b0fd42f6e12e7e315b9966f04b4ac1e5f1862aae2b2ea256e43c5f305995d94a039e2a85e0b10716845abb8119a681c02faf03bed1a510b07dd
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
class Blackjack
|
|
2
|
+
class Card
|
|
3
|
+
attr_reader :rank, :suit
|
|
4
|
+
def initialize(rank, suit)
|
|
5
|
+
@rank = rank
|
|
6
|
+
@suit = suit
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def display
|
|
10
|
+
rank.to_s + suit
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def is_ace?
|
|
14
|
+
rank == 'A'
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def is_facecard?
|
|
18
|
+
'JQK'.include?(rank.to_s)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def value
|
|
22
|
+
case
|
|
23
|
+
when is_facecard?
|
|
24
|
+
10
|
|
25
|
+
when is_ace?
|
|
26
|
+
1
|
|
27
|
+
else
|
|
28
|
+
rank
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
require_relative "card"
|
|
2
|
+
class Blackjack
|
|
3
|
+
class Deck
|
|
4
|
+
SUITS = ["♠", "♥", "♦", "♣"]
|
|
5
|
+
RANKS = [2, 3, 4, 5, 6, 7, 8, 9, 10, 'J', 'Q', 'K', 'A']
|
|
6
|
+
attr_reader :cards
|
|
7
|
+
def initialize
|
|
8
|
+
@cards = make_cards
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def deal_to(player_cards)
|
|
12
|
+
card = cards.pop
|
|
13
|
+
player_cards << card
|
|
14
|
+
card
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
private
|
|
18
|
+
def make_cards
|
|
19
|
+
SUITS.map {|suit| RANKS.map {|rank| Card.new(rank, suit)}}.flatten.shuffle!
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
class Blackjack
|
|
2
|
+
class Game
|
|
3
|
+
attr_accessor :player, :computer, :deck, :input
|
|
4
|
+
def initialize(name)
|
|
5
|
+
@player = Player.new(name: name, hand: Hand.new, human: true)
|
|
6
|
+
@computer = Player.new(name: "Dealer", hand: Hand.new, human: false)
|
|
7
|
+
@deck = Deck.new
|
|
8
|
+
@input = " "
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def play
|
|
12
|
+
player_turn = Turn.new(player: player, deck: deck)
|
|
13
|
+
player_turn.take
|
|
14
|
+
computer_turn = Turn.new(player: computer, deck: deck)
|
|
15
|
+
computer_turn.take unless player.bust?
|
|
16
|
+
puts announce_winner
|
|
17
|
+
play_again?
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
private
|
|
21
|
+
|
|
22
|
+
def play_again?
|
|
23
|
+
print "Would you like to play again? (y/n) "
|
|
24
|
+
input = gets.chomp.downcase
|
|
25
|
+
input == "y" ? Game.new(player.name).play : puts("Have a nice life now!")
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def announce_winner
|
|
29
|
+
if player.bust?
|
|
30
|
+
"Bust! " + computer.wins!
|
|
31
|
+
elsif computer.bust?
|
|
32
|
+
"Bust! " + player.wins!
|
|
33
|
+
elsif player.score == computer.score
|
|
34
|
+
"It's a tie! |-o-|\n" + computer.wins!
|
|
35
|
+
elsif player.score > computer.score
|
|
36
|
+
player.wins!
|
|
37
|
+
else
|
|
38
|
+
computer.wins!
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
class Blackjack
|
|
2
|
+
class Hand
|
|
3
|
+
attr_reader :cards
|
|
4
|
+
def initialize(cards = [])
|
|
5
|
+
@cards = cards
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def cards_value
|
|
9
|
+
value = cards.inject(0) { |sum, card| sum + card.value }
|
|
10
|
+
account_for_aces(value)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def account_for_aces(value)
|
|
14
|
+
value < 12 && contains_ace? ? value + 10 : value
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def contains_ace?
|
|
18
|
+
cards.any? { |card| card.is_ace? }
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def bust?
|
|
22
|
+
cards_value > 21
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
class Blackjack
|
|
2
|
+
class Player
|
|
3
|
+
attr_accessor :name, :hand
|
|
4
|
+
def initialize(args)
|
|
5
|
+
@name = args[:name]
|
|
6
|
+
@hand = args[:hand]
|
|
7
|
+
@human = args[:human] || false
|
|
8
|
+
@stand = false
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def stand!
|
|
12
|
+
@stand = true
|
|
13
|
+
"#{name} stands!\n\n"
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def human?
|
|
17
|
+
@human
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def stand?
|
|
21
|
+
@stand
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def score
|
|
25
|
+
hand.cards_value
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def formatted_score
|
|
29
|
+
"#{name}'s score: #{score}\n\n"
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def bust?
|
|
33
|
+
hand.bust?
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def wants_to_hit?
|
|
37
|
+
score < 17
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def wins!
|
|
41
|
+
"#{name} wins!"
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
class Blackjack
|
|
2
|
+
class Turn
|
|
3
|
+
HIT = "h"
|
|
4
|
+
VALID_OPTIONS = ['h', 's']
|
|
5
|
+
|
|
6
|
+
attr_accessor :player, :deck, :input
|
|
7
|
+
def initialize(args)
|
|
8
|
+
@player = args[:player]
|
|
9
|
+
@deck = args[:deck]
|
|
10
|
+
@input
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def take
|
|
14
|
+
2.times do puts deal_to_player end
|
|
15
|
+
until over?
|
|
16
|
+
puts player.formatted_score
|
|
17
|
+
puts hit_or_stand
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
private
|
|
22
|
+
|
|
23
|
+
def over?
|
|
24
|
+
player.bust? || player.stand?
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def deal_to_player
|
|
28
|
+
"#{player.name} was dealt #{deck.deal_to(player.hand.cards).display}"
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def hit_or_stand
|
|
32
|
+
get_input if player.human?
|
|
33
|
+
player_hits! ? deal_to_player : player.stand!
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def player_hits!
|
|
37
|
+
player.human? ? player_chooses_hit? : player.wants_to_hit?
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def player_chooses_hit?
|
|
41
|
+
input == HIT
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def get_input
|
|
45
|
+
self.input = invalid_input
|
|
46
|
+
until VALID_OPTIONS.include?(self.input)
|
|
47
|
+
puts "That is not a valid choice!" if !self.input == invalid_input
|
|
48
|
+
print "Hit or Stand? (h/s) "
|
|
49
|
+
self.input = get_action
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def invalid_input
|
|
54
|
+
"#{VALID_OPTIONS}"
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def get_action
|
|
58
|
+
gets.chomp.downcase
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
data/lib/blackjack.rb
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
require_relative "blackjack/deck"
|
|
2
|
+
require_relative "blackjack/hand"
|
|
3
|
+
require_relative "blackjack/game"
|
|
4
|
+
require_relative "blackjack/player"
|
|
5
|
+
require_relative "blackjack/turn"
|
|
6
|
+
class Blackjack
|
|
7
|
+
def self.play
|
|
8
|
+
puts "Welcome to Blackjack!"
|
|
9
|
+
print "What's your name? "
|
|
10
|
+
name = gets.chomp.capitalize
|
|
11
|
+
game = Blackjack::Game.new(name)
|
|
12
|
+
game.play
|
|
13
|
+
end
|
|
14
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: blackjack-ken
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Ken Thomas
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2016-08-25 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: Allows a user to play blackjack vs a computer in the console
|
|
14
|
+
email: ken.thomas.3185@gmail.com
|
|
15
|
+
executables: []
|
|
16
|
+
extensions: []
|
|
17
|
+
extra_rdoc_files: []
|
|
18
|
+
files:
|
|
19
|
+
- lib/blackjack.rb
|
|
20
|
+
- lib/blackjack/card.rb
|
|
21
|
+
- lib/blackjack/deck.rb
|
|
22
|
+
- lib/blackjack/game.rb
|
|
23
|
+
- lib/blackjack/hand.rb
|
|
24
|
+
- lib/blackjack/player.rb
|
|
25
|
+
- lib/blackjack/turn.rb
|
|
26
|
+
homepage: http://rubygems.org/gems/blackjack-ken
|
|
27
|
+
licenses:
|
|
28
|
+
- MIT
|
|
29
|
+
metadata: {}
|
|
30
|
+
post_install_message:
|
|
31
|
+
rdoc_options: []
|
|
32
|
+
require_paths:
|
|
33
|
+
- lib
|
|
34
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
35
|
+
requirements:
|
|
36
|
+
- - ">="
|
|
37
|
+
- !ruby/object:Gem::Version
|
|
38
|
+
version: '0'
|
|
39
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
40
|
+
requirements:
|
|
41
|
+
- - ">="
|
|
42
|
+
- !ruby/object:Gem::Version
|
|
43
|
+
version: '0'
|
|
44
|
+
requirements: []
|
|
45
|
+
rubyforge_project:
|
|
46
|
+
rubygems_version: 2.4.5.1
|
|
47
|
+
signing_key:
|
|
48
|
+
specification_version: 4
|
|
49
|
+
summary: Play blackjack in the console
|
|
50
|
+
test_files: []
|