0

Edit: working code at the bottom

Im relatively new to programming, and Im trying to understand why this isnt working. I decided to deal cards through a function instead of main() to keep it as modular as possible. Here's my code below; I know the error is something to do with the pointers, but I dont understand what Im doing wrong.

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

int dealCards (char* cards[52][30], int* x, int* y, int* a, int* b)
{
    time_t t;
    srand((unsigned) time(&t));

    printf("\n\nNow let's give you two random cards!\n\n");

    *x = rand() % 53;
    if (x != 0) {
        *x = x - 1;
    }

    *y = rand() % 53;
    if (y == x) {
        while (y == x) {
            y = rand() % 53;
        }
    }
    if (y != 0) {
        y = y - 1;
    }

    int i;
    printf("Card 1 >\n");
    for (i = 0; i < 30; i++) {
            printf("%c", cards[*x][i]);
        }
    printf("\nCard 2 >\n");
    for (i = 0; i < 30; i++) {
            printf("%c", cards[*y][i]);
        }

    printf("\n%d\n", x);
    printf("%d", y);

    printf("\n\nNow let's give your opponent two random cards!\n\n");

    *a = rand() % 53;
    if (a != 0) {
        a = a - 1;
    }
    if ((a == x) || (a == y)) {
        while ((a == x) || (a == y)) {
            *a = rand() % 53;
        }
    }

    *b = rand() % 53;
    if (b == a) {
        while ((b == a) || (b == x) || (b == y)) {
                *b = rand() % 53;
        }
    }
    if (b != 0) {
        *b = b - 1;
    }

    printf("Card 1 >\n");
    for (i = 0; i < 30; i++) {
            printf("%c", cards[*a][i]);
        }
    printf("\nCard 2 >\n");
    for (i = 0; i < 30; i++) {
            printf("%c", cards[*b][i]);
        }

    printf("\n%d\n", a);
    printf("%d", b);
}

int main()
{
    char deck[52][30] = { 
        {"_____\n|2♥ |\n|   |\n|_2♥|\n"},
        {"_____\n|3♥ |\n|   |\n|_3♥|\n"},
        {"_____\n|4♥ |\n|   |\n|_4♥|\n"},
        {"_____\n|5♥ |\n|   |\n|_5♥|\n"},
        {"_____\n|6♥ |\n|   |\n|_6♥|\n"},
        {"_____\n|7♥ |\n|   |\n|_7♥|\n"},
        {"_____\n|8♥ |\n|   |\n|_8♥|\n"},
        {"_____\n|9♥ |\n|   |\n|_9♥|\n"},
        {"_____\n|10♥|\n|   |\n|10♥|\n"},
        {"_____\n|J♥ |\n|   |\n|_J♥|\n"},
        {"_____\n|Q♥ |\n|   |\n|_Q♥|\n"},
        {"_____\n|K♥ |\n|   |\n|_K♥|\n"},
        {"_____\n|A♥ |\n|   |\n|_A♥|\n"},

        {"_____\n|2♦ |\n|   |\n|_2♦|\n"},
        {"_____\n|3♦ |\n|   |\n|_3♦|\n"},
        {"_____\n|4♦ |\n|   |\n|_4♦|\n"},
        {"_____\n|5♦ |\n|   |\n|_5♦|\n"},
        {"_____\n|6♦ |\n|   |\n|_6♦|\n"},
        {"_____\n|7♦ |\n|   |\n|_7♦|\n"},
        {"_____\n|8♦ |\n|   |\n|_8♦|\n"},
        {"_____\n|9♦ |\n|   |\n|_9♦|\n"},
        {"_____\n|10♦|\n|   |\n|10♦|\n"},
        {"_____\n|J♦ |\n|   |\n|_J♦|\n"},
        {"_____\n|Q♦ |\n|   |\n|_Q♦|\n"},
        {"_____\n|K♦ |\n|   |\n|_K♦|\n"},
        {"_____\n|A♦ |\n|   |\n|_A♦|\n"},

        {"_____\n|2♠ |\n|   |\n|_2♠|\n"},
        {"_____\n|3♠ |\n|   |\n|_3♠|\n"},
        {"_____\n|4♠ |\n|   |\n|_4♠|\n"},
        {"_____\n|5♠ |\n|   |\n|_5♠|\n"},
        {"_____\n|6♠ |\n|   |\n|_6♠|\n"},
        {"_____\n|7♠ |\n|   |\n|_7♠|\n"},
        {"_____\n|8♠ |\n|   |\n|_8♠|\n"},
        {"_____\n|9♠ |\n|   |\n|_9♠|\n"},
        {"_____\n|10♠|\n|   |\n|10♠|\n"},
        {"_____\n|J♠ |\n|   |\n|_J♠|\n"},
        {"_____\n|Q♠ |\n|   |\n|_Q♠|\n"},
        {"_____\n|K♠ |\n|   |\n|_K♠|\n"},
        {"_____\n|A♠ |\n|   |\n|_A♠|\n"},

        {"_____\n|2♣ |\n|   |\n|_2♣|\n"},
        {"_____\n|3♣ |\n|   |\n|_3♣|\n"},
        {"_____\n|4♣ |\n|   |\n|_4♣|\n"},
        {"_____\n|5♣ |\n|   |\n|_5♣|\n"},
        {"_____\n|6♣ |\n|   |\n|_6♣|\n"},
        {"_____\n|7♣ |\n|   |\n|_7♣|\n"},
        {"_____\n|8♣ |\n|   |\n|_8♣|\n"},
        {"_____\n|9♣ |\n|   |\n|_9♣|\n"},
        {"_____\n|10♣|\n|   |\n|10♣|\n"},
        {"_____\n|J♣ |\n|   |\n|_J♣|\n"},
        {"_____\n|Q♣ |\n|   |\n|_Q♣|\n"},
        {"_____\n|K♣ |\n|   |\n|_K♣|\n"},
        {"_____\n|A♣ |\n|   |\n|_A♣|\n"},
    };

    int i, j;
    for (i = 0; i < 52; i++) {
        for (j = 0; j < 30; j++) {
            printf("%c", deck[i][j]);
        }
    }

    //dealing
    int heroFirst, heroSecond, villainFirst, villainSecond;

    dealCards(deck, &heroFirst, &heroSecond, &villainFirst, &villainSecond);


    //bets
}

