Thread: Poker Game Dealer & Player Swapping Cards

  1. #1
    Registered User
    Join Date
    Nov 2017
    Posts
    2

    Poker Game Dealer & Player Swapping Cards

    Hi guys and girls! I'm currently making a poker game with two players and a single player version that deals 5 cards to each player and it analyses who wins. I need to make it so the player and the dealer are able to change 1 or 2 cards from their hand but I'm not sure how to make it work since the hands aren't stored in an Arrays.

    Code:
    #include <stdio.h>#include <time.h>#include "stdafx.h"#include <string.h>
    
    
    
    #define SUITS 4#define FACES 13#define RANKS 13
    
    //global variablesint straight, flush, four, three;int pairs = 0;
    
    void analyzeHand(int sInHand[], int fInHand[]){    int num_consec = 0;    int rank, suit;
    
        straight = 0;    flush = 0;    four = 0;    three = 0;    pairs = 0;
    
        for (suit = 0; suit < SUITS; suit++)        if (sInHand[suit] == 5)            flush = 1;
    
        rank = 0;    while (fInHand[rank] == 0)        rank++;
    
        for (; rank < FACES && fInHand[rank]; rank++)        num_consec++;
    
        if (num_consec == 5) {        straight = 1;        return;    }
    
        for (rank = 0; rank < RANKS; rank++) {        if (fInHand[rank] == 4)            four = 1;        if (fInHand[rank] == 3)            three = 1;        if (fInHand[rank] == 2)            pairs++;    }
    
    }
    
    
    
    
    
    void dealCard(int sInHand[], int fInHand[], char *suits[], char *faces[], int deck[][FACES]) {    int suitIndex, faceIndex;
    
        suitIndex = rand() % 4;         // generates 1 random card     faceIndex = rand() % 13;    // generates 1 random card 
    
    
    
        while (deck[suitIndex][faceIndex] == 1) {  // checks if array slot is empty        suitIndex = rand() % 4;         // checks if the card has been given out         faceIndex = rand() % 13;        // checks if the card has been given out 
    
    
    
        }    deck[suitIndex][faceIndex] = 1; // checks if array slot is empty // fills the deck with 1's 
    
                                        // stores the power of the card
    
    
    
        printf("%s of %s \n", faces[faceIndex], suits[suitIndex]);   // number of the card in the array 
    
    
    
        fInHand[faceIndex]++;    sInHand[suitIndex]++;    printf("%d", fInHand[faceIndex]);    printf("%d", sInHand[suitIndex]);    }
    
    
    
    void Question5(){    int i, j;    int swap;    int q;    int ni, nj;    
    
        int DeckC[4][13] = {        { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 },    { 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26 },    { 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39 },    { 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52 }    };    printf("Before\n");    for (i = 0; i < 4; i++)    {        for (j = 0; j < 13; j++)
    
            {            printf("%d ", DeckC[i][j]);        }        printf("\n");    }   printf("\n");    for (q = 0; q < 3; q++)        for (i = 0; i < 4; i++)            for (j = 0; j < 13; j++)            {                ni = rand() % 4;                nj = rand() % 13;                swap = DeckC[i][j];                DeckC[i][j] = DeckC[ni][nj];                DeckC[ni][nj] = swap;    }     printf("After\n");    for (i = 0; i < 4; i++)    {        for (j = 0; j < 13; j++)
    
            {            printf("%d ", DeckC[i][j]);        }        printf("\n");    }}
    
    
    
    
    
    
    
    
    
    
    
    
    
    void dealHand(int sInHand[], int fInHand[], char *suits[], char *faces[], int deck[][FACES]) {    int i;    for (i = 0; i < 5; i++)
    
                dealCard(sInHand, fInHand, suits, faces, deck);    printf("\n");}
    
    
    
    
    
    /* assign value to hand and print results */void printResult(int sInHand[], int fInHand[], int *pointValue)
    
    {    int points;
    
        analyzeHand(sInHand, fInHand);
    
        if (straight && flush)     {        printf("Straight flush\n\n");        *pointValue = 9;        points = *pointValue;    }
    
        else if (four) {
    
            printf("Four of a kind\n\n");
    
            *pointValue = 8;
    
            points = *pointValue;
    
        }
    
        else if (three && pairs == 1) {
    
            printf("Full house\n\n");
    
            *pointValue = 7;
    
            points = *pointValue;
    
        }
    
        else if (flush) {        printf("Flush\n\n");        *pointValue = 6;
    
            points = *pointValue;
    
    
    
    
    
        }
    
        else if (straight) {
    
            printf("Straight\n\n");
    
            *pointValue = 5;
    
            points = *pointValue;
    
    
    
    
    
        }
    
        else if (three) {
    
            printf("Three of a kind\n\n");
    
            *pointValue = 4;
    
            points = *pointValue;
    
    
    
    
    
        }
    
        else if (pairs == 2) {
    
            printf("Two pairs\n\n");
    
            *pointValue = 3;
    
            points = *pointValue;
    
    
    
    
    
        }
    
        else if (pairs == 1) {
    
            printf("Pair\n\n");
    
            *pointValue = 2;
    
            points = *pointValue;
    
    
    
    
    
        }
    
        else if (pairs == 0) {        printf("High card\n\n");        *pointValue = 1;        points = *pointValue;
    
        }
    
    }
    
    
    
    
    
    
    
    int main() {    char *suits[4] = { "Hearts", "Diamonds", "Spades", "Clubs" }; /// 8    char *faces[13] = { "Two", "Three", "Four", "Five", "Six", "Seven",        "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace" }; ///     int deck[4][13] = { { 0 } }; /// initialises with "AVAILABLE" 
    
    
    
        int sInHand[4] = { 0 };  // player one    int fInHand[13] = { 0 }; // player one    int SInHand2[4] = { 0 }; // player two    int FInHand2[13] = { 0 }; // player two    int numbersw;    int i;    int j;    int P1Points = 0;    int P2Points = 0;    srand(time(NULL));    printf("Press 1 for single player and 2 for multiplayer or 3 to display good shuffling");    scanf_s("%d", &numbersw);    if (numbersw == 1)    {        printf("Player 1 was dealt:\n\n");        dealHand(sInHand, fInHand, suits, faces, deck);        analyzeHand(sInHand, fInHand);        printResult(sInHand, fInHand, &P1Points);
    
    
    
            printf("Player 2 was dealt:\n\n");        dealHand(SInHand2, FInHand2, suits, faces, deck);        analyzeHand(SInHand2, FInHand2);        printResult(SInHand2, FInHand2, &P2Points);
    
    
    
            if (P1Points > P2Points) {            printf("Player One has won\n");        }        else if (P1Points < P2Points) {            printf("Player Two has won\n");        }        else            printf("A Tie\n");        system("pause");    }
    
        else if (numbersw == 2)    {        printf("Player 1 was dealt:\n\n");        dealHand(sInHand, fInHand, suits, faces, deck);        analyzeHand(sInHand, fInHand);        printResult(sInHand, fInHand, &P1Points);
    
    
    
            printf("Dealer was dealt:\n\n");        //dealHand(SInHand2, FInHand2, suits, faces, deck); // to hide the cards        analyzeHand(SInHand2, FInHand2);        printResult(SInHand2, FInHand2, &P2Points);
    
            if (P1Points > P2Points) {            printf("Player One has won\n");        }        else if (P1Points < P2Points) {            printf("Dealer has won\n");        }        else            printf("A Tie\n");        system("pause");    }    else if (numbersw == 3)    {        Question5();    }
    
        else if (numbersw != 1 || 2)    {        printf("Incorrect input\n");        printf("Press 1 for single player and 2 for multiplayer\n");        scanf_s("%d", &numbersw);    }    }

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    The way you've stored the hand data detaches the suits from the values, which turns out to be okay for determining what the hand is worth but you can't reconnect the suits and values if you want to replace cards with new cards.

    How about storing the info like this:
    Code:
    typedef struct Card {
        int rank;
        int suit;
    } Card;
    
    Card cards[MAX_CARDS];
    
    cards[i].rank = ...;
    cards[i].suit = ...;
    Then in the analyze function you can first build the other representation (counts of ranks and suits) and do your calculations with those.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Is it just me, or are most of the newlines in that unreadable mess missing?
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Poker game dealer and player swapping cards
    By wowpr0mania in forum C Programming
    Replies: 0
    Last Post: 01-01-2018, 02:32 PM
  2. Replies: 10
    Last Post: 12-18-2013, 07:08 AM
  3. Help with Poker Game
    By Eulogy in forum C Programming
    Replies: 3
    Last Post: 10-24-2012, 01:15 AM
  4. Sorting hand of cards in poker
    By yangladesh in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2011, 04:25 PM
  5. Poker Game
    By Smiley**123 in forum Game Programming
    Replies: 6
    Last Post: 12-14-2003, 09:48 AM

Tags for this Thread