Blackjack!

Brad Cupp
2014

Formatting Functions

printf <- function(...) {
  cat(sprintf(...))
}

clear <- function() {
  cat("\014")
}

Suits and Ranks

require("Unicode")

suits <- c("U+2661","U+2663","U+2662","U+2660")
suits <- as.u_char(suits)
suits <- intToUtf8(suits)
ranks <- c("2","3","4","5","6",
           "7","8","9","10",
           "J","Q","K","A")

The `rankNum` vector records a point value for each rank

rankNum <- c(2:10,10,10,10,11)

Variables

p1 <- NULL
cpu <- NULL
deck <- NULL
pot <- 1000
bet <- 5

Function stock

stock <- function() {
  cards <- c()
  for (i in 1:4) {
    for (rank in ranks) {
      suit <- substring(suits,i,i,sep="")
      card <- paste(rank,suit)
      cards <- append(cards, card)
    }
  }
  cards <- sample(cards)
  eval.parent(substitute(deck<-cards))
}

Function report

report <- function() {
  clear()
  printf("Pot: $%d\tBet:$%d\n",pot,bet)
  cat(rep("-",30),"\n")
  printf("Dealer:\n\t")
  printf(cpu)
  printf("\nYou:\n\t")
  printf(p1)
  printf("\n")
}

Function deal

deal <- function() {
  if (length(deck) < 3) {
    stock()
  }
  printf("The bet is $%d.\n\n", bet)
  p <- pot - bet
  eval.parent(substitute(pot<-p))
  cards <- deck
  hand <- sample(cards,1)
  cards <- cards[!cards %in% hand]
  hand2 <- sample(cards,2)
  cards <- cards[!cards %in% hand2]
  eval.parent(substitute(deck<-cards))
  eval.parent(substitute(cpu<-hand))
  eval.parent(substitute(p1<-hand2))
  report()
}

Function value

ranks <- c("2","3","4","5","6",
           "7","8","9","10",
           "J","Q","K","A")

rankNum <- c(2:10,10,10,10,11)


value <- function(card) {
  rankStr <- substring(card,1,nchar(card)-1)
  return(rankNum[match(rankStr,ranks)])
}

Function score

score <- function(cards) {
  rank <- substring(cards,1,1)
  noAces <- cards[rank != "A"]
  aces <- cards[rank == "A"]
  x <- sum(value(noAces)) + length(aces) + 10
  if (x > 21) {
    x <- x - 10
  }
  return(x)
}

Function hit

hit <- function() {
  if (length(deck)==0) {
    stock()
  }
  cards <- deck
  hitCard <- sample(cards,1)
  cards <- cards[!cards %in% hitCard]
  playerHand <- append(p1,hitCard)
  eval.parent(substitute(deck<-cards))
  eval.parent(substitute(p1<-playerHand))
  report()
  if (score(phand) > 21) {
    printf("\nBust! You lose $%d.\n", bet)
  }
}

Function stand

stand <- function() {
  cards <- deck
  printf("You stand with %d.\n", score(p1))
  if (length(cards) < 5) {
    stock()
  }
  card <- sample(cards,1)
  cards <- cards[!cards %in% card]
  cpuHand <- append(cpu,card)
  eval.parent(substitute(cpu<-cpuHand))
  report()

Continued on next slide

Function stand (cont.)

  while (score(cpuHand) < 17) {
    readline("\nDealer must hit. Press enter...")
    card <- sample(cards,1)
    cards <- cards[!cards %in% card]
    cpuHand <- append(cpuHand,card)
    eval.parent(substitute(cpu<-cpuHand))
    report()
  }

Continued on next slide

Function stand (cont.)

  if (score(cpuHand) > 21) {
    p <- pot + bet + bet
    eval.parent(substitute(pot<-p))
    report()
    printf("\nBust! You win $%d.\n", bet)
  } else if (score(cpuHand) == score(p1)) {
    p <- pot + bet
    eval.parent(substitute(pot<-p))
    report()
    printf("\nPush!\n")
  } else if (score(cpuHand) > score(p1)) {
    printf("\nYou lose $%d.\n", bet)
  } else {
    p <- pot + bet + bet
    eval.parent(substitute(pot<-p))
    report()
    printf("\nYou win $%d!\n", bet)
  }

Function stand (concluded)

  eval.parent(substitute(deck<-cards))
  eval.parent(substitute(cpu<-NULL))
  eval.parent(substitute(p1<-NULL))
}

Set up the game

stock()
clear()