-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblackjack_advisor.rb
129 lines (117 loc) · 5.81 KB
/
blackjack_advisor.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
def card_value
value = ""
until (1..11).include?(value.to_i)
value = gets.chomp
if (1..10).include?(value.to_i)
value.to_i
elsif ["k", "q", "j"].include?(value.downcase)
value = 10
elsif value.downcase == "a"
value = 11
else
print "Not a valid card, please try again: "
end
end
return value
end
def twentyplus(hand_total)
if hand_total == 20 || hand_total == 21
return "stand"
else
return "split"
end
end
def check_soft(hand_total, dealer_card, soft)
return soft[hand_total][dealer_card]
end
def check_pair(hand_total, dealer_card, pair)
return pair[hand_total][dealer_card]
end
def bypass_hash(hand_total)
if hand_total < 8
return "hit"
elsif hand_total > 16
return "stand"
end
end
def check_hard(hand_total, dealer_card, hard)
return hard[hand_total][dealer_card]
end
soft = {13 => {2 => "hit", 3 => "hit", 4 => "double or hit",
5 => "double or hit", 6 => "double or hit", 7 => "hit", 8 => "hit",
9 => "hit", 10 => "hit", 11 => "hit"},
14 => {2 => "hit", 3 => "hit", 4 => "double or hit",
5 => "double or hit", 6 => "double or hit", 7 => "hit", 8 => "hit",
9 => "hit", 10 => "hit", 11 => "hit"},
15 => {2 => "hit", 3 => "hit", 4 => "double or hit",
5 => "double or hit", 6 => "double or hit", 7 => "hit", 8 => "hit",
9 => "hit", 10 => "hit", 11 => "hit"},
16 => {2 => "hit", 3 => "hit", 4 => "double or hit",
5 => "double or hit", 6 => "double or hit", 7 => "hit", 8 => "hit",
9 => "hit", 10 => "hit", 11 => "hit"},
17 => {2 => "double or hit", 3 => "double or hit", 4 => "double or hit",
5 => "double or hit", 6 => "double or hit", 7 => "hit", 8 => "hit",
9 => "hit", 10 => "hit", 11 => "hit"},
18 => {2 => "stand", 3 => "double or stand", 4 => "double or stand",
5 => "double or stand", 6 => "double or stand", 7 => "stand",
8 => "stand", 9 => "hit", 10 => "hit", 11 => "stand"},
19 => {2 => "stand", 3 => "stand", 4 => "double or stand",
5 => "double or stand", 6 => "double or stand", 7 => "stand",
8 => "stand", 9 => "hit", 10 => "hit", 11 => "stand"}}
pair = {4 => {2 => "split", 3 => "split", 4 => "split", 5 => "split",
6 => "split", 7 => "split", 8 => "hit", 9 => "hit", 10 => "hit", 11 => "hit"},
6 => {2 => "split", 3 => "split", 4 => "split", 5 => "split",
6 => "split", 7 => "split", 8 => "split", 9 => "hit", 10 => "hit", 11 => "hit"},
8 => {2 => "hit", 3 => "hit", 4 => "split", 5 => "split",
6 => "split", 7 => "hit", 8 => "hit", 9 => "hit", 10 => "hit", 11 => "hit"},
10 => {2 => "double or hit", 3 => "double or hit", 4 => "double or hit",
5 => "double or hit", 6 => "double or hit", 7 => "double or hit",
8 => "double or hit", 9 => "double or hit", 10 => "hit", 11 => "hit"},
12 => {2 => "split", 3 => "split", 4 => "split", 5 => "split",
6 => "split", 7 => "split", 8 => "hit", 9 => "hit", 10 => "hit", 11 => "hit"},
14 => {2 => "split", 3 => "split", 4 => "split", 5 => "split",
6 => "split", 7 => "split", 8 => "split", 9 => "hit", 10 => "stand", 11 => "hit"},
16 => {2 => "split", 3 => "split", 4 => "split", 5 => "split",
6 => "split", 7 => "split", 8 => "split", 9 => "split", 10 => "split", 11 => "split"},
18 => {2 => "split", 3 => "split", 4 => "split", 5 => "split",
6 => "split", 7 => "stand", 8 => "split", 9 => "split", 10 => "stand", 11 => "stand"}}
hard = {8 => {2 => "hit", 3 => "hit", 4 => "hit", 5 => "double or hit",
6 => "double or hit", 7 => "hit", 8 => "hit", 9 => "hit", 10 => "hit", 11 => "hit"},
9 => {2 => "double or hit", 3 => "double or hit", 4 => "double or hit",
5 => "double or hit", 6 => "double or hit", 7 => "hit", 8 => "hit",
9 => "hit", 10 => "hit", 11 => "hit"},
10 => {2 => "double or hit", 3 => "double or hit", 4 => "double or hit",
5 => "double or hit", 6 => "double or hit", 7 => "double or hit",
8 => "double or hit", 9 => "double or hit", 10 => "hit", 11 => "hit"},
11 => {2 => "double or hit", 3 => "double or hit", 4 => "double or hit",
5 => "double or hit", 6 => "double or hit", 7 => "double or hit",
8 => "double or hit", 9 => "double or hit", 10 => "double or hit", 11 => "double or hit"},
12 => {2 => "hit", 3 => "hit", 4 => "stand", 5 => "stand", 6 => "stand",
7 => "hit", 8 => "hit", 9 => "hit", 10 => "hit", 11 => "hit"},
13 => {2 => "stand", 3 => "stand", 4 => "stand", 5 => "stand", 6 => "stand",
7 => "hit", 8 => "hit", 9 => "hit", 10 => "hit", 11 => "hit"},
14 => {2 => "stand", 3 => "stand", 4 => "stand", 5 => "stand", 6 => "stand",
7 => "hit", 8 => "hit", 9 => "hit", 10 => "hit", 11 => "hit"},
15 => {2 => "stand", 3 => "stand", 4 => "stand", 5 => "stand", 6 => "stand",
7 => "hit", 8 => "hit", 9 => "hit", 10 => "hit", 11 => "hit"},
16 => {2 => "stand", 3 => "stand", 4 => "stand", 5 => "stand", 6 => "stand",
7 => "hit", 8 => "hit", 9 => "hit", 10 => "hit", 11 => "hit"}}
print "Please enter your first card: "
first_card = card_value.to_i
print "Please enter your second card: "
second_card = card_value.to_i
print "Please enter the dealer's card: "
dealer_card = card_value.to_i
hand_total = first_card + second_card
if hand_total > 19
move = twentyplus(hand_total)
elsif first_card == 11 || second_card ==11
move = check_soft(hand_total, dealer_card, soft)
elsif first_card == second_card
move = check_pair(hand_total, dealer_card, pair)
elsif hand_total < 8 || hand_total > 16
move = bypass_hash(hand_total)
else
move = check_hard(hand_total, dealer_card, hard)
end
puts "Your optimal move is #{move}."