-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblackjack.rb
142 lines (115 loc) · 2.36 KB
/
blackjack.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
class Blackjack
def initialize(*args)
if args.size < 1
@@deck = Array.new
@@playerScore = 0
@@playerHand = Array.new
@@dealerHand = Array.new
buildDeck
deal(2, @@playerHand)
deal(1, @@dealerHand)
@@playerScore = checkScore(@@playerHand)
else
@@deck = args[0]
@@playerHand = args[1]
@@dealerHand = args[2]
@@playerScore = checkScore(@@playerHand)
@@dealerScore = checkScore(@@dealerHand)
end
end
#ruby doesn't support multiple constructors...
def deck
@@deck
end
def playerScore
@@playerScore
end
def playerHand
@@playerHand
end
def dealerHand
@@dealerHand
end
def buildDeck()
cardName = ""
(1..52).each do |i|
if i % 13 === 1
cardName = "A"
elsif i % 13 === 10
cardName = "T"
elsif i % 13 === 11
cardName = "J"
elsif i % 13 === 12
cardName = "Q"
elsif i % 13 === 0
cardName = "K"
else
cardName = i % 13
end
if i <= 13
suit = "C"
elsif i <= 26
suit = "H"
elsif i <= 39
suit = "S"
else
suit = "D"
end
@@deck[i-1] = cardName.to_s + suit.to_s
end
end
def deal(numCards, hand)
(1..numCards).each do |i|
chosenCard = Random.rand(1..@@deck.length)
hand.push(@@deck[chosenCard])
@@deck.delete(chosenCard)
end
end
def hit(hand)
chosenCard = Random.rand(1..@@deck.length)
hand.push(@@deck[chosenCard])
@@deck.delete(chosenCard)
checkScore(hand)
end
def checkScore(hand)
score = 0;
hand.each do |i|
currentCard = i
currentValue = currentCard[0]
if currentValue === "T" || currentValue === "J" || currentValue === "Q" || currentValue === "K"
score = score + 10
elsif currentValue === "A"
var validAce = false
while (!validAce) do
puts "1 or 11?"
if gets.chomp === 11
score = score + 11
validAce = true
else if gets.chomp === 1
score = score + 1
validAce = true
else
puts "That wasn't right. Try again."
end
end
else
score = score + currentValue.to_i
end
end
return score
end
def gameLoop()
while @@playerScore <= 21
puts "Your hand: #{@@playerHand}"
puts "Your score: #{@@playerScore}"
puts "enter your move"
move = ""
move = gets.chomp
if (move == "hit")
hit(@@playerHand)
end
@@playerScore = checkScore(@@playerHand)
end
puts "you lost, or something broke lol"
end
end