Blackjack Race

By Degausser on Jan 17, 2017

Blackjack Race is a channel based multiplayer game, supporting up to your standard 7 players.

I saw a few versions of Blackjack on this site, but none of them were scripted well, whether it be the the style of code, the game pace or the the logic within the code to match the game. So here you go :)

Features include:

  • Simultaneous games within multiple channels at the same time
  • A "first to" style, meaning rather than betting, you're trying to accumulate a certain amount of coins to win
  • Derived from the real casino game, but adapted to suit IRC
  • A real shoe, with shuffling points when shoe density falls below a threshold
  • Accurate dealer simulation, standing with hard 17.
  • Strategy play options: !hit !stand !double !split !surrender
  • Resplit as many times as you require! 10 seconds are added to the timer for each "layer" of hands all players go through
  • As an example, Jerry splits twice, and Paul splits 4 times. The most is 4, so there will be an additional 40 seconds in the round
  • AI opponents, with an intelligence range from 1 to 10 whom apply strategy
  • Automatic AI participation if only 1 player joins
  • AI vs AI simulation if conditions require this to start a game, or no human players join
  • Easily configure number of decks, awarded coins and AI within the script
  • No messy global variables or external files

Image

To start a game, type !blackjack
To join a game, type !join
To start a game with AI opponents ready to join, type !blackjack N, where N is a number from 1 to 7.
To start a game with specific AI opponents, for example Jess and Kyle, type !blackjack Jess Kyle
To leave a game type !replace. This will replace you with an AI opponent
To view a list of AI opponents and their intelligence level, type !ai
To forcefully stop a game, type !blackjack stop (Operators only)

  • UPDATE 20/01/17 - Added AI, primarily so you can start a game by yourself
  • UPDATE 21/01/17 - Added !split and !surrender, for completions sake. Integration was a bit rough but i scraped by
  • UPDATE 22/01/17 - Fixed the randomization of cards in the shoe. Added !replace
  • UPDATE 24/01/17 - Fixed small conflicting error, added an easter egg
;########################################################
;########################################################
; SETTINGS (8 decks max)
;########################################################
;########################################################

; THE DEALER HAS AN EDGE. If you win as much as you lose, all player balances will gradually decrease making the 'finish' impossible to reach.
; to compensate, you can award points for draws, and reward more money for a win than you take away for a loss. The same applies for doubles.
; for faster games: 200 40 20 30 0 -10 5 10 -3 1-8
; more realistic game values: 150 40 20 30 -10 -25 0 0 -5 1-8
; AI opponents. Just seperate the names by spaces, if you want to add more.
; The intelligence levels correlate to the names, so Jess matches 6 and Kyle matches the last 1.

alias -l finish return 150
alias -l winDouble return 40
alias -l winSingle return 20
alias -l winBlackjack return 30
alias -l loseSingle return -10
alias -l loseDouble return -25
alias -l drawSingle return 0
alias -l drawDouble return 0
alias -l surrender return -5
alias -l decks return 2

alias -l ai return Jess Trump DeathKnight AIMaster Jerry Lacey Jonorama Degausser NotTheSun Kyle
alias -l aiIntel return 6 9 8 10 3 5 4 7 2 1

;########################################################
;########################################################
; TEXT EVENTS
;########################################################
;########################################################

on *:TEXT:*:#:{
  if ($hget($g($chan,1))) {
    if ($hget($g($chan,1), $nick)) && ($hget($g($chan,1), $nick) != check) && (!$isAI($chan, $nick)) {
      if ($timer($+(BlackJack,$chan,DrawTimer))) {
        if ($1 == !hit) || ($1 == !card) drawCardAction $chan $nick
        elseif ($1 == !double) drawCardAction $chan $nick -d
        elseif ($1 == !stand) standAction $chan $nick
        elseif ($1 == !split) splitAction $chan $nick
        elseif ($1 == !surrender) surrenderAction $chan $nick
      }
      if ($1 == !replace) || ($1 == !drop) replaceAction $chan $nick   
    }
    elseif ($1 == !join) && ($timer($+(BlackJack,$chan,JoinTimer))) addPlayer $chan $nick
    if ($1-2 == !blackjack stop) && ($nick isop $chan) triggerBlackjackStop
    elseif ($1-2 == !blackjack stop) .notice $nick You must be an operator to stop a game!
    elseif ($1 == !abort) && ($hget($s($chan), stop)) triggerAbort $1-
  }
  elseif ($1 == !blackjack) triggerBlackjack $1-
  if ($1 == !ai) .notice $nick List of computer opponents: $listAI
}

