-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlayingCards.R
59 lines (45 loc) · 1.13 KB
/
PlayingCards.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
shuffle <- function(deck) {
deck <- deck[sample(nrow(deck)),]
rownames(deck) <- 1:nrow(deck)
deck
}
generate_deck <- function(numberOfJokers = 0) {
suits <- c("C", "D", "H", "S")
deck <- data.frame(rep(1:13, 4), as.character(suits), stringsAsFactors=FALSE)
names(deck) <- c("value", "suit")
if (numberOfJokers > 0) {
for (i in 1:numberOfJokers) {
deck <- rbind(deck, c("0", "Joker"))
}
}
deck$suit <- as.factor(deck$suit)
deck
}
hand <- function(deck, size = 5) {
head(deck, size)
}
pokerrank <- function(cards) {
# based https://en.wikipedia.org/wiki/List_of_poker_hands
# currently ignoring jokers
values <- sort(h$value)
# check for empty hand
if (length(cards) <= 1) { return(9) }
# check for "Five of a kind"
for (i in 2:values) {
if (values[i] != values[i-1]) {
break
}
return(0)
}
# check for "Straight flush"
# check for "Four of a kind"
# check for "Full House"
# check for "Flush"
# check for "Straight"
# check for "Three of a kind"
# check for "Two pair"
# check for "One pair"
for (i in 2:values) {
if (values[i] == values[i-1]) { return(8) }
}
}