-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblackjack_controller.rb
147 lines (123 loc) · 2.94 KB
/
blackjack_controller.rb
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
class BlackjackController
# Game settings
attr_accessor :amount_decks
# Game variables
attr_accessor :display, :player, :dealer, :deck
def initialize
@display = BlackjackView.new
@player = Player.new
@dealer = Player.new(true)
@dealer.name = "Dealer"
@deck = Deck.new(2)
end
def run
display.clear
display.welcome
@player.name = @display.ask_name
if @player.name.empty?
@player.name = "Player"
end
display.pause
display.say_thankyou(@player.name)
display.pause
display.announce("Starting game...")
display.pause(2)
setup
start_game
end
def setup
render_cards(true)
2.times do
card = deck.take_card
card.is_hidden = dealer.cards.empty?
dealer.cards << card
render_cards(true)
player.cards << deck.take_card
render_cards(player.cards.size < 2)
end
end
def render_cards(hide_turn=false)
display.clear
if hide_turn
display.display_setup_turn
else
display.display_player_turn(player.name)
end
display.display_cards(player, dealer)
display.pause(2)
end
def render_dealer_cards
display.clear
display.display_dealer_turn
display.display_cards(player, dealer)
display.pause(2)
end
def start_game
if check_blackjacks
next_game
return
end
choice = ''
while 's' != choice && player.cards_points < 22
choice = display.ask_hit_stay
if choice == 'h'
player.cards << deck.take_card
render_cards
end
end
if player.cards_points > 21
display.display_player_busted
else
# dealer's turn
dealer.reveal_all_cards
render_dealer_cards
while dealer.cards_points < 17
dealer.cards << deck.take_card
render_dealer_cards
end
# Announce winner
if dealer.cards_points > 21
display.display_dealer_busted
elsif player.cards_points > dealer.cards_points
display.display_player_wins
elsif player.cards_points == dealer.cards_points
display.display_tie
else
display.display_player_loses
end
end
next_game
end
def check_blackjacks
if player.cards_points == 21 && dealer.cards_points == 21
display.display_dealer_blackjack
display.pause
display.display_player_blackjack_tie
display.pause
display.display_tie
elsif dealer.cards_points == 21
display.display_dealer_blackjack
display.pause
display.display_player_loses
elsif player.cards_points == 21
display.display_player_blackjack
display.pause(3)
display.display_player_wins
end
player.cards_points == 21 || dealer.cards_points == 21
end
def next_game
reset
if display.ask_play_again == 'y'
setup
start_game
else
display.display_goodbye
end
end
def reset
player.cards.clear
dealer.cards.clear
@deck = Deck.new(2)
end
end