| | |
Making War card game
![]() |
•
•
Join Date: May 2008
Posts: 82
Reputation:
Solved Threads: 0
Hey
I have to make the game War (card game: http://en.wikipedia.org/wiki/War_(card_game) ) in C. Im not sure where (how...) to start. My basic thoughts/ideas/etc:
Its a 40 (4 times 10) card version with 2 players. A way to do it would be set up a structure with 4 faces values: a, b, c, and d. Now I need that each has a value of 1 - 10 (a1, a2, a3....a10) but nonchanging. After this each player has a another structure (or array (?) ) holding 20 cards. Now out of the 40 card deck structure we have to randomly pick them out and assign them to the 2 players (this is one of the 2 big problems; No idea how to randomly pick)
The game afterwards is pretty simple: Simply each player puts out a card. If the card is larger, that player gets his card back (in a seperete deck) and the other players card. If the card is smaller, the player looses his card. If tie, player puts 2 cards with hidden value and a third card with value revelaed. That shouldnt be too hard. The game is ended when a player is without cards
Now here a hard part: There will be a clock running during the game. If someone beats the game and is in the top 10 of the fastest, his name will be added to the ranking. This is saved/loaded from a simple text file. I have no clue on how to do this.
I hope I can get some help because Ive thought about it and honestly I dont even know where to start.
I have to make the game War (card game: http://en.wikipedia.org/wiki/War_(card_game) ) in C. Im not sure where (how...) to start. My basic thoughts/ideas/etc:
Its a 40 (4 times 10) card version with 2 players. A way to do it would be set up a structure with 4 faces values: a, b, c, and d. Now I need that each has a value of 1 - 10 (a1, a2, a3....a10) but nonchanging. After this each player has a another structure (or array (?) ) holding 20 cards. Now out of the 40 card deck structure we have to randomly pick them out and assign them to the 2 players (this is one of the 2 big problems; No idea how to randomly pick)
The game afterwards is pretty simple: Simply each player puts out a card. If the card is larger, that player gets his card back (in a seperete deck) and the other players card. If the card is smaller, the player looses his card. If tie, player puts 2 cards with hidden value and a third card with value revelaed. That shouldnt be too hard. The game is ended when a player is without cards
Now here a hard part: There will be a clock running during the game. If someone beats the game and is in the top 10 of the fastest, his name will be added to the ranking. This is saved/loaded from a simple text file. I have no clue on how to do this.
I hope I can get some help because Ive thought about it and honestly I dont even know where to start.
Last edited by riahc3; Aug 18th, 2008 at 1:29 pm.
•
•
Join Date: Jul 2008
Posts: 14
Reputation:
Solved Threads: 3
You should be able to store the data for each card within an integer. You could then get the suit as card / 10 and you could get the rank as card % 10. I find this approach simplifies things a little.
You could then have an array size 40 to hold the cards, which would be shuffled when you start the game.
Instead of passing structs around between the deck an the players you keep an index of the stack position in the deck. Every time you deal a card copy the card value at the stack position to the players hand and increment the stack position. When you reach the end of the deck the you can reshuffle the cards or something.
Heres crude example of how to get a random number:
A simple way to time the game would be to call clock() at the start of the game and save the return value. At the end of the game call clock again and subtract the start time from the end time. You then might want to convert the result tom minutes and seconds.
To write to files look up fopen and fprintf.
You could then have an array size 40 to hold the cards, which would be shuffled when you start the game.
Instead of passing structs around between the deck an the players you keep an index of the stack position in the deck. Every time you deal a card copy the card value at the stack position to the players hand and increment the stack position. When you reach the end of the deck the you can reshuffle the cards or something.
Heres crude example of how to get a random number:
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> #include <time.h> int main() { srand(time(NULL)); //seed randomisation only once at start of game printf("Random number (0 - 39): %d\n", rand() % 40); // print a random umber return 0; }
A simple way to time the game would be to call clock() at the start of the game and save the return value. At the end of the game call clock again and subtract the start time from the end time. You then might want to convert the result tom minutes and seconds.
To write to files look up fopen and fprintf.
•
•
Join Date: May 2008
Posts: 82
Reputation:
Solved Threads: 0
•
•
•
•
You should be able to store the data for each card within an integer. You could then get the suit as card / 10 and you could get the rank as card % 10. I find this approach simplifies things a little.
•
•
•
•
You could then have an array size 40 to hold the cards, which would be shuffled when you start the game.
•
•
•
•
Instead of passing structs around between the deck an the players you keep an index of the stack position in the deck. Every time you deal a card copy the card value at the stack position to the players hand and increment the stack position. When you reach the end of the deck the you can reshuffle the cards or something.
Structs is something Im use to working with.
•
•
•
•
Heres crude example of how to get a random number:
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> #include <time.h> int main() { srand(time(NULL)); //seed randomisation only once at start of game printf("Random number (0 - 39): %d\n", rand() % 40); // print a random umber return 0; }
•
•
•
•
A simple way to time the game would be to call clock() at the start of the game and save the return value. At the end of the game call clock again and subtract the start time from the end time. You then might want to convert the result tom minutes and seconds.
I used fseek, fopen, fclose, etc but I seem to always do a pointer/memory violation as a popup window the red X pops up always when it gets to saving/opening. Some tips or pages with examples would be nice

Thanks alot mike_g for the help. I hope to hear from you soon
•
•
Join Date: Jul 2008
Posts: 14
Reputation:
Solved Threads: 3
•
•
•
•
>>You should be able to store the data for each card within an integer. You could >>then get the suit as card / 10 and you could get the rank as card % 10. I find this approach >>simplifies things a little.
Not getting this fully. The suit and the rank is where you lost me
C Syntax (Toggle Plain Text)
int card = 39; char suit = (card / 10)+'A'; int rank = (card % 10)+1; printf("suit: %c, rank: %d", suit, rank);
•
•
•
•
]I used fseek, fopen, fclose, etc but I seem to always do a pointer/memory violation as a popup window the red X pops up always when it gets to saving/opening. Some tips or pages with examples would be nice
every time i see this title Making War in the list, i think of that segment from the movie "Clerks" ... you know, the Russian metal head dude who sings his "Berserker" song for Dante's girlfriend after Jay and Silent Bob encourage him:
i suppose 90% of the folks here won't get this reference.
oh well
.
•
•
•
•
My love for you is like a truck, Berserker
Would you like some making f--k, Berserker
oh well

.
Last edited by jephthah; Aug 26th, 2008 at 6:28 pm.
![]() |
Similar Threads
- trojans...now nothing opens and I get a paint can't open error (Viruses, Spyware and other Nasties)
Other Threads in the C Forum
- Previous Thread: errors type
- Next Thread: Need help to convert a string to an integer
| Thread Tools | Search this Thread |
* adobe ansi api array arrays binarysearch calculate centimeter char character cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling gtkwinlinux hacking highest homework i/o inches incrementoperators intmain() iso km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. lowest match matrix microsoft mqqueue mysql oddnumber odf open opendocumentformat openwebfoundation pattern pdf performance posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprograming socketprogramming stack standard strchr string suggestions test unix urboc user variable voidmain() whythiscodecausesegmentationfault win32api windows.h windowsapi






