blackjack-cli 0.0.0
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/Gemfile +5 -0
- data/README.md +3 -0
- data/bin/blackjack +4 -0
- data/config/environment.rb +9 -0
- data/lib/Card.rb +9 -0
- data/lib/Cli.rb +66 -0
- data/lib/Deck.rb +64 -0
- data/lib/Game.rb +268 -0
- data/lib/Player.rb +10 -0
- data/lib/players/Computer.rb +17 -0
- data/lib/players/Human.rb +3 -0
- metadata +56 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: e290cf77ac0762eb379f00cb91a52d02d32045fc
|
|
4
|
+
data.tar.gz: 143d19db3b61d77f1ae5054d9643ac9195fdc958
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 1010a3e1f9f8abc58d2685a53db44be819d22ae2b1a82f2e79b4f3a392a4a5b509544af13d1e46ffb940cb66a46aa5817fa23e94023204f3754138530f3efd48
|
|
7
|
+
data.tar.gz: 22aaa95c127d7f3e9ad47e5874a7da3e1ecb33e67b7b5c902106a23e66edee32a74b638bdc29e0ed05691d7c5b932e3d8f2a33895909b357def28e484b979450
|
data/Gemfile
ADDED
data/README.md
ADDED
data/bin/blackjack
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
require 'pry'
|
|
2
|
+
|
|
3
|
+
require_relative '../lib/Cli.rb'
|
|
4
|
+
require_relative '../lib/Game.rb'
|
|
5
|
+
require_relative '../lib/Deck.rb'
|
|
6
|
+
require_relative '../lib/Card.rb'
|
|
7
|
+
require_relative '../lib/Player.rb'
|
|
8
|
+
require_relative '../lib/players/Human.rb'
|
|
9
|
+
require_relative '../lib/players/Computer.rb'
|
data/lib/Card.rb
ADDED
data/lib/Cli.rb
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
class Cli
|
|
2
|
+
def run
|
|
3
|
+
puts "Welcome to Blackjack Cli!"
|
|
4
|
+
initialize_players
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
def error_message
|
|
8
|
+
puts "Incorrect input, please try again!"
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def initialize_game
|
|
12
|
+
puts "Type start to begin or exit to exit program"
|
|
13
|
+
input = gets.strip
|
|
14
|
+
case input
|
|
15
|
+
when "start"
|
|
16
|
+
puts "Loading Game..."
|
|
17
|
+
sleep(2)
|
|
18
|
+
puts "Game loaded"
|
|
19
|
+
sleep(1)
|
|
20
|
+
Game.new(@player_1, @player_2, @type).play
|
|
21
|
+
when "exit"
|
|
22
|
+
return
|
|
23
|
+
else
|
|
24
|
+
error_message
|
|
25
|
+
initialize_game
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def ask_for_player1_name
|
|
30
|
+
puts "Player 1, What is your name?"
|
|
31
|
+
input = gets.strip
|
|
32
|
+
@player_1 = Human.new(input)
|
|
33
|
+
@type = "1 player"
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def ask_for_player2_name
|
|
37
|
+
puts "Player 2, What is your name?"
|
|
38
|
+
input = gets.strip
|
|
39
|
+
@player_2 = Human.new(input)
|
|
40
|
+
@type = "2 player"
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def initialize_players
|
|
44
|
+
puts "Type 1 for 1 player, 2 for 2 players, or exit to exit:"
|
|
45
|
+
input = gets.strip
|
|
46
|
+
puts ""
|
|
47
|
+
case input
|
|
48
|
+
when "1"
|
|
49
|
+
ask_for_player1_name
|
|
50
|
+
@player_2 = Computer.new
|
|
51
|
+
initialize_game
|
|
52
|
+
when "2"
|
|
53
|
+
ask_for_player1_name
|
|
54
|
+
puts ""
|
|
55
|
+
ask_for_player2_name
|
|
56
|
+
puts ""
|
|
57
|
+
initialize_game
|
|
58
|
+
when "exit"
|
|
59
|
+
return
|
|
60
|
+
else
|
|
61
|
+
error_message
|
|
62
|
+
initialize_players
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
end
|
data/lib/Deck.rb
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
class Deck
|
|
2
|
+
attr_accessor :cards
|
|
3
|
+
|
|
4
|
+
def initialize
|
|
5
|
+
@cards = get_all_cards
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def get_all_cards
|
|
9
|
+
[
|
|
10
|
+
Card.new('Ace', 'Hearts'),
|
|
11
|
+
Card.new('Ace', 'Diamonds'),
|
|
12
|
+
Card.new('Ace', 'Clubs'),
|
|
13
|
+
Card.new('Ace', 'Spades'),
|
|
14
|
+
Card.new('2', 'Hearts'),
|
|
15
|
+
Card.new('2', 'Diamonds'),
|
|
16
|
+
Card.new('2', 'Clubss'),
|
|
17
|
+
Card.new('2', 'Spades'),
|
|
18
|
+
Card.new('3', 'Hearts'),
|
|
19
|
+
Card.new('3', 'Diamonds'),
|
|
20
|
+
Card.new('3', 'Clubs'),
|
|
21
|
+
Card.new('3', 'Spades'),
|
|
22
|
+
Card.new('4', 'Hearts'),
|
|
23
|
+
Card.new('4', 'Diamonds'),
|
|
24
|
+
Card.new('4', 'Clubs'),
|
|
25
|
+
Card.new('4', 'Spades'),
|
|
26
|
+
Card.new('5', 'Hearts'),
|
|
27
|
+
Card.new('5', 'Diamonds'),
|
|
28
|
+
Card.new('5', 'Clubs'),
|
|
29
|
+
Card.new('5', 'Spades'),
|
|
30
|
+
Card.new('6', 'Hearts'),
|
|
31
|
+
Card.new('6', 'Diamonds'),
|
|
32
|
+
Card.new('6', 'Clubs'),
|
|
33
|
+
Card.new('6', 'Spades'),
|
|
34
|
+
Card.new('7', 'Hearts'),
|
|
35
|
+
Card.new('7', 'Diamonds'),
|
|
36
|
+
Card.new('7', 'Clubs'),
|
|
37
|
+
Card.new('7', 'Spades'),
|
|
38
|
+
Card.new('8', 'Hearts'),
|
|
39
|
+
Card.new('8', 'Diamonds'),
|
|
40
|
+
Card.new('8', 'Clubs'),
|
|
41
|
+
Card.new('8', 'Spades'),
|
|
42
|
+
Card.new('9', 'Hearts'),
|
|
43
|
+
Card.new('9', 'Diamonds'),
|
|
44
|
+
Card.new('9', 'Clubs'),
|
|
45
|
+
Card.new('9', 'Spades'),
|
|
46
|
+
Card.new('10', 'Hearts'),
|
|
47
|
+
Card.new('10', 'Diamonds'),
|
|
48
|
+
Card.new('10', 'Clubs'),
|
|
49
|
+
Card.new('10', 'Spades'),
|
|
50
|
+
Card.new('Jack', 'Hearts'),
|
|
51
|
+
Card.new('Jack', 'Diamonds'),
|
|
52
|
+
Card.new('Jack', 'Clubs'),
|
|
53
|
+
Card.new('Jack', 'Spades'),
|
|
54
|
+
Card.new('Queen', 'Hearts'),
|
|
55
|
+
Card.new('Queen', 'Diamonds'),
|
|
56
|
+
Card.new('Queen', 'Clubs'),
|
|
57
|
+
Card.new('Queen', 'Spades'),
|
|
58
|
+
Card.new('King', 'Hearts'),
|
|
59
|
+
Card.new('King', 'Diamonds'),
|
|
60
|
+
Card.new('King', 'Clubs'),
|
|
61
|
+
Card.new('King', 'Spades')
|
|
62
|
+
]
|
|
63
|
+
end
|
|
64
|
+
end
|
data/lib/Game.rb
ADDED
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
require 'pry'
|
|
2
|
+
class Game
|
|
3
|
+
attr_accessor :deck, :player_1, :player_2, :turn_count
|
|
4
|
+
|
|
5
|
+
def initialize(player_1, player_2, type)
|
|
6
|
+
@deck = Deck.new.cards
|
|
7
|
+
@player_1 = player_1
|
|
8
|
+
@player_2 = player_2
|
|
9
|
+
@type = type
|
|
10
|
+
@turn_count = 0
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def current_player
|
|
14
|
+
@turn_count % 2 == 0 ? @player_1 : @player_2
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def opposite_player
|
|
18
|
+
current_player == @player_1 ? @player_2 : @player_1
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def card_value(card, player=current_player)
|
|
22
|
+
case card.name
|
|
23
|
+
when "1"
|
|
24
|
+
return 1
|
|
25
|
+
when "2"
|
|
26
|
+
return 2
|
|
27
|
+
when "3"
|
|
28
|
+
return 3
|
|
29
|
+
when "4"
|
|
30
|
+
return 4
|
|
31
|
+
when "5"
|
|
32
|
+
return 5
|
|
33
|
+
when "6"
|
|
34
|
+
return 6
|
|
35
|
+
when "7"
|
|
36
|
+
return 7
|
|
37
|
+
when "8"
|
|
38
|
+
return 8
|
|
39
|
+
when "9"
|
|
40
|
+
return 9
|
|
41
|
+
when "10"
|
|
42
|
+
return 10
|
|
43
|
+
when "Jack"
|
|
44
|
+
return 10
|
|
45
|
+
when "Queen"
|
|
46
|
+
return 10
|
|
47
|
+
when "King"
|
|
48
|
+
return 10
|
|
49
|
+
when "Ace"
|
|
50
|
+
view_cards(player) if player.is_a?(Human)
|
|
51
|
+
puts "Would you like the Ace of #{card.type} to equal 1 or 11? Type 1 or 11" if player.is_a?(Human)
|
|
52
|
+
input = gets.strip if player.is_a?(Human)
|
|
53
|
+
input = player.get_ace_value if player.is_a?(Computer)
|
|
54
|
+
case input
|
|
55
|
+
when "1"
|
|
56
|
+
return 1
|
|
57
|
+
when "11"
|
|
58
|
+
return 11
|
|
59
|
+
else
|
|
60
|
+
Cli.new.error_message
|
|
61
|
+
card_value(card)
|
|
62
|
+
end
|
|
63
|
+
else
|
|
64
|
+
Cli.new.error_message
|
|
65
|
+
card_value(card)
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def deal_cards(player)
|
|
70
|
+
player.deck.push(@deck[0])
|
|
71
|
+
@deck.shift
|
|
72
|
+
player.deck.push(@deck[0])
|
|
73
|
+
@deck.shift
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def hit(player)
|
|
77
|
+
player.deck.push(@deck[0])
|
|
78
|
+
get_player_card_values(player)
|
|
79
|
+
@deck.shift
|
|
80
|
+
@turn_count += 1
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def get_player_card_values(player)
|
|
84
|
+
player.deck_value = 0
|
|
85
|
+
player.deck.each{|card| player.deck_value += card_value(card, player)}
|
|
86
|
+
player.deck_value
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def draw?
|
|
90
|
+
@player_1.deck_value == @player_2.deck_value
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def bust(player)
|
|
94
|
+
player.deck_value > 21
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def won?
|
|
98
|
+
if @player_1.deck_value > @player_2.deck_value && !bust(@player_1)
|
|
99
|
+
@player_1
|
|
100
|
+
elsif @player_2.deck_value > @player_1.deck_value && !bust(@player_2)
|
|
101
|
+
@player_2
|
|
102
|
+
elsif bust(@player_1) && !bust(@player_2)
|
|
103
|
+
@player_2
|
|
104
|
+
elsif bust(@player_2) && !bust(@player_1)
|
|
105
|
+
@player_1
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def winner
|
|
110
|
+
if winner = won?
|
|
111
|
+
winner
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def lost
|
|
116
|
+
if bust(@player_1)
|
|
117
|
+
puts "Player 1 busted!"
|
|
118
|
+
elsif bust(@player_2)
|
|
119
|
+
puts "Player 2 busted!"
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def game_over
|
|
124
|
+
if @player_1.status == "stand" && @player_2.status == "stand"
|
|
125
|
+
true
|
|
126
|
+
elsif bust(@player_1) || bust(@player_2)
|
|
127
|
+
true
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
def show_hands
|
|
132
|
+
view_cards(@player_1)
|
|
133
|
+
puts "Total card value: #{@player_1.deck_value}#{bust(@player_1) ? ' - Busted!' : nil}"
|
|
134
|
+
sleep(1)
|
|
135
|
+
puts ""
|
|
136
|
+
view_cards(@player_2)
|
|
137
|
+
puts "Total card value: #{@player_2.deck_value}#{bust(@player_2) ? ' - Busted!' : nil}"
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
def stand(player)
|
|
141
|
+
player.status = "stand"
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
def help
|
|
145
|
+
puts "Type cards to see your cards, exit to exit game, menu to go back to the menu"
|
|
146
|
+
puts ""
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
def view_cards(player)
|
|
150
|
+
puts "#{player.name}'s cards:"
|
|
151
|
+
player.deck.each{|card| puts "#{card.name} of #{card.type}"}
|
|
152
|
+
puts ""
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
def turn
|
|
156
|
+
@turn_count += 1 if current_player.status == "stand"
|
|
157
|
+
view_cards(current_player) if current_player.is_a?(Human)
|
|
158
|
+
get_player_card_values(current_player)
|
|
159
|
+
puts "A total value of #{current_player.deck_value}" if current_player.is_a?(Human)
|
|
160
|
+
puts ""
|
|
161
|
+
options
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
def update_player_values
|
|
165
|
+
puts "Updating #{@player_1.name}'s hand..."
|
|
166
|
+
sleep(1)
|
|
167
|
+
get_player_card_values(@player_1)
|
|
168
|
+
sleep(1)
|
|
169
|
+
puts "Updating #{@player_2.name}'s hand..."
|
|
170
|
+
sleep(1)
|
|
171
|
+
get_player_card_values(@player_2)
|
|
172
|
+
sleep(1)
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
def options
|
|
176
|
+
puts "#{current_player.name} turn: Type hit to draw a card, stand to keep your hand, or help for more options!" if current_player.is_a?(Human)
|
|
177
|
+
input = gets.strip if current_player.is_a?(Human)
|
|
178
|
+
input = current_player.move if current_player.is_a?(Computer)
|
|
179
|
+
puts ""
|
|
180
|
+
case input
|
|
181
|
+
when "hit"
|
|
182
|
+
puts "#{current_player.name} draws a card!"
|
|
183
|
+
puts ""
|
|
184
|
+
hit(current_player)
|
|
185
|
+
when "help"
|
|
186
|
+
help
|
|
187
|
+
options
|
|
188
|
+
when "cards"
|
|
189
|
+
view_cards(current_player)
|
|
190
|
+
puts "A total value of #{current_player.deck_value}"
|
|
191
|
+
puts ""
|
|
192
|
+
options
|
|
193
|
+
when "exit"
|
|
194
|
+
exit
|
|
195
|
+
when "menu"
|
|
196
|
+
Cli.new.initialize_players
|
|
197
|
+
when "stand"
|
|
198
|
+
puts "#{current_player.name} stands!"
|
|
199
|
+
puts ""
|
|
200
|
+
stand(current_player)
|
|
201
|
+
@turn_count += 1
|
|
202
|
+
else
|
|
203
|
+
Cli.new.error_message
|
|
204
|
+
turn
|
|
205
|
+
end
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
def play
|
|
209
|
+
puts "Shuffling Deck..."
|
|
210
|
+
@deck.shuffle!
|
|
211
|
+
sleep(1)
|
|
212
|
+
puts "Dealing Cards..."
|
|
213
|
+
deal_cards(@player_1)
|
|
214
|
+
deal_cards(@player_2)
|
|
215
|
+
sleep(1)
|
|
216
|
+
update_player_values
|
|
217
|
+
puts ""
|
|
218
|
+
until game_over
|
|
219
|
+
turn
|
|
220
|
+
end
|
|
221
|
+
puts "Players, show your hands!"
|
|
222
|
+
puts ""
|
|
223
|
+
sleep(1)
|
|
224
|
+
show_hands
|
|
225
|
+
sleep(1)
|
|
226
|
+
puts ""
|
|
227
|
+
|
|
228
|
+
if won?
|
|
229
|
+
puts "Congratulations #{winner.name}. You won with #{winner.deck_value}!"
|
|
230
|
+
elsif draw?
|
|
231
|
+
puts "The game ends in a draw!"
|
|
232
|
+
else
|
|
233
|
+
lost
|
|
234
|
+
end
|
|
235
|
+
sleep(1)
|
|
236
|
+
puts ""
|
|
237
|
+
end_game
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
def end_game
|
|
241
|
+
puts "Main Menu? Type 'menu', Play again? Type 'y' for yes or 'n' for no:"
|
|
242
|
+
input = gets.strip
|
|
243
|
+
|
|
244
|
+
case input
|
|
245
|
+
when "menu"
|
|
246
|
+
puts ""
|
|
247
|
+
Cli.new.initialize_players
|
|
248
|
+
when "y"
|
|
249
|
+
puts ""
|
|
250
|
+
if @type == "1 player"
|
|
251
|
+
@player_1 = Human.new(@player_1.name)
|
|
252
|
+
@player_2 = Computer.new
|
|
253
|
+
Game.new(@player_1, @player_2, "1 player").play
|
|
254
|
+
else
|
|
255
|
+
@player_1 = Human.new(@player_1.name)
|
|
256
|
+
@player_2 = Human.new(@player_2.name)
|
|
257
|
+
Game.new(@player_1, @player_2, "2 player").play
|
|
258
|
+
end
|
|
259
|
+
when "n"
|
|
260
|
+
return
|
|
261
|
+
else
|
|
262
|
+
puts ""
|
|
263
|
+
Cli.new.error_message
|
|
264
|
+
end_game
|
|
265
|
+
end
|
|
266
|
+
end
|
|
267
|
+
|
|
268
|
+
end
|
data/lib/Player.rb
ADDED
metadata
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: blackjack-cli
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Enoch Griffith
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2016-12-24 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: A simple Blackjack Cli Game
|
|
14
|
+
email:
|
|
15
|
+
- enoch2k2@hotmail.com
|
|
16
|
+
executables:
|
|
17
|
+
- blackjack
|
|
18
|
+
extensions: []
|
|
19
|
+
extra_rdoc_files: []
|
|
20
|
+
files:
|
|
21
|
+
- Gemfile
|
|
22
|
+
- README.md
|
|
23
|
+
- bin/blackjack
|
|
24
|
+
- config/environment.rb
|
|
25
|
+
- lib/Card.rb
|
|
26
|
+
- lib/Cli.rb
|
|
27
|
+
- lib/Deck.rb
|
|
28
|
+
- lib/Game.rb
|
|
29
|
+
- lib/Player.rb
|
|
30
|
+
- lib/players/Computer.rb
|
|
31
|
+
- lib/players/Human.rb
|
|
32
|
+
homepage: http://rubygems.org/gems/enoch2k2-blackjack-cli
|
|
33
|
+
licenses:
|
|
34
|
+
- MIT
|
|
35
|
+
metadata: {}
|
|
36
|
+
post_install_message:
|
|
37
|
+
rdoc_options: []
|
|
38
|
+
require_paths:
|
|
39
|
+
- lib
|
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
41
|
+
requirements:
|
|
42
|
+
- - ">="
|
|
43
|
+
- !ruby/object:Gem::Version
|
|
44
|
+
version: '0'
|
|
45
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
46
|
+
requirements:
|
|
47
|
+
- - ">="
|
|
48
|
+
- !ruby/object:Gem::Version
|
|
49
|
+
version: '0'
|
|
50
|
+
requirements: []
|
|
51
|
+
rubyforge_project:
|
|
52
|
+
rubygems_version: 2.5.1
|
|
53
|
+
signing_key:
|
|
54
|
+
specification_version: 4
|
|
55
|
+
summary: Blackjack
|
|
56
|
+
test_files: []
|