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");
}