;########################################################
;########################################################
; LITTLE ALIASES
;########################################################
;########################################################

alias -l suits return ♣♠♥♦
alias -l cardify return $replace($1-,10,E,11,J,12,Q,13,K,1,A,E,10)
alias -l bracks return $+($chr(40),$1-,$chr(41))
alias -l cb1 return $+(07,$1-,)
alias -l cb2 return $+(03,$1-,)
alias -l cb3 return $+(06,$1-,)
alias -l c1 return $+(07,$1-,,)
alias -l c2 return $+(03,$1-,,)
alias -l c3 return $+(06,$1-,,)
alias -l g return $+(bj_game,_,$1,_,$2)
alias -l a return $+(bj_action,_,$1,_,$2)
alias -l d return $+(bj_deck,_,$1)
alias -l w return $+(bj_score,_,$1)
alias -l s return $+(bj_settings,_,$1)

alias -l hfreedom {
  if ($hget($g($1,1))) hfree $g($1,1)
  if ($hget($a($1,1))) hfree $a($1,1)
  if ($hget($d($1))) hfree $d($1)
  if ($hget($w($1))) hfree $w($1)
  if ($hget($s($1))) hfree $s($1)
  hfreedomMirror $1
}

alias -l hfreedomMirror {
  var %x = 2
  while ($hget($g($1,%x))) {
    hfree $hget($g($1,%x))
    hfree $hget($a($1,%x))
    inc %x
  }
}

;########################################################
;########################################################
; GAME TRIGGERS
;########################################################
;########################################################

alias triggerBlackjack {
  if (!$2) blackjackStart $chan $nick
  elseif ($istok(1.2.3.4.5.6.7,$2,46)) blackjackStart $chan $nick $2
  elseif ($filterAI($2-)) blackjackStart $chan $nick $ifmatch
  else {
    .notice $nick Invalid parameter. To start a game with AI opponents ready to join, type !blackjack N. N can be a number from 1-7
    .notice $nick You can also specify certain AI opponents you want in the game, for example: !blackjack AIMaster Degausser Trump
  }
}

alias triggerBlackjackStop {
  if ($cardsRemaining($chan)) {
    if (!$hget($s($chan), stop)) stopAction $chan $nick
    else .notice $nick The game will end after the current round!
  }
  elseif ($timer($+(BlackJack,$chan,JoinTimer))) {
    $+(.timerBlackJack,$chan,JoinTimer) off
    msg $chan $cb3(Blackjack Race) has been stopped by $c3($nick)
    blackjackStop $chan -f
  }
  else .notice $nick You can't perform this action now!
}

alias -l triggerAbort {
  if ($timer($+(BlackJack,$chan,DrawTimer))) && ($nick isop $chan) abortAction $chan $nick
  elseif ($nick !isop $chan) .notice $nick You must be an operator to abort the games termination!
  else .notice $nick You can't perform that action now
}

;########################################################
;########################################################
; ACTIONS
;########################################################
;########################################################

alias -l drawCardAction {
  if ($numerifyTotal($cardTotal($1,-u,$2).hard) < 21) && (!$istok(double.stand, $hget($a($1,1), $2), 46)) {
    var %d = $iif(d isin $3,$true,$false)
    if (%d) && ($numtok($hget($g($1,1), $2), 32) > 2) .notice $nick You can only double with the first two cards!
    else {
      var %card = $drawCard($1)
      hadd $g($1,1) $2 $hget($g($1,1), $2) %card
      if ($numerifyTotal($cardTotal($1,-u,$2).hard) > 20) && (!%d) hadd $a($1,1) $2 stand
      else hadd $a($1,1) $2 $iif(%d,double,hit)
      msg $1 $c2($2) $iif(%d,doubled,hit) and drew: $cardflair(%card) :: Current hand: $cardflair($hget($g($1,1), $2)) $bracks($cardTotal($1,-u,$2))
      if ((%d) || ($numerifyTotal($cardTotal($1,-u,$2).hard) > 20)) {
        hinc $s($1) completed 1
        if ($splitHand($1,$2)) createSplits $1 $2 $ifmatch
        elseif ($actionsComplete($1)) checkWinner $1 -f
      }
    }
  }
  else .notice $2 You can't draw anymore cards!
}

