Thread: Strange error when running code

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    May 2018
    Posts
    2

    Exclamation Strange error when running code

    Hello,
    I am having a trouble trying to run my program. I was trying to make a program stimulating poker game (just 1 of the exercises in the book I am reading). I had finished the first program to deal 5 cards to one player and determine some of the hand combinations. Then, I tried to modify it so that it can work for 2 players.

    This was when the problem began. Every time I run the modified code, the old code still run instead of the new code. This is the picture where I am so frustrated and delete almost of it, but the result is still the same (picture: In the picture, the running code is compiled from the code background).


    Strange error when running code-code-jpg


    This is my code

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #define MAXC 5 /* define number of cards on your hand here */
    #define PLAYER 2 /* define number of players */
    
    /* prototype */
    void shuffle (int a[][13]);
    void deal (const int a[][13], const char *b[], const char *c[], const int player);
    void record (const int dDeck[][13], int hSuit[], int hFace[], const int player);
    int pair ( const int hFace[] );
    int flush ( const int hSuit[] );
    int straight ( const int hFace[] );
    
    
    int main (void)
    {
        // Initialize face, suit and hand arrays
        const char *suit[4] = {"Hearts", "Diamonds", "Clubs", "Spades"};
        const char *face[13] =     {"Ace", "Two", "Three", "Four",
                                    "Five", "Six", "Seven", "Eight",
                                    "Nine", "Ten", "Jack", "Queen", "King"};
        int deck [4][13] = {0};
        int handSuit1[MAXC] = {0};
        int handFace1[MAXC] = {0};
        int handSuit2[MAXC] = {0};
        int handFace2[MAXC] = {0};
        int *playerSuit [2] = {handSuit1, handSuit2};
        int *playerFace [2] = {handFace1, handFace2};
        
        // Shuffle and deal
        shuffle (deck);
        
        for (int i=0; i<=PLAYER-1; i++)
        {
            printf ("%s%d%s", "Player ", i+1, ":\n");
            deal (deck, suit, face, i);
            record (deck, playerSuit[i], playerFace[i], i);    
            switch (pair (playerFace[i]))
            {
                case 1:
                    puts("You have a pair");
                    break;
                case 2:
                    puts("You have two pair");
                    break;
                case 3:
                    puts("You have three of a kind");
                    break;
                case 0:
                    break;
                default:
                    puts("You have four of a kind!");
            }
            if (flush(playerSuit[i]))
                puts("You have a flush!");
            if (straight(playerFace[i]))
                puts("You have a straight!");
            puts("");
        }
        return 0;
    }
    
    /* shuffle the deck */
    void shuffle (int sDeck[][13])
    {
        srand(time(0));
        int row, col;
        for (int cards = 1; cards <= 52; cards++)
        {
            do
            {
                row = rand() % 4;
                col = rand() % 13;
            }
            while (sDeck[row][col] != 0);
            sDeck[row][col] = cards;
        }
    }
    
    /* deal the cards */
    void deal (const int dDeck[][13], const char *dSuit[], const char *dFace[], const int player)
    {
        for (int cards = player * MAXC + 1; cards <= (player+1)*MAXC; cards++)
            for (int row = 0; row <= 3; row++)
                for (int col = 0; col <= 12; col++)
                    if (dDeck[row][col] == cards)
                        printf ("%5s%s%-8s\n", dFace[col], " of ", dSuit[row]);
    }
    
    /* record hands of players into arrays */
    void record (const int dDeck[][13], int hSuit[], int hFace[], const int player)
    {
        for (int cards = player * MAXC + 1; cards <= (player+1)*MAXC; cards++)
            for (int row = 0; row <= 3; row++)
                for (int col = 0; col <= 12; col++)
                    if (dDeck[row][col] == cards)
                    {    
                        hSuit[cards - 1] = row;
                        hFace[cards - 1] = col;
                    }
    }
    
    /* determine if pairs exist in hand */
    int pair ( const int hFace[] )
    {
        int counter = 0;
        for ( int i = 0; i <= (MAXC - 2); i++)
            for ( int j=i+1; j <= (MAXC - 1); j++ )
                if (hFace[i] == hFace[j])
                    counter++;
        return counter;
    }
    
    /* determine if flush exist in hand */
     int flush ( const int hSuit[] )
    {
        for ( int i = 0; i <= (MAXC - 2); i++)
            for ( int j=i+1; j <= (MAXC - 1); j++ )
                if (hSuit[i] != hSuit[j])
                    return 0;
        return 1;
    }
    
    /* determine if straight exist in hand */
     int straight ( const int hFace[] )
    {
        for ( int i = 0; i <= (MAXC - 2); i++)
            for ( int j=i+1; j <= (MAXC - 1); j++ )
                if ((hFace[i] == hFace[j]) || (abs(hFace[i] - hFace[j]) >= MAXC))
                    return 0;
        return 1;
    }

    I asked my friend to run it, and it worked just fine (The cards on hand of player too is determined wrong, but I don't think it is the problem). Can you please have me to identify the error? Maybe because of my compiler?
    I am using Dev-C++ from sourgeforce.net, and I am new to C, so if possible please tell me where I can improve.
    Thank you!
    Last edited by Truong111299; 05-22-2018 at 11:15 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 03-06-2015, 05:15 PM
  2. Code now running but......
    By BenAber in forum C Programming
    Replies: 1
    Last Post: 04-27-2014, 08:42 AM
  3. Very strange error in managed code
    By VirtualAce in forum C# Programming
    Replies: 5
    Last Post: 02-28-2010, 07:49 PM
  4. Running Exe in C++ Code.
    By Ti22 in forum C++ Programming
    Replies: 5
    Last Post: 03-23-2006, 01:29 PM
  5. running code EXACTLY once
    By Trauts in forum C++ Programming
    Replies: 12
    Last Post: 07-11-2003, 12:24 PM

Tags for this Thread