HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaModerate

Blackjack odds calculator

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
oddscalculatorblackjack

Problem

I'm working on getting switch and if statements down and I came up with this. It works well and I don't have any problems with it running properly. I was wondering if I could get some feedback on how I put it together. If there are things I am doing wrong or could do better, I'm very open to suggestions!

```
public static void main(String[] args) {

double edge = 0;

Scanner sc = new Scanner(System.in);

System.out.println("Enter the name of your Casino.");
String casino = sc.nextLine();

System.out.println("How many decks? (1, 2, 4, 6, 8)");
int decks = sc.nextInt();
System.out.println("Dealer hits Soft 17 (1 for Yes; 2 or No)");
int soft17 = sc.nextInt();
System.out.println("Double after Splits Allowed (1 for Yes; 2 or No)");
int doubleAfterSplit = sc.nextInt();
System.out.println("Double 10/11 Only (1 for Yes; 2 or No)");
int double1011 = sc.nextInt();
System.out.println("Double 9/10/11 Only (1 for Yes; 2 or No)");
int double91011 = sc.nextInt();
System.out.println("Resplit Aces (1 for Yes; 2 or No)");
int rSA = sc.nextInt();
System.out.println("Late Surrender (1 for Yes; 2 or No)");
int lateSurrender = sc.nextInt();
System.out.println("Early Surrender (1 for Yes; 2 or No)");
int earlySurrender = sc.nextInt();
System.out.println("Lose All Doubles/Splits vs. Natural (1 for Yes; 2 or No)");
int loseAllDS = sc.nextInt();
sc.close();

//Number of Decks Option
if (decks == 1) {
edge += 0.01;
} else if (decks == 2) {
edge -= 0.32;
} else if (decks == 4) {
edge -= 0.49;
} else if (decks == 6) {
edge -= 0.54;
} else if (decks == 8) {
edge -= 0.57;
} else {
System.out.println("You made a mistake. Start over.");
}

//Soft 17 Options
if (soft17 == 1) {
edge -= 0.20;
} else {
edge += 0;
}

//Double After Splits Option
if (doubleAfterSplit == 1) {
edge +=

Solution

edge += 0; doesn't do anything You can just remove it and the else branches where you do it.

The number of decks handling can be done with an array:

int[] deckEdge = new int[]{0, 0.01, -0.32, 0, -0.49, 0, -0.54, 0, -0.57};//insert 0 in invalid places;

edge += deckEdge[decks];


(there is a trick you can use here by dividing the number of decks by 2 to remove the inserted 0s but that is less readable and understandable)

The switch solution is perhaps the most straightforward setup for this though:

switch(decks){
    case 1:
        edge += 0.01;
        break;
    case 2:
        edge -= 0.32;
        break;
    case 4:
        edge -= 0.49;
        break;
    case 6:
        edge -= 0.54;
        break;
    case 8:
        edge -= 0.57;
        break;
    default:
        System.out.println("You made a mistake.  Start over.");
        return;//stop program;
}


You also just continue on when a wrong number of decks is specified. You can either use return to stop immediately (like in the above switch) or use a do...while loop while reading it:

do{
    System.out.println("How many decks? (1, 2, 4, 6, 8)");
    int decks = sc.nextInt();
}while(decks!=1 && decks!=2 && decks!=4 && decks!=6 && decks!=8);

Code Snippets

int[] deckEdge = new int[]{0, 0.01, -0.32, 0, -0.49, 0, -0.54, 0, -0.57};//insert 0 in invalid places;

edge += deckEdge[decks];
switch(decks){
    case 1:
        edge += 0.01;
        break;
    case 2:
        edge -= 0.32;
        break;
    case 4:
        edge -= 0.49;
        break;
    case 6:
        edge -= 0.54;
        break;
    case 8:
        edge -= 0.57;
        break;
    default:
        System.out.println("You made a mistake.  Start over.");
        return;//stop program;
}
do{
    System.out.println("How many decks? (1, 2, 4, 6, 8)");
    int decks = sc.nextInt();
}while(decks!=1 && decks!=2 && decks!=4 && decks!=6 && decks!=8);

Context

StackExchange Code Review Q#78975, answer score: 10

Revisions (0)

No revisions yet.