alias -l standAction {
  if ($numerifyTotal($cardTotal($1,-u,$2).hard) < 21) && (!$istok(double.stand, $hget($a($1,1), $2), 46)) {
    hadd $a($1,1) $2 stand
    hinc $s($1) completed 1
    msg $1 $c2($2) stands with the hand: $cardflair($hget($g($1,1), $2)) $bracks($cardTotal($1,-u,$2))
    if ($splitHand($1,$2)) createSplits $1 $2 $ifmatch
    elseif ($actionsComplete($1)) checkWinner $1 -f
  }
  else .notice $2 You can't perform this action!
}

alias -l splitAction {
  if ($cardPair($hget($g($1,1), $2))) && (!$istok(double.stand, $hget($a($1,1), $2), 46)) {
    msg $1 $c2($2) splits the hand: $cardflair($hget($g($1,1), $2)) $bracks($cardTotal($1,-u,$2))
    hadd $newSplitTable($1, $2) $2 $gettok($hget($g($1,1), $2),2,32)
    hadd $g($1,1) $2 $gettok($hget($g($1,1), $2),1,32)
    hinc $s($1) actions 1
    createSplits $1 $2
  }
  else .notice $2 You can't perform this action!
}

alias -l surrenderAction {
  if ($numtok($hget($g($1,1), $2), 32) == 2) && ($numerifyTotal($cardTotal($1,-u,$2).hard) < 21) && (!$hget($m($1,1),$2)) && (!$istok(double.stand, $hget($a($1,1), $2), 46)) {
    msg $1 $c2($2) surrenders their hand: $cardflair($hget($g($1,1), $2)) $bracks($cardTotal($1,-u,$2)) and $c1($iif($surrender > 0,wins,loses) $abs($surrender) $iif($abs($surrender) == 1,coin,coins))
    hdel $a($1,1) $2
    hadd $g($1,1) $2 check
    hinc $w($1) $2 $surrender
    hinc $s($1) completed 1
    if ($actionsComplete($1)) checkWinner $1 -f
  }
  else .notice $2 You can't perform this action!
}

alias -l replaceAction {
  if (!$istok($hget($s($1),replace),$nick,32)) {
    hadd $s($1) replace $hget($s($1), replace) $2
    msg $1 $cb3($2) $c3(will be replaced after the next round!)
  }
}

alias -l stopAction {
  hadd $s($1) stop $2
  msg $1 $cb3(Blackjack Race) $c3(will be stopped after the next round!) Type !abort to cancel
}

alias -l abortAction {
  if ($hget($s($1),stop)) hdel $s($1) stop
  msg $1 $cb3(Blackjack Race) $c3(will no longer be stopped after this round!)
}

alias -l actionsComplete {
  if ($hget($s($1), completed) == $hget($s($1), actions)) return $true
  return $false
}

;########################################################
;########################################################
; SPLIT TABLES
;########################################################
;########################################################

alias -l newSplitTable {
  var %x = 2
  while (%x) {
    if (!$hget($g($1, %x))) {
      hmake $g($1, %x) 1
      hmake $a($1, %x) 1
      $+(.timerBlackJack,$1,DrawTimer) -o 1 $calc($timer($+(BlackJack,$1,DrawTimer)).secs + 10) checkWinner $1
    }
    if (!$hget($g($1, %x), $2)) return $g($1,%x)
    inc %x
  }
}

alias -l splitHand {
  var %x = 2
  while ($hget($g($1,%x),$2)) {
    if ($numtok($ifmatch,32) == 1) return %x
    inc %x
  }
  return $false
}

;########################################################
;########################################################
; ARTIFICIAL INTELLIGENCE
;########################################################
;########################################################

alias -l getIntel return $iif($findtok($ai,$1,1,32) <= $numtok($aiIntel,32),$iif($gettok($aiIntel,$findtok($ai,$1,1,32),32) isnum 1-10, $v1, 5),5)
alias -l isAI return $istok($hget($s($1), ai),$2,32)
alias -l timeAI $+(.timerBlackJack,$1,DrawTimer,$2) -o 1 $rand(2,7) performAIAction $1 $2

alias -l listAI {
  var %x = 1, %y = $numtok($ai,32), %r, %u
  while (%x <= %y) {
    %u = $gettok($ai,%x,32)
    %r = %r $c2(%u) $c1($bracks($getIntel(%u))) /
    inc %x
  }
  return $left(%r,-1)
}

