-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame_controller.rb
More file actions
132 lines (117 loc) · 3.17 KB
/
game_controller.rb
File metadata and controls
132 lines (117 loc) · 3.17 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
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
require_relative 'interface'
require_relative 'interface_messages'
require_relative 'game'
require_relative 'card_deck'
require_relative 'black_jack'
class GameController
include InterfaceMessages
include BlackJack
def initialize
@interface = Interface.new
init
run
end
private
def set_up
@game = Game.new(@user_name)
@user = @game.user
@dealer = @game.dealer
@bank = @game.bank
make_bets
end
def make_bets
@bank.receive(@user.bank.bet)
@bank.receive(@dealer.bank.bet)
end
def try_again_set_up(winner)
if winner
winner.bank.receive(@bank.give_cash)
else
cash = @bank.give_cash
@dealer.bank.receive(cash / 2)
@user.bank.receive(cash / 2)
end
end_game(@user) if @user.bank.empty?
end_game(@dealer) if @dealer.bank.empty?
@dealer.reset_hand
@user.reset_hand
make_bets
@game.card_deck.refresh
@interface.refresh_menu_items
end
def run
loop do
@interface.cls
@interface.show_game_screen(@dealer, @user)
menu = @interface.compose_actions_menu
choice = @interface.process_empty_choice(@interface.show_menu_and_get_input(menu), menu)
case choice
when :skip then action_skip
when :pull_card then action_pull_card
when :open_cards then action_open_cards
else return
end
game_over(nil) if @user.points == @dealer.points
game_over(@user, true) if @user.points == BLACK_JACK
game_over(winner) if @user.hand.full? && @dealer.hand.full?
end
end
def init(clear = true)
@interface.cls if clear
@interface.show_msg(WELCOME_MESSAGE)
choice = @interface.show_menu_and_get_input(START_MENU)
case choice
when :start_game
@interface.cls
@user_name = @interface.show_prompt_and_get_input(USER_NAME_PROMPT)
set_up
when :quit_game then return
else
@interface.show_msg(INVALID_CHOICE)
init(false)
end
end
def action_skip
@interface.actions_menu_items = [:pull_card, :open_cards] if @dealer.hand.full?
@dealer.move
game_over(@user) if @dealer.points > BLACK_JACK
@interface.actions_menu_items = [:pull_card, :open_cards]
end
def action_pull_card
@interface.actions_menu_items = [:open_cards]
@user.hand.pull_card(@game.card_deck.draw_card)
game_over(@dealer) if @user.points > BLACK_JACK
end
def action_open_cards
@dealer.hand.visible = true
game_over(winner)
end
def winner
@game.determine_winner
end
def game_over(winner, black_jack = false)
@interface.cls
@dealer.hand.visible = true
@interface.show_game_screen(@dealer, @user)
if black_jack
@interface.show_msg(BLACK_JACK_MSG)
elsif winner
@interface.show_congratulation(winner, @user_name)
else
@interface.show_msg(DRAW_MSG)
end
choice = @interface.process_empty_choice(@interface.show_menu_and_get_input(GAME_OVER_MENU), GAME_OVER_MENU)
case choice
when :try_again
try_again_set_up(winner)
when :exit then exit
else exit
end
end
def end_game(looser)
@interface.cls
@interface.show_empty_bank_msg(looser)
@interface.show_game_end(looser)
init(false)
end
end