Prints out same output everytime

Poker Game

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 1403 Views - Last Post: 03 April 2010 - 05:06 PM Rate Topic: -----

#1 sf18  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 148
  • Joined: 29-November 09

Prints out same output everytime

Posted 03 April 2010 - 12:47 PM

Every hand dealt is a 'high card.' I thought I fixed the counters but I guess there still not counting the suits and faces correctly. Can someone enlighten me on how to determine if the player has a flush, straight, three-of-kind, etc.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define SUITS 4
#define FACES 13
#define AVAILABLE 0
#define TAKEN 1
#define TRUE 1
#define FALSE 0
#define NUM_RANKS 13

void dealACard(int suitsInHand[], int facesInHand[],char *suits[], char *faces[], int deck[][FACES]);
void dealAHand(int suitsInHand[], int facesInHand[], char *suits[], char *faces[], int deck[][FACES]);
void analyzeHand(int suitsInHand[], int facesInHand[]);

main(){
	char *suits[4] = {"Hearts", "Diamonds", "Spades", "Clubs"};
	char *faces[13] = {"Two", "Three", "Four", "Five", "Six", "Seven",
		"Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace"};
	int deck[4][13] = { AVAILABLE };
	int i;

	int suitsInHand[4];
	int facesInHand[13];

	int suitsInHand2[4];
	int facesInHand2[13];

	srand(time(NULL));

	dealAHand(suitsInHand, facesInHand, suits, faces, deck);
	analyzeHand(suitsInHand, facesInHand);
	dealAHand(suitsInHand2, facesInHand2, suits, faces, deck);
	analyzeHand(suitsInHand2, facesInHand2);
	
        system("pause");

}
void dealAHand(int suitsInHand[], int facesInHand[], char *suits[], char *faces[], int deck[][FACES]){
	int i;
	for(i = 0; i < 5; i++)
		dealACard(suitsInHand, facesInHand, suits, faces, deck);

	printf("\n");
}
void dealACard(int suitsInHand[], int facesInHand[], char *suits[], char *faces[], int deck[][FACES]){
	int suitIndex, faceIndex;

	suitIndex = rand() % 4;
	faceIndex = rand() %13;
	
	while( deck[suitIndex][faceIndex] == TAKEN ){
		suitIndex = rand() % 4;
		faceIndex = rand() %13;
	}
	deck[suitIndex][faceIndex] = TAKEN;
        
        // Here are the counters but they're not working. Prints out junk #s.
	facesInHand[faceIndex]++;
	suitsInHand[suitIndex]++;

	printf("%s of %s \n", faces[faceIndex], suits[suitIndex]);
	
}

void analyzeHand(int suitsInHand[], int facesInHand[]){
	int num_consec = 0;
	int rank, suit;

	int straight = FALSE;
	int flush = FALSE;
	int four = FALSE;
	int three = FALSE;
	int pairs = 0;

	// check for flush – 5 cards of the same suit
	for (suit = 0; suit < SUITS; suit++)
		if (suitsInHand[suit] == 5)
			flush = TRUE;

	// check for straight – eg. One each of 5,6,7,8,9
	// locate the first card
	rank = 0;
	while (facesInHand[rank] == 0) 
		rank++;

	// count the consecutive non-zero faces
	for (; rank < FACES && facesInHand[rank]; rank++)
		num_consec++;

	if (num_consec == 5) {
		straight = TRUE;
		return;
	}

	for (rank = 0; rank < NUM_RANKS; rank++) {
		if (facesInHand[rank] == 4) 
			four = TRUE;
		if (facesInHand[rank] == 3) 
			three = TRUE;
		if (facesInHand[rank] == 2) 
			pairs++;
	}
	if (straight && flush) 
		printf("Straight flush\n\n");
	else if (four)         
		printf("Four of a kind\n\n");
	else if (three && pairs == 1)   
		printf("Full house\n\n");
	else if (flush)        
		printf("Flush\n\n");
	else if (straight)     
		printf("Straight\n\n");
	else if (three)        
		printf("Three of a kind\n\n");
	else if (pairs == 2)   
		printf("Two pairs\n\n");
	else if (pairs == 1)   
		printf("Pair\n\n");
	else                   
		printf("High card\n\n");
}


Here is an output from the code above:

Two of Spades
Six of Diamonds
Two of Diamonds
Three of Diamonds
Five of Diamonds

High card

Nine of Diamonds
Three of Clubs
Four of Spades
Seven of Spades
Eight of Diamonds

High card

Press any key to continue . . .


Any suggestions? Thanks for any helpful replies.

This post has been edited by sf18: 03 April 2010 - 12:48 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Prints out same output everytime

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Prints out same output everytime

Posted 03 April 2010 - 01:54 PM

Are you sure? It give me different output each time. Wrong output, but always different.