alias -l filterAI {
  var %x = 1, %y = $numtok($1-,32), %return, %a
  while (%x <= %y) {
    %a = $gettok($1-,%x,32)
    if ($istok($ai,%a,32)) && (!$istok(%return,%a,32)) %return = $addtok(%return,%a,32)
    inc %x
  }
  return %return
}

alias -l generateAI {
  var %x = 1, %y = $numtok($ai,32), %s, %u, %n = $iif($2- isnum 1-7,$iif($2- <= $numtok($ai,32),$2-,$numtok($ai,32)),$numtok($2-,32))
  while (%x <= %n) {
    if ($2- isnum 1-7) {
      while ($istok(%s,%u,32) || !%u) {
        %u = $gettok($ai,$rand(1,$numtok($ai,32)),32)
      }
      %s = %s %u
    }
    elseif ($istok($ai,$gettok($2-,%x,32),32)) %u = $gettok($2-,%x,32)
    else %u = $null
    if (%u) $+(.timerBlackJack,$1,JoinTimer,%u) -o 1 $rand(2,28) addPlayer $1 %u -a
    inc %x
  }
}

alias -l rollAI {
  var %x = 1, %ai = $hget($s($1), ai), %y = $numtok(%ai, 32), %player
  while (%x <= %y) {
    %player = $gettok(%ai,%x,32)
    if (!$istok(double.stand, $hget($a($1,1), %player), 46)) timeAI $1 %player
    inc %x
  }
}

alias -l performAIAction {
  if ($timer($+(BlackJack,$1,DrawTimer))) {
    var %action = stand, %intel = $getIntel($2), %dealer = $cardTotal($1, -d).hard, %hard = $cardTotal($1,-u,$2).hard, %soft = $cardTotal($1,-u,$2).soft, %d = $iif($numtok($hget($g($1,1), $2), 32) == 2,$true), %s = $cardPair($hget($g($1,1), $2))
    if (%hard < 12) || (%soft < 8) %action = hit
    if (%intel > 1) && (%hard < 17) %action = hit
    if (%intel > 1) && (%s) && (%soft == 2) %action = split
    if (%intel > 2) && (%dealer < 11) && (%hard isnum 10-11) %action = double
    if (%intel > 2) && (%dealer > 6) && ((%soft > 7) && (%hard > 13)) %action = stand
    if (%intel > 3) && (%dealer isnum 2-6) && (%hard > 11) && (%soft > 7) %action = stand
    if (%intel > 3) && (%s) && (%hard == 16) && (%dealer < 11) %action = split
    if (%intel > 4) && (%dealer isnum 2-3) && (%hard < 14) && (!$istok(double.split,%action,46)) %action = hit
    if (%intel > 4) && (%dealer > 6) && ((%hard < 15) || (%soft < 8) && (!$istok(double.split,%action,46))) %action = hit
    if (%intel > 5) && (%dealer == 9) && (%soft < 9) && (%action != %split) %action = hit
    if (%intel > 5) && (%dealer isnum 10-11) && (%soft == 8) %action = hit
    if (%intel > 5) && (%s) && (%dealer isnum 3-6) && ($istok(4.6.12.14.18,%hard,46)) %action = split
    if (%intel > 6) && (%dealer == 10) && (%hard isnum 10-11) %action = hit
    if (%intel > 6) && (%dealer == 9) && (%hard isnum 10-11) %action = double
    if (%intel > 6) && (%dealer == 11) && (%hard < 17) && (%soft > 2) %action = hit
    if (%intel > 7) && (%dealer == 3) && (%hard == 13) && (%soft == 13) %action = stand
    if (%intel > 7) && (%dealer > 7) && (%hard isnum 12-16) && (!$istok(double.split,%action,46)) %action = hit
    if (%intel > 7) && (%s) && (%dealer == 2) && (%hard isnum 4-6) %action = split
    if (%intel > 7) && (%s) && (%dealer == 7) && ($istok(4.6.14.16,%hard,46)) %action = split
    if (%intel > 8) && (%dealer isnum 3-6) && (%hard == 9) %action = double
    if (%intel > 8) && (%dealer isnum 5-6) && (%soft isnum 2-8) && (%hard > 11) %action = $iif(!%d && %soft == 8,stand,double)
    if (%intel > 8) && (%dealer == 7) && (%hard isnum 15-16) && (%action != %split) %action = hit
    if (%intel > 8) && (%s) && (%hard == 18) && ($istok(4.5.6.8.9,%dealer,46)) %action = split
    if (%intel > 9) && (%dealer == 4) && (%soft isnum 5-8) && (%hard > 11) && (%action != split) %action = $iif(!%d && %soft == 8,stand,double)
    if (%intel > 9) && (%dealer == 3) && (%soft isnum 7-8) && (%hard > 11) && (%action != split) %action = $iif(!%d && %soft == 8,stand,double)
    if (%action == hit) || ((%action == double) && (!%d)) drawCardAction $1 $2
    elseif (%action == double) drawCardAction $1 $2 -d
    elseif (%action == split) splitAction $1 $2
    else standAction $1 $2
    if (!$istok(double.stand, $hget($a($1,1), $2), 46)) timeAI $1 $2
  }
}

;########################################################
;########################################################
; SHOE MANAGEMENT
;########################################################
;########################################################

alias -l createDeck {
  var %i = 1, %d = $calc($hget($s($1), decks) * 52), %x, %s = $suits
  while (%i <= %d) {
    hadd $d($1) %i $+($mid(%s,$calc((%i % 4) + 1),1),_,$cardify($calc((%i % 13) + 1)))
    inc %i
  }
}

alias -l cardsRemaining return $hget($d($1), 0).item

alias -l drawCard {
  if (!$cardsRemaining($1)) checkShuffle $1
  var %r = $rand(1,$cardsremaining($1)), %t = $hget($d($1), %r).data
  hdel $d($1) $hget($d($1), %r).item
  return %t
}

;########################################################
;########################################################
; GAME FLOW
;########################################################
;########################################################

alias -l blackjackStart {
  hfreedom $1
  hmake $g($1,1) 1
  hmake $a($1,1) 1
  hmake $d($1) $floor($calc(($iif($decks isnum 1-8,$v1,2) * 52) / 10))
  hmake $w($1) 1
  hmake $s($1) 1
  hadd $s($1) decks $iif($decks isnum 1-8,$v1,2)
  msg $1 $c2($2) has started a game of $cb3(Blackjack Race!) Type $c2(!join) to join the game, you have $c1(30 seconds!)
  $+(.timerBlackJack,$1,JoinTimer) -o 1 30 startGame $1
  if ($3) generateAI $1 $3-
}

alias -l addPlayer {
  if (!$hget($g($1,1), $2)) {
    hadd $g($1,1) $2 check
    hadd $w($1) $2 0
    if ($3) && (a isin $3) hadd $s($1) ai $iif($hget($s($1), ai),$ifmatch $2,$2)
    msg $1 $c2($2) $+ $iif(($3) && (a isin $3),$+($chr(32),$bracks(AI level $getIntel($2)))) has joined the game! $c1($players($1)) players joined! $iif($hget($g($1,1), 0).item < 7 && $timer($+(BlackJack,$1,JoinTimer)).secs > 1,You've $c1($timer($+(BlackJack,$1,JoinTimer)).secs) seconds left to join!)
    if ($hget($g($1,1), 0).item > 6) startGame $1 -f
  }
}

alias -l startGame {
  $+(.timerBlackJack,$1,JoinTimer*) off
  if (!$players($1)) {
    msg $1 Nobody has joined the game. $c2(1 player) is required! Turning off.
    blackjackStop $1
  }
  else {
    if ($players($1) == 1) {
      while ($players($1) < 2) {
        if (($isAI($1, $hget($g($1,1), 1).item)) && ($numtok($ai,32) < 2)) || (!$ai) addPlayer $1 $+(Agent,$rand(11111,99999)) -a
        else addPlayer $1 $gettok($ai,$rand(1,$numtok($ai,32)),32) -a
      }
    }
    createDeck $1
    var %text = The aim is to win $cb1($finish coins) from the $c2(Dealer) before everyone else. Bet amounts are in units of $winSingle $+ , $cb3($iif($hget($s($1), decks) > 1,$v1 decks,1 deck)) in use. Get ready...
    if ($2) && (f isin $2) msg $1 There are a maximum of $c1($players($1) players!) %text
    else msg $1 $c1(Time is up!) There are $c1($players($1) players!) %text
    $+(.timerBlackJack,$1,RepeatGame) -o 1 10 startRound $1
  }
}

alias -l startRound {
  createDefaults $1
  if ($actionsComplete($1)) $+(.timerBlackJack,$1,AutoTimer) -o 1 2 checkWinner $1 -f
  else {
    $+(.timerBlackJack,$1,DrawTimer) -o 1 30 checkWinner $1
    if ($hget($s($1), ai)) rollAI $1
  }
}

alias -l createDefaults {
  var %x = 1, %p = $players($1), %player, %actions
  hadd $s($1) dealer $drawCard($1)
  hadd $s($1) actions $hget($g($1,1),0).item
  hadd $s($1) completed 0
  while (%x <= %p) {
    %player = $hget($g($1,1), %x).item
    hadd $g($1,1) %player $drawCard($1) $drawCard($1)
    %actions = $iif($numerifyTotal($cardTotal($1,-u,%player).hard) < 21,hit,stand)
    hadd $a($1,1) %player %actions
    if (%actions == stand) hinc $s($1) completed 1
    msg $1 $c2($+(%player,:)) Your cards are: $cardflair($hget($g($1,1), %player)) :: Total: $bracks($cardTotal($1,-u,%player)) $iif(%actions == hit,:: You can: !hit !stand !double $+ $iif($cardPair($hget($g($1,1), %player)),$+($chr(32),!split))) $+ $iif(%actions == hit,$+($chr(32),!surrender)))
    inc %x
  }
  msg $1 The $cb2(Dealer) is showing: $cardflair($hget($s($1), dealer))
}

