Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I'm pretty new to C programming and not very good at coding it.
I want to create a card game, but I cant seem to figure one thing out.
How can I distribute a fixed number of cards from one deck between 2 players in turn, so the first card goes to the first player, the second card to the second player, the third card again to the first player and so on.

What I have tried:

Not much, cant figure it out! ....................................................
Posted
Updated 20-Nov-16 13:20pm
Comments
Richard MacCutchan 20-Nov-16 12:44pm    
You probably need to use a random function to select cards. However, if you are new to coding you may want to start with some simpler projects.
PIEBALDconsult 20-Nov-16 13:52pm    
I wouldn't bother. A computer simulation is not bound by the same constraints as the physical reality it simulates.
In the real world most of us can't simply take X number of cards off a deck, but the computer can.

Looking at your other question, I'd suggest that you create an array of Cards, 52 of them, and fill it with your Card Instances: 13 Spades, 13, Clubs, ...
Then write a function to shuffle the deck.
Add two integers: Number of cards in deck (set to 52), top card (set to 0)
To deal a Card, take the card at the top index, and increment the index. Decrement the count. If the count is now zero, shuffle the deck, and reset the two integers.
 
Share this answer
 
Comments
PIEBALDconsult 20-Nov-16 13:54pm    
Shuffling is also required only in the real world; a computer is able to simply select a card at random.
OriginalGriff 20-Nov-16 14:20pm    
Normally, I'd agree - but that means removing used cards from the collection, and with only a basic array to play with for a beginner that can get difficult. Shuffling means you can use a moving index to ensure that cards don't get repeated without the hassle of actually removing them.
And shuffling isn't difficult: two random indexes to swap, and a loop large enough to give a good level of randomness to the results is pretty simple and intuitive to implement.
PIEBALDconsult 20-Nov-16 14:27pm    
Removing an item from an array-based linked list is simple.
OriginalGriff 20-Nov-16 14:30pm    
Have a look at his other question: I don't think he knows what a pointer is yet, mush less a linked list! :laugh:
That'll come in time...
PIEBALDconsult 20-Nov-16 14:40pm    
I did; he's bitten off too much methinks.
But learning array-based linked lists might be a worthwhile precursor to learning pointers.
Quote:
I'm pretty new to C programming and not very good at coding it.

Advice: put aside your project for now and take time to learn properly how to make programs, follow tutorials.

Master some analyse methods, Dijkstra Top-Down method is a good start.
https://en.wikipedia.org/wiki/Top-down_and_bottom-up_design[^]
https://en.wikipedia.org/wiki/Structured_programming[^]
https://en.wikipedia.org/wiki/Edsger_W._Dijkstra[^]
https://www.cs.utexas.edu/users/EWD/ewd03xx/EWD316.PDF[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900