The problem with your facesInHand and suitsInHand counter arrays is that you never initialize them. You have to loop through both arrays and set all elements to 0 before dealing each hand.

edit: Well, you can initialize them to 0 without looping, but you do have to loop through them to reset to 0 between hands.

This post has been edited by r.stiltskin: 03 April 2010 - 01:57 PM

Was This Post Helpful? 0
  • +
  • -

#3 sf18  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 148
  • Joined: 29-November 09

Re: Prints out same output everytime

Posted 03 April 2010 - 02:14 PM

@ r.stiltskin Thanks for the reply.

I tried this for loop:

void dealACard(int suitsInHand[], int facesInHand[],char *suits[], char *faces[], int deck[][FACES]){
	int suitIndex, faceIndex;

	suitIndex = rand() % 4;
	faceIndex = rand() %13;

	//Here is the loop. Maybe i need to use differnt variables?
	for(faces = 0; faces < FACES; faces++){
		facesInHand[faceIndex] = 0;
	}
	for(suits = 0; suits < SUITS; suits++){
		suitsInHand[faceIndex] = 0;
	}
	while( deck[suitIndex][faceIndex] == TAKEN ){
		suitIndex = rand() % 4;
		faceIndex = rand() %13;
	}
	deck[suitIndex][faceIndex] = TAKEN;


     facesInHand[faceIndex]++; 
     suitsInHand[suitsIndex]++;  
	
    printf("%s of %s \n", faces[faceIndex], suits[suitIndex]);
}



But it doesn't work. I've run out of ideas. How should I fix my loop, so that it count the faces and suits correctly?

P.s IDK I still keep getting "High Card'

This post has been edited by sf18: 03 April 2010 - 02:16 PM

Was This Post Helpful? 0
  • +
  • -

#4 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Prints out same output everytime

Posted 03 April 2010 - 02:29 PM

Perhaps that is what you mean to do, but it seems like a logic mistake for me:
suitIndex = rand() % 4; 
        faceIndex = rand() %13; 
 
        //Here is the loop. Maybe i need to use differnt variables? 
        for(faces = 0; faces < FACES; faces++){ 
                facesInHand[faceIndex] = 0; 
        } 
        for(suits = 0; suits < SUITS; suits++){ 
                suitsInHand[faceIndex] = 0; 
        } 

in that code you assign a random value to suitIndex and faceIndex, then inside the for loops, you use faces and suits variables as indexes. but you refer to faceIndex as index twice (both in the faces loop, and in the suits loops).

maybe you meant:
for(faces = 0; faces < FACES; faces++){ 
                facesInHand[faces] = 0; //faces
        } 
        for(suits = 0; suits < SUITS; suits++){ 
                suitsInHand[suits] = 0; //<<suits
        } 

Was This Post Helpful? 0
  • +
  • -

#5 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Prints out same output everytime

Posted 03 April 2010 - 02:41 PM

Japanir is right.

But actually there's a much easier way. When I posted before I missed the fact that you have different arrays for the two hands. I was thinking that you were reusing the same arrays for both hands. So you can just initialize them like this:

        int suitsInHand[4] = {0};
        int facesInHand[13] = {0};

        int suitsInHand2[4] = {0};
        int facesInHand2[13] = {0};



Oh, and by the way, you should fix your initializer of the deck. It should be:
        int deck[4][13]= {{AVAILABLE}};


This post has been edited by r.stiltskin: 03 April 2010 - 02:46 PM

Was This Post Helpful? 1
  • +
  • -

#6 sf18  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 148
  • Joined: 29-November 09

Re: Prints out same output everytime

Posted 03 April 2010 - 02:44 PM

Yea your right...

This post has been edited by sf18: 03 April 2010 - 05:27 PM

Was This Post Helpful? 0
  • +
  • -

#7 sf18  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 148
  • Joined: 29-November 09

Re: Prints out same output everytime

Posted 03 April 2010 - 02:51 PM

@ rstiltskin I initialized them in main ( ) suitsInHand [4] = {0}, etc. I had a similar problem like this in another program I was working on .I don't know why I keep doing this... But thank you it seems to be counting correctly now.

Here is an output:
Eight of Spades
Ace of Diamonds
Two of Clubs
Seven of Hearts
Ten of Clubs

High card

Two of Hearts
Four of Clubs
Seven of Clubs
Six of Diamonds
Six of Hearts

Pair

Press any key to continue . . .

EDIT: I deleted the for loop out of the code.

This post has been edited by sf18: 03 April 2010 - 02:52 PM

Was This Post Helpful? 0
  • +
  • -

#8 sf18  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 148
  • Joined: 29-November 09

Re: Prints out same output everytime

Posted 03 April 2010 - 03:09 PM

Does anyone know how I could add a Royal Flush?

This post has been edited by sf18: 03 April 2010 - 03:10 PM

Was This Post Helpful? 0
  • +
  • -

