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 style of code, the game pace or 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
  • The Edge - An optional lower boundary which kicks players from the game when overstepped
  • AI opponents, with an intelligence range from 1 to 10
  • Automatic AI participation if only 1 player joins
  • Easily configure options and house rules 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 OR !stop (Operators only)
To abort the games termination, type !abort

  • 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
  • UPDATE 27/02/17 - Added house options, optional lower boundary and N card tricks, fixed dealer logic if all surrender
;########################################################
;########################################################
; SETTINGS (for YOU.... yes YOU!!!)
;########################################################
;########################################################

;# 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
;# more realistic game values: 150 40 20 30 -10 -25 0 0 -5

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

;# theEdge - Set to 0 to disable. Kicks players from the game aftering falling below -N

alias -l theEdge return -100

;# Bonus payouts. N card tricks represent an auto-payout if a player's hand consists of N cards. set to 0 to disable, otherwise set to the amount which should be paid out.

alias -l var5CardTrickPayout return 5
alias -l var6CardTrickPayout return 10
alias -l var7CardTrickPayout return 15
alias -l var5Card21Payout return 20
alias -l var6Card21Payout return 40
alias -l var7Card21Payout return 100

;# House rules. 'decks' can be from 2-8. The other lines respresent rules which can be used in the game. To "activate" a rule, change the 0 to a 1.

alias -l decks return 2
alias -l dealerHitsOnSoft17 return 0
alias -l doubleWith3orMoreCards return 0
alias -l noResplitWithAces return 0
alias -l splitAcesOneCardOnly return 0

;# Colors. 3 are used, each represented below. Use 2 digits, from 00-15

alias -l color1 return 07
alias -l color2 return 03
alias -l color3 return 06

;# 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 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 ($istok(!join.join,$1,46)) && ($timer($+(BlackJack,$chan,JoinTimer))) addPlayer $chan $nick
    if (($1-2 == !blackjack stop) || ($1 == !stop)) triggerBlackjackStop $chan -m $nick
    elseif ($1 == !abort) && ($hget($s($chan), stop)) triggerAbort $chan $nick
  }
  elseif ($1 == !blackjack) triggerBlackjack $chan $nick $2-
  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 $+(,$color1,,$1-,)
alias -l cb2 return $+(,$color2,,$1-,)
alias -l cb3 return $+(,$color3,,$1-,)
alias -l c1 return $+(,$color1,$1-,,)
alias -l c2 return $+(,$color2,$1-,,)
alias -l c3 return $+(,$color3,$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 (!$3) blackjackStart $1 $2
  elseif ($istok(1.2.3.4.5.6.7,$3,46)) blackjackStart $1-3
  elseif ($filterAI($3-)) blackjackStart $1-
  else {
    .notice $2 Invalid parameter. To start a game with AI opponents ready to join, type !blackjack N. N can be a number from 1-7
    .notice $2 You can also specify certain AI opponents you want in the game, for example: !blackjack AIMaster Degausser Trump
  }
}

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

