#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Constants
#define ST 4
#define FC 13
#define RK 13
#define TR 1
#define FA 0
#define AVL 0
#define OCP 1
// Variables
int str, flu, fok, tok;
int pr = 0;
// Function prototypes
void shff( int dck[][ 13 ] ); // shuffle deck
void dlCrd(int stHnd[], int fcHnd[],char *st[], char *fc[], int dck[][FC]); // deal a single card
void dlHnd(int stHnd[], int fcHnd[], char *st[], char *fc[], int dck[][FC]); // deal a five card hand
void dtrHnd(int stHnd[], int fcHnd[]); // determine hand value
void prtWnr(int stHnd[], int fcHnd[], int *ptVl); // print winner of hand to screen
int main() // begin program execution
{
char *st[4] = { "Hearts", "Diamonds", "Spades", "Clubs" }; // initialize suit array
char *fc[13] = { "Two", "Three", "Four", "Five", "Six", "Seven", //initialize face value array
"Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace" };
int dck[4][13] = { { AVL } }; // initialize deck array
int stHnd[4] = {0}; // initialize array for suits in human hand
int fcHnd[13] = {0}; // initialize array for face values in human hand
int stCmpHnd[4] = {0}; // initialize array for suits in computer hand
int fcCmpHnd[13] = {0}; // initialize array for face values in computer hand
int hndCnt = 0; // hand counting variable
int hmnpts = 0; // human point counter
int cmppts = 0; // computer point counter
srand((unsigned int)time( NULL ) ); // initialize random number generator
while(hndCnt<=9)
//for(hndCnt=0; hndCnt<=9; ++hndCnt)
{
// Function call to shuffle deck
shff( dck );
printf("Human dealt:\n\n"); // print to screen human hand
dlHnd(stHnd, fcHnd, st, fc, dck); // function call to deal human hand
dtrHnd(stHnd, fcHnd); // function call to determine value of human hand
prtWnr(stHnd, fcHnd, &hmnpts); // function call to print winner of hand
printf("Computer dealt:\n\n"); // display computer hand
dlHnd(stCmpHnd, fcCmpHnd, st, fc, dck); // function call to deal computer hand
dtrHnd(stCmpHnd, fcCmpHnd); // function call to determine value of computer hand
prtWnr(stCmpHnd, fcCmpHnd, &cmppts); // function call to print winner of hand
if(hmnpts > cmppts) // if human wins hand
{
printf( "Oh, you suck! You won't be so lucky next time!.\n" ); // printed to screen if human wins hand
}
else if(hmnpts < cmppts) // if computer wins hand
{
printf( "Ha! I win! You'd better go home Mister!.\n" ); // printed to screen if computer wins hand
}
else // if hand is a draw
printf( "Aw man! It's a draw! How boring is that!\n" ); // printed to screen if hand is a draw
printf( "\n");
printf("%d", hmnpts);
printf("%d", cmppts);
hndCnt++;
printf("%d", hndCnt);
}
return 0; // indicates successful program termination
}
void shff( int dck[][ 13 ] ) // shuffle function definition
{
int rw; // suit value of card
int clm; // face value of card
int cd; // loop control variable
for ( cd = 1; cd <= 52; cd++ ) // choose random slot in deck for each of the 52 cards
{
do // find available slot in deck
{
rw = rand() % 4; // randomly select row
clm = rand() % 13; // randomly select column
}
while( dck[ rw ][ clm ] != 0 ); // end do...while
dck[ rw ][ clm ] = cd; // place card number in randomly chosen available slot of deck
} // end for
} // end shuffle function
void dlHnd(int stHnd[], int fcHnd[], char *st[], char *fc[], int dck[][FC]) // deal hand function definition
{
int i; // loop control variable
for(i = 0; i < 5; i++) // card dealing loop to deal a five card hand
dlCrd(stHnd, fcHnd, st, fc, dck); // function call to deal a single card
printf("\n");
} // end deal hand function
void dlCrd(int stHnd[], int fcHnd[], char *st[], char *fc[], int dck[][FC]) // deal single card function definition
{
int stIdx, fcIdx; // variables to hold suits and face values of cards dealt
stIdx = rand() % 4; // choose random available suit value
fcIdx = rand() %13; // choose random available face value
while( dck[stIdx][fcIdx] == OCP ) // while chosen suit and face values are occupied, choose another
{
stIdx = rand() % 4; // choose random available suit value
fcIdx = rand() %13; // choose random available face value
}
dck[stIdx][fcIdx] = OCP; // once available suit and face index found, assign "occupied" to that index
fcHnd[fcIdx]++; // increment number of card faces dealt
stHnd[stIdx]++; // increment number of card suits dealt
printf("%s of %s \n", fc[fcIdx], st[stIdx]); // print to screen face and suit of card dealt
}
void dtrHnd(int stHnd[], int fcHnd[]) // determine value of hand function definition
{
int num_consec = 0; // variable to test for straight
int rank, suit;
str = FA;
flu = FA;
fok = FA;
tok = FA;
pr = 0;
for (suit = 0; suit < ST; suit++)
if (stHnd[suit] == 5)
flu = TR;
rank = 0;
while (fcHnd[rank] == 0)
rank++;
for (; rank < FC && fcHnd[rank]; rank++)
num_consec++;
if (num_consec == 5) {
str = TR;
return;
}
for (rank = 0; rank < RK; rank++) {
if (fcHnd[rank] == 4)
fok = TR;
if (fcHnd[rank] == 3)
tok = TR;
if (fcHnd[rank] == 2)
pr++;
}
}
/* assign value to hand and print results */
void prtWnr(int stHnd[], int fcHnd[], int *ptVl)
{
int points;
dtrHnd(stHnd, fcHnd);
if (str && flu) {
printf("Straight flu\n\n");
*ptVl = 9;
points = *ptVl;
}
else if (fok) {
printf("Four of a kind\n\n");
*ptVl = 8;
points = *ptVl;
}
else if (tok && pr == 1) {
printf("Full house\n\n");
*ptVl = 7;
points = *ptVl;
}
else if (flu) {
printf("Flush\n\n");
*ptVl = 6;
points = *ptVl;
}
else if (str) {
printf("Straight\n\n");
*ptVl = 5;
points = *ptVl;
}
else if (tok) {
printf("Three of a kind\n\n");
*ptVl = 4;
points = *ptVl;
}
else if (pr == 2) {
printf("Two pr\n\n");
*ptVl = 3;
points = *ptVl;
}
else if (pr == 1) {
printf("Pair\n\n");
*ptVl = 2;
points = *ptVl;
}
}
6 Replies - 486 Views - Last Post: 22 August 2012 - 05:58 PM
#1
Cannot get a loop to work. Loop begins at shuffle function.
Posted 22 August 2012 - 03:30 PM
Replies To: Cannot get a loop to work. Loop begins at shuffle function.
#2
Re: Cannot get a loop to work. Loop begins at shuffle function.
Posted 22 August 2012 - 03:45 PM
What's your loop not doing that you expect it to do?
#3
Re: Cannot get a loop to work. Loop begins at shuffle function.
Posted 22 August 2012 - 04:08 PM
OMG! I've not seen code with short identifier names since the early 80's when identifiers needed to be 8 (or was it 10?) characters or shorter for C compilers of that era.
Unless you are still using an ancient C compiler, or you are maintaining old code, it would make it much easier to read your code if you used more meaningful names. That way you can actually name variable what they actually are or what they do instead of having to tag a comment shortly afterwards.
Is this your code that you wrote from scratch or something you got from somebody else?
I'm wondering why you would fill your deck with zeros on line 30, call shff() on line 48, and then expect the do-while loop on lines 94-100 to magically find the deck not to be filled with zeros anymore.
Let's try that again... On your first pass the deck is filled with zeros and shff() fills deck with card values. On the second pass, shff() is trying to find slots to put into the deck, but the deck still has values from the first pass and so it can't find any zeroed out slots to put the new cards in.
Unless you are still using an ancient C compiler, or you are maintaining old code, it would make it much easier to read your code if you used more meaningful names. That way you can actually name variable what they actually are or what they do instead of having to tag a comment shortly afterwards.
Is this your code that you wrote from scratch or something you got from somebody else?
Let's try that again... On your first pass the deck is filled with zeros and shff() fills deck with card values. On the second pass, shff() is trying to find slots to put into the deck, but the deck still has values from the first pass and so it can't find any zeroed out slots to put the new cards in.
This post has been edited by Skydiver: 22 August 2012 - 04:25 PM
#4
Re: Cannot get a loop to work. Loop begins at shuffle function.
Posted 22 August 2012 - 04:21 PM
Skydiver, on 23 August 2012 - 02:08 AM, said:
I'm wondering why you would fill your deck with zeros on line 30, call shff() on line 48, and then expect the do-while loop on lines 94-100 to magically find the deck not to be filled with zeros anymore.
Looked alright to me. Just thought that finding the last few zero filled elements would take an age.
#5
Re: Cannot get a loop to work. Loop begins at shuffle function.
Posted 22 August 2012 - 04:25 PM
But all the subsequent times you want to shuffle the deck they won't be zero.
#6
Re: Cannot get a loop to work. Loop begins at shuffle function.
Posted 22 August 2012 - 05:27 PM
GWatt, on 22 August 2012 - 03:45 PM, said:
What's your loop not doing that you expect it to do?
The program is supposed to deal ten hands of five card poker, the players being the human and the computer, of course.
So the loop is supposed to call the shuffle, deal, analyze hand, and print results functions, 10 times. Then the program is to declare a winner of the ten hands.
This is not my original code. I really have no idea how to even begin programming this difficult a program in C, although I'm sure most of you think it's pretty simple. I've actually pieced a lot of it together from many different Internet sources.
All the program will do right now is deal one hand, display them on the screen, and declare a winner of that hand. Then it just hangs up and won't do anything. It stops after the first iteration of the loop. It compiles just fine, but the program doesn't do what it's supposed to do.
Thanks in advance for any suggestions.
#7
Re: Cannot get a loop to work. Loop begins at shuffle function.
Posted 22 August 2012 - 05:58 PM
The first thing I would do is change the representation of the deck. It would be far simpler and easier to shuffle if you use a struct that records the suite and rank.
e.g.:
When you want to shuffle the deck you can iterate through each card and just swap it with another card.
e.g.:
struct card {
int suite;
int rank;
};
When you want to shuffle the deck you can iterate through each card and just swap it with another card.
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|