Welcome to the Builder Academy

Question Trigger of the Day - Blackjack

More
22 Jun 2012 08:51 #181 by Rumble
By Relic
Blackjack Example - Advanced
Hey all, figured I'd submit my Blackjack triggers in case anyone feels like using/proofreading/improving them. This was my first attempt at a large script, and I'm sure there are better ways to go about everything, so all feedback is welcome! It's a series of commands (bet, hit, stand) that are attached to a dealer mob. It functions on basic Vegas rules: Dealer must hit when < 17, and must stand when >= 17. Player may hit until stand, bust, or 5-card charlie. I still need to implement double-down and splits... Control is passed from the player to the dealer when the player stands.

Again, all feedback is welcome!

I created a new trigger that will setup the deck when the mob loads. This removes the need to setup the deck with every command. Also added some loops for getting/deleting/remoting player data.

New Deck Setup Trigger:
Code:
1) Name : Blackjack Deck Setup 2) Intended for : Mobiles 3) Trigger types: Load 4) Numeric Arg : 100 5) Arguments : 6) Commands: eval deck[01] Ace 11 Diamonds eval deck[02] Two 2 Diamonds eval deck[03] Three 3 Diamonds eval deck[04] Four 4 Diamonds eval deck[05] Five 5 Diamonds eval deck[06] Six 6 Diamonds eval deck[07] Seven 7 Diamonds eval deck[08] Eight 8 Diamonds eval deck[09] Nine 9 Diamonds eval deck[10] Ten 10 Diamonds eval deck[11] Jack 10 Diamonds eval deck[12] Queen 10 Diamonds eval deck[13] King 10 Diamonds eval deck[14] Ace 11 Hearts eval deck[15] Two 2 Hearts eval deck[16] Three 3 Hearts eval deck[17] Four 4 Hearts eval deck[18] Five 5 Hearts eval deck[19] Six 6 Hearts eval deck[20] Seven 7 Hearts eval deck[21] Eight 8 Hearts eval deck[22] Nine 9 Hearts eval deck[23] Ten 10 Hearts eval deck[24] Jack 10 Hearts eval deck[25] Queen 10 Hearts eval deck[26] King 10 Hearts eval deck[27] Ace 11 Spades eval deck[28] Two 2 Spades eval deck[29] Three 3 Spades eval deck[30] Four 4 Spades eval deck[31] Five 5 Spades eval deck[32] Six 6 Spades eval deck[33] Seven 7 Spades eval deck[34] Eight 8 Spades eval deck[35] Nine 9 Spades eval deck[36] Ten 10 Spades eval deck[37] Jack 10 Spades eval deck[38] Queen 10 Spades eval deck[39] King 10 Spades eval deck[40] Ace 11 Clubs eval deck[41] Two 2 Clubs eval deck[42] Three 3 Clubs eval deck[43] Four 4 Clubs eval deck[44] Five 5 Clubs eval deck[45] Six 6 Clubs eval deck[46] Seven 7 Clubs eval deck[47] Eight 8 Clubs eval deck[48] Nine 9 Clubs eval deck[49] Ten 10 Clubs eval deck[50] Jack 10 Clubs eval deck[51] Queen 10 Clubs eval deck[52] King 10 Clubs eval cnum 1 while %cnum% <= 52 if %cnum% < 10 eval value 0%cnum% else eval value %cnum% end global deck[%value%] eval cnum %cnum% + 1 done eval nam 1 eval val 2 eval suit 3 global nam global val global suit

