Thread: Craps Game, possibly logical error with enumeration constant

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    11

    Craps Game, possibly logical error with enumeration constant

    Below is the code to my craps game, Im using enumeration constants for game status to determine whether to continue or not.

    The Problem: After Crapping Out, the enumeration gameStatus evaluated to LOSE (confirmed by printf()), but the loops condition: while(gameStatus == CONTINUE ), evaluated to true. Why did my enumeration revert back to its previous state?

    Starting Line 154

    Code:
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    
    /*Define Constants for Array Element*/
    #define FOUR 0
    #define FIVE 1
    #define SIX 2
    #define EIGHT 3
    #define NINE 4
    #define TEN 5
    
    #define HARD_TWO 0
    #define HARD_FOUR 1
    #define HARD_SIX 2
    #define HARD_EIGHT 3
    #define HARD_TEN 4
    #define HARD_TWELVE 5
    /***********************************/
    
    /*getUserInput Constants*/
    #define MENU 6
    #define BET_NORMAL 7
    #define BET_HARDWAYS 8
    #define AMOUNT 9
    #define OPTION 10
    
    /**/
    #define COME 1000
    #define DONT_COME 2000
    #define PASS_LINE 3000
    #define DONT_PASS_LINE 4000
    #define HARD 5000
    
    /*Game Status*/
    enum Status {WIN, LOSE, CONTINUE};
    enum Status gameStatus;
    
    int pot = 900;
    int pass_line = 0;
    int dont_pass_line = 0;
    int roll_number = 0;
    
    /*Bet Arrays*/
    int come_bet[6] = {0};
    int dont_come_bet[6] = {0};
    int hard_bet[6] = {0};
    int hard_bet_odds[6] = {1}; /*MODIFY for ODDS*/
    
    /*Print Functions (and Arrays)*/
    void printMenu(void);
    void printTable(void);
    char isPrintComeBet(int i);
    char isPrintDontComeBet(int i);
    int table[6] = {4, 5, 6, 8, 9, 10};
    
    /*Bet funtions*/
    void makeBet(int number, int amount, int come_dontcome);
    int rollDice(void);
    void pay(int item, int option);
    void clear(int item, int option);
    void withdraw(int item, int amout,  int option);
    
    /*User Funtions*/
    int getUserInput(int InputType);
    
    int main(void){
    
        int input;
        int bet_number, bet_amount;
        int dice_1, dice_2, sum;
        int point, option;
        int i; /*Counter*/
    
        srand(time(0));
        
        printTable();
        printMenu();
        /*Get User Input*/
        input = getUserInput(MENU);
    
        while(input != 9){        /*While not exit option*/
            
            switch(input){
            case 1:    /*COME BET*/
                bet_number = getUserInput(BET_NORMAL);
                bet_amount = getUserInput(AMOUNT);
                makeBet(bet_number, bet_amount, COME);
                printMenu();
                input = getUserInput(MENU);
                break;
            case 2:    /*DONT COME BET*/
                bet_number = getUserInput(BET_NORMAL);
                bet_amount = getUserInput(AMOUNT);
                makeBet(bet_number, bet_amount, DONT_COME);
                printMenu();
                input = getUserInput(MENU);
                break;
            case 4:    /*PASS LINE OPTION*/
                bet_amount = getUserInput(AMOUNT);
                makeBet(0, bet_amount, PASS_LINE);
                printMenu();
                input = getUserInput(MENU);
                break;
            case 5:    /*DONT PASS LINE OPTION*/
                bet_amount = getUserInput(AMOUNT);
                makeBet(0, bet_amount, DONT_PASS_LINE);
                printMenu();
                input = getUserInput(MENU);
                break;
            case 6: /*Withdraw Bet (non pass lines)*/
                bet_number = getUserInput(BET_NORMAL);
                bet_amount = getUserInput(AMOUNT);
                option = getUserInput(OPTION);
                withdraw(bet_number, bet_amount, option);
                printMenu();
                input = getUserInput(MENU);
                break;
            case 7:
                dice_1 = rollDice();
                dice_2 = rollDice();
                sum = dice_1 + dice_2;
                roll_number++; 
                break;
            case 8:
                printTable();
                printMenu();
                input = getUserInput(MENU);
                break;
            }
    
            /*Game Status*/
            /**********COME OUT ROLL*****************************/
            if(roll_number == 1 && (sum == 7 || sum == 11)){ /*Determine come-out roll and point*/
                clear(0, DONT_PASS_LINE);
                pay(0, PASS_LINE);
                withdraw(0, pass_line, PASS_LINE);
                roll_number = 0;
                gameStatus = WIN;
            }else if(roll_number == 1 && (sum == 2 || sum == 3)){
                clear(0, PASS_LINE);
                pay(0, DONT_PASS_LINE);
                withdraw(0, dont_pass_line, DONT_PASS_LINE);
                roll_number = 0;
                gameStatus = LOSE;
            }else if(roll_number == 12){
                gameStatus = WIN; /*OR LOSE, DOESNT MATTER*/
            }else if(roll_number == 0){
                continue;
            }else{
                gameStatus = CONTINUE;
            }
            /*******************************************************************/
            /***********AFTER COME OUT ROLL******************/
            while(gameStatus == CONTINUE){
                printf("Game Status: %d\n", gameStatus);
                point = sum;
                /**/
                printf("Press any key to continue.....");
                getchar();
                dice_1 = rollDice();
                dice_2 = rollDice();
                sum = dice_1 + dice_2;
                printf("Dice :    %d    %d\n", dice_1, dice_2);
    
                if(sum == 7){
                    gameStatus = LOSE;
                    printf("Crapped Out %d\n", gameStatus);
                    for(i = 0; i < 6; i++){ /*Clear COME BETS*/
                        clear(i, COME);
                    }
                    roll_number = 0; /*Reset roll counter*/
                }else if(sum == 2 || sum == 12 || sum == 11){
                    gameStatus = CONTINUE;
                    printf("Game status else-if %d\n", gameStatus);
                }else if(sum == point){ /*Clear DONT COMES also*/
                    pay(point, COME);
                    pay(0, PASS_LINE);
                    gameStatus = WIN;
                    for(i = 0; i < 6; i++){
                        clear(i, DONT_COME);
                    }
                    roll_number = 0;
                }else{                    /*MODIFY for HARDWAYS*/
                    pay(sum, COME);
                }
            }
        }
        return 0;
    }
    
    void printTable(void){
    
        int i;
        
        printf("----------------------\n");
        printf("|%-15s: $%2d|\n", "Pot", pot);
        printf("|--------------------|\n");
        printf("|%-15s: $%2d|\n", "Pass Line", pass_line);
        printf("|%-15s: $%2d|\n", "Dont Pass Line", dont_pass_line);
        printf("|--------------------|\n");
        printf("|DONT                |\n");
        printf("|COME            COME|\n");
        printf("|--------------------|\n");
    
        /*for loop to print table numbers*/
        for(i = 0;  i < 6; i++){
            printf("| %3d |    %-2d  | %3d |\n", dont_come_bet[i], table[i], come_bet[i]);        /*MODIFY*/
        }
    
        printf("|--------------------|\n");
    }
    
    char isPrintComeBet(int i){
    
        return (come_bet[i] == 0 ? ' ' : come_bet[i]);
    }
    
    
    char isPrintDontComeBet(int i){
    
        return (dont_come_bet[i] == 0 ? ' ' : 'X');
    }
    
    void printMenu(void){
    
        printf("1 - Make Come Bet\n");
        printf("2 - Make Dont Come Bet\n");
        printf("3 - Make Hardways Bet\n");
        printf("4 - Make Pass Line Bet\n");
        printf("5 - Make Dont Pass Line Bet\n");
        printf("6 - Withdraw Bet\n");
        printf("7 - Roll Dice\n");
        printf("8 = Print Table\n");
        printf("9 - Exit\n");
    
    }
    
    int getUserInput(int inputType){
    
        int x;
    
        if(inputType == MENU){
            do{
                printf(">");
                scanf("\n%d", &x);
            }while(x < 1 || x > 9);
        }else if(inputType == BET_NORMAL){
            do{
                printf("NUMBER> ");
                scanf("\n%d", &x);
            }while(x != 4 && x != 5  && x != 6 && x != 8 && x != 9 && x != 10);
        }else if(inputType == BET_HARDWAYS){
            do{
                printf("NUMBER > ");
                scanf("\n%d", &x);
            }while(x != 2 && x != 4 && x != 6 && x != 8 && x != 10 && x != 12);
        }else if(inputType == AMOUNT){
            do{
                printf("Amount > $");
                scanf("\n%d", &x);
            }while(x < 0);
        }else if(inputType == OPTION){
            do{
                printf("(1 - COME, 2 - DONT COME, 3 - PASS,"
                       " 4 - DONT PASS, 5 - HARDWAYS) > ");
                scanf("\n%d", &x);
            }while(x < 1 || x > 5);
            switch(x){
            case 1:
                return COME;
                break;
            case 2:
                return DONT_COME;
                break;
            case 3:
                return PASS_LINE;
                break;
            case 4:
                return DONT_PASS_LINE;
                break;
            case 5:
                return HARD;
                break;
            }
        }else{
            x = 0;
        }
    
        return x;
    }
    
    void makeBet(int number, int amount, int option){
    
        if(option == COME){   /*MODIFY FOR ODDS*/
            switch(number){
            case 4:
                come_bet[FOUR] = amount;
                pot -= amount;
                break;
            case 5:
                come_bet[FIVE] = amount;
                pot -= amount;
                break;
            case 6:
                come_bet[SIX] = amount;
                pot -= amount;
                break;
            case 8:
                come_bet[EIGHT] = amount;
                pot -= amount;
                break;
            case 9:
                come_bet[NINE] = amount;
                pot -= amount;
                break;
            case 10:
                come_bet[TEN] = amount;
                pot -= amount;
                break;
            }
        }else if(option == DONT_COME){
            switch(number){
            case 4:
                dont_come_bet[FOUR] = amount;
                pot -= amount;
                break;
            case 5:
                dont_come_bet[FIVE] = amount;
                pot -= amount;
                break;
            case 6:
                dont_come_bet[SIX] = amount;
                pot -= amount;
                break;
            case 8:
                dont_come_bet[EIGHT] = amount;
                pot -= amount;
                break;
            case 9:
                dont_come_bet[NINE] = amount;
                pot -= amount;
                break;
            case 10:
                dont_come_bet[TEN] = amount;
                pot -= amount;
                break;
            }
    
        }else if(option == PASS_LINE){
            pass_line = amount;
            pot -= amount;
        }else if(option == DONT_PASS_LINE){
            dont_pass_line = amount;
            pot -= amount;
        }else{
            printf("END OF MAKEBET() OPTIONS.\n");
        }
    
    }
    
    void pay(int item, int option){
    
        if(option == COME){
            pot += come_bet[item];
        }else if(option == DONT_COME){
            pot += dont_come_bet[item];
        }else if(option == HARD){
            pot += hard_bet_odds[item] * hard_bet[item];
        }else if(option == PASS_LINE){
            pot += pass_line;
        }else if(option == DONT_PASS_LINE){
            pot += dont_pass_line;
        }else{
            printf("Incorrect pay() option.\n");
        }
    }
    
    /*void clear(int item, int option){
    
        if(option == COME){
            pot += come_bet[item];
        }else if(option == DONT_COME){
            pot += dont_come_bet[item];
        }else if(option == HARD){
            pot += hard_bet_odds[item] * hard_bet[item];
        }else if(option == PASS_LINE){
            pot += pass_line;
        }else if(option == DONT_PASS_LINE){
            pot += dont_pass_line;
        }else{
            printf("Incorrect clear() option.\n");
        }
    }*/
    void withdraw(int item, int amount, int option){
    
        if(option == COME){
            if(amount > come_bet[item])
                printf("Attempted withdraw() exceeds amount on table.\n");
            else{
                pot += amount;
                come_bet[item] -= amount;
            }
        }else if(option == DONT_COME){
            if(amount > dont_come_bet[item])
                printf("Attempted withdraw() exceeds amount on table.\n");
            else{
                pot += amount;
                dont_come_bet[item] -= amount;
            }
        }else if(option == HARD){
            if(amount > hard_bet[item])
                printf("Attempted withdraw() exceeds amount on table.\n");
            else{
                pot += amount;
                hard_bet[item] -= amount;
            }
            
        }else if(option == PASS_LINE){
            if(roll_number > 0){
                printf("Cannot Withdraw from Passline.\n");
            }else{
                if(amount > pass_line)
                    printf("Attempted withdraw() exceeds amount on table.\n");
                else{
                    pot += amount;
                    pass_line -= amount;
                }
            }
            
        }else if(option == DONT_PASS_LINE){
            if(roll_number > 0){
                printf("Cannot Withdraw from Dont Pass line.\n");
            }else{
                if(amount > dont_pass_line)
                    printf("Attempted withdraw() exceeds amount on table.\n");
                else{
                    pot += amount;
                    dont_pass_line -= amount;
                }
            }
        }else{
            printf("Incorrect withdraw() option.\n");
        }
    }
    
    void clear(int item, int option){
    
        if(option == COME){
            come_bet[item] = 0;
        }else if(option == DONT_COME){
            come_bet[item] = 0;
        }else if(option == HARD){
            hard_bet[item] = 0;
        }else if(option == PASS_LINE){
            pass_line = 0;
        }else if(option == DONT_PASS_LINE){
            dont_pass_line = 0;
        }else{
            printf("Incorrect clear() option.\n");
        }
    }
    
    int rollDice(void){
    
        int dice;
    
        dice = 1 + rand() % 6;
    
        return dice;
    }

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    your printfs don't really show the sequence of what is happening. i set a breakpoint on line 167 where it hits LOSE. the program exits the while loop as expected, but the main loop doesn't detect the LOSE so it keeps rolling the dice and you get back to the while(gamestatus==continue) line.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Craps game in C!
    By Maria22 in forum C Programming
    Replies: 2
    Last Post: 04-08-2011, 11:13 PM
  2. Craps game.....Compile error.. Help whit this Code please
    By thebridge in forum C Programming
    Replies: 4
    Last Post: 10-07-2010, 03:52 PM
  3. Craps Game in C
    By clipsit in forum C Programming
    Replies: 3
    Last Post: 02-19-2008, 03:09 PM
  4. Craps game
    By egomaster69 in forum C Programming
    Replies: 2
    Last Post: 10-25-2004, 07:00 PM
  5. craps game
    By loki221 in forum C Programming
    Replies: 5
    Last Post: 12-02-2001, 09:33 PM