Search code examples
javaprobabilityplaying-cards

Java HiLow percent chance calculator -- Apparent Logic Issue


I'm having some difficulty with a Java-based probability calculator for an iteration of a HiLow game (specifically this one: http://bitino.com/hi-lo/). The objective of the program is to statistically suggest a bet given a CHANCE_THRESHOLD value. The program also keeps track of cards played. The issue I am running into has to do with the chance calculations. Thus far I have successfully addressed the values of 'A' (an Ace) but have run into issue testing other values. Evidently there must be something wrong with logic behind the valuation of percent chance.

import java.util.Scanner;

public class HiLow {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        String flip = "";
        double flipInt = 0.0;

        final double CHANCE_THRESHOLD = 0.75;
        double chanceLower = 0;
        double chanceHigher = 0;

        String cardArray[] = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };
        int cardValue[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };

        int cardCounter[] = { 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 };

        int remainingCards = 0;

        while (!(flip.equalsIgnoreCase("e"))) {

            for (int x = 0; x < cardCounter.length; x++) {
                cardCounter[x] = 4;
            }

            chanceLower = 0;
            chanceHigher = 0;

            System.out.print(" > ");
            flip = scan.next();

            while (!(flip.equalsIgnoreCase("r")) && !(flip.equalsIgnoreCase("e"))) {

                for (int x = 0; x < 13; x++) {
                    if (flip.equalsIgnoreCase(cardArray[x])) {
                        flipInt = cardValue[x];
                        cardCounter[x]--;
                    }
                }

                remainingCards = 0;
                for (int x = 0; x < cardCounter.length; x++) {
                        remainingCards = remainingCards + cardCounter[x];
                }

                chanceLower = (((flipInt) - 1) + cardCounter[(int) (flipInt - 1)]) / remainingCards;
                System.out.println((((flipInt) - 1) + cardCounter[(int) (flipInt - 1)]) + " / " + remainingCards + " = " + chanceLower);

                // This line should output 0.98039215686 given the input of 'a'
                //chanceHigher = ((13 - (flipInt)) + cardCounter[(int) (flipInt - 1)]) / remainingCards;
                //System.out.println(((13 - (flipInt)) + cardCounter[(int) (flipInt - 1)]) + " / " + remainingCards + " = " + chanceHigher);

                chanceHigher = Math.abs(1 - chanceLower);
                System.out.println(Math.abs((((flipInt) - 1) + cardCounter[(int) (flipInt - 1)]) - remainingCards) + " / " + remainingCards + " = " + chanceHigher);


                if (chanceLower > chanceHigher) {
                    if (chanceLower >= CHANCE_THRESHOLD) {
                        System.out.println("Bet Lower.");
                    } else if (chanceLower > (CHANCE_THRESHOLD - 0.10)){
                        System.out.println("Bet with Caution.");
                    } else {
                        System.out.println("Risk Outside Threshold.");
                    }

                } else {
                    if (chanceHigher >= CHANCE_THRESHOLD) {
                        System.out.println("Bet Higher.");
                    } else if (chanceHigher > (CHANCE_THRESHOLD - 0.10)){
                        System.out.println("Bet with Caution.");
                    } else {
                        System.out.println("Risk Outside Threshold.");
                    }
                }

                for (int x = 0; x < cardCounter.length; x++) {
                    System.out.print(cardCounter[x] + ":" + cardArray[x] + " ");
                }
                System.out.print("\n");

                System.out.print(" > ");
                flip = scan.next();

            }
        }
    }
}

As always, help is greatly appreciated. Thank you ahead of time.


Solution

  • First of all, You are not closing the scanner, but I guess that it's just been overlooked.

    Secondly remainingCards is is not enough. For once, you shouldn't count remaining cards of the same value - these are not higher nor lower, just the same.

    There is no need to calculate whole array of greater / lower cards, 2 variables are enough

    remainingLowerCards = 0;
    remainingHigherCards = 0;
    for (int x = 0; x < cardCounter.length; x++) {
        remainingCards = remainingCards + cardCounter[x];
        if (x < flipInt - 1) {
            remainingLowerCards += cardCounter[x];
        } else if (x >= flipInt) {
            remainingHigherCards += cardCounter[x];
        }
    }
    
    chanceLower = ((double) remainingLowerCards) / remainingCards;
    System.out.println("Lower: " + remainingLowerCards + " / " + remainingCards + " = " + chanceLower);
    
    // This line should output 0.98039215686 given the input of 'a'
    chanceHigher = ((double) remainingHigherCards) / remainingCards;
    System.out.println("Higher: " + remainingHigherCards + " / " + remainingCards + " = "
            + chanceHigher);