Updated Bet Command:
Code:
1) Name : Blackjack Bet Cmd 2) Intended for : Mobiles 3) Trigger types: Command 4) Numeric Arg : 1 5) Arguments : bet 6) Commands: eval plr %actor.id% if bet == %cmd% if %actor.varexists(playing_blackjack)% %send% %actor% You're already playing! Finish your current game first. halt end if %arg% < 100 || %arg% > 100000 || !%arg% %send% %actor% You must bet an amount from 100 to 100,000. halt else eval betamt %arg% if %actor.gold% < %betamt% %send% %actor% You don't have that much gold! halt else nop %actor.gold(-%betamt%)% %echo% %self.name% shuffles the deck. ********************************************************** * New Game - Draw first card eval i %random.52% if %i% < 10 eval i 0%i% end * Get the card from the deck eval pcard1 %%deck[%i%]%% * Add the card to the used string. Since this is the * first card, simply eval it eval used %i% * Extract the values from the array extract pnam1 %nam% %pcard1% extract pval1 %val% %pcard1% extract psuit1 %suit% %pcard1% %echoaround% %actor% %self.name% deals %actor.name% a card face-down. %send% %actor% %self.name% deals you a %pnam1% of %psuit1%, worth %pval1% points. * Done with first player card ********************************************************** * Draw first dealer card eval i %random.52% if %i% < 10 eval i 0%i% end * Check the used string to see if this card is already used eval test 0 while %test% == 0 * If the card # is a substring of the used string, pick another if %used% /= %i% eval i %random.52% if %i% < 10 eval i 0%i% end else * Card has not been used, add to used string eval dcard1 %%deck[%i%]%% eval used %used% %i% eval test 1 end done * Extract the values from the array extract dnam1 %nam% %dcard1% extract dval1 %val% %dcard1% extract dsuit1 %suit% %dcard1% %echo% %self.name% deals %self.himher%self a card face-down. * Done with first dealer card ********************************************************** * Draw second player card eval i %random.52% if %i% < 10 eval i 0%i% end * Check the used string to see if this card is already used eval test 0 while %test% == 0 * If the card # is a substring of the used string, pick another if %used% /= %i% eval i %random.52% if %i% < 10 eval i 0%i% end else * Card has not been used, add to used string eval pcard2 %%deck[%i%]%% eval used %used% %i% eval test 1 end done * Extract the values from the array extract pnam2 %nam% %pcard2% extract pval2 %val% %pcard2% extract psuit2 %suit% %pcard2% if %pval1% == 11 && %pval2% == 11 eval pval1 1 end %echoaround% %actor% %self.name% deals %actor.name% a %pnam2% of %psuit2%, worth %pval2% points. %send% %actor% %self.name% deals you a %pnam2% of %psuit2%, worth %pval2% points. ********************************************************** * Draw second dealer card eval i %random.52% if %i% < 10 eval i 0%i% end * Check the used string to see if this card is already used eval test 0 while %test% == 0 * If the card # is a substring of the used string, pick another if %used% /= %i% eval i %random.52% if %i% < 10 eval i 0%i% end else * Card has not been used, add to used string eval dcard2 %%deck[%i%]%% eval used %used% %i% eval test 1 end done * Extract the values from the array extract dnam2 %nam% %dcard2% extract dval2 %val% %dcard2% extract dsuit2 %suit% %dcard2% if %dval1% == 11 && %dval2% == 11 eval dval1 1 end eval cards 2 %echo% %self.name% deals %self.himher%self a %dnam2% of %dsuit2%, worth %dval2% points. eval ptotal %pval1% + %pval2% %send% %actor% Your %cards% cards total %ptotal%. eval dtotal %dval1% + %dval2% %echo% %self.name% checks %self.hisher% cards. * If we have blackjack, check for dealer blackjack tie, quit if %ptotal% == 21 * Did dealer also get blackjack? Return bet if so if %dtotal% == 21 %echo% %self.name% looks boggled. %echo% %self.name% reveals their hand showing %dnam1% of %dsuit1% and %dnam2% of %dsuit2%. say Well, I guess we tie on this one, %actor.name%. %send% %actor% %self.name% hands you back your %betamt% coins. nop %actor.gold(%betamt%)% halt end if %psuit1% == %psuit2% * Same-suit blackjack pays out 6:1 say Same-suit Blackjack! You win the big payout, %actor.name%! eval prize %betamt% * 6 nop %actor.gold(%prize%)% %send% %actor% You win %prize% coins! halt else * Regular blackjack pays out 3:1 say Blackjack! You win, %actor.name%! eval prize %betamt% * 3 nop %actor.gold(%prize%)% %send% %actor% You win %prize% coins! halt end end if %dtotal% == 21 say Woohoo! Blackjack! Sorry, %actor.name%, you lose! halt end * No blackjack, assign variables to player eval playing_blackjack 1 remote playing_blackjack %plr% remote ptotal %plr% remote dtotal %plr% remote pval1 %plr% remote pnam1 %plr% remote psuit1 %plr% remote pval2 %plr% remote pnam2 %plr% remote psuit2 %plr% remote dval1 %plr% remote dnam1 %plr% remote dsuit1 %plr% remote dval2 %plr% remote dnam2 %plr% remote dsuit2 %plr% remote cards %plr% remote used %plr% remote betamt %plr% say Ok, no blackjacks. Would you like to hit, stand, or know your total, %actor.name%? halt * Done with second card ********************************************************** end end else return 0 end

