Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Q.1 Write python code to implement the following scenarios. Kindly answer both q

ID: 3834338 • Letter: Q

Question

Q.1 Write python code to implement the following scenarios. Kindly answer both questions. Thanks

a) Poker Hand Ranking

Write a code to accept user input for a poker hand (5 cards). Then determine what rank the hand has used the table in the slides.

Make sure you have at least one function in the code!

b) Poker Hand with random dealing

Using your existing poker hand code, use random numbers to generate the cards instead of the user asking for input.

Check to make sure you didn't deal the same card twice before checking the rank of the hand!

Explanation / Answer

#Ryan's video poker simulator.
#January 2012

library(ggplot2)

##########################
# UTILITY FUNCTIONS
##########################
#Converts a vector of 0s and 1s into binary, and into an integer.
#Used to compute a straight.
bin2dec <- function(x) {
if (all(x %in% c(0,1))) { sum(x * 2^(rev(seq_along(x)) - 1)) }
else { FALSE }
}

#################################
# WIN-CHECKING UTILITY FUNCTIONS
#################################
#Most of these functions are not called in my simulation because
#than hands are not possible.
is.pair.job <- function(h) {
#Row sum is greater than 1 for any row 10 or larger.
any(rowSums(h)[10:13] > 1)
}

is.two.pair <- function(h) {
   #Two or more rows have rowSums greater than 1.
   length(which(rowSums(h) > 1)) > 1
}

is.flush <- function(h) {
   #A column with sum 5.
   any(colSums(h) == 5)
}

is.straight <- function(h) {
int <- bin2dec(rowSums(h))
if (!int)
{
FALSE
}
else {
   (bin2dec(rowSums(h)) / 31) %% 2 == 0 || bin2dec(rowSums(h)) == 7681
}
}

is.three.of.kind <- function(h) {
   any(rowSums(h) == 3)
}

is.full.house <- function(h) {
   any(rowSums(h) == 3) && any(rowSums(h) == 2)
}

is.four.of.kind <- function(h) {
   any(rowSums(h) == 4)
}

check.wins <- function(hand, wins) {
highest.win <- 0   #By default, no win.
straight <- flush <- straight.flush <- FALSE
   #Jacks or better?
   if (is.pair.job(hand)) { highest.win <- 1 }
   #Two pair?
   if (is.two.pair(hand)) { highest.win <- 2 }
   #Three of a kind?
   if (is.three.of.kind(hand)) { highest.win <- 3 }
   #Straight?
   if (is.straight(hand)) {
       highest.win <- 4
       straight <- TRUE
   }
   #Flush?
   if (is.flush(hand)) {
       highest.win <- 5
       flush <- TRUE
   }
   #Full house?
   if (is.full.house(hand)) { highest.win <- 6 }
   #Four of a kind?
   if (is.four.of.kind(hand)) { highest.win <- 7 }
   #Straight flush?
   if (straight && flush) {
highest.win <- 8
straight.flush <- TRUE
}
   #Royal flush
   if (straight.flush) {
#If the highest card is an ace, Royal Flush.
if (all(rowSums(hand[10:13]) == 1)) {
highest.win <- 9
}
}
if (highest.win > 0) {
   wins[highest.win] <- wins[highest.win] + 1
}
   wins
}

##########################
# GAME CONTROL FUNCTIONS
##########################
#Deal the first hand, consisting of a pair of low cards and one face card.
first.hand <- function() {
#Choose the numerical denomination for the pair.
low.denom <- sample(2:10, 1)
#Choose the suit for the numerical pair.
low.suit <- sample(suits, size=2, replace=FALSE)
   #Choose a face card.
   face.denom <- sample(11:14, 1)
#Choose the suit for the face card.
face.suit <- sample(suits, size=1)
   #Choose the remaining cards. CANNOT be low.denom OR face.denom. CANNOT have any other pairs.
   misc.denoms <- sample(seq(2, 14)[-c(face.denom-1, low.denom-1)], 2)
misc.suits <- sample(suits, size=2, replace=TRUE)
#Arrange all cards into a hand.
   card.denoms <- c(low.denom, low.denom, face.denom, misc.denoms)
card.suits <- c(low.suit, face.suit, misc.suits)
#Now shuffle the order of this hand, keeping denoms and suits together.
#This may not be necessary.
indices <- sample(seq(1,5))
card.denoms <- card.denoms[indices] #Ranges from 2 to 14
card.suits <- card.suits[indices] #Ranges from 1 to 4.
   deck[list(13*(card.suits-1)+card.denoms-1)[[1]]] <- TRUE #Any better way to do this?
   deck
}

hold.cards <- function(strategy, deck) {
   #Get the index of the held pair of low cards.
   row.of.low.pair <- which(rowSums(deck) == 2)
held <- which(deck & row(deck) == row.of.low.pair)
   if (strategy == 2) {
       #Pick a dealt face card at random:
#Identify the face cards.
       faces.dealt <- intersect(which(deck), which(row(deck) >= 10))
#Sample one of them to hold.
       face.held <- sample(faces.dealt, 1)
       held <- c(held, face.held)
   }
   held
}

deal <- function(d, h) {
   new.cards <- sample(which(!d), size=(5 - length(h)), replace=FALSE)
   #Before marking these new cards in the deck, remove the mark for thrown away cards.
   thrown.away <- setdiff(which(d), h)
   d[thrown.away] <- FALSE
   d[new.cards] <- TRUE
   d
}

play <- function(strategy) {
   wins <- c(jacks.or.better=0, two.pair=0, three.of.kind=0, straight=0, flush=0, full.house=0, four.of.kind=0,
       straight.flush=0, royal.flush=0)
   start <- first.hand()
   held.cards <- hold.cards(strategy, start)
   resulting.hand <- deal(start, held.cards)
   wins <- check.wins(resulting.hand, wins)
wins
}

##########################
# SIMULATION
##########################

denoms <- seq(2, 14)
suits <- seq(1, 4)
deck <- matrix(rep(FALSE, 4*13), nrow=13, ncol=4)
colnames(deck) <- c("Heart", "Diamond", "Spade", "Club")
row.names(deck) <- c("2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A")

runsim <- function(N, strategy) {
k <- 0
cum.wins <- list(jacks.or.better=0, two.pair=0, three.of.kind=0, straight=0, flush=0, full.house=0, four.of.kind=0,
       straight.flush=0, royal.flush=0)
repeat {
wins <- play(strategy)
cum.wins <- mapply("+", cum.wins, wins)
ifelse(k > N, break, k <- k + 1)
}
cum.wins
}

#Graphics and table
games <- 1000
strat1 <- vector(length=games)
strat2 <- vector(length=games)
for (i in 1:games) {
strat1[i] <- sum(as.vector(runsim(games, 1))/games)
strat2[i] <- sum(as.vector(runsim(games, 2))/games)
}
res.df <- data.frame(proportion=c(strat1, strat2), strategy=factor(c(rep(1,games),rep(2,games))))
ggplot(res.df) + aes(proportion,fill=strategy) + geom_density(alpha=0.4) + xlab("Proportion")+ylab("Smoothed Count")