Unhandled exception at 0x66df984f (msvcr90d.dll).

This is a discussion on Unhandled exception at 0x66df984f (msvcr90d.dll). within the C Programming forums, part of the General Programming Boards category; I just spent a lot of time on this program, and suddenly it's not working. I evaluate it in Microsoft ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    14

    Unhandled exception at 0x66df984f (msvcr90d.dll).

    I just spent a lot of time on this program, and suddenly it's not working. I evaluate it in Microsoft Visual Studio, and there are no errors or warnings, but when I to Debug it, it always either freezes or gives me an "Unhandled exception at 0x66df984f (msvcr90d.dll) in Poker.exe: 0xC0000005: Access violation reading location 0xcccccccc." error. If I break there, it points to Line 1643 in Output.c, which relates to the #else command. I've tried to troubleshoot this, but can't figure out the source of the problem.


    Here's my code (1 header file and two source files):

    poker.h
    Code:
    #ifndef POKER_H
    #define POKER_H
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    void shuffle (int wDeck[][13]);
    void deal (const int wDeck[][13], const char *wFace[], const char *wSuit[], int wHand[][5], int *dealtp, int *player);
    void high_card (int wHand[][5], int *highcard);
    void pair (int wHand[][5], int *eval, int *highcard);
    void twopair (int wHand[][5], int *eval, int *highcard);
    void threekind (int wHand[][5], int *eval, int *highcard);
    void straight (int wHand[][5], int *eval, int *highcard);
    void flush (int wHand[][5], int *eval, int *highcard);
    void fourkind (int wHand[][5], int *eval, int *highcard);
    
    void dealerAI (const int wDeck[][13], int wHand[][5], int *dealtp, int *eval, int *highcard);
    void showhand (int wHand[][5], int *eval, int *highcard, const char *wFace[], const char *wSuit[]);
    /*void draw (const int wDeck[][13], int wHand[][5], int *dealtp, int *eval, int *highcard);*/
    
    #endif
    poker.c
    Code:
    #include "poker.h"
    
    /* shuffle cards in deck */
    
    void shuffle (int wDeck[][13])
    {
    	int row = 0;    /* row number */
    	int column = 0; /*column number */
    	int card = 0;   /* card counter */
    
    	/* for each of the 52 cards, choose slot of deck randomly */
    	for (card = 1; card <= 52; card++)
    	{
    		/* choose new random location until unoccupied slot found */
    		do
    		{
    			row = rand () % 4;
    			column = rand () % 13;
    		} while (wDeck[row][column] != 0);
    
    		/* place card number in chosen slot of deck */
    		wDeck[row][column] = card;
    	}
    }
    
    /* deal cards in deck */
    void deal (const int wDeck[][13], const char *wFace[], const char *wSuit[], int wHand[][5], int *dealtp, int *player)
    {
    	int row = 0;    /* row number */
    	int column = 0; /*column number */
    	int card = 0;   /* card counter */
     
    	/* deal 5 cards */
    	for (card = 1; card <= 5; card++)
    	{
    		*dealtp = *dealtp + 1;
    		/* loop through rows of wDeck */
    		for (row = 0; row <= 3; row++)
    		{
    			/* loop through columns of wDeck for current row */
    			for (column = 0; column <= 13; column++)
    			{
    				/* if slot contains current card, display card */
    				if (wDeck[row][column] == *dealtp)
    				{
    					if (*player==1)
    						{
    						printf ("%5s of %-8s%c", wFace[column], wSuit[row], card % 2 == 0 ? '\n' : '\t');
    						}
    					wHand[0][card - 1] = row;
    					wHand[1][card - 1] = column;
    				}
    			}
    		}
    	}
    }
    
    void high_card (int wHand[][5], int *highcard)
    {
    	int column = 0;
    
    	*highcard = wHand[1][0]; /*initial value*/
    	for (column = 0; column = 3; column ++)
    	{
    		if (wHand[1][column+1] > wHand[1][column])
    		{
    			*highcard = wHand[1][column+1];
    		}
    	}
    }
    
    void pair (int wHand[][5], int *eval, int *highcard)
    {
    	int column1 = 0;
    	int column2 = 0;
    
    	for (column1 = 0; column1 <=3; column1++)
    	{
    		for (column2 = column1+1; column2 <=4; column2++)
    		{
    			if (wHand[1][column1] == wHand[1][column2]) /*pair*/
    			{
    				*eval = 2; /*pair*/
    				*highcard = wHand[1][column1];
    			}
    		}
    	}
    }
    void twopair (int wHand[][5], int *eval, int *highcard)
    {
    	int column1 = 0;
    	int column2 = 0;
    	int column3 = 0;
    	int column4 = 0;
    
    	for (column1 = 0; column1 <=1; column1++)
    	{
    		for (column2 = column1+1; column2 <=4; column2++)
    		{
    			if (wHand[1][column1] == wHand[1][column2]) /*1 pair*/
    			{
    				for (column3 = column1+1; column3 <=3; column3++)
    				{
    					for (column4 = column3+1; column4 <=4; column4++)
    					{
    						if (wHand[1][column3] == wHand[1][column4]) /*2 pair*/
    						{
    							*eval = 3; /*two pair*/
    							if (wHand[1][column1] > wHand[1][column3])
    							{
    								*highcard = wHand[1][column1];
    							}
    							else
    							{
    								*highcard = wHand[1][column3];
    							}
    						}
    					}			
    				}
    			}
    		}
    	}
    }
    void threekind (int wHand[][5], int *eval, int *highcard)
    {
    	int column1 = 0;
    	int column2 = 0;
    	int column3 = 0;
    
    	for (column1 = 0; column1 <=2; column1++)
    	{
    		for (column2 = column1+1; column2 <=3; column2++)
    		{
    			if (wHand[1][column1] == wHand[1][column2]) /*1 pair*/
    			{
    				for (column3 = column2+1; column3 <=4; column3++)
    				{
    					if (wHand[1][column2] == wHand[1][column3]) /*3 of a kind*/
    					{
    						*eval = 4; /*three of a kind*/
    						*highcard = wHand[1][column1];
    					}
    				}
    			}
    		}
    	}
    }
    
    void straight (int wHand[][5], int *eval, int *highcard)
    {
    	int fill = 0;	  /*element in array*/
    	int	fill2 = 0;	  /*element in array*/
    	int tempface = 0;	  /*temporary storage of face value*/
    	int tempsuit = 0;	  /*temporary storage of suit value*/
    	int check = 1; /*checks for straight, 0 = not straight, 1 = straight */
    	
    	for (fill = 0; fill <= 3; fill++)
    	{
    		for (fill2 = 0; fill2 <= (3-fill); fill2++)
    		{
    			if(wHand[1][fill2+1] < wHand[1][fill2]) /*exchange values*/
    			{
    				tempface = wHand[1][fill2];
    				tempsuit = wHand[0][fill2];
    				wHand[1][fill2] = wHand[1][fill2+1];
    				wHand[0][fill2] = wHand[0][fill2+1];
    				wHand[1][fill2+1] = tempface;
    				wHand[0][fill2+1] = tempsuit;
    			}
    		}
    	}
    	for (fill = 0; fill <=3; fill++)
    	{
    		if ((wHand[1][fill+1]-wHand[1][fill])!=0)
    		{
    			check = 0;
    		}
    	}
    	if (check == 1);
    	{
    		*eval = 5; /*straight*/
    		*highcard = wHand[1][4];
    	}
    }
    
    void flush (int wHand[][5], int *eval, int *highcard)
    {
    	int column = 0;
    	int check = 1; /*checks for flush, 0 = not flush, 1 = straight*/
    
    	for (column = 0; column <=3; column++)
    	{
    		if (wHand[0][column+1]!=wHand[0][column])
    		{
    			check = 0;
    		}
    		if (check == 1);
    		{
    			*eval = 6; /*flush*/
    			*highcard = wHand[1][4]; /*as array was sorted in straight function*/
    		}
    	}
    }
    
    void fourkind (int wHand[][5], int *eval, int *highcard)
    {
    	int column1 = 0;
    
    	for (column1 = 0; column1 <=1; column1++)
    	{
    		if (wHand[1][column1]==wHand[1][column1+3]) /*simplified due to sorting in straight function*/
    		{
    			*eval = 7; /*four of a kind*/
    			*highcard = wHand[1][column1];
    		}
    	}
    }
    
    void dealerAI (const int wDeck[][13], int wHand[][5], int *dealtp, int *eval, int *highcard)
    {
    	int row = 0; /*rows of deck*/
    	int column = 0; /*columns of deck*/
    	int card1 = 0; /*indicates whether to replace card.  0 == don't replace, 1 == replace*/
    	int card2 = 0;
    	int card3 = 0;
    	int card4 = 0;
    	int card5 = 0;
    
    	/*able to simplify statements due to number sorting in straight function*/
    
    	/*do nothing for eval == 5, 6 or 7*/
    	if (*eval == 4) /*three of a kind*/
    	{
    		if (wHand[1][0]!=wHand[1][1])
    		{
    			card1 = 1;
    			card5 = 1;
    		}
    		else if (wHand[1][1]!=wHand[1][2])
    		{
    			card1 = 1;
    			card2 = 1;
    		}
    		else
    		{
    			card4 = 1;
    			card5 = 1;
    		}
    	}
    	if (*eval == 3) /*two pair*/
    	{
    		if (wHand[1][0]!=wHand[1][1])
    		{
    			card1 = 1;
    		}
    		else if (wHand[1][3]!=wHand[1][4])
    		{
    			card5 = 1;
    		}
    		else
    		{
    			card3 = 1;
    		}
    	}
    	if (*eval == 2) /*one pair*/
    	{
    		if (wHand[1][0]==wHand[1][1])
    		{
    			card3 = 1;
    			card4 = 1;
    			card5 = 1;
    		}
    		else if (wHand[1][1]==wHand[1][2])
    		{
    			card1 = 1;
    			card4 = 1;
    			card5 = 1;
    		}
    		else if (wHand[1][2]==wHand[3][1])
    		{
    			card1 = 1;
    			card2 = 1;
    			card5 = 1;
    		}
    		else
    		{
    			card1 = 1;
    			card2 = 1;
    			card3 = 1;
    		}
    	}
    	if (*eval == 0) /*high card*/
    	{
    		/*replace three lowest cards*/
    		card1 = 1; 
    		card2 = 1;
    		card3 = 1;
    	}
    
    	/*deal AI new cards*/
    	if (card1 == 1)
    	{
    		*dealtp = *dealtp + 1;
    		/* loop through rows of wDeck */
    		for (row = 0; row <= 3; row++)
    		{
    			/* loop through columns of wDeck for current row */
    			for (column = 0; column <= 13; column++)
    			{
    				if (wDeck[row][column] == *dealtp)
    				{
    					wHand[0][0] = row;
    					wHand[1][0] = column;
    				}
    			}
    		}
    	}
    	if (card2 == 1)
    	{
    		*dealtp = *dealtp + 1;
    		/* loop through rows of wDeck */
    		for (row = 0; row <= 3; row++)
    		{
    			/* loop through columns of wDeck for current row */
    			for (column = 0; column <= 13; column++)
    			{
    				if (wDeck[row][column] == *dealtp)
    				{
    					wHand[0][1] = row;
    					wHand[1][1] = column;
    				}
    			}
    		}
    	}
    	if (card3 == 1)
    	{
    		*dealtp = *dealtp + 1;
    		/* loop through rows of wDeck */
    		for (row = 0; row <= 3; row++)
    		{
    			/* loop through columns of wDeck for current row */
    			for (column = 0; column <= 13; column++)
    			{
    				if (wDeck[row][column] == *dealtp)
    				{
    					wHand[0][2] = row;
    					wHand[1][2] = column;
    				}
    			}
    		}
    	}
    	if (card4 == 1)
    	{
    		*dealtp = *dealtp + 1;
    		/* loop through rows of wDeck */
    		for (row = 0; row <= 3; row++)
    		{
    			/* loop through columns of wDeck for current row */
    			for (column = 0; column <= 13; column++)
    			{
    				if (wDeck[row][column] == *dealtp)
    				{
    					wHand[0][3] = row;
    					wHand[1][3] = column;
    				}
    			}
    		}
    	}
    	if (card5 == 1)
    	{
    		*dealtp = *dealtp + 1;
    		/* loop through rows of wDeck */
    		for (row = 0; row <= 3; row++)
    		{
    			/* loop through columns of wDeck for current row */
    			for (column = 0; column <= 13; column++)
    			{
    				if (wDeck[row][column] == *dealtp)
    				{
    					wHand[0][4] = row;
    					wHand[1][4] = column;
    				}
    			}
    		}
    	}
    }
    void showhand (int wHand[][5], int *eval, int *highcard, const char *wFace[], const char *wSuit[])
    {
    	int suit = 0;
    	int face = 0;
    
    	for (suit = 0; suit <= 4; suit++)
    	{
    		face = suit;
    		printf ("%5s of %-8s%c\n", wFace[face], wSuit[suit], 5 % 2 == 0 ? '\n' : '\t');
    	}
    	if (*eval == 7)
    	{
    		printf ("You have a Four of a Kind,\n%6s High!", wFace[*highcard]);
    	}
    	else if (*eval == 6)
    	{
    		printf ("You have a Flush,\n%6s High!", wFace[*highcard]);
    	}
    	else if (*eval == 5)
    	{
    		printf ("You have a Straight,\n%6s High!", wFace[*highcard]);
    	}
    	else if (*eval == 4)
    	{
    		printf ("You have a Three of a Kind,\n%6s High!", wFace[*highcard]);
    	}
    	else if (*eval == 3)
    	{
    		printf ("You have Two Pair,\n%6s High.", wFace[*highcard]);
    	}
    	else if (*eval == 2)
    	{
    		printf ("You have a Pair,\n%6s High.", wFace[*highcard]);
    	}
    	else
    	{
    		printf ("Garbage.\n%6s High.", wFace[*highcard]);
    	}
    }
    main.c
    Code:
    #include "poker.h"
    
    
    int main (void)
    {
    	/* initialize suit array */
    	const char *suit[4] = {"Hearts", "Diamonds", "Clubs", "Spades"};
    
    	/* initialize face array */
    	const char *face[13] = {"Ace", "Deuce", "Three", "Four", "Five", "Size", "Seven", "Eight",
    		"Nine", "Ten", "Jack", "Queen", "King"};
    
    	int deck[4][13] = {0}; /*initialize deck array*/
    	int handP1[2][5] = {0}; /*initialize player's hand array*/
    	int handAI[2][5] = {0}; /*initialize opponent's hand array*/
    	int player = 0; /*determines whose turn it is*/
    	int dealt = 0; /*tracks total number of cards dealt*/
    	int evalP1 = 0; /*evaluates player's hand*/
    	int evalAI = 0; /*evaluates opponent's hand*/
    	int highP1 = 0; /*highest-value card in player's hand that is part of a set*/
    	int highAI = 0; /*highest-value card in opponent's hand that is part of a set*/
    
    	srand ((unsigned) time (NULL)); /* see random-number generator */
    
    	shuffle (deck);
    	player = 1;
    	deal (deck, face, suit, handP1, &dealt, &player);
    	player = 2;
    	deal (deck, face, suit, handAI, &dealt, &player);
    
    	/*evaluate player's hand*/
    	high_card (handP1, &highP1);
    	pair (handP1, &evalP1, &highP1);
    	twopair (handP1, &evalP1, &highP1);
    	threekind (handP1, &evalP1, &highP1);
    	straight (handP1, &evalP1, &highP1);
    	flush (handP1, &evalP1, &highP1);
    	fourkind (handP1, &evalP1, &highP1);
    
    	/*evaluate opponent's hand*/
    	high_card (handAI, &highAI);
    	pair (handAI, &evalAI, &highAI);
    	twopair (handAI, &evalAI, &highAI);
    	threekind (handAI, &evalAI, &highAI);
    	straight (handAI, &evalAI, &highAI);
    	flush (handAI, &evalAI, &highAI);
    	fourkind (handAI, &evalAI, &highAI);
    
    	/*dealer draws cards*/
    	dealerAI (deck, handAI, &dealt, &evalAI, &highAI);
    
    	/*show player his hand*/
    	showhand (handP1, &evalP1, &highP1, face, suit);
    	/*player draws cards*/
    
    
    	return 0;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    c++ - When and why will an OS initialise memory to 0xCD, 0xDD, etc. on malloc/free/new/delete? - Stack Overflow
    0xCC means you're reading uninitialised data.

    > for (card = 1; card <= 52; card++)
    Arrays START AT ZERO!
    99.99% chance here that you wandered off the end of the array and into la-la land.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    14
    Quote Originally Posted by Salem View Post
    c++ - When and why will an OS initialise memory to 0xCD, 0xDD, etc. on malloc/free/new/delete? - Stack Overflow
    0xCC means you're reading uninitialised data.

    > for (card = 1; card <= 52; card++)
    Arrays START AT ZERO!
    99.99% chance here that you wandered off the end of the array and into la-la land.

    I just checked that, and that doesn't seem to be where the problem is. card is just an integer value inserted into the 4x13 array wDeck.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    I think your suit loop goes too far:
    Code:
    for (suit = 0; suit <= 4; suit++)

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    I found at least half a dozen other array overruns, but I've no interest at all in pointing out all of them to you.

    I've told you how arrays work, it's up to you to find and fix all the instances where you get it wrong.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    14
    Quote Originally Posted by nonoob View Post
    I think your suit loop goes too far:
    Code:
    for (suit = 0; suit <= 4; suit++)
    Unfortunately not (it serves a different purpose than you think...I haven't had a chance to add all my notes yet), but thanks for mentioning that. I noticed that part was coded wrong for another reason. It should be:
    Code:
    void showhand (int wHand[][5], int *eval, int *highcard, const char *wFace[], const char *wSuit[])
    {
    	int suit = 0;
    	int face = 0;
    	int column = 0;
    
    	for (column = 0; column <= 4; suit++)
    	{
    		wHand[0][column] = suit;
    		wHand[1][column] = face;
    		printf ("%d. %5s of %-8s%c", suit+1, wFace[face], wSuit[suit], 5 % 2 == 0 ? '\n' : '\t');
    	}
    	if (*eval == 7)
    	{
    		printf ("You have a Four of a Kind,\n%6s High!", wFace[*highcard]);
    	}
    	else if (*eval == 6)
    	{
    		printf ("You have a Flush,\n%6s High!", wFace[*highcard]);
    	}
    	else if (*eval == 5)
    	{
    		printf ("You have a Straight,\n%6s High!", wFace[*highcard]);
    	}
    	else if (*eval == 4)
    	{
    		printf ("You have a Three of a Kind,\n%6s High!", wFace[*highcard]);
    	}
    	else if (*eval == 3)
    	{
    		printf ("You have Two Pair,\n%6s High.", wFace[*highcard]);
    	}
    	else if (*eval == 2)
    	{
    		printf ("You have a Pair,\n%6s High.", wFace[*highcard]);
    	}
    	else
    	{
    		printf ("Garbage.\n%6s High.", wFace[*highcard]);
    	}
    }

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Code:
    for (column = 0; column <= 4; suit++)
    column++, surely?

  8. #8
    Registered User
    Join Date
    Nov 2010
    Posts
    14
    Quote Originally Posted by tabstop View Post
    Code:
    for (column = 0; column <= 4; suit++)
    column++, surely?
    Ah crap. I've been up way too long.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Posts
    14
    Quote Originally Posted by Salem View Post
    I found at least half a dozen other array overruns, but I've no interest at all in pointing out all of them to you.

    I've told you how arrays work, it's up to you to find and fix all the instances where you get it wrong.
    Okay, I rewrote all the arrays so they start at zero, but I'm still getting the same error.

    New poker.c:
    Code:
    #include "poker.h"
    
    /* shuffle cards in deck */
    
    void shuffle (int wDeck[][13])
    {
    	int row = 0;    /* row number */
    	int column = 0; /*column number */
    	int card = 0;   /* card counter */
    
    	/* for each of the 52 cards, choose slot of deck randomly */
    	for (card = 0; card <= 51; card++)
    	{
    		/* choose new random location until unoccupied slot found */
    		do
    		{
    			row = rand () % 4;
    			column = rand () % 13;
    		} while (wDeck[row][column] != 0);
    
    		/* place card number in chosen slot of deck */
    		wDeck[row][column] = card+1;
    	}
    }
    
    /* deal cards in deck */
    void deal (const int wDeck[][13], const char *wFace[], const char *wSuit[], int wHand[][5], int *dealtp, int *player)
    {
    	int row = 0;    /* row number */
    	int column = 0; /*column number */
    	int card = 0;   /* card counter */
     
    	/* deal 5 cards */
    	for (card = 0; card <= 4; card++)
    	{
    		*dealtp = *dealtp + 1;
    		/* loop through rows of wDeck */
    		for (row = 0; row <= 3; row++)
    		{
    			/* loop through columns of wDeck for current row */
    			for (column = 0; column <= 13; column++)
    			{
    				/* if slot contains current card, display card */
    				if (wDeck[row][column] == *dealtp)
    				{
    					if (*player==1)
    						{
    						printf ("%5s of %-8s%c", wFace[column], wSuit[row], card % 2 == 0 ? '\n' : '\t');
    						}
    					wHand[0][card - 1] = row;
    					wHand[1][card - 1] = column;
    				}
    			}
    		}
    	}
    }
    
    void high_card (int wHand[][5], int *highcard)
    {
    	int column = 0;
    
    	*highcard = wHand[1][0]; /*initial value*/
    	for (column = 0; column = 3; column ++)
    	{
    		if (wHand[1][column+1] > wHand[1][column])
    		{
    			*highcard = wHand[1][column+1];
    		}
    	}
    }
    
    void pair (int wHand[][5], int *eval, int *highcard)
    {
    	int column1 = 0;
    	int column2 = 0;
    
    	for (column1 = 0; column1 <=3; column1++)
    	{
    		for (column2 = 0; column2 <=3; column2++)
    		{
    			if (wHand[1][column1] == wHand[1][column2+1]) /*pair*/
    			{
    				*eval = 2; /*pair*/
    				*highcard = wHand[1][column1];
    			}
    		}
    	}
    }
    void twopair (int wHand[][5], int *eval, int *highcard)
    {
    	int column1 = 0;
    	int column2 = 0;
    	int column3 = 0;
    	int column4 = 0;
    
    	for (column1 = 0; column1 <=1; column1++)
    	{
    		for (column2 = 0; column2 <=3; column2++)
    		{
    			if (wHand[1][column1] == wHand[1][column2+1]) /*1 pair*/
    			{
    				for (column3 = 0; column3 <= 2; column3++)
    				{
    					for (column4 = 0; column4 <=1; column4++)
    					{
    						if (wHand[1][column3+2] == wHand[1][column4+3]) /*2 pair*/
    						{
    							*eval = 3; /*two pair*/
    							if (wHand[1][column1] > wHand[1][column3])
    							{
    								*highcard = wHand[1][column1];
    							}
    							else
    							{
    								*highcard = wHand[1][column3];
    							}
    						}
    					}			
    				}
    			}
    		}
    	}
    }
    void threekind (int wHand[][5], int *eval, int *highcard)
    {
    	int column1 = 0;
    	int column2 = 0;
    	int column3 = 0;
    
    	for (column1 = 0; column1 <=2; column1++)
    	{
    		for (column2 = 0; column2 <=2; column2++)
    		{
    			if (wHand[1][column1] == wHand[1][column2+1]) /*1 pair*/
    			{
    				for (column3 = 0; column3 <=2; column3++)
    				{
    					if (wHand[1][column1] == wHand[1][column3+2]) /*3 of a kind*/
    					{
    						*eval = 4; /*three of a kind*/
    						*highcard = wHand[1][column1];
    					}
    				}
    			}
    		}
    	}
    }
    
    void straight (int wHand[][5], int *eval, int *highcard)
    {
    	int fill = 0;	  /*element in array*/
    	int	fill2 = 0;	  /*element in array*/
    	int tempface = 0;	  /*temporary storage of face value*/
    	int tempsuit = 0;	  /*temporary storage of suit value*/
    	int check = 1; /*checks for straight, 0 = not straight, 1 = straight */
    	
    	for (fill = 0; fill <= 3; fill++)
    	{
    		for (fill2 = 0; fill2 <= (3-fill); fill2++)
    		{
    			if(wHand[1][fill2+1] < wHand[1][fill2]) /*exchange values*/
    			{
    				tempface = wHand[1][fill2];
    				tempsuit = wHand[0][fill2];
    				wHand[1][fill2] = wHand[1][fill2+1];
    				wHand[0][fill2] = wHand[0][fill2+1];
    				wHand[1][fill2+1] = tempface;
    				wHand[0][fill2+1] = tempsuit;
    			}
    		}
    	}
    	for (fill = 0; fill <=3; fill++)
    	{
    		if ((wHand[1][fill+1]-wHand[1][fill])!=0)
    		{
    			check = 0;
    		}
    	}
    	if (check == 1);
    	{
    		*eval = 5; /*straight*/
    		*highcard = wHand[1][4];
    	}
    }
    
    void flush (int wHand[][5], int *eval, int *highcard)
    {
    	int column = 0;
    	int check = 1; /*checks for flush, 0 = not flush, 1 = straight*/
    
    	for (column = 0; column <=3; column++)
    	{
    		if (wHand[0][column+1]!=wHand[0][column])
    		{
    			check = 0;
    		}
    		if (check == 1);
    		{
    			*eval = 6; /*flush*/
    			*highcard = wHand[1][4]; /*as array was sorted in straight function*/
    		}
    	}
    }
    
    void fourkind (int wHand[][5], int *eval, int *highcard)
    {
    	int column1 = 0;
    
    	for (column1 = 0; column1 <=1; column1++)
    	{
    		if (wHand[1][column1]==wHand[1][column1+3]) /*simplified due to sorting in straight function*/
    		{
    			*eval = 7; /*four of a kind*/
    			*highcard = wHand[1][column1];
    		}
    	}
    }
    
    void dealerAI (const int wDeck[][13], int wHand[][5], int *dealtp, int *eval, int *highcard)
    {
    	int row = 0; /*rows of deck*/
    	int column = 0; /*columns of deck*/
    	int card1 = 0; /*indicates whether to replace card.  0 == don't replace, 1 == replace*/
    	int card2 = 0;
    	int card3 = 0;
    	int card4 = 0;
    	int card5 = 0;
    
    	/*able to simplify statements due to number sorting in straight function*/
    
    	/*do nothing for eval == 5, 6 or 7*/
    	if (*eval == 4) /*three of a kind*/
    	{
    		if (wHand[1][0]!=wHand[1][1])
    		{
    			card1 = 1;
    			card5 = 1;
    		}
    		else if (wHand[1][1]!=wHand[1][2])
    		{
    			card1 = 1;
    			card2 = 1;
    		}
    		else
    		{
    			card4 = 1;
    			card5 = 1;
    		}
    	}
    	if (*eval == 3) /*two pair*/
    	{
    		if (wHand[1][0]!=wHand[1][1])
    		{
    			card1 = 1;
    		}
    		else if (wHand[1][3]!=wHand[1][4])
    		{
    			card5 = 1;
    		}
    		else
    		{
    			card3 = 1;
    		}
    	}
    	if (*eval == 2) /*one pair*/
    	{
    		if (wHand[1][0]==wHand[1][1])
    		{
    			card3 = 1;
    			card4 = 1;
    			card5 = 1;
    		}
    		else if (wHand[1][1]==wHand[1][2])
    		{
    			card1 = 1;
    			card4 = 1;
    			card5 = 1;
    		}
    		else if (wHand[1][2]==wHand[3][1])
    		{
    			card1 = 1;
    			card2 = 1;
    			card5 = 1;
    		}
    		else
    		{
    			card1 = 1;
    			card2 = 1;
    			card3 = 1;
    		}
    	}
    	if (*eval == 0) /*high card*/
    	{
    		/*replace three lowest cards*/
    		card1 = 1; 
    		card2 = 1;
    		card3 = 1;
    	}
    
    	/*deal AI new cards*/
    	if (card1 == 1)
    	{
    		*dealtp = *dealtp + 1;
    		/* loop through rows of wDeck */
    		for (row = 0; row <= 3; row++)
    		{
    			/* loop through columns of wDeck for current row */
    			for (column = 0; column <= 13; column++)
    			{
    				if (wDeck[row][column] == *dealtp)
    				{
    					wHand[0][0] = row;
    					wHand[1][0] = column;
    				}
    			}
    		}
    	}
    	if (card2 == 1)
    	{
    		*dealtp = *dealtp + 1;
    		/* loop through rows of wDeck */
    		for (row = 0; row <= 3; row++)
    		{
    			/* loop through columns of wDeck for current row */
    			for (column = 0; column <= 13; column++)
    			{
    				if (wDeck[row][column] == *dealtp)
    				{
    					wHand[0][1] = row;
    					wHand[1][1] = column;
    				}
    			}
    		}
    	}
    	if (card3 == 1)
    	{
    		*dealtp = *dealtp + 1;
    		/* loop through rows of wDeck */
    		for (row = 0; row <= 3; row++)
    		{
    			/* loop through columns of wDeck for current row */
    			for (column = 0; column <= 13; column++)
    			{
    				if (wDeck[row][column] == *dealtp)
    				{
    					wHand[0][2] = row;
    					wHand[1][2] = column;
    				}
    			}
    		}
    	}
    	if (card4 == 1)
    	{
    		*dealtp = *dealtp + 1;
    		/* loop through rows of wDeck */
    		for (row = 0; row <= 3; row++)
    		{
    			/* loop through columns of wDeck for current row */
    			for (column = 0; column <= 13; column++)
    			{
    				if (wDeck[row][column] == *dealtp)
    				{
    					wHand[0][3] = row;
    					wHand[1][3] = column;
    				}
    			}
    		}
    	}
    	if (card5 == 1)
    	{
    		*dealtp = *dealtp + 1;
    		/* loop through rows of wDeck */
    		for (row = 0; row <= 3; row++)
    		{
    			/* loop through columns of wDeck for current row */
    			for (column = 0; column <= 13; column++)
    			{
    				if (wDeck[row][column] == *dealtp)
    				{
    					wHand[0][4] = row;
    					wHand[1][4] = column;
    				}
    			}
    		}
    	}
    }
    void showhand (int wHand[][5], int *eval, int *highcard, const char *wFace[], const char *wSuit[])
    {
    	int suit = 0;
    	int face = 0;
    	int column = 0;
    
    	for (column = 0; column <= 4; column++)
    	{
    		wHand[0][column] = suit;
    		wHand[1][column] = face;
    		printf ("%d. %5s of %-8s%c", suit+1, wFace[face], wSuit[suit], 5 % 2 == 0 ? '\n' : '\t');
    	}
    	if (*eval == 7)
    	{
    		printf ("You have a Four of a Kind,\n%6s High!", wFace[*highcard]);
    	}
    	else if (*eval == 6)
    	{
    		printf ("You have a Flush,\n%6s High!", wFace[*highcard]);
    	}
    	else if (*eval == 5)
    	{
    		printf ("You have a Straight,\n%6s High!", wFace[*highcard]);
    	}
    	else if (*eval == 4)
    	{
    		printf ("You have a Three of a Kind,\n%6s High!", wFace[*highcard]);
    	}
    	else if (*eval == 3)
    	{
    		printf ("You have Two Pair,\n%6s High.", wFace[*highcard]);
    	}
    	else if (*eval == 2)
    	{
    		printf ("You have a Pair,\n%6s High.", wFace[*highcard]);
    	}
    	else
    	{
    		printf ("Garbage.\n%6s High.", wFace[*highcard]);
    	}
    }
    void draw (const int wDeck[][13], int wHand[][5], int *dealtp, int *eval, int *highcard)
    {
    	int row = 0;
    	int column = 0;
    	int card1 = 0;
    	int card2 = 0;
    	int card3 = 0;
    	int check = 3;
    
    	if (check == 3)
    	{
    		printf ("Please select a card to replace, or type 0 to hold: ");
    		scanf ("%d", &card1);
    		if (card1 == 0)
    		{
    			check == 0;
    		}
    		else
    		{
    			check--;
    		}
    	}
    	if (check == 2)
    	{
    		printf ("Please select a second card to replace, or type 0 to hold: ");
    		scanf ("%d", &card2);
    		if (card2 == 0)
    		{
    			check == 0;
    		}
    		else
    		{
    			check--;
    		}
    	}
    	if (check == 1)
    	{
    		printf ("Please select a third card to replace, or type 0 to hold: ");
    		scanf ("%d", &card3);
    		check--;
    	}
    	if (card1!=0)
    	{
    		*dealtp = *dealtp + 1;
    		/* loop through rows of wDeck */
    		for (row = 0; row <= 3; row++)
    		{
    			/* loop through columns of wDeck for current row */
    			for (column = 0; column <= 13; column++)
    			{
    				if (wDeck[row][column] == *dealtp)
    				{
    					wHand[0][card1-1] = row;
    					wHand[1][card1-1] = column;
    				}
    			}
    		}
    	}
    	if (card2!=0)
    	{
    		*dealtp = *dealtp + 1;
    		/* loop through rows of wDeck */
    		for (row = 0; row <= 3; row++)
    		{
    			/* loop through columns of wDeck for current row */
    			for (column = 0; column <= 13; column++)
    			{
    				if (wDeck[row][column] == *dealtp)
    				{
    					wHand[0][card2-1] = row;
    					wHand[1][card2-1] = column;
    				}
    			}
    		}
    	}
    	if (card3!=0)
    	{
    		*dealtp = *dealtp + 1;
    		/* loop through rows of wDeck */
    		for (row = 0; row <= 3; row++)
    		{
    			/* loop through columns of wDeck for current row */
    			for (column = 0; column <= 13; column++)
    			{
    				if (wDeck[row][column] == *dealtp)
    				{
    					wHand[0][card3-1] = row;
    					wHand[1][card3-1] = column;
    				}
    			}
    		}
    	}
    }
    
    void compare_hands (int *eval1, int *eval2, int *highcard1, int *highcard2, int *winner)
    {
    	if (*eval1 > *eval2)
    	{
    		*winner = 1;
    	}
    	else if (*eval2 > *eval1)
    	{
    		*winner = 2;
    	}
    	else
    	{
    		if (*highcard1 > *highcard2)
    		{
    			*winner = 1;
    		}
    		else if (*highcard2 > *highcard1)
    		{
    			*winner = 2;
    		}
    		else
    		{
    			*winner = 0;
    		}
    	}
    }
    
    void won (int *winner)
    {
    	if (*winner == 1)
    	{
    		printf("Congratulations!  You won!\n");
    	}
    	else if (*winner == 2)
    	{
    		printf("House wins!  Try again!\n");
    	}
    	else
    	{
    		printf("It's a draw!  No winner and no loser!\n");
    	}
    }

  10. #10
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    In deal():
    Code:
    for (column = 0; column <= 13; column++)

  11. #11
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,579
    In high_card():
    Code:
    for (column = 0; column = 3; column ++)
    Triple-check all your for loops.

    I strongly recommend you use a more conventional for loop (example below) with constants instead of magic numbers like 3, 4, and 12, especially since you really mean 4 suits, 5 cards in a hand and 13 face values. It's also best to use constants for those values. Then you can use them to define array lengths too, so that everything is of the same, correct size. Clean, easy to read and follow code also helps us find your more serious errors.

    Code:
    #define CARDS_IN_HAND   5
    ...
    Card player_hand[CARDS_IN_HAND];   // don't actually make this a global
    ...
    for (card = 0; card < CARDS_IN_HAND; card++)  // note the < instead of <=

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error in Exceptions.h file during build of QuickFix library
    By arupsarkar in forum C++ Programming
    Replies: 3
    Last Post: 07-16-2010, 10:30 AM
  2. Unhandled exception in file read
    By ronrardin in forum C Programming
    Replies: 4
    Last Post: 04-09-2010, 10:26 AM
  3. Replies: 3
    Last Post: 11-11-2006, 05:46 PM
  4. unhandled exception error
    By trends in forum C++ Programming
    Replies: 4
    Last Post: 11-15-2002, 05:54 PM
  5. Unhandled Exception
    By StringQuartet13 in forum C++ Programming
    Replies: 1
    Last Post: 03-04-2002, 04:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21