Slashdot Mirror


Play Blackjack with PHP

Zack Coburn writes "The latest Codewalkers PHP Coding Contest invites PHP programmers to compete by creating a PHP script that interfaces with a special PHP library to play the game of Blackjack. Judging will happen as follows: 'A number of random decks will be generated, for each random rules will be used. Each script will then play all these decks, so each script will play under exactly the same conditions. In the end, the sum of all the money gained will determine the winner. In case of a tie, the running time will be the tie-breaker.' Some of the sixteen prizes include Cerberus Encoder, PHPEd, Macromedia Studio MX, SourceGuardian Pro, and Zend Studio." Here's a hint: count cards. :)

28 comments

  1. Count cards? by rylin · · Score: 5, Funny

    Hey, I've been blacklisted at casinos for doing that, you insensitive clod!

  2. Good Timing by DrunkBastard · · Score: 5, Funny

    Nice, post about this when there's only 3 days left to submit your scripts...

  3. Deadline is fast approaching by Acidic_Diarrhea · · Score: 4, Insightful

    Just a word to the wise, the deadline for submission to this contest is February 29th. Clearly it's possible to write this type of script in a short period of time but for hobby-type programming contents, perhaps it would be more worthwhile to get this contest posted a bit before the deadline?

    --
    I hate liberals. If you are a liberal, do not reply.
  4. Warning to those who will enter by vslashg · · Score: 5, Informative
    From the contest rules:
    If both you and the dealer bust, the hand is a tie.
    I don't know what game that is, but it's not casino blackjack. If your plan is to quickly translate some blackjack code you've already written into PHP, you're going to have to compensate for their misunderstanding of the real rules.
    1. Re:Warning to those who will enter by Chris+Pimlott · · Score: 4, Interesting

      Perhaps this is intentional, to require people to do some thoughtful analysis instead of just copying down a known strategy posted on a web page somewhere.

    2. Re:Warning to those who will enter by notsoclever · · Score: 3, Insightful
      How's it even possible for both the player and dealer to bust? I mean, if the player does their entire hand first (based only on the visible dealer card) as is normal for casino blackjack, then the player will always bust first - meaning that the dealer always wins.

      I wonder if they're just doing an "else if" in the rules even if it's a logic path which can never even be encountered to begin with.

      --
      There are 10 kinds of people: ones who understand ternary, ones who don't, and ones who think this joke is about binary
    3. Re:Warning to those who will enter by karnal · · Score: 2, Informative

      Most blackjack rules I've seen have a "dealer must hit to 17", which means the dealer can't just sit there with a 2 showing and not hit, unless the whole table (players) are bust...

      --
      Karnal
    4. Re:Warning to those who will enter by notsoclever · · Score: 2, Informative

      That's only if the player hasn't busted already. If the player busts, it's an automatic win for the house.

      --
      There are 10 kinds of people: ones who understand ternary, ones who don't, and ones who think this joke is about binary
    5. Re:Warning to those who will enter by karnal · · Score: 1

      Which might be why I said "Unless the whole table is bust."

      Durr. Of course it's a win for the house, even if one person is bust, but.... the dealer still has to deal himself unless the whole table busts.

      Wow.

      --
      Karnal
  5. not a good test by TekZen · · Score: 1

    No matter what you do, it seems the winner will still have a little luck. Sure you can count card, but that just helps your odds. If you can figure out how to program luck then you definitely should win.

    -Jackson

    1. Re:not a good test by funbobby · · Score: 5, Insightful

      My first reaction to this is that they test them all against the same deals, so there's not much luck, but on further reflection there is luck, it's just more subtle than the luck you deal with in the casino.

      The luck in this game is whether or not the deals that everyone gets are the type of deals that your scripts handles well.

      So based on this, I think the winning strategy is the following:
      Write a script that will do very well against a certain kinds of draws, and poorly against others. This would be a poor strategy if you were playing for money, and got to keep the net, but that isn't the game. The game here is that if you do the best (or one of the best) you win, and everything else loses. So losing badly isn't any worse than doing well but not the best. So take advantage of this by writing an aggresive strategy that will lose on most draws, but is sure to win on some. This is much better than writing a conservative script that will do well on any draw, but will always lose to whichever aggressive script happened to hit on the particular draw that was used.

      When they run the contest, most of the entries will play a conservative strategy, and end up in a nice bell curve around a reasonable return. But the winner will be some off the wall script that does something crazy that happened to pay off against the draw they used.

      How crazy it will take to win will of course depend on the number of entries.

      You can be a sure winner if you get enough friends to enter with different aggresive strategies to cover all possible draws.

      I wish I wasn't too busy to do it myself :(

      - Funbobby

    2. Re:not a good test by funbobby · · Score: 1

      Forgot to mention above, if you use my idea and win, let me know. I don't care about the prize, I'd just be happy to see it work.

    3. Re:not a good test by Anonymous Coward · · Score: 1, Informative

      I'm sorry to say that your idea is great, but not correct. The best way to play any card game is to play in the statistically best way possible. The way you determine what your odds are is by collecting information. i.e. counting cards is blackjack is the way a good player collects information. In a game like Hold'em, you watch the cards, but you also try to watch the players and collect information that way too.

      Once you know the odds on the best play to make, in the end if you play that best way every time you come out ahead. There really is nothing more involved than that in a game like blackjack where you don't try to read other people. So, given that card counting strategies have been well established some time now, since these programs will have a perfect memory and perfect set of math functions, if everyone programs theirs correctly they will all come out with the same amount of money in the end.

      The way to do best in a game like this where the opponent follows a strict set of rules is to play optimally all the time. This is something you learn if you start reading in the AI of games at all. Only if your opponent is playing suboptimally can you reap greater reward by varying your strategy. Of course the casino would not be letting blackjack dealers play suboptimally, otherwise their return wouldn't be garaunteed. This is why dealers don't participate in games like Hold'em where they cannot follow a strict set of rules.

    4. Re:not a good test by Anonymous Coward · · Score: 0

      Man, I guess I should read the whole post instead of skimming over it before I comment... You are right, but what you are suggesting is writing a program that basically plays randomly. Unless the set of hands played is fairly small, I'm willing to bet the optimal player is still going to win this contest.

  6. This is great! by Anonymous Coward · · Score: 5, Funny

    Looking around the web, and in my inbox, I see a total lack of anything like an online casino. Now that everyone has been told how to do php blackjack, maybe someone will get off the ball and fill in this much-needed online niche! Way to go!

    1. Re:This is great! by damien_kane · · Score: 1

      Maybe you should take a little bit longer look

    2. Re:This is great! by RevAaron · · Score: 1

      ...and maybe you should take a little bit longer to think. :) It was a joke. See how he mentioned his inbox? A lot of folks get "ONLINE CASINO!@$!" email. Think about it.

      --

      Working toward a usable PDA environment in the spirit of Newton OS: Dynapad
  7. Things to ponder by arrow · · Score: 4, Insightful

    Counting cards dosent really help you untill atleast half way through the deck. Hence why most good cheats will bet the minimum every hand and count the cards as they play, then bring in another player to bet the max while relying on signals from the player who has the count.

    I'm not saying player swapping is nessisary in a PHP implementation, but anyone working on this should not rely on the count data untill atleast half the cards are gone. Which will leave another loophole for the programmer to account for: there can be anywhere from 1 to 10 decks.

    A good blackjack player (not just a cheat) can not only count cards, but intimately knows their favorite version of the blackjack odds table, and most importantly how to bet to minimize the risk to their seed money and maximize profits when the cards are in their favor.

    Another thing that must be compensated for is the greatest tool in any players bag of tricks. The ability to just get up and walk away. If the cards aren't treating you right, move to another table. This dosen't work in tournements, but is a great tactic for limiting your losses.. which is casino speak for "winning".

    Oh, and when testing, make sure your shuffle is truely random.

    --
    symetrix. We are building a religion, a limited edition.
    1. Re:Things to ponder by T-Ranger · · Score: 2, Informative
      The vast majority of counting strategies are based on adjusting your bet. As a counting player, you always play the 'basic strategy', and adjust your bet according to your count.

      Well, almost 'always', anyway. If your in third base, holding a 12 against a dealers 3, and the last 7 cards have been faces, then a hit would be a good thing, even though it is against basic strategy.

      The other exception is if you are keeping a side count of faces, you can use that information to choose to take insurance. Playing basic strategy, insurance (or even money on a BJ) is always a bad idea.

      On your second to last point, I wounder how well a program : "sitdown(); bed(0); leave(); " would do ....

    2. Re:Things to ponder by allstarska · · Score: 2, Informative

      I don't know about the math behind it - but I have read and heard that shuffling is not truely random.

  8. Count cards by Burb · · Score: 2, Funny

    Count cards .. Count cards .. K-Mart sucks .. Who's on first .. Count cards .. Fly Quantas we never crash .. K-Mart sucks .. There are 1002 toothpicks on the table .. memorise phone book A-L .. who's on first .. count cards...

    --

    1. Re:Count cards by supersteve1440 · · Score: 1

      82 82 82 246 total Uh oh, fifteen minutes to Judge Wapner

    2. Re:Count cards by supersteve1440 · · Score: 1

      [let's try that again in 'Plain Old Text']

      82,
      82,
      82

      246 total.

      Uh oh, fifteen minutes to Judge Wapner.

  9. Counting Cards Not a Big Help by Anonymous Coward · · Score: 3, Insightful
    Here's a hint: count cards.

    Bad hint.

    Card counting will be of limited value, since:
    - the number of decks you are playing is so low compared to the variance of the game that you are not even close to the "long run". Luck will dominate.
    - the game has some non-standard rules (i.e. push if you and dealer both bust; player blackjack loses to dealer blackjack) that most strategies out there are not correct for the game.

    Given the low number of hands to play, you should exploit tournament strategies, not card counting.

    Posted anonymously since I don't want to end up in the Griffin book.......
    1. Re:Counting Cards Not a Big Help by Anonymous Coward · · Score: 0

      Would the player blackjack losing to dealer blackjack rule affect basic strategy, though, since there's no choice on play being made?

  10. Definitely by Anonymous Coward · · Score: 0

    ...an excellent driver... definitely an excellent driver... Bang! 97X, the power of Rock'n'Roll...

  11. Counting cards (sort of) would work... by agwis · · Score: 1

    It's a known fact that when playing blackjack that high cards favour the player and low cards favour the dealer.

    I wish I had read this earlier and had the time to code a program using a simple algorithm that I think would work great...regardless of how many decks there are.

    I think if someone were to use a constant integer (starting at a value of 0) that either gets incremented or decremented depending upon each card played you could adjust your bet accordingly.

    Say the first 2 cards dealt out are face cards. Your counter would be set to a value of -2. Then 3 more cards are dealt that are all low cards. Your counter would now be +1. The higher the counter is the higher your bet should be for the next hand. Vice versa if the counter is negative.

    The first few hands won't be very accurate, but towards the end of the deck(s) if the count is very high or very low, it should be a big indicator on how your odds are for the last hands.

    Due to a pile of work I have to get done and the short notice about this game I can't test my theory...but it would be cool if someone took this idea and tried it out and posted back here whether it worked or whether it sucked!

    -Pat