Updated Hit Command:
Code:
1) Name : Blackjack Hit Cmd 2) Intended for : Mobiles 3) Trigger types: Command 4) Numeric Arg : 1 5) Arguments : hit 6) Commands: eval plr %actor.id% if hit == %cmd% if !%actor.varexists(playing_blackjack)% %send% %actor% You must place a bet first! halt else * Already playing, get existing cards eval cards %actor.cards% eval i 1 while %i% <= %cards% eval pval%i% %%actor.pval%i%%% eval pnam%i% %%actor.pnam%i%%% eval psuit%i% %%actor.psuit%i%%% if %i% <= 2 eval dval%i% %%actor.dval%i%%% eval dnam%i% %%actor.dnam%i%%% eval dsuit%i% %%actor.dsuit%i%%% end eval i %i% + 1 done eval ptotal %actor.ptotal% eval dtotal %actor.dtotal% eval used %actor.used% eval betamt %actor.betamt% * Done initializing ************************************************ * Deal hit cards switch %cards% case 2 * Draw third player card eval i %random.52% * This makes i = 01 or 08, etc instead of 1 or 8 if %i% < 10 eval i 0%i% end * Check the used string to see if this card is already used eval test 0 while %test% == 0 * If the card # is a substring of the used string, pick another if %used% /= %i% eval i %random.52% if %i% < 10 eval i 0%i% end else * Card has not been used, add to used string eval pcard3 %%deck[%i%]%% eval used %used% %i% eval test 1 end done * Extract the values from the array extract pnam3 %nam% %pcard3% extract pval3 %val% %pcard3% extract psuit3 %suit% %pcard3% * Calc total and check if Ace value is too high if %pval3% == 11 if %ptotal% < 11 eval pval3 11 else eval pval3 1 end end eval ptotal %pval1% + %pval2% + %pval3% if %ptotal% > 21 if %pval1% == 11 eval pval1 1 remote pval1 %plr% elseif %pval2% == 11 eval pval2 1 remote pval2 %plr% end end %echoaround% %actor% %self.name% deals %actor.name% a card. %send% %actor% %self.name% deals you a %pnam3% of %psuit3%, worth %pval3% points. eval ptotal %pval1% + %pval2% + %pval3% * If bust, quit and delete player vars, else add card3 to player if %ptotal% > 21 say Sorry, %actor.name%, you bust with %ptotal%! eval i 1 while %i% <= 2 rdelete pval%i% %plr% rdelete pnam%i% %plr% rdelete psuit%i% %plr% rdelete dval%i% %plr% rdelete dnam%i% %plr% rdelete dsuit%i% %plr% eval i %i% + 1 done rdelete ptotal %plr% rdelete dtotal %plr% rdelete cards %plr% rdelete used %plr% rdelete betamt %plr% rdelete playing_blackjack %plr% halt else eval cards 3 remote cards %plr% remote pval3 %plr% remote pnam3 %plr% remote psuit3 %plr% remote used %plr% remote ptotal %plr% %send% %actor% Your %cards% cards total %ptotal%. say Would you like to hit, stand, or know your total, %actor.name%? halt end * Done with third card break case 3 * Draw fourth player card eval i %random.52% * This makes i = 01 or 08, etc instead of 1 or 8 if %i% < 10 eval i 0%i% end * Check the used string to see if this card is already used eval test 0 while %test% == 0 * If the card # is a substring of the used string, pick another if %used% /= %i% eval i %random.52% if %i% < 10 eval i 0%i% end else * Card has not been used, add to used string eval pcard4 %%deck[%i%]%% eval used %used% %i% eval test 1 end done * Extract the values from the array extract pnam4 %nam% %pcard4% extract pval4 %val% %pcard4% extract psuit4 %suit% %pcard4% * Calc total and check if Ace value is too high if %pval4% == 11 if %ptotal% < 11 eval pval4 11 else eval pval4 1 end end eval ptotal %pval1% + %pval2% + %pval3% + %pval4% if %ptotal% > 21 if %pval1% == 11 eval pval1 1 remote pval1 %plr% elseif %pval2% == 11 eval pval2 1 remote pval2 %plr% elseif %pval3% == 11 eval pval3 1 remote pval3 %plr% end end %echoaround% %actor% %self.name% deals %actor.name% a card. %send% %actor% %self.name% deals you a %pnam4% of %psuit4%, worth %pval4% points. eval ptotal %pval1% + %pval2% + %pval3% + %pval4% * If bust, quit and delete player vars, else add card3 to player if %ptotal% > 21 say Sorry, %actor.name%, you bust with %ptotal%! eval i 1 while %i% <= 3 rdelete pval%i% %plr% rdelete pnam%i% %plr% rdelete psuit%i% %plr% if %i% <= 2 rdelete dval%i% %plr% rdelete dnam%i% %plr% rdelete dsuit%i% %plr% end eval i %i% + 1 done rdelete ptotal %plr% rdelete dtotal %plr% rdelete cards %plr% rdelete used %plr% rdelete betamt %plr% rdelete playing_blackjack %plr% halt else eval cards 4 remote cards %plr% remote pval4 %plr% remote pnam4 %plr% remote psuit4 %plr% remote used %plr% remote ptotal %plr% %send% %actor% Your %cards% cards total %ptotal%. say Would you like to hit, stand, or know your total, %actor.name%? halt end * Done with fourth card break case 4 * Draw fifth card eval i %random.52% * This makes i = 01 or 08, etc instead of 1 or 8 if %i% < 10 eval i 0%i% end * Check the used string to see if this card is already used eval test 0 while %test% == 0 * If the card # is a substring of the used string, pick another if %used% /= %i% eval i %random.52% if %i% < 10 eval i 0%i% end else * Card has not been used, add to used string eval pcard5 %%deck[%i%]%% eval used %used% %i% eval test 1 end done * Extract the values from the array and say to player extract pnam5 %nam% %pcard5% extract pval5 %val% %pcard5% extract psuit5 %suit% %pcard5% * Calc total and check if Ace value is too high if %pval5% == 11 if %ptotal% < 11 eval pval5 11 else eval pval5 1 end end %echoaround% %actor% %self.name% deals %actor.name% a card. %send% %actor% %self.name% deals you a %pnam5% of %psuit5%, worth %pval5% points. eval ptotal %pval1% + %pval2% + %pval3% + %pval4% + %pval5% * Last card, don't need to update remotes if %ptotal% > 21 if %pval1% == 11 eval pval1 1 elseif %pval2% == 11 eval pval2 1 elseif %pval3% == 11 eval pval3 1 elseif %pval4% == 11 eval pval4 1 end end eval ptotal %pval1% + %pval2% + %pval3% + %pval4% + %pval5% eval cards 5 %send% %actor% Your %cards% cards total %ptotal%. * Check for bust or 5-card charlie. Delete player vars. if %ptotal% > 21 say Sorry, %actor.name%, you bust with %ptotal%! else say 5-Card Charlie! You win, %actor.name%! eval prize %betamt% * 5 nop %actor.gold(%prize%)% %send% %actor% You win %prize% coins! end eval i 1 while %i% <= 4 rdelete pval%i% %plr% rdelete pnam%i% %plr% rdelete psuit%i% %plr% if %i% <= 2 rdelete dval%i% %plr% rdelete dnam%i% %plr% rdelete dsuit%i% %plr% end eval i %i% + 1 done rdelete ptotal %plr% rdelete dtotal %plr% rdelete cards %plr% rdelete used %plr% rdelete betamt %plr% rdelete playing_blackjack %plr% halt * Done with fifth card break end else return 0 end

