943,910 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1951
  • C RSS
Aug 18th, 2008
0

Making War card game

Expand Post »
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.
Similar Threads
Reputation Points: 49
Solved Threads: 1
Posting Whiz
riahc3 is offline Offline
306 posts
since May 2008
Aug 18th, 2008
0

Re: Making War card game

Look into the time functions in timer.h for ways to do the counters. If you need help in other areas, please be specific.
Moderator
Reputation Points: 3278
Solved Threads: 892
Posting Sage
WaltP is online now Online
7,730 posts
since May 2006
Aug 18th, 2008
0

Re: Making War card game

Click to Expand / Collapse  Quote originally posted by WaltP ...
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?
Reputation Points: 49
Solved Threads: 1
Posting Whiz
riahc3 is offline Offline
306 posts
since May 2008
Aug 19th, 2008
0

Re: Making War card game

I hope you understand what I ment with my last post.
Reputation Points: 49
Solved Threads: 1
Posting Whiz
riahc3 is offline Offline
306 posts
since May 2008
Aug 19th, 2008
0

Re: Making War card game

I just wanna know if the idea is right or not.
Reputation Points: 49
Solved Threads: 1
Posting Whiz
riahc3 is offline Offline
306 posts
since May 2008
Aug 20th, 2008
0

Re: Making War card game

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.
Reputation Points: 12
Solved Threads: 3
Newbie Poster
mike_g is offline Offline
14 posts
since Jul 2008
Aug 20th, 2008
0

Re: Making War card game

Click to Expand / Collapse  Quote originally posted by mike_g ...
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

Click to Expand / Collapse  Quote originally posted by mike_g ...
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.


Click to Expand / Collapse  Quote originally posted by mike_g ...
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.

Click to Expand / Collapse  Quote originally posted by mike_g ...
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.

Click to Expand / Collapse  Quote originally posted by mike_g ...
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.

Click to Expand / Collapse  Quote originally posted by mike_g ...
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
Reputation Points: 49
Solved Threads: 1
Posting Whiz
riahc3 is offline Offline
306 posts
since May 2008
Aug 22nd, 2008
0

Re: Making War card game

Quote ...
>>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);

Quote ...
]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.
Reputation Points: 12
Solved Threads: 3
Newbie Poster
mike_g is offline Offline
14 posts
since Jul 2008
Aug 26th, 2008
0

Re: Making War card game

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:

Quote ...
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.
Reputation Points: 2143
Solved Threads: 178
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: errors type
Next Thread in C Forum Timeline: Need help to convert a string to an integer





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC