-
Notifications
You must be signed in to change notification settings - Fork 0
/
Blackjack.R
124 lines (99 loc) · 3.68 KB
/
Blackjack.R
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
d.value <- c("A", "A", "A", "A", 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 9, 9, 9, 9, 8, 8, 8, 8, 7, 7, 7, 7, 6, 6, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 3, 3, 3, 3, 2, 2, 2, 2)
d.type <- c("A", "A", "A", "A", "K", "K", "K", "K", "Q", "Q", "Q", "Q", "J", "J", "J", "J", 10, 10, 10, 10, 9, 9, 9, 9, 8, 8, 8, 8, 7, 7, 7, 7, 6, 6, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 3, 3, 3, 3, 2, 2, 2, 2)
test <- c(11, 11, 11, 11, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 9, 9, 9, 9, 8, 8, 8, 8, 7, 7, 7, 7, 6, 6, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 3, 3, 3, 3, 2, 2, 2, 2)
df.cards <- data.frame(d.value, d.type)
function.test.value <- 18
#USER: Player k = 1
blackjack <- function(n){
n <- sample(test, 1)
while(n <= 11){
ifelse(n < 11, n <- n + sample(test, 1), n <- n)
}
while(n <= 14){
ifelse(n <= 14, n <- n + sample(test, 1), n <- n)
}
while(n <= 15){
ifelse(n <= 15, n <- n + sample(test, 1), n <- n)
}
ifelse(n >= 18, n <- n, n + sample(test, 1))
ifelse(n > 20, stop("You bust!", call. = FALSE), n <- n)
}
blackjack()
#--------------------------------------------------------------------------------------------------------------------
#DEALER: Player k = 2
test.blackjack <- function(k){
k <- sample(test, 1)
ifelse(k < 11, k <- k + sample(test, 1) & ifelse(exists("A", k), "A" <- 11, k <- k), k <- k)
ifelse(k <= 15, k <- k + sample(test, 1), k <- k)
ifelse(k >= 17, k <- k & ifelse(exists("A", k), "A" <- 1, k <- k), k + sample(test, 1))
ifelse(k > 20, stop("You bust!", call. = FALSE), k <- k)
}
test.blackjack()
Maggie [11:27 AM]
hand <- function(){
card1 <- sample(numCard,size = 1)
numCard <<- numCard[-match(card1,numCard)]
print(card1)
card2 <- sample(numCard,size = 1)
numCard <<- numCard[-match(card2,numCard)]
print(card2)
if(card1 == "A" && card2 == "A"){
card1 <- 1
card2 <- 11
}else if(card1 == "A" && card2 != "A"){
card1 = 11
}else if(card1 != "A" && card2 == "A"){
card2 = 11
}
Score <- as.numeric(card1) + as.numeric(card2)
return(Score)
}
hand()
hand
blackjack2 <- function(){
SetAmountofDecks <- function(deckAmount){
if(is.numeric(deckAmount) == FALSE){
stop("The deck amount needs to be an numerical entry!")
}
deckOfCards <- c('A','A','A','A','2','2','2','2','3','3','3','3','4','4','4','4','5','5','5','5','6','6','6','6',
'7','7','7','7','8','8','8','8','9','9','9','9','10','10','10','10','J','J','J','J',
'Q','Q','Q','Q','K','K','K','K')
tDeckAmount <- rep(deckOfCards, deckAmount)
return(tDeckAmount)
}
hand <- function(){
card1 <- sample(numCard,size = 1)
numCard <<- numCard[-match(card1,numCard)]
print(card1)
card2 <- sample(numCard,size = 1)
numCard <<- numCard[-match(card2,numCard)]
print(card2)
if(card1 == "A" && card2 == "A"){
card1 <- 1
card2 <- 11
}else if(card1 == "A" && card2 != "A"){
card1 = 11
}else if(card1 != "A" && card2 == "A"){
card2 = 11
ranchance <- sample(0:1, 1)
if (ranchance == 1){
shuffle <- df.deck[sample(1:nrow(df.deck)), ]
}
}
Score <- as.numeric(card1) + as.numeric(card2)
return(Score)
}
n <- sample(test, 1)
while(n <= 11){
ifelse(n < 11, n <- n + sample(test, 1), n <- n)
}
while(n <= 14){
ifelse(n <= 14, n <- n + sample(test, 1), n <- n)
}
while(n <= 15){
ifelse(n <= 15, n <- n + sample(test, 1), n <- n)
}
ifelse(n >= 18, n <- n, n + sample(test, 1))
ifelse(n > 20, stop("You bust!", call. = FALSE), n <- n)
}
blackjack2()