#9 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Prints out same output everytime

Posted 03 April 2010 - 03:17 PM

best if you could sort your hand array, and check if the first value is 10, then 11... so on.
check also the first suit. and compare all suits to that certain suit. if all suits are the same, and you have all numbers 10 -14 its a royal flash.
Was This Post Helpful? 0
  • +
  • -

#10 sf18  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 148
  • Joined: 29-November 09

Re: Prints out same output everytime

Posted 03 April 2010 - 03:26 PM

Hmmm....Also if it I did it that way it would be easier to determine a winner, loser, and tie?

But I'm really not sure on how to implement that using a function?

This post has been edited by sf18: 03 April 2010 - 03:28 PM

Was This Post Helpful? 0
  • +
  • -

#11 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Prints out same output everytime

Posted 03 April 2010 - 03:35 PM

well, something like that would do:

int isRoyalFlash(int suitsInHand[], int facesInhand[]){
    sortArray(facesInHand);//create that function also.
    if(suitsInHand[0] != 10){
       return 0;
    }
    else {
       int suit = suitInHand[0];
       for(int i = 1 ; i < 5; i++){
           //check if the faces are in an ascending order (with a difference of 1)
           if(facesInHand[i] != facesInHand[i - 1] + 1){
                return 0;
           }
           //check if all suits are equal
           else if(suitsInhand[i] != suit){
                return 0;
           }
       }
    return 1;
    }
}


edited, removed c++ syntax

This post has been edited by japanir: 03 April 2010 - 04:02 PM

Was This Post Helpful? 0
  • +
  • -

#12 sf18  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 148
  • Joined: 29-November 09

Re: Prints out same output everytime

Posted 03 April 2010 - 03:52 PM

I've been compiling this code for like 45 minutes and I never get a flush, straight, straight flush, full house, or four-of-kind. I hope my coding is right? I guess this odds are slim but still...I think something maybe wrong with my analyzeHand function?

This post has been edited by sf18: 03 April 2010 - 03:56 PM

Was This Post Helpful? 0
  • +
  • -

#13 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Prints out same output everytime

Posted 03 April 2010 - 04:00 PM

sorry, havn't noticed you use c syntax (i demonstrated using c++ bool..)
anyways, i'll edit my sample code.
sorry... :stupid:
Was This Post Helpful? 0
  • +
  • -

#14 sf18  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 148
  • Joined: 29-November 09

Re: Prints out same output everytime

Posted 03 April 2010 - 04:18 PM

I'm not sure why the other hands(flush, straight, etc) are not being read correctly?

#define SUITS 4
#define FACES 13
#define AVAILABLE 0
#define TAKEN 1
#define TRUE 0 // I think this is the problem. If I changed to zero. Then they all read straight flush
#define FALSE 1
#define NUM_RANKS 13



void analyzeHand(int suitsInHand[], int facesInHand[]){
	//
	int num_consec = 0;
	int rank, suit;

	int straight = FALSE;// Should I change this?
	int flush = FALSE;
	int four = FALSE;
	int three = FALSE;
	int pairs = 0;

	for (suit = 0; suit < SUITS; suit++)
		if (suitsInHand[suit] == 5)
			flush = TRUE;

	rank = 0;
	while (facesInHand[rank] == 0) 
		rank++;

	for (; rank < FACES && facesInHand[rank]; rank++)
		num_consec++;

	if (num_consec == 5) {
		straight = TRUE;
		return;
	}

	for (rank = 0; rank < NUM_RANKS; rank++) {
		if (facesInHand[rank] == 4) 
			four = TRUE;
		if (facesInHand[rank] == 3) 
			three = TRUE;
		if (facesInHand[rank] == 2) 
			pairs++;
	}

	if (straight && flush) 
		printf("Straight flush\n\n");
	else if (four)         
		printf("Four of a kind\n\n");
	else if (three && pairs == 1)   
		printf("Full house\n\n");
	else if (flush)        
		printf("Flush\n\n");
	else if (straight)     
		printf("Straight\n\n");
	else if (three)        
		printf("Three of a kind\n\n");
	else if (pairs == 2)   
		printf("Two pairs\n\n");
	else if (pairs == 1)   
		printf("Pair\n\n");
	else                   
		printf("High card\n\n");
}


This post has been edited by sf18: 03 April 2010 - 04:18 PM

Was This Post Helpful? 0
  • +
  • -

#15 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Prints out same output everytime

Posted 03 April 2010 - 04:20 PM

Way too much work guys. You have to check for straight and flush anyway, and you're already putting those together to test for straightflush. So, if it's a straightflush AND it contains an Ace AND it contains a King it's a royal flush.

To test the analyzeHand function, you can very easily write a "modified dealAHand" function that deals a predetermined hand of your choosing (either by hard coding a specific hand or by prompting you to enter specific cards), and see if analyzeHand identifies it correctly.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2