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;
}