mIRC Homepage

BlackJack Script Help

Posted By: Newbie

BlackJack Script Help - 02/10/17 10:43 PM

I've written up this blackjack script, I know it's not super optimal and after a while I just started copying and pasting when I got lazy.

I wanted to know how to make the Ace card stand for both a 1 and 11? I haven't been able to figure that out. Thanks guys!

Code:
alias draw { 
  var %cRNG $rand(1,$lines(cards.txt))
  var %card $read(cards.txt,n,%cRNG)
  var %cards $hget(cards,$1)
  hadd -m cards $1 %cards $+ , $+ %card
  write -dl $+ %cRNG cards.txt
  return %card
}

alias cCards {
  var %pCardsT $gettok($hget(cards,$1),0,44)
  while (%pCardsT) {
    var %pCards $gettok($hget(cards,$1),%pCardsT,44)
    var %pCard %pCard %pCards
    dec %pCardsT
  }
  return %pCard  
}

alias calcC {
  var %Ccards $gettok($hget(cards,$1),0,44)
  while (%Ccards) {
    var %cardNum $gettok($hget(cards,$1),%Ccards,44)
    var %cardNumb $gettok(%cardNum,1,46)
    if (%cardNumb isnum) {
      var %CcardsT $calc(%cardNumb + %CcardsT) 
    }
    elseif (%cardNumb == A) {
      var %cardNumb 1
      var %CcardsT $calc(%cardNumb + %CcardsT)
    }
    else {
      var %cardNumb 10 
      var %CcardsT $calc(%cardNumb + %CcardsT) 
    }
    dec %Ccards
  }
  return %CcardsT
}

alias dealer {
  var %dcard $draw(B)  
  while ($calcC(B) < 17) {
    unset %dcount
    $draw(B)
    set %dCount $calcC(B) 
  }
  return $hget(cards,B)
}

alias bjlose {
  hdel bjcoins $nick 
  hdel cards $nick
  if (%i == 0) {
    unset %bj 
    unset %i
    unset %pot
    unset %dealerC
    unset %dCount
    hfree bjcoins
    hfree cards
  }
}

alias bjwin {
  hdel bjcoins $nick 
  hdel cards $nick
  var %bjDcoins $readini(Dinocoins.ini,$+($1),dinocoins) 
  writeini -n Dinocoins.ini $+($1) dinocoins $calc(%bjDcoins + $2)
  var %bjDcoins $readini(Dinocoins.ini,$+($1),dinocoins) 
  if (%i == 0) {
    unset %bj 
    unset %i
    unset %pot
    unset %dealerC
    unset %dCount
    hfree bjcoins
    hfree cards
  }
  return %bjDcoins
}