Working code:

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

int dealCards (char cards[52][30], int *x, int *y, int *a, int *b)
{
    time_t t;
    srand((unsigned) time(&t));

    printf("\n\nYour cards:\n\n");

    *x = rand() % 53;
    if (*x != 0) {
        *x = *x - 1;
    }

    *y = rand() % 53;
    if (*y == *x) {
        while (*y == *x) {
            *y = rand() % 53;
        }
    }
    if (*y != 0) {
        *y = *y - 1;
    }

    int i;
    printf("Card 1 >\n");
    for (i = 0; i < 30; i++) {
            printf("%c", cards[*x][i]);
        }
    printf("\nCard 2 >\n");
    for (i = 0; i < 30; i++) {
            printf("%c", cards[*y][i]);
        }

    printf("\n\nVillain cards:\n\n");

    *a = rand() % 53;
    if (*a != 0) {
        *a = *a - 1;
    }
    if ((*a == *x) || (*a == *y)) {
        while ((*a == *x) || (*a == *y)) {
            *a = rand() % 53;
        }
    }

    *b = rand() % 53;
    if (*b == *a) {
        while ((*b == *a) || (*b == *x) || (*b == *y)) {
                *b = rand() % 53;
        }
    }
    if (*b != 0) {
        *b = *b - 1;
    }

    printf("Card 1 >\n");
    for (i = 0; i < 30; i++) {
            printf("%c", cards[*a][i]);
        }
    printf("\nCard 2 >\n");
    for (i = 0; i < 30; i++) {
            printf("%c", cards[*b][i]);
        }
}

