Class: TwentyOne::Game

Inherits:
Object
  • Object
show all
Defined in:
lib/twenty_one/game.rb

Constant Summary collapse

@@BLACKJACK =
21
@@DEALER_MIN =
17
@@MAX_BET =
20
@@MIN_BET =
1
@@SHOE_DECKS_COUNT =
4

Class Method Summary collapse

Class Method Details

.playObject



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
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/twenty_one/game.rb', line 14

def self.play 
	@playing = true
	@phase = :bet

	shoe = [] 

	@@SHOE_DECKS_COUNT.times do
		shoe.concat Deck.new.shuffle
	end

	@dealer = Dealer.new "Harold", shoe

	puts <<-EOS
	You went into the Wolf's Den
	After they made you sign with that pen.
	You said were there to settle a bet.
	The Dealer shuffled his deck.
	“What was your name again?”
	EOS
	puts "Type your name:" 
	name = gets
	@player = Player.new name 
	puts

	while @playing
		case @phase
		when :bet
			puts '"How much would you like to bet?" (minimum of $1)'
			amount = gets.chomp.to_i

			if Chip.get_amount(@player.chips) == 0 
				puts '"You\'re completely out of money, friend. The game is over."'
				@pahse = :gameover
			elsif Chip.get_amount(@player.chips) <= amount
				puts' "You don\'t have enough money to make that bet."'
			elsif amount > 0
				@phase = :deal
			end
		when :deal
			@player.make_bet amount

			puts
			puts "The Dealer dealt the cards"
			puts

			# Deal cards
			2.times do @dealer.hit(@player) end
			2.times do @dealer.hit(@dealer) end	

			puts "You received:"
			puts

			@player.hand.cards.each { |card| puts card }

			puts "The Dealer received a #{@dealer.hand.cards.first.to_s}. He kept the his second card face-down."
			puts

			@phase = :player_turn
		when :side_rules
			#TODO: Side Rules
		when :player_turn
			puts '"What would you like to do?" (type hit or stand)'
		 	choice = gets.chomp	

			case choice
			when "hit"
				@dealer.hit @player

				puts "You received a #{@player.hand.cards.last.to_s}"
				puts

				if @player.hand.cards.last.is_a?(AceCard) && @player.hand.cards.last.name == :ace
					puts "You received an #{@player.hand.cards.last.to_s}."
					puts '"Would you like it to value 1 or 11?" (type 1 or 11)'
					new_ace_value = gets.chomp

					case new_ace_value
					when "1"
							@player.hand.cards.last.use_lower
					when "11"	
							@player.hand.cards.last.use_upper
					end

					puts "Your ace's new value is #{@player.hand.cards.last.value}"
					puts
				end

				if @player.hand.value > @@BLACKJACK
					puts "You received more than 21, which meant you lost."
					puts		
					@phase = :results
				end
			when "stand"
				puts "Then it was The Dealer's turn."
				puts
				@phase = :showdown
			end
		when :dealer_turn
		when :showdown
			puts "The dealer reveals his card!"
			puts "It's a #{@dealer.hand.cards.last.to_s}"
			puts

			while @dealer.hand.value < @@DEALER_MIN && @dealer.hand.value != @@BLACKJACK 
				@dealer.hit(@dealer)

				puts "The dealer drew a #{@dealer.hand.cards.last.to_s}"
			end

			result = @dealer.showdown(@player)

			case result
			when :twenty_one
				puts "21!"	
			when :win
				puts "You won!"
			when :bust
				puts "You lost!"
			when :push
				puts "Tie!"
			end

			@phase = :results
		when :results
			puts "Here were your game stats:"
			puts
			puts "Chips: #{@player.chips.size}"
			puts "Total value: $#{Chip.get_amount(@player.chips)}"
			puts "TwentyOnes: #{@player.twenty_ones}"
			puts "Wins: #{@player.wins}"
			puts "Busts: #{@player.busts}"
			puts "Pushes: #{@player.pushes}"
			puts
			puts '"Play again?"'
			answer = gets.chomp

			case answer
			when "no"
				@playing = false
			when "yes"
				@phase = :bet
			end
		end
	end
end