on *:TEXT:!bj *:#: {
  if (%gamble > 1) { msg # Newbie's words echoed..."There's a time and place for everything but not now!" }
  else {
    if ($0 > 2) { msg # To play blackjack type --> !bj dinocoins <--. }
    else {
      if ($2 isnum) {
        if ($bjcoins($nick,$2) < 0) { msg # @ $+ $nick you don't have enough dinocoins. } 
        else { 
          if (!%bj) {
            deck
            set %i 1
            set %bj 1
            set %pot $2
            set %dealerC $dealer
            .timer 1 10 set %bj 2
            msg # $nick wants to play blackjack, join by typing --> !bj dinocoins <--. 
            .timer 1 1 msg # @ $+ $nick your cards are $draw($nick) and $draw($nick)
            .timer 1 11 msg # Times up, the game will now begin. Botsaurus draws $gettok(%dealerC,0,44) cards and reveals $gettok(%dealerC,1,44)
            .timer 1 12 msg # Do you !bj hit or !bj surrender or !bj hold or !bj doubledown? 
          }
          elseif (%bj == 1) {
            if (!$hget(cards,$nick)) {
              set %i $calc(%i + 1)
              set %pot $calc(%pot + $2)
              if ($lines(cards.txt) == 0) {
                deck
                msg # @ $+ $nick has joined the game with the cards, $draw($nick) and $draw($nick)
              }
              else { 
                msg # @ $+ $nick has joined the game with the cards, $draw($nick) and $draw($nick) 
              }
            }
            else {
              msg # You already have cards, they are $cCards($nick) 
            }
          }
          else {
            msg # You cannot place a bet when the game is running. 
          }
        }
      }
      elseif ($2 == cards) { 
        if (!$hget(cards,$nick)) {
          msg # You don't have a set of cards! 
        }
        else {
          msg # @ $+ $nick your cards are $cCards($nick)
        } 
      }
      elseif ($2 == hit) {
        if (%bj == 2) {
          msg # @ $+ $nick gets another card: $draw($nick) and now has $cCards($nick)
          if ($calcC($nick) > 21) {
            .timer 1 1 msg # @ $+ $nick you lose! Good day sir!
            set %i $calc(%i - 1) 
            bjlose
          }
          elseif ($calcC($nick) == 21) {
            set %i $calc(%i - 1)
            var %wbjCoins $calc(%pot * 2))
            .timer 1 1 msg # @ $+ $nick winner, winner, chicken dinner. $nick won and now has $bjwin($nick,%wbjCoins) dinocoins!
          }
          else { return }
        }
        else { msg # You can't do this yet. }
      }
      elseif ($2 == hold) {
        if (%bj == 2) {
          if (%dCount > 21) {
            set %i $calc(%i - 1)
            var %playPoints $hget(bjcoins,$nick)
            var %wbjCoins $calc(%playPoints * 2)
            .timer 1 1 msg # @ $+ $nick botsaurus has gone over 21. $nick won and now has $bjwin($nick,%wbjCoins) dinocoins! 
          }
          else {
            if ($calcC($nick) < %dCount) {
              .timer 1 1 msg # @ $+ $nick you lose! Good day sir!
              set %i $calc(%i - 1) 
              bjlose
            }
            elseif ($calcC($nick) > %dCount) {
              set %i $calc(%i - 1)
              var %playPoints $hget(bjcoins,$nick)
              var %wbjCoins $calc(%playPoints * 2)
              .timer 1 1 msg # @ $+ $nick winner, winner, chicken dinner. $nick won and now has $bjwin($nick,%wbjCoins) dinocoins!  
            }
            else {
              set %i $calc(%i - 1)
              var %wbjCoins $calc(%pot * 2))
              .timer 1 1 msg # @ $+ $nick winner, winner, chicken dinner. $nick won and now has $bjwin($nick,%wbjCoins) dinocoins!
            }
          }
        }
        else { msg # You can't do this yet. } 
      }
      elseif ($2 == doubledown) {
        msg # @ $+ $nick gets another card: $draw($nick)
        if ($calcC($nick) > 21) {
          .timer 1 1 msg # @ $+ $nick you lose! Good day sir!
          set %i $calc(%i - 1) 
          bjlose
        }
        elseif ($calcC($nick) == 21) {
          set %i $calc(%i - 1)
          var %wbjCoins $calc(%pot * 2))
          .timer 1 1 msg # @ $+ $nick winner, winner, chicken dinner. $nick won and now has $bjwin($nick,%wbjCoins) dinocoins!
        }
        else {  
          if (%dCount > 21) {
            set %i $calc(%i - 1)
            var %playPoints $hget(bjcoins,$nick)
            var %wbjCoins $calc(%playPoints * 2)
            .timer 1 1 msg # @ $+ $nick botsaurus has gone over 21. $nick won and now has $bjwin($nick,%wbjCoins) dinocoins! 
          }
          else {
            if (%dCount > 21) {
              set %i $calc(%i - 1)
              var %playPoints $hget(bjcoins,$nick)
              var %wbjCoins $calc(%playPoints * 2)
              .timer 1 1 msg # @ $+ $nick botsaurus has gone over 21. $nick won and now has $bjwin($nick,%wbjCoins) dinocoins! 
            }
            else {

              if ($calcC($nick) < %dCount) {
                .timer 1 1 msg # @ $+ $nick you lose! Good day sir!
                set %i $calc(%i - 1) 
                bjlose
              }
              elseif ($calcC($nick) > %dCount) {
                set %i $calc(%i - 1)
                var %playPoints $hget(bjcoins,$nick)
                var %wbjCoins $calc(%playPoints * 2)
                .timer 1 1 msg # @ $+ $nick winner, winner, chicken dinner. $nick won and now has $bjwin($nick,%wbjCoins) dinocoins!  
              }
              else {
                set %i $calc(%i - 1)
                var %wbjCoins $calc(%pot * 2))
                .timer 1 1 msg # @ $+ $nick winner, winner, chicken dinner. $nick won and now has $bjwin($nick,%wbjCoins) dinocoins!
              }
            }
          }
        }
      }
      else { msg # Not a command. }
    }
  }
}
Posted By: Newbie

Re: BlackJack Script Help - 02/10/17 10:44 PM

The main point to focus on is the alias calcC which replaces A with 1, but I want to know if it's possible to make it 11 if the person has a 10.
Posted By: Degausser

Re: BlackJack Script Help - 21/11/17 07:58 PM

Edited after I realized your actual problem. You just need to keep track of the soft status as you loop. Use ace as 1 by default.

$calcC(...) = hard total eg: 17
$calcC().soft = soft total eg: 7
$calcC().softReadable = for display eg: 7/17

Use hard totals for most things..

Code:
alias calcC {
  var %Ccards $gettok($hget(cards,$1),0,44), %soft = 0, %x = 0
  while (%Ccards) {
    var %cardNum $gettok($hget(cards,$1),%Ccards,44)
    var %cardNumb $gettok(%cardNum,1,46)
    if (%cardNumb isnum) {
      var %CcardsT $calc(%cardNumb + %CcardsT) 
    }
    elseif (%cardNumb == A) {
      var %cardNumb 1
      if (!%soft) && (%CcardsT < 12) %soft = 1
      var %CcardsT $calc(%cardNumb + %CcardsT)
    }
    else {
      var %cardNumb 10 
      var %CcardsT $calc(%cardNumb + %CcardsT) 
    }
    if (%soft) && (%CcardsT > 11) %soft = 0
    dec %Ccards
    inc %x
  }
  ; to return something else for blackjack, replace
  ; $iif(%x == 2,21,21) with $iif(%x == 2,newReturn,21)
  if (%soft) {
    if (%CcardsT == 11) return $iif(%x == 2,21,21)
    if ($prop == soft) return %CcardsT
    elseif ($prop == softReadable) return $+(%CcardsT,/,$calc(%CcardsT + 10))
    else return $calc(%CcardsT + 10)
  }
  return %CcardsT
}
© 2019 mIRC Discussion Forums