Today brings us the fifth and final entry about the Lucky Deuce. This is a series of challenges, brought to you by our pals over at Infragistics, where we call on you to help us build a “scoundrel’s casino”. Read to the end, because this week's challenge has a bigger prize- some TDWTF-emblazoned hoodies for the best entries.

Last week was your first shot at a “straight” solution, and the entries really showed it. Pretty much everybody got straight into the problem.

Honorable Mentions

Robert for example, implemented his solution as a straight-forward WinForms application in C#. What gets him his credit is… interesting extension to a simple die class he wrote which helps him count die combinations later:

    class DieEnumerator : Die{
        public DieEnumerator(int faces, Random randSource) : base(faces, randSource) {
            _current = 1;
            _overflow = false;

        public int current { get { return _current; } }
        private int _current;

        public bool overflow { get { return _overflow; } }
        private bool _overflow;

        public void Next() {
            _overflow = false;
            if (_current == faces + 1) {
                _current = 1;
                _overflow = true;

There’s nothing crazy in there, and it does the job, which gets Robert the It Does the Job award.

Jonathan is back again this week, with his “Bruce Said So” random number generator, and this time he gives us a lovely function for handling overflows with cryptic variable names and a goto:

    static void MultiplyReduceRatio(uint64_t aa, uint64_t ab, uint64_t ba, uint64_t bb, uint64_t& ca, uint64_t& cb)
        /* try to keep as much ratio precision as possible, by avoiding floating-point */
        uint64_t ad, bd, cd;

        ad = gcd(aa,ab);
        bd = gcd(ba,bb);
        aa /= ad;
        ab /= ad;
        ba /= bd;
        bb /= bd;

        ad = gcd(aa,bb);
        bd = gcd(ba,ab);
        aa /= ad;
        bb /= ad;
        ba /= bd;
        ab /= bd;

        if(Log2(aa) + Log2(ba) > 64 || Log2(ab) + Log2(bb) > 64) {
            /* not enough integer precision */
            if(aa > ba)
                aa /= 2;
                ba /= 2;
            if(ab > bb)
                ab /= 2;
                bb /= 2;
            goto repeat;

        ca = aa * ba;
        cb = ab * bb;

        /* final reduction */
        cd = gcd(ca,cb);
        ca /= cd;
        cb /= cd;

I don’t know what it does, but it works well enough for a Going Gone award. Jonathan capped his solution at 31 dice, and raises the point: “Hence games up to 4d250, 14d20, 24d6, 27d5 or 31d4 can be constructed. I think 31 dice in play is quite enough for most people, especially given the caltrap-like properties of d4s.”

The Winner

This week’s winner does a great job choosing a platform that’s simple to use, extensible, and integrates with Microsoft’s vision of the future: PowerShell. Scott implemented his solution as a Cmdlet which means you can chain it together with a larger, PowerShell driven casino solution that no one will be able to maintain.

    # The logic for when the player gets to roll again is a bit complicated in this
    # simulation, so I broke the test into several lines to manage it. PowerShell,
    # however, cannot handle line breaks between some (but not all) of its tokens.
    # The single backtick tells the parser to ignore the next character, allowing me
    # to embed line breaks without interfering with the farce of a parser.
    …} while                                                                        `
        (                                                                          `
            (                                                                      `
                (                                                                  `
                    (-not $Test)                                                   `
                -and                                                               `
                    $Point -eq $null                                               `
                )                                                                  `
            -or                                                                    `
                $WholeTurn                                                         `
            )                                                                      `
        -or                                                                        `
            (                                                                      `
                $Test                                                              `
            -and                                                                   `
                $TestNumber -lt ($Maximum - $Minimum + 1)                          `
            )                                                                      `

Clarity itself.

The Lucky Deuce: Getting Comped

You’ve got your code checked in, but the roach motel you’re staying in gives you the bum’s rush before Paula makes contact again. You know you can rebuild your bankroll with the hacks already in place, but Paula made it sound like she had a big score.

You’re on the street with barely any cash, and no real direction, so you camp out next to the dumpster behind some trendy coffee shop promising hand-roasted beans and artisinal almond milk creamers. It doesn’t matter what they’re serving- the dumpster smells like any other, but you can get on their wi-fi and nobody’s paying you any attention back there.

You log into the Lucky Deuce’s admin console. You haven’t got a lot of permissions, but you notice two things. First, the Lucky Deuce has launched a new marketing campaign, and the second is that since the campaign launched a huge number of new accounts signed up- and a few of them came from your current IP. Paula must have sneaked a script onto your laptop that ran when you connected to wi-fi.


On a whim, you log into the Lucky Deuce storefront using one of those- password Br1llant- and there’s a message waiting in your player’s inbox from Paula: “Get ready, kid. This is it.”

Then you put it together. The Lucky Deuce’s new marketing campaign is a digital version of the traditional casino’s “comps”. If you sit at a table, playing a game, eventually a waiter will wander by and give you a free drink, or a snack- anything to loosen you up and keep you at the table, pumping money into the House Edge- the casino’s advantage over the players. The Lucky Deuce can’t buy you drinks over the Internet, but it can do the next best thing: throw a few dollars into a gift-card in the Pennsylvania state-controlled liquor monopoly. In a state where you can’t even buy beer in grocery stores, those cards are worth their weight in gold.

You and Paula are going to do a comps hustle. These can get really complicated in a regular casino- a wary Pit Boss will spot a lazy hustler, but the Lucky Deuce isn’t that wary.

The Requirements

You have control of as many user accounts as you like. Choose one of the betting games from the previous weeks, and write a program that coordinates two or more “players” so that their wins and losses offset each other (the simplest example would be having one player bet on Red and the other on Black in roulette), so that the total loss across all players you control is minimal, and thus you can play as long as possible. The longer you play, the more comps you’ll earn.

You don’t have to use code from your previous entry, but if you have already put an entry in, it will obviously make your job easier. You can take advantage of your cheats as part of your strategy, but remember, the goal isn’t to win big at the table, but to take them to the cleaners on comps.

Alex has written up a more detailed explanation of the comps hustle if you need more help.

The prizes are gonna be a little bigger than a mug or some stickers- we’re going offer up a few TDWTF-emblazoned hoodies for the best entries. You won’t just be the first programmer on your floor to have one- you’ll be the only programmer you know that has one.

Entering & Judging

To enter, send an email to with a link or attachment of your code. In the body of the email, explain how your cheat works and what we need to do to run your code. You can use any language you like, but we have to be able to run it with minimal setup.

You don’t need to build a GUI, but if you do, and you do it using tools from Infragistics, we'll send you a free license (one per entrant, supplies limited). Consider this your Infragistics bonus.

Assume we have access to stock Windows, Linux and OSX instances, if we need to run your software locally. You could target MUMPS running on a mainframe, but we can't run it, and you probably won't win. You must get your submission in before 11:59PM Eastern Time, Sunday the 6th of September to be eligible for judging. We'll announce the winners next Wednesday.

The overall winner will be chosen by how interesting and fun we think their solution and cheat is.

Thanks to Infragistics for making this possible.


A worldwide leader in user experience, Infragistics helps developers build amazing applications. More than a million developers trust Infragistics for enterprise-ready user interface toolsets that deliver high-performance applications for Web, Windows and mobile applications. Their Indigo Studio is a design tool for rapid, interactive prototyping.

[Advertisement] Manage IT infrastructure as code across all environments with Puppet. Puppet Enterprise now offers more control and insight, with role-based access control, activity logging and all-new Puppet Apps. Start your free trial today!