Making War card game

Reply

Join Date: May 2008
Posts: 82
Reputation: riahc3 is an unknown quantity at this point 
Solved Threads: 0
riahc3 riahc3 is offline Offline
Junior Poster in Training

Making War card game

 
0
  #1
Aug 18th, 2008
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.
Last edited by riahc3; Aug 18th, 2008 at 1:29 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Making War card game

 
0
  #2
Aug 18th, 2008
Look into the time functions in timer.h for ways to do the counters. If you need help in other areas, please be specific.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 82
Reputation: riahc3 is an unknown quantity at this point 
Solved Threads: 0
riahc3 riahc3 is offline Offline
Junior Poster in Training

Re: Making War card game

 
0
  #3
Aug 18th, 2008
Originally Posted by WaltP View Post
Look into the time functions in timer.h for ways to do the counters. If you need help in other areas, please be specific.
My thinking on how to do these things...Is it correct or is another method better?
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 82
Reputation: riahc3 is an unknown quantity at this point 
Solved Threads: 0
riahc3 riahc3 is offline Offline
Junior Poster in Training

Re: Making War card game

 
0
  #4
Aug 19th, 2008
I hope you understand what I ment with my last post.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 82
Reputation: riahc3 is an unknown quantity at this point 
Solved Threads: 0
riahc3 riahc3 is offline Offline
Junior Poster in Training

Re: Making War card game

 
0
  #5
Aug 19th, 2008
I just wanna know if the idea is right or not.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 14
Reputation: mike_g is an unknown quantity at this point 
Solved Threads: 3
mike_g mike_g is offline Offline
Newbie Poster

Re: Making War card game

 
0
  #6
Aug 20th, 2008
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:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. int main()
  6. {
  7. srand(time(NULL)); //seed randomisation only once at start of game
  8.  
  9. printf("Random number (0 - 39): %d\n", rand() % 40); // print a random umber
  10. return 0;
  11. }

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.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 82
Reputation: riahc3 is an unknown quantity at this point 
Solved Threads: 0
riahc3 riahc3 is offline Offline
Junior Poster in Training

Re: Making War card game

 
0
  #7
Aug 20th, 2008
Originally Posted by mike_g View Post
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

Originally Posted by mike_g View Post
You could then have an array size 40 to hold the cards, which would be shuffled when you start the game.
This was one of my ideas. Actually two seperate arrays; one with the organized get and another with the unorganized deck with would get it from the organized one.


Originally Posted by mike_g View Post
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.
Im sorry but here I honestly have no idea what you are talking about.

Structs is something Im use to working with.

Originally Posted by mike_g View Post
Heres crude example of how to get a random number:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. int main()
  6. {
  7. srand(time(NULL)); //seed randomisation only once at start of game
  8.  
  9. printf("Random number (0 - 39): %d\n", rand() % 40); // print a random umber
  10. return 0;
  11. }
Ah yes. Thats what it was. Thank you very much.

Originally Posted by mike_g View Post
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.
Very easy and very effective. Didn't know about clock so my apoligies for asking such a simple question.

Originally Posted by mike_g View Post
To write to files look up fopen and fprintf.
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 14
Reputation: mike_g is an unknown quantity at this point 
Solved Threads: 3
mike_g mike_g is offline Offline
Newbie Poster

Re: Making War card game

 
0
  #8
Aug 22nd, 2008
>>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
Something like:
  1. int card = 39;
  2. char suit = (card / 10)+'A';
  3. int rank = (card % 10)+1;
  4. 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
IMO the easiest way to do this would be to load the entire file to ram, then overwrite the file when youre done. You could use fscanf to load the file content, and fprintf to print the output. Google should give you pleny examples of their usage.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,602
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: Making War card game

 
0
  #9
Aug 26th, 2008
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:

My love for you is like a truck, Berserker
Would you like some making f--k, Berserker
i suppose 90% of the folks here won't get this reference.

oh well




.
Last edited by jephthah; Aug 26th, 2008 at 6:28 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC