Thursday, May 9, 2013

Blackjack Simulation

I wanted to create a simulation based on how I play Blackjack. I assume that I sit down with $200 at a table and want to double my money. The table minimum is $10 per hand. If I am ahead I raise my bet if I am down I chase my money. I assume the odds of winning at 43% and the odds of tying are 8%. 

The output looks like this:


After 500 Attempts you lost:$39200
Finished Up:42, Finished Down:8, Lost Everything:335, Hit Goal:115



This a my Java based simulation:

 public void test() {
        int simCount=500;
        int hitGoal=0;
        int lostEverything=0;
        int finishedUp=0;
        int finishedDown=0;
        int totalLost=0;
        for (int sim = 0; sim < simCount; sim++) {
            int minBet = 10;
            int startingPurse = 200;
            int chanceOfWinning = 43;
            int chanceOfTying = 8;
            int curBet = minBet;
            int purse = startingPurse;
            int maxHands = 200;
            int handCount = 0;
            int topPurse = 0;
            int topHand = 0;
            int maxBet = 0;
            int goal = 400;
            int winningCombos[] = new int[100];
            for (int i = 0; i < 100; i++) {
                winningCombos[i] = 0;
            }
            for (int i = 0; i < chanceOfWinning; i++) {
                int num = (int) Math.floor(Math.random() % 100);
                while (winningCombos[num] == 1) {
                    num = (int) Math.floor(Math.random() * 10000 % 100);
                }
                winningCombos[num] = 1;
            }
            for (int i = 0; i < chanceOfTying; i++) {
                int num = (int) Math.floor(Math.random() % 100);
                while (winningCombos[num] != 0) {
                    num = (int) Math.floor(Math.random() * 10000 % 100);
                }
                winningCombos[num] = 2;
            }
            while (handCount++ < maxHands && purse > 0 && purse < goal) {
                int num = (int) Math.floor(Math.random() * 10000 % 100);
                if (winningCombos[num] == 1) {
                    purse += curBet;
                }
                else if (winningCombos[num] == 2) {
                }
                else {
                    purse -= curBet;
                }
                if (purse < startingPurse) {
                    curBet = startingPurse - purse + minBet;
                    if (purse - curBet < 0) {
                        curBet = purse;
                    }
                }
                else if(purse>startingPurse&&purse<startingPurse+(3*curBet)){
                    curBet = minBet;
                }
                if (curBet > maxBet) {
                    maxBet = curBet;
                }
                if (purse > topPurse) {
                    topPurse = purse;
                    topHand = handCount;
                }
                if(purse>startingPurse+(3*curBet)){
                    curBet*=2;
                }
            }
            System.out.println("------------------------------------------------");
            System.out.println("Sat down at a $" + minBet + " minimum table with $" + startingPurse);
            System.out.println("Finished after " + (handCount - 1) + " hands with $" + purse);
            System.out.println("Has a maximum of $" + topPurse + " on hand " + topHand);
            System.out.println("Most bet on a hand $" + maxBet);
            if(purse>startingPurse){
                if(purse>=goal){
                    hitGoal++;
                }
                else{
                    finishedUp++;
                }
            }
            else{
                if(purse==0){
                    lostEverything++;
                }
                else{
                    finishedDown++;
                }
            }
            totalLost+=(200-purse);
        }
        System.out.println("------------------------------------------------");
        System.out.println("After "+simCount+" Attempts you lost:$"+totalLost);
        System.out.println("Finished Up:"+finishedUp+", Finished Down:"+finishedDown+", Lost Everything:"+lostEverything+", Hit Goal:"+hitGoal);
    }