Updated Stand Command:
Code:
1) Name : Blackjack Stand Cmd 2) Intended for : Mobiles 3) Trigger types: Command 4) Numeric Arg : 1 5) Arguments : stand 6) Commands: eval plr %actor.id% if stand == %cmd% if !%actor.varexists(playing_blackjack)% return 0 else * Already playing, get existing cards eval cards %actor.cards% eval i 1 while %i% <= %cards% eval pval%i% %%actor.pval%i%%% eval pnam%i% %%actor.pnam%i%%% eval psuit%i% %%actor.psuit%i%%% if %i% <= 2 eval dval%i% %%actor.dval%i%%% eval dnam%i% %%actor.dnam%i%%% eval dsuit%i% %%actor.dsuit%i%%% end eval i %i% + 1 done eval used %actor.used% eval betamt %actor.betamt% eval ptotal %actor.ptotal% eval dtotal %actor.dtotal% %send% %actor% You stand with %ptotal%. %echoaround% %actor% %actor.name% stands with %ptotal%. * Done initializing ************************************************ * First check to see if dealer total >= 17 if %dtotal% >= 17 %echo% %self.name% reveals a %dnam1% of %dsuit1%, worth %dval1% points. %echo% %self.name% reveals a %dnam2% of %dsuit2%, worth %dval2% points. %echo% %self.name% stands with %dtotal%. if %ptotal% > %dtotal% say You beat my %dtotal% with %ptotal%. You win, %actor.name%! eval prize %betamt% * 2 nop %actor.gold(%prize%)% %send% %actor% You win %prize% coins! elseif %ptotal% == %dtotal% say It's a push, %actor.name%. Here's your coins back. nop %actor.gold(%betamt%)% %send% %actor% %self.name% gives you %betamt% coins. elseif %ptotal% < %dtotal% say Sorry %actor.name%, but my %dtotal% beats your %ptotal%. You lose! end eval endgame 1 end * Now deal cards to dealer until dtotal >= 17, bust, * or 5-card charlie. if !%endgame% eval i %random.52% * This makes i = 01 or 08, etc instead of 1 or 8 if %i% < 10 eval i 0%i% end * Check the used string to see if this card is already used eval test 0 while %test% == 0 * If the card # is a substring of the used string, pick another if %used% /= %i% eval i %random.52% if %i% < 10 eval i 0%i% end else * Card has not been used, add to used string eval dcard3 %%deck[%i%]%% eval used %used% %i% eval test 1 end done * Extract the values from the array extract dnam3 %nam% %dcard3% extract dval3 %val% %dcard3% extract dsuit3 %suit% %dcard3% * Calc total and check if Ace value is too high if %dval3% == 11 if %dtotal% < 11 eval dval3 11 else eval dval3 1 end end eval dtotal %dval1% + %dval2% + %dval3% if %dtotal% > 21 if %dval1% == 11 eval dval1 1 elseif %dval2% == 11 eval dval2 1 end end %echo% %self.name% deals %self.himher%self a %dnam3% of %dsuit3%, worth %dval3% points. eval dtotal %dval1% + %dval2% + %dval3% if %dtotal% > 21 say Whoops, I bust! You win, %actor.name%! eval prize %betamt% * 2 nop %actor.gold(%prize%)% %send% %actor% You win %prize% coins! eval endgame 1 end if %dtotal% >= 17 && !%endgame% %echo% %self.name% reveals a %dnam1% of %dsuit1%, worth %dval1% points. %echo% %self.name% reveals a %dnam2% of %dsuit2%, worth %dval2% points. %echo% %self.name% reveals a %dnam3% of %dsuit3%, worth %dval3% points. %echo% %self.name% stands with %dtotal%. if %ptotal% > %dtotal% say You beat my %dtotal% with %ptotal%. You win, %actor.name%! eval prize %betamt% * 2 nop %actor.gold(%prize%)% %send% %actor% You win %prize% coins! elseif %ptotal% == %dtotal% say It's a push, %actor.name%. Here's your coins back. nop %actor.gold(%betamt%)% %send% %actor% %self.name% gives you %betamt% coins. elseif %ptotal% < %dtotal% say Sorry %actor.name%, but my %dtotal% beats your %ptotal%. You lose! end eval endgame 1 end end * Done with dealer third card ******************************************************** if !%endgame% eval i %random.52% * This makes i = 01 or 08, etc instead of 1 or 8 if %i% < 10 eval i 0%i% end * Check the used string to see if this card is already used eval test 0 while %test% == 0 * If the card # is a substring of the used string, pick another if %used% /= %i% eval i %random.52% if %i% < 10 eval i 0%i% end else * Card has not been used, add to used string eval dcard4 %%deck[%i%]%% eval used %used% %i% eval test 1 end done * Extract the values from the array extract dnam4 %nam% %dcard4% extract dval4 %val% %dcard4% extract dsuit4 %suit% %dcard4% * Calc total and check if Ace value is too high if %dval4% == 11 if %dtotal% < 11 eval dval4 11 else eval dval4 1 end end eval dtotal %dval1% + %dval2% + %dval3% + %dval4% if %dtotal% > 21 if %dval1% == 11 eval dval1 1 elseif %dval2% == 11 eval dval2 1 elseif %dval3% == 11 eval dval3 1 end end %echo% %self.name% deals %self.himher%self a %dnam4% of %dsuit4%, worth %dval4% points. eval dtotal %dval1% + %dval2% + %dval3% + %dval4% if %dtotal% > 21 say Whoops, I bust! You win, %actor.name%! eval prize %betamt% * 2 nop %actor.gold(%prize%)% %send% %actor% You win %prize% coins! eval endgame 1 end if %dtotal% >= 17 && !%endgame% %echo% %self.name% reveals a %dnam1% of %dsuit1%, worth %dval1% points. %echo% %self.name% reveals a %dnam2% of %dsuit2%, worth %dval2% points. %echo% %self.name% reveals a %dnam3% of %dsuit3%, worth %dval3% points. %echo% %self.name% reveals a %dnam4% of %dsuit4%, worth %dval4% points. %echo% %self.name% stands with %dtotal%. if %ptotal% > %dtotal% say You beat my %dtotal% with %ptotal%. You win, %actor.name%! eval prize %betamt% * 2 nop %actor.gold(%prize%)% %send% %actor% You win %prize% coins! elseif %ptotal% == %dtotal% say It's a push, %actor.name%. Here's your coins back. nop %actor.gold(%betamt%)% %send% %actor% %self.name% gives you %betamt% coins. elseif %ptotal% < %dtotal% say Sorry %actor.name%, but my %dtotal% beats your %ptotal%. You lose! end eval endgame 1 end end * Done with dealer fourth card ******************************************************** * Dealer fifth card if !%endgame% eval i %random.52% * This makes i = 01 or 08, etc instead of 1 or 8 if %i% < 10 eval i 0%i% end * Check the used string to see if this card is already used eval test 0 while %test% == 0 * If the card # is a substring of the used string, pick another if %used% /= %i% eval i %random.52% if %i% < 10 eval i 0%i% end else * Card has not been used, add to used string eval dcard5 %%deck[%i%]%% eval used %used% %i% eval test 1 end done * Extract the values from the array extract dnam5 %nam% %dcard5% extract dval5 %val% %dcard5% extract dsuit5 %suit% %dcard5% * Calc total and check if Ace value is too high if %dval5% == 11 if %dtotal% < 11 eval dval5 11 else eval dval5 1 end end eval dtotal %dval1% + %dval2% + %dval3% + %dval4% + %dval5% if %dtotal% > 21 if %dval1% == 11 eval dval1 1 elseif %dval2% == 11 eval dval2 1 elseif %dval3% == 11 eval dval3 1 elseif %dval4% == 11 eval dval4 1 end end %echo% %self.name% deals %self.himher%self a %dnam5% of %dsuit5%, worth %dval5% points. eval dtotal %dval1% + %dval2% + %dval3% + %dval4% + %dval5% if %dtotal% > 21 say Whoops, I bust! You win, %actor.name%! eval prize %betamt% * 2 nop %actor.gold(%prize%)% %send% %actor% You win %prize% coins! eval endgame 1 end if %dtotal% >= 17 && !%endgame% say Woohoo! 5-Card Charlie! Sorry, %actor.name%, but I win! end end eval i 1 while %i% <= %cards% rdelete pval%i% %plr% rdelete pnam%i% %plr% rdelete psuit%i% %plr% if %i% <= 2 rdelete dval%i% %plr% rdelete dnam%i% %plr% rdelete dsuit%i% %plr% end eval i %i% + 1 done rdelete ptotal %plr% rdelete dtotal %plr% rdelete cards %plr% rdelete used %plr% rdelete betamt %plr% rdelete playing_blackjack %plr% halt end else return 0 end

Rumble
The Builder Academy
tbamud.com 9091
rumble@tbamud.com

Please Log in or Create an account to join the conversation.

Time to create page: 0.192 seconds