-
Notifications
You must be signed in to change notification settings - Fork 0
/
TwentyOne.rb
124 lines (109 loc) · 2.27 KB
/
TwentyOne.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
class TwentyOne
require_relative "Card"
def initialize
end
def twentyOne
puts "Welcome to Twenty-One!"
$cards = 52
$total = 0
$player2total = 0
$deckObj = Deck.new
$deck = $deckObj.createCards()
$outOfDeck = Array.new
puts "Would you like 1 or 2 players?"
players = gets.chomp
if players == "1"
gameLoop()
end
if players == "2"
twoPlayer()
end
end
def gameLoop()
while $total <= 21
puts "\"hit\" and you be dealt a card. Type \"stop\" to stop."
cmd = gets.chomp
if cmd == "hit"
hit()
elsif cmd == "stop"
puts "Your total was " + $total.to_s
break
end
end
$dealerNum = 9 + rand(14)
if ($dealerNum > 21)
$dealerNum = 21
end
puts "The dealer totaled " + $dealerNum.to_s
if $total <= $dealerNum
puts "Sorry, the dealer beat you!"
elsif $total > $dealerNum and $total <= 21
puts "Congrats! You beat the dealer!"
end
end
def hit()
num = 1 + rand($cards)
currentCard = $deck[num]
currentCard.toString
$total = $total + currentCard.value.to_i
puts "Your total is " + $total.to_s
if $total == 21
puts "21! Congratulations!"
elsif $total > 21
puts "Sorry, you went over! Better luck next time!"
$p1stop = true
end
end
def hit2()
num = 1 + rand($cards)
currentCard = $deck[num]
currentCard.toString
$player2total = $player2total + currentCard.value.to_i
puts "Your total is " + $player2total.to_s
if $player2total == 21
puts "21! Congratulations!"
elsif $player2total > 21
puts "Sorry, you went over! Better luck next time!"
$p2stop = true
end
end
def twoPlayer
$p1stop = false
$p2stop = false
$gamestop = false
while $gamestop == false
while $p1stop == false and $total < 21
puts "Player 1, it's your turn."
cmd = gets.chomp
if cmd == "hit"
hit()
elsif cmd == "stop"
puts "Your total was " + $total.to_s
$p1stop = true
end
end
while $p2stop == false and $player2total < 21
puts "Player 2, it's your turn."
cmd = gets.chomp
if cmd == "hit"
hit2()
elsif cmd == "stop"
puts "Your total was " + $player2total.to_s
$p2stop == true
end
end
if $p1stop == true and $p2stop == true
$gamestop = true
end
end
print "\n Player 1's total was " + $total.to_s + " and Player 2's total was "
puts $player2total.to_s + "."
if $player2total > $total and $player2total <= 21
puts "Congrats Player 2, you win!"
elsif $total > $player2total and $total <= 21
puts "Congrats Player 1, you win!"
else
puts "No winner this time!"
end
end
end