-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblackjack.rb
More file actions
98 lines (85 loc) · 2.42 KB
/
blackjack.rb
File metadata and controls
98 lines (85 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
require_relative 'deck'
require_relative 'hand'
class Blackjack
attr_reader :player_hand, :dealer_hand, :deck, :playing
attr_accessor :current_gamer, :result
def initialize(suits, ranks)
@player_hand = nil
@dealer_hand = nil
@deck = Deck.new(suits, ranks)
@deck.shuffle
@playing = false
@current_gamer = 'Player'
@result = ''
end
def deal
unless @playing
@player_hand = Hand.new
@dealer_hand = Hand.new
2.times do
dealer_hand.add_card(@deck.deal_card)
player_hand.add_card(@deck.deal_card)
end
end
dealer_hand.dealt_cards.first.show = false
@playing = true
values_of_ten = ['10', 'Jack', 'Queen', 'King']
player_cards = player_hand.dealt_cards
if values_of_ten.include?(player_cards.first.rank) && player_cards.last.rank == 'Ace' || player_cards.first.rank == 'Ace' && values_of_ten.include?(player_cards.last.rank)
@current_gamer = "Dealer"
end
end
def hit
# Dealing a new card to the player, current_gamer = player, playing is true
if playing
if current_gamer == "Player"
add_new_card player_hand
elsif current_gamer == "Dealer"
add_new_card dealer_hand
end
end
end
def stand
if @playing
if current_gamer == "Player"
@current_gamer = "Dealer"
dealer_hand.dealt_cards.first.show = true
end
while dealer_hand.get_value < 17
hit
end
end
end
def show_hands
"Player's hand: #{player_hand}\nDealer's hand: #{dealer_hand}"
end
def set_results
if player_hand.get_value > 21
@result = "Player Busted!"
elsif dealer_hand.get_value > 21
@result = "Dealer Busted!"
elsif current_gamer == "Dealer"
if player_hand.get_value == dealer_hand.get_value
@result = "It's a tie!"
elsif player_hand.get_value > dealer_hand.get_value
@result = "Player wins!"
elsif player_hand.get_value < dealer_hand.get_value
@result = "Dealer wins!"
end
end
end
def to_s
puts "Player hand is : #{player_hand.get_value}"
puts "Player has : #{player_hand.dealt_cards.count}"
puts "Dealer hand is : #{dealer_hand.get_value}"
puts "Dealer has : #{dealer_hand.dealt_cards.count}"
end
private
def add_new_card(hand)
hand.add_card(@deck.deal_card)
if hand.get_value > 21
@result = "#{current_gamer} busted!"
@playing = false
end
end
end