alias -l triggerAbort {
  if ($timer($+(BlackJack,$1,DrawTimer))) && ($2 isop $1) abortAction $1 $2
  elseif ($2 !isop $1) .notice $2 You must be an operator to abort the games termination!
  else .notice $2 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)) && (!$doubleWith3orMoreCards) .notice $nick You can only double with the first two cards!
    else {
      var %card = $drawCard($1), %z
      hadd $g($1,1) $2 $hget($g($1,1), $2) %card
      %z = $numerifyTotal($cardTotal($1,-u,$2).hard)
      if (%z > 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 (%z == 21) && ($($+($chr(36),var,$numtok($hget($g($1,1),$2),32),Card21Payout),2)) {
        hinc $w($1) $2 $ifmatch
        msg $1 $c1($2) has hit a $cb3($numtok($hget($g($1,1),$2),32) Card 21) and is rewarded $c2($ifmatch coins)
      }
      elseif (%z <= 21) && ($($+($chr(36),var,$numtok($hget($g($1,1),$2),32),CardTrickPayout),2)) {
        hinc $w($1) $2 $ifmatch
        msg $1 $c1($2) has hit a $cb3($numtok($hget($g($1,1),$2),32) Card Trick) and is rewarded $c2($ifmatch coins)
      }
      if ((%d) || (%z > 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 {
  var %hand = $hget($g($1,1),$2)
  if ($cardPair(%hand)) && (!$istok(double.stand,$hget($a($1,1),$2),46)) {
    if ($noResplitWithAces) && ($gettok($gettok(%hand,1,32),2,95) == A) && ($hget($g($1,2),$2)) .notice $2 You can't split Aces more than once!
    else {
      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!)
}

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
}

;########################################################
;########################################################
; THE EDGE
;########################################################
;########################################################

alias -l offTheEdge {
  if ($theEdge < 0) {
    var %x = 1, %n = $hget($w($1),0).item, %p, %s, %e = $v1, %count = 1, %countvar, %minimum
    while (%x <= %n) {
      %s = $hget($w($1),%x).data
      if (!$len(%countvar)) %countvar = %s
      elseif (%countvar == %s) inc %count
      if (%s > %minimum) || (!$len(%minimum)) %minimum = %s
      inc %x
    }
    if (%count != %n) {
      if (%minimum > %e) %minimum = %e
      %x = 1
      while ($len($hget($w($1),%x).item)) {
        %p = $hget($w($1),%x).item
        %s = $hget($w($1),%p)
        if (%s < %minimum) {
          msg $1 $cb2(%p) has tipped over $c3(The Edge) and fallen into obscurity! $cb3(Goodbye %p $+ !)
          hdel $g($1,1) %p
          hdel $w($1) %p
        }
        else {
          if (%s == %minimum) msg $1 $c2(%p) is on $cb3(The Edge) with $+(04,%s,) coins!
          inc %x
        }
      }
    }
  }
}

;########################################################
;########################################################
; 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 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 || $doubleWith3orMoreCards,1,0), %s = $iif($cardPair($hget($g($1,1),$2)),$ifmatch,0)
    if (%s == A) && ($hget($g($1,2),$2)) && ($noResplitWithAces) %s = 0
    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
  }
}

alias -l timeAI {
  $+(.timerBlackJack,$1,DrawTimer,$2) -o 1 $rand(2,7) performAIAction $1 $2
}

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

alias -l createDeck {
  var %i = 1, %d = $calc($hget($s($1), decks) * 52), %e = $d($1)
  while (%i <= %d) {
    hadd %e %i %i
    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).item
  hdel $d($1) %t
  return $+($mid($suits,$calc((%t % 4) + 1),1),_,$cardify($calc((%t % 13) + 1)))
}

;########################################################
;########################################################
; 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 -f
  }
  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 %one = $iif(($splitAcesOneCardOnly) && ($gettok($gettok($hget($g($1,1),$2),1,32),2,95) == A),1,0), %actions = $iif($numerifyTotal($cardTotal($1,-u,$2).hard) < 21 && !%one,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 && !%one,:: You can: !hit !stand !double $+ $iif($cardPair($hget($g($1,1), $2)) && (($cardPair($hget($g($1,1), $2)) != A) || (!$noResplitWithAces)),$+($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
  elseif ($2 == 4) msg $1 %finished Everyone $c1(surrendered) so the $cb2(Dealer) doesn't draw cards
  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) && (%lose) && ($istok(1.2,$2,46)) msg $1 The $cb2(Dealer) beat everyone with: $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)
  offTheEdge $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.) $cb2(%winner) wins the game, finishing with $c1(%coins coins!)
    $+(.timerBlackJack,$1,endGame) -o 1 4 blackjackStop $1 -m %winner
  }
  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 -f
  }
  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
  hdel $s($1) replace
  while (%x <= %y) {
    %user = $gettok(%u,%x,32)
    if ($hget($g($1,1), %user)) {
      while (!%a) || ($hget($g($1,1), %a)) {
        %a = $iif($rand(1,20) == 1,$+($chr(74),$chr(109),m,$chr(105),$chr($+(10,1))),$+(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
  }
}

;########################################################
;########################################################
; 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 $v1
  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, %c, %d
  while (%x <= %y) {
    %u = $gettok($2-,%x,32)
    %hand = $hget($g($1,%split), %u)
    if ($dealerVS($1, %hand) == $prop) {
      if (!%i) { %return = %return %u $+ : $bracks($cardflair(%hand)) / | %i = 1 }
      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
    %i = 0
    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, %q = 1
  while (%x <= %y) {
    %p = $hget($g($1,1),%x).item
    if ($hget($g($1,%m),%p) == check) inc %q
    else {
      %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
  if (%q < %x) return %return
  return 4
}

alias -l dealerDraw {
  while ($cardTotal($1,-d).hard < 17) || (($dealerHitsOnSoft17) && ($cardTotal($1,-d).soft < 8)) {
    if ($istok(0.3.4,$2,46)) 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 (%x == 2) return $hget($w($1), 1).item
  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   -  Feb 13, 2017

very good script it's the bomb

 Respond  
Karlien   -  Feb 08, 2017

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  -  Feb 13, 2017

No problem :)

dma  -  Feb 18, 2017

Best game here.

Degausser  -  Feb 18, 2017

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.