int main()
{
    char deck[52][30] = { 
        {"_____\n|2♥ |\n|   |\n|_2♥|\n"},
        {"_____\n|3♥ |\n|   |\n|_3♥|\n"},
        {"_____\n|4♥ |\n|   |\n|_4♥|\n"},
        {"_____\n|5♥ |\n|   |\n|_5♥|\n"},
        {"_____\n|6♥ |\n|   |\n|_6♥|\n"},
        {"_____\n|7♥ |\n|   |\n|_7♥|\n"},
        {"_____\n|8♥ |\n|   |\n|_8♥|\n"},
        {"_____\n|9♥ |\n|   |\n|_9♥|\n"},
        {"_____\n|10♥|\n|   |\n|10♥|\n"},
        {"_____\n|J♥ |\n|   |\n|_J♥|\n"},
        {"_____\n|Q♥ |\n|   |\n|_Q♥|\n"},
        {"_____\n|K♥ |\n|   |\n|_K♥|\n"},
        {"_____\n|A♥ |\n|   |\n|_A♥|\n"},

        {"_____\n|2♦ |\n|   |\n|_2♦|\n"},
        {"_____\n|3♦ |\n|   |\n|_3♦|\n"},
        {"_____\n|4♦ |\n|   |\n|_4♦|\n"},
        {"_____\n|5♦ |\n|   |\n|_5♦|\n"},
        {"_____\n|6♦ |\n|   |\n|_6♦|\n"},
        {"_____\n|7♦ |\n|   |\n|_7♦|\n"},
        {"_____\n|8♦ |\n|   |\n|_8♦|\n"},
        {"_____\n|9♦ |\n|   |\n|_9♦|\n"},
        {"_____\n|10♦|\n|   |\n|10♦|\n"},
        {"_____\n|J♦ |\n|   |\n|_J♦|\n"},
        {"_____\n|Q♦ |\n|   |\n|_Q♦|\n"},
        {"_____\n|K♦ |\n|   |\n|_K♦|\n"},
        {"_____\n|A♦ |\n|   |\n|_A♦|\n"},

        {"_____\n|2♠ |\n|   |\n|_2♠|\n"},
        {"_____\n|3♠ |\n|   |\n|_3♠|\n"},
        {"_____\n|4♠ |\n|   |\n|_4♠|\n"},
        {"_____\n|5♠ |\n|   |\n|_5♠|\n"},
        {"_____\n|6♠ |\n|   |\n|_6♠|\n"},
        {"_____\n|7♠ |\n|   |\n|_7♠|\n"},
        {"_____\n|8♠ |\n|   |\n|_8♠|\n"},
        {"_____\n|9♠ |\n|   |\n|_9♠|\n"},
        {"_____\n|10♠|\n|   |\n|10♠|\n"},
        {"_____\n|J♠ |\n|   |\n|_J♠|\n"},
        {"_____\n|Q♠ |\n|   |\n|_Q♠|\n"},
        {"_____\n|K♠ |\n|   |\n|_K♠|\n"},
        {"_____\n|A♠ |\n|   |\n|_A♠|\n"},

        {"_____\n|2♣ |\n|   |\n|_2♣|\n"},
        {"_____\n|3♣ |\n|   |\n|_3♣|\n"},
        {"_____\n|4♣ |\n|   |\n|_4♣|\n"},
        {"_____\n|5♣ |\n|   |\n|_5♣|\n"},
        {"_____\n|6♣ |\n|   |\n|_6♣|\n"},
        {"_____\n|7♣ |\n|   |\n|_7♣|\n"},
        {"_____\n|8♣ |\n|   |\n|_8♣|\n"},
        {"_____\n|9♣ |\n|   |\n|_9♣|\n"},
        {"_____\n|10♣|\n|   |\n|10♣|\n"},
        {"_____\n|J♣ |\n|   |\n|_J♣|\n"},
        {"_____\n|Q♣ |\n|   |\n|_Q♣|\n"},
        {"_____\n|K♣ |\n|   |\n|_K♣|\n"},
        {"_____\n|A♣ |\n|   |\n|_A♣|\n"},
    };

    int i, j;
    for (i = 0; i < 52; i++) {
        for (j = 0; j < 30; j++) {
            printf("%c", deck[i][j]);
        }
    }

    double heroStack, villainStack;
    printf("\n-----------------------------");
    printf("\nWelcome to Heads Up Hold 'Em!");
    printf("\n-----------------------------");

    printf("\nHow many chips are your going to start off with?\n> ");
    scanf("%lf", &heroStack);
    villainStack = heroStack;

    printf("\nYour chipstack: %.lf\n", heroStack);
    printf("Villain's chipstack: %.lf\n", villainStack);

    //dealing
    int heroFirst, heroSecond, villainFirst, villainSecond;

    dealCards(deck, &heroFirst, &heroSecond, &villainFirst, &villainSecond);
    printf("\n%d\n", heroFirst);
    printf("%d\n", heroSecond);
    printf("%d\n", villainFirst);
    printf("%d\n", villainSecond);

    //bets
    //double heroBet, villainBet;
    //printf("\nYour move");
}
displayName
  • 155
  • 1
  • 9
  • 3
    `char* cards[52][30]` --> `char cards[52][30]` – BLUEPIXY Aug 14 '17 at 13:39
  • 4
    `*x = x - 1;` - Doesn't it look suspicious? `a = a - 1;` - while `a` is a pointer? And many, many similar issues. – Eugene Sh. Aug 14 '17 at 13:39
  • 1
    Always take your compiler's warning serious. – alk Aug 14 '17 at 13:41
  • 1
    You should be getting compiler warnings for that code. Turn on/up compiler warnings and fix them! (For `gcc` add `-Wall` to the compile line.) – Klas Lindbäck Aug 14 '17 at 13:42
  • 1
    Also this `if (y != 0) { y = y - 1; }` ought to be `if (*y != 0) { *y = *y - 1; }` Same for all non `*` prefixed `a`, `b`, `x` and `y`. – alk Aug 14 '17 at 13:43
  • 1
    [srand() — why call it only once?](https://stackoverflow.com/questions/7343833/srand-why-call-it-only-once). – Lundin Aug 14 '17 at 13:43
  • 1
    `*x = rand() % 53` produces an array index in the range `0` to `52` which will break the bounds when used as `cards[*x][i]` – Weather Vane Aug 14 '17 at 13:48
  • @WeatherVane - I think OP tries to subtract 1 (if `*x` isn't zero) so it shouldn't go out of bounds. But `if (x != 0) {*x = x - 1;}` isn't the way to do it... However, I find it strange that OP doesn't use `% 52` from the start. – Support Ukraine Aug 14 '17 at 13:55
  • @BLUEPIXY Im trying to pass `deck[][]` as an argument for a pointer in my `dealCards()` function. – displayName Aug 14 '17 at 14:06
  • @alk Can you recommend a resource to clarify this for me? Im trying to understand when to add the `*` before each variable and when I dont have to. Im learning C on my own. – displayName Aug 14 '17 at 14:06
  • @4386427 I did not use `% 52` in because there are 52 cards in the deck, `deck[0][0]` being `2♥` and `deck[52][0]` being `A♣`. The range `% 53` chooses an int between `0` and `52`, which is what I need – displayName Aug 14 '17 at 14:06
  • @4386427 thanks didn't notice that but index `0` now is twice as likely. Loaded deck. And as pointed out should be `if(*x != 0) {*x = *x - 1;}` not `if(x != 0) {*x = x - 1;}` – Weather Vane Aug 14 '17 at 14:07
  • @MichaelKhoussid `char* cards[52][30]` meant 2D-Array of `char*`. – BLUEPIXY Aug 14 '17 at 14:09
  • @WeatherVane How can I circumvent this and ensure optimal randomness? – displayName Aug 14 '17 at 14:10
  • 2
    With `card = rand() % 52` and worry about the quality of the RNG distribution when the code works. But for a casual game exercise, that's unimportant. – Weather Vane Aug 14 '17 at 14:11
  • Also when `Type array[n]`, The elements are `array[0]`...`array[n-1]`. `array[n]` is out of bounds. – BLUEPIXY Aug 14 '17 at 14:12
  • You might like to chose one more of the resources listed here: https://stackoverflow.com/q/562303/694576 – alk Aug 14 '17 at 14:14
  • @MichaelKhoussid - You commented that `deck[52][0] being A♣`. That is wrong - `deck[51]` is holding the string for A♣. Access to `deck[52]` is illegal. – Support Ukraine Aug 14 '17 at 15:05
  • @4386427 Apologies, that's what I meant – displayName Aug 14 '17 at 20:03
  • @MichaelKhoussid And therefore you should do `% 52` and remove the code where you subtract 1 – Support Ukraine Aug 14 '17 at 20:06

1 Answers1

0

The problem is you are mixing confusingly between getting value and calculating value This if (and similar), do you know what are you doing?

*x = rand() % 53; if (x != 0) { *x = x - 1; }

Well, x in your function is a pointer, which means, if you want to get or to assign a value, you must use operator * like *x = rand() % 53 But when you compare x != 0, the compiler will think that you are checking whether x is NULL or not (by default, NULL is 0). So, it is not the value of the above statement, which will lead you to logic error.

If you look closer in your main, you should see that x, y, and similar argument you pass is never NULL, since they are created locally in main function and still existing until you leave it. Therefore, your statement *x = x - 1 will work unexpectedly, which may cause your segfault

I think that you are using the deal function to get randomly values of these variables

int heroFirst, heroSecond, villainFirst, villainSecond;

Thus, you can pass those value by reference by declare the function like this: int dealCards (char cards[52][30], int& x, int& y, int& a, int& b) then call your function like this dealCards (deck, heroFirst, heroSecond, villainFirst, villainSecond)

In your dealCards definition, just remove the * like *x or something like that, your function will work normally (if you know what you are going to do, with those x y) Moreover, you can safely print the whole string without using for loop like this, it seem crappy, isn't it?

for (i = 0; i < 30; i++) { printf("%c", cards[*y][i]); }

Just print it normally by printf("%s", cards[*y]);

Eddited:

  • Grammar

  • You should re-declare your cardDecks as above comments to make it works

Loi Ly
  • 164
  • 12
  • 1
    This is a C question - passing by reference as you've described doesn't exist. Maybe you're thinking of C++? – Chris Turner Aug 14 '17 at 14:16
  • Thank you, but the reason I initially chose to use pointers such as `*x` is because I didnt want to return anything from the `dealCards` function, because Im not sure how to do that. Whenever I try to return values, I call function in my `main` and then I test by `printf("%d", heroFirst)` and it throws me a random number not in the `0 - 52` range – displayName Aug 14 '17 at 14:16
  • @ChrisTurner, Oops, sorry about that, thanks for notice me (y) @MichaelKhoussid it is random because of this and similar lines: `*x = x - 1;` You should change to `*x = *x - 1` instead – Loi Ly Aug 14 '17 at 14:18
  • @LoiLy you should edit your answer and remove the misleading stuff that is ruining an otherwise useful answer – Chris Turner Aug 14 '17 at 14:40