alias -l createSplits {
  if ($3) {
    var %temp = $hget($g($1,1), $2)
    hadd $g($1,1) $2 $hget($g($1,$3), $2)
    hadd $g($1,$3) $2 %temp
    hadd $a($1,$3) $2 $hget($a($1,1), $2)
  }
  hadd $g($1,1) $2 $hget($g($1,1), $2) $drawCard($1)
  var %actions = $iif($numerifyTotal($cardTotal($1,-u,$2).hard) < 21,hit,stand)
  hadd $a($1,1) $2 %actions
  msg $1 $c2($+($2,:)) Your cards are: $cardflair($hget($g($1,1), $2)) :: Total: $bracks($cardTotal($1,-u,$2)) $iif(%actions == hit,:: You can: !hit !stand !double $+ $iif($cardPair($hget($g($1,1), $2)),$+($chr(32),!split)))
  if (%actions == stand) {
    hinc $s($1) completed 1
    if ($splitHand($1,$2)) forwardSplitter $1 $2 $splitHand($1,$2)
    elseif ($actionsComplete($1)) checkWinner $1 -f
  }
}

alias -l forwardSplitter {
  createSplits $1-
}

alias -l blackjackStop {
  hfreedom $1
}

;########################################################
;########################################################
; POST HAND
;########################################################
;########################################################

alias -l checkWinner {
  $+(.timerBlackJack,$1,DrawTimer,*) off
  var %dealer = $dealerOption($1)
  dealerDraw $1 %dealer
  roundDisplay $1 %dealer $iif($2,$2)
}

alias -l roundDisplay {
  var %t = $hget($s($1), dealer), %win = $getWinner($1).win, %draw = $getWinner($1).draw, %lose = $getWinner($1).lose, %finished = $cb1($iif(f isin $3,All hands are finished!,Time is up!))
  var %displaywin = $formatPlayers($1, %win).win, %displaylose = $formatPlayers($1, %lose).lose, %displaydraw = $formatPlayers($1, %draw).draw
  if ($istok(1.2,$2,46)) msg $1 %finished The $cb2(Dealer) shows $cardflair($gettok(%t,1,32)) and $iif($2 = 2,draws the following cards:,drew the card:) $cardflair($gettok(%t,2-,32)) :: Total: $cb2($cardTotal($1, -d).hard)
  elseif ($2 == 0) msg $1 %finished Everyone went $c1(bust) so the $cb2(Dealer) wins
  else msg $1 %finished The $cb2(Dealer) can't make $c1(Blackjack) so doesn't draw cards.
  if (%win) msg $1 $c1($iif($numtok(%win, 32) != 1,$v1 players,%win)) beat the dealer $+ $iif($numtok(%win, 32) != 1,: %displaywin,: $gettok(%displaywin,2-,32))
  if (%draw) msg $1 $c1($iif($numtok(%draw, 32) != 1,$v1 players,%draw)) managed to draw $+ $iif($numtok(%draw, 32) != 1,: %displaydraw,: $gettok(%displaydraw,2-,32))
  if (!%win) && (!%draw) && ($istok(1.2,$2,46)) msg $1 The $cb2(Dealer) beat everyone with: $cb1($cardflair(%t)) :: Total: $cb2($cardTotal($1,-d).hard)
  $+(.timerBlackJack,$1,nextRound) -o 1 2 nextRound $1
}

alias -l checkShuffle {
  var %r = $cardsRemaining($1)
  if (%r < $calc(($players($1) + 1) * 7)) {
    msg $1 $cb3(The cards are getting shuffled and reloaded) $bracks($+($round($calc((%r / ($hget($s($1), decks) * 52)) * 100),2),$chr(37)) density)
    createDeck $1
  }
  else msg $1 $cb3(%r cards) remain in the shoe $bracks($+($round($calc((%r / ($hget($s($1), decks) * 52)) * 100),2),$chr(37)) density)
}

alias -l nextRound {
  msg $1 Current scores: $allScores($1)
  if ($overallWinner($1)) {
    var %winner = $ifmatch, %coins = $hget($w($1), %winner)
    $+(.timerBlackJack,$1,announceWinner) -o 1 2 msg $1 Okay we have an $cb3(overall winner.) $c2(%winner) wins the game, finishing with $c1(%coins coins!)
    $+(.timerBlackJack,$1,endGame) -o 1 4 blackjackStop $1
  }
  elseif ($hget($s($1), stop)) {
    $+(.timerBlackJack,$1,announceStop) -o 1 2 msg $1 $cb3(Blackjack Race) was set to be stopped by $c2($hget($s($1), stop)) and has been terminated 
    $+(.timerBlackJack,$1,endGame) -o 1 4 blackjackStop $1
  }
  else {
    hdel -w $a($1,1) *
    hfreedomMirror $1
    $+(.timerBlackJack,$1,ShuffleTimer) -o 1 5 checkShuffle $1
    if ($hget($s($1),replace)) $+(.timerBlackJack,$1,ReplaceUser) -o 1 10 replaceUser $1
    $+(.timerBlackJack,$1,RepeatGame) -o 1 12 startRound $1
  }
}

alias -l replaceUser {
  var %x = 1, %u = $hget($s($1),replace), %y = $numtok(%u,32), %user, %a
  while (%x <= %y) {
    %user = $gettok(%u,%x,32)
    while ($hget($g($1,1), %a)) || (!%a) {
      %a = $iif($rand(1,20) == 1,Jamie,$+(Agent,$rand(10000,99999)))
    }
    hadd $g($1,1) %a $hget($g($1,1),%user)
    hadd $w($1) %a $hget($w($1),%user)
    hdel $g($1,1) %user
    hdel $w($1) %user
    hadd $s($1) ai $iif($hget($s($1), ai),$ifmatch %a,%a)
    msg $1 $cb3(%a) replaces $c2(%user) and joins the game!
    inc %x
  }
  hdel $s($1) replace
}

;########################################################
;########################################################
; CARD FUNCTIONS
;########################################################
;########################################################

alias -l numerifyTotal {
  if ($1- == TOO MANY) return 30
  if ($1- == BLACKJACK) return 22
  return $1-
}

alias cardflair {
  tokenize 32 $1-
  var %x = 1 ,%s ,%d
  while ($eval($+($,%x),2)) {
    %d = $remove($ifmatch,_)
    %s = %s $iif($left(%d,1) isin $left($suits,2),%d,$+(04,%d,))
    inc %x
  }
  return %s
}

alias -l cardPair {
  if ($gettok($gettok($1-,1,32),2,95) == $gettok($gettok($1-,2-,32),2-,95)) return $true
  return $false
}

alias -l cardTotal {
  var %s = $iif($2 == -u,$hget($g($1,1), $3),$iif($2 == -d,$hget($s($1), dealer),$2-)), %x = 1, %c = $numtok(%s, 32), %return = 0, %n, %soft = 0
  while (%x <= %c) {
    %n = $replace($gettok($gettok(%s,%x,32),2,95),A,1,J,10,Q,10,K,10)
    inc %return %n
    if (%n == 1) && (!%soft) && (%return < 12) inc %soft
    elseif (%soft) && (%return > 11) dec %soft
    if (%return > 21) return TOO MANY
    inc %x
  }
  if (%soft) {
    if (%return == 11) return $iif(%x == 3,BLACKJACK,21)
    if (!$prop) return $+(%return,/,$calc(%return + 10))
    if ($prop == hard) return $calc(%return + 10)
  }
  return %return
}

