This one is really vexing me. I'm writing a rudimentary poker program, and I created this function that, as far as I can tell, should work. It's designed to evaluate a 2x5 array called wHand--where row 0 represents the suit and row 1 represents the face value--and determine if there's a pair:
Code:
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];
}
}
}
}
The thing is, when I run it, even if the hand has a pair, eval and highcard don't change from their default value of zero. What am I doing wrong?
Full code (one header, 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 pair (int wHand[][5], int eval, int highcard);
/*void twopair (wHand[][5], int eval, int highcard);
void threekind (wHand[][5], int eval, int highcard);
void straight (wHand[][5], int eval, int highcard);
void flush (wHand[][5], int eval, int highcard);
void fourkind (wHand[][5], 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 */
int eval1 = 0;
int evalAI = 0;
/* 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 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];
}
}
}
}
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"};
/* initalize deck array */
int deck[4][13] = {0};
int handP1[2][5] = {0};
int handAI[2][5] = {0};
int player = 0;
int dealt = 0;
int evalP1 = 0;
int evalAI = 0;
int highP1 = 0;
int highAI = 0;
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);
pair (handP1, evalP1, highP1);
return 0;
}