# Evolutionary Genetics, Bioinformatics, HS 2020 # Blackjack function # Simona Ruffener # -------------------------------------------------------------------- blackjack <- function() { # One-player against the dealer # The player gets two cards and can decide whether to draw another or not as long as his/her toal points are below 21. # The dealer starts with one card and has to draw cards as long as his/her score is below 17. # Then the player's and the dealer's scores are compared, the higher one wins (if <= 21). # With a score over 21, the player/dealer is "busted" and thus loses. This leads to the end of the game. # A direct win either for the dealer or player is caused by a score of 21 points (blackjack). # The value of the drawn cards is indicated, aces count 11 or 1. ## Build a deck cards <- c(rep("two", 4), rep("three", 4), rep("four", 4), rep("five", 4), rep("six", 4), rep("seven", 4), rep("eight", 4),rep("nine", 4), rep("ten", 4), rep("jack", 4), rep("queen", 4), rep("king", 4), rep("ace", 4)) values <- c(rep(2, 4), rep(3, 4), rep(4, 4), rep(5, 4), rep(6, 4), rep(7, 4), rep(8, 4), rep(9, 4), rep(10, 16), rep(11, 4)) # create a data frame with all cards from the deck in one column and their values in another column df <- data.frame(cards, values) #-------------------------------------------------------------------- # Getting started #-------------------------------------------------------------------- # Define variable for two linebreaks lb <- c("\n", "\n") # Start the game by drawing two cards cat("Let me draw two cards for you. ", "\n") # two rows from the dataframe are randomly sampled without replacement playercards <- df[sample(nrow(df), 2, replace = FALSE),] # ??? # Delete the rows that were sampled (delete playercards from dataframe) # df <- df[which(!df %in% playercards),] # ??? cat("These are your cards and their values: ", lb) print(playercards, row.names = FALSE) # row names are omitted cat("\n") # If an ace was drawn, choose if it should count 1 or 11 points if (any(playercards == "ace")) { acecount <- readline("Should the ace count 1 or 11? (1/11) ") if (acecount == 1) playercards[playercards == 11] <- 1 } ## Count score sumplayer <- sum(playercards[, 2]) cat("Total points: ", sumplayer, lb) ## Score > 21, busted if (sumplayer > 21) { cat("You're busted!", lb) } ## Black-Jack (21 points) if (sumplayer == 21) { cat("Winner, winner, chicken dinner! Congratulations!", lb) } #--------------------------------------------------------------------- # Player's turn #--------------------------------------------------------------------- ## Does the player want to draw another card? answerplayer <- readline("Would you like another card? (yes/no) ") cat("\n") while (answerplayer == "yes" || answerplayer == "y") { # add another randomly sampled row from the dataframe (a card and it's value) to "playercards" playercards <- rbind(playercards, df[sample(nrow(df), 1),]) cat("These are your cards:", lb) print(playercards, row.names = FALSE) cat("\n") if (any(playercards == "ace")) { acecount <- readline("Should the ace count 1 or 11? (1/11) ") if (acecount == 1) playercards[playercards == 11] <- 1 } ## Count score sumplayer <- sum(playercards[, 2]) # sum up the playercard's second column (values) cat("Total points: ", sumplayer, lb) ## Busted if > 21 points if (sumplayer > 21) { cat("You're busted! Maybe next time...", lb) break #to exit the while loop } ## Win if = 21 points (blackjack) if (sumplayer == 21) { cat("Winner, winner, chicken dinner! You win!", lb) break } ## Does the player want another card (if score < 21)? if (sumplayer < 21) { answerplayer <- readline("Would you like another card? (yes/no) ") cat("\n") } } #--------------------------------------------------------------------- # Dealer's turn #--------------------------------------------------------------------- ## It's the dealer's turn if the player does not want any more cards or is busted. if (sumplayer < 21) { cat("Now it is my turn.", lb) dealercard <- df[sample(nrow(df), 1),] # draw first card for the dealer cat("My first card: ", lb) print(dealercard, row.names = FALSE) cat("\n") dealercard <- rbind(dealercard, df[sample(nrow(df), 1),]) cat("My next card: ", lb) print(dealercard, row.names = FALSE) cat(lb) ## Score count sumdealer <- sum(dealercard[, 2]) # If an ace was drawn and the dealer has > 21 points, the ace should only count 1 point if (any(dealercard == "ace")) { if (sumdealer > 21) dealercard[dealercard == 11] <- 1 } cat("Total points: ", sumdealer, lb) ## Draw more cards if score < 17 while (sumdealer < 17) { cat("My score is below 17, I'll draw another card.", lb) dealercard <- rbind(dealercard, df[sample(nrow(df), 1),]) cat("My next card: ", lb) print(dealercard, row.names = FALSE) cat("\n") sumdealer <- sum(dealercard[, 2]) cat("Total points: ", sumdealer, lb) } ## No more cards if the score is between > 17, score comparison if (sumdealer >= 17 && sumdealer <= 20) { cat("Enough cards for me. ", lb) cat("Your score: ", sumplayer, ", my score: ", sumdealer, lb) if (sumplayer > sumdealer) cat("Congratulations, you win!", lb) if (sumdealer > sumplayer) cat("I win, maybe you're lucky next time.", lb) } ## Blackjack if (sumdealer == 21) { cat("Winner, winner, chicken dinner! I win.", lb) } ## Busted if (sumdealer > 21) { cat("I'm busted. Congratulations, you win!", lb) } } }