You know this would be a perfect place for a C++ class. If you do choose to go the linked list method, you would probably mess up the pointer math enough for it to be a pain.
I''m not bashing strict C by any means, but I think a hybrid of OOP and functions is the best way to go.
(Making a deck of cards was my method for introducing classes to a class of high school computer science students (The funny thing is that I was wasn''t even in the class ;-) )
I think linked lists are a little too advanced for what he wants.
When I look at a deck of card, I see deck on which things like shuffling, drawing a card, etc.. are done. If that doesn''t sound like a good case for a class I don''t know what is. Maybe if you are really inventive, you could create a template of a deck, then plug in the cards, but that is too advanced for most people.
Oh, here is some good advice when writing code like that, think before you write. Spending 15 minutes writing all those ifs is a lot longer than 5 minutes of thinking and 2 minutes of typing.
Also, functions should be short as possible.
Sometimes the simplest solution is the best, if not, there''s always high explosives around to hide your mistakes
Never give up. Be like the energizer bunny, just keep going and going and going, until you fall off a cliff like a lemming.
Randomizer For Card Game (Coded in C)
Oh, for crying out loud. This is the most simple problem.
What is so complicated about this.
Edited by - bishop_pass on June 30, 2000 4:56:52 AM
Edited by - bishop_pass on June 30, 2000 4:58:18 AM
Edited by - bishop_pass on June 30, 2000 4:59:22 AM
What is so complicated about this.
//make your deckint deck[52];int j;for (j = 0; j < 52; j++) deck[j] = j;// well that was pretty tough, wasn't it?// now, identify a card// how to determine suit#define get_suit(c) (c / 13)// how to determine numerical value#define get_value(c) (c % 13 + 1)// how tough was that?// here's a function to shuffle the deckshuffle (int deck[52]){ int s, j; int c; // shuffle the deck 10 times for (j = 0; j < 10; j++) { for (s = 0; s < 52; s++) { // pick a card to swap with deck[s] c = rand () % 52; swap (deck[s], deck[c]); } }}// here's our swapping macro#define swap(u,v) {int temp; temp = u; u = v; v = temp;}// and to draw a card...static top_card = 0;int draw_card (int deck[52]){ if (top_card == 52) return -1; // no more cards draw = deck[top_card++]; return draw; }
Edited by - bishop_pass on June 30, 2000 4:56:52 AM
Edited by - bishop_pass on June 30, 2000 4:58:18 AM
Edited by - bishop_pass on June 30, 2000 4:59:22 AM
_______________________________
"To understand the horse you'll find that you're going to be working on yourself. The horse will give you the answers and he will question you to see if you are sure or not."
- Ray Hunt, in Think Harmony With Horses
ALU - SHRDLU - WORDNET - CYC - SWALE - AM - CD - J.M. - K.S. | CAA - BCHA - AQHA - APHA - R.H. - T.D. | 395 - SPS - GORDIE - SCMA - R.M. - G.R. - V.C. - C.F.
"To understand the horse you'll find that you're going to be working on yourself. The horse will give you the answers and he will question you to see if you are sure or not."
- Ray Hunt, in Think Harmony With Horses
ALU - SHRDLU - WORDNET - CYC - SWALE - AM - CD - J.M. - K.S. | CAA - BCHA - AQHA - APHA - R.H. - T.D. | 395 - SPS - GORDIE - SCMA - R.M. - G.R. - V.C. - C.F.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement