r/C_Homework • • 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?

2 Upvotes

5 comments sorted by

2

u/vdrummer4 Nov 14 '21

Regarding your edit #2: In your nested for-loop, you're accessing temp->face and temp->suit but 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)

1

u/Dpopov Nov 17 '21

Sweet thanks! I have another question about that, is there a way to randomize a linked list?

I was thinking of creating a second array, named "deck[SIZE]" and then copying the information, then using rand (), but I have a feeling it's not going to work. First, when I try printing the array normally to make sure everything copied properly, it doesn't print anything. I wanted to print it separately without having to initialize everything again so I created a user defined function but I think I screwed up something, it doesn't print anything at all. I have a "head" which is a pointer to the first element of the array, and then, I was trying to have the pointer move down the list. I tried several iterations ,

void shuffledeck(Card* head, Card* deck[]) {
int i;
   for (i = 0; i < SIZE; i++) {
      deck[i]->face = head->face;
      deck[i]->suit = head->suit;
       head = head->next;
      printf("%d%c ", deck[i]->face, deck[i]->suit);
    }

}

How would you randomize a linked list?

→ More replies (1)

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 = B evaluates to the thing that is assigned. So A = B evaluates to B.

If you use that in an if statement like if (i = 0), then this will become if (0). C treats 0 als false and everything non-zero as true. So your first if statement is ignored, and the following ones are executed, meaning temp->suit changes from S to D to C and stays at C after your if blocks regardless of the value of i.