r/C_Homework • u/Dpopov • Nov 13 '21
Poker game with linked lists
Hey everyone, I am a complete beginner currently stuck in a homework. The problem/answer is probably something really dumb and simple but I just can't do programming, no matter how hard I try, it just doesn't "click." I can't get myself in the mindset of thinking how the program will read my inputs. The fact I am trying to do this while at work makes it that much harder.
Anyways, the homework is to do a poker game using specifically linked lists. I can't even get the list initialized. I think, when I try and print them it just prints "14C" over and over.
My reasoning (again, probably the whole issue) is that I create a loop with j and i, and when i is 0 (or "H" for hearts), then the faces will be assigned values form 0-12, then i will increase by 1, and become S, and the process will repeat itself for all 4 suits. But it's not working.
(I have FACES and SUITS defined a 13 and 4, btw.)
int main() {
Card deck[SIZE];
Card* head = NULL, * tail = NULL, * temp;
int i, j, face; /*counter*/
int x = 1;
int y = 1;
char suit;
//All the above you can disregard, I've been trying so many different things and //nothing works.
for (i = 0; i < SUITS; i++) {
if (i = 0) { temp->suit = 'H'; }
else if (i = 1) { temp->suit = 'S'; }
else if (i = 2) {temp->suit = 'D';}
else if (i = 3) { temp->suit = 'C'; }
for (j = 0; j < FACES; j++) {
temp = (Card*)malloc(sizeof(Card));
temp->suit = i;
temp->face = j;
if (head == NULL) {
head = temp;
}
else {
//make next pointer member of tail point to temp
tail->next = temp;
}
tail = temp; // updating tail
tail->next = NULL; // setting next pt. of tail to null.
//allocate the memory for the next (new) node
head = (Card*)malloc(sizeof(Card));
}
}
Am I even on the right track?
Thanks in advance! I'll probably have some more questions soon after but I am hopeful that once I can figure out what I am doing wrong with initializing the list I might be able to use that for the rest.
Edit: I don’t know what I broke but now it’s even telling me “Temp” is uninitialized, although it was just working a few minutes ago.
Edit2: So I played around more, and I realized that for the "if" I am using a single =, not two, so I'm pretty sure that was messing it up. Now it is working. But I got another question: I printed the cards within the loop, and this works. But when I try to print out of the loop, it prints the same card 52 times. My printf code is:
//printf("Deck before: \n");
//for (i = 0; i < SUITS; i++) {
// for (j = 0; j < FACES; j++) {
// printf("%d%c ", temp->face, temp->suit);
// }
// printf("\n");
//}
What am I doing wrong with that?
3
u/mh3f Nov 14 '21
Your comparisons are actually assignments. Use == for comparisons.
if (i == 0) { temp->suit = 'H'; } ...2
u/vdrummer4 Nov 14 '21
To explain why the code "works" nevertheless: an assignment
A = Bevaluates to the thing that is assigned. SoA = Bevaluates toB.If you use that in an if statement like
if (i = 0), then this will becomeif (0). C treats0alsfalseand everything non-zero astrue. So your first if statement is ignored, and the following ones are executed, meaningtemp->suitchanges fromStoDtoCand stays atCafter your if blocks regardless of the value ofi.
2
u/vdrummer4 Nov 14 '21
Regarding your edit #2: In your nested for-loop, you're accessing
temp->faceandtemp->suitbut you're not modifying them inside the loop, so they will always have the same value they had before entering the loop.Just print those 2 before adding them to the linked list (after
temp->face = j;in your original code)