alias -l formatPlayers {
  var %x = 1, %y = $numtok($2-,32), %u, %return, %split = 1, %hand, %i
  while (%x <= %y) {
    %u = $gettok($2-,%x,32)
    %hand = $hget($g($1,%split), %u)
    if ($dealerVS($1, %hand) == $prop) {
      if (%i != %x) { %return = %return %u $+ : $bracks($cardflair(%hand)) / | %i = %x }
      else %return = $left(%return,-1) $bracks($cardflair(%hand)) /
      if ($prop == win) && ($numerifyTotal($cardTotal($1, %hand).hard) == 22) hinc $w($1) %u $winBlackjack
      else hinc $w($1) %u $eval($+($,$prop,$iif($hget($a($1,%split),%u) == Double,$v2,Single)),2)
    }
    inc %split
    if ($hget($g($1,%split), %u)) continue
    %split = 1
    inc %x
  }
  return $left(%return,-1)
}

;########################################################
;########################################################
; DEALER FUNCTIONS
;########################################################
;########################################################

alias -l dealerOption {
  var %x = 1, %y = $hget($g($1,1),0).item, %return = 0, %t, %m = 1, %p
  while (%x <= %y) {
    %p = $hget($g($1,1),%x).item
    %t = $numerifyTotal($cardTotal($1, $hget($g($1,%m), %p)).hard)
    if (%t < 22) return 2
    elseif (%t == 22) %return = 1
    inc %m
    if ($hget($g($1,%m), %p)) continue
    %m = 1
    inc %x
  }
  if (%return == 1) && (!$istok(10.11,$cardTotal($1,-d).hard,46)) return 3
  return %return
}

alias -l dealerDraw {
  while ($cardTotal($1,-d).hard < 17) {
    if ($2 == 0) || ($2 == 3) break
    hadd $s($1) dealer $hget($s($1), dealer) $drawCard($1)
    if ($2 == 1) break
  }
}

alias -l dealerVS {
  var %cards = $numerifyTotal($cardTotal($1,$2-).hard), %dealer = $numerifyTotal($cardTotal($1,-d).hard)
  if ((%cards < %dealer) && (%dealer < 30)) || (%cards == 30) return lose
  if (%cards == %dealer) return draw
  return win
}

;########################################################
;########################################################
; OTHER FUNCTIONS
;########################################################
;########################################################

alias -l players return $iif($hget($g($1,1), 0).item, $ifmatch, 0)

alias -l getWinner {
  var %return , %x = 1, %p = $players($1), %player, %split = 1, %h
  while (%x <= %p) {
    %player = $hget($g($1,1), %x).item
    if ($hget($g($1,1),%player) != check) {
      %h = $hget($g($1, %split), %player)
      inc %split
      if ($prop == $dealerVS($1,%h)) %return = $addtok(%return,%player,32)
      elseif ($hget($g($1,%split), %player)) continue
      %split = 1
    }
    inc %x
  }
  return %return
}

alias -l allScores {
  var %return, %x = 1, %y = $hget($w($1), 0).item, %s
  while (%x <= %y) {
    %s = $hget($w($1), %x).data
    %return = %return $hget($w($1), %x).item $+ : $iif(%s == 0,0,$+(,$iif(%s < 0,04,$iif(%s > 100,06,03)),%s,)) ::
    inc %x
  }
  return $left(%return, -2)
}

alias -l overallWinner {
  var %return, %x = 1, %y = $hget($w($1), 0).item, %score = $finish - 1, %data
  while (%x <= %y) {
    %data = $hget($w($1), %x).data
    if (%data >= %score) {
      if (%data > %score) %return = $hget($w($1), %x).item
      else %return = %return $hget($w($1), %x).item
      %score = %data
    }
    inc %x
  }
  if ($numtok(%return, 32) == 1) return %return
  return $false
}

MediaDriven never4get

We have a few active mIRC scripters! Find our small community at irc.lunarirc.net

Comments

Sign in to comment.
dma   -  7 days ago

very good script it's the bomb

 Respond  
Karlien   -  12 days ago

Works great! Nice game play and options. We're having lots of fun playing this.
Just trying to mess with the colors a tad is a bit tricky, but really they are fine as is :)
~Thanks for your time on this one~

Degausser  -  7 days ago

No problem :)

dma  -  2 days ago

Best game here.

Degausser  -  2 days ago

hehe thanks <3

Quite proud of this one myself, some of the functions really pack a lot of power. Had a lot of fun writing this :)

Sign in to comment

Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.