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.

Recommended Answers

All 8 Replies

Look into the time functions in timer.h for ways to do the counters. If you need help in other areas, please be specific.

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?

I hope you understand what I ment with my last post.

I just wanna know if the idea is right or not.

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:

#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.

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

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.

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.

Heres crude example of how to get a random number:

#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;	
}

Ah yes. Thats what it was. Thank you very much.

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.

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 :)

>>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:

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

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.

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 :P


.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.