Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Need some help (http://www.go4expert.com/forums/need-some-help-t14810/)

Dak914 27Oct2008 06:43

Need some help
 
I have a app that I am programming called war. This app lets you play the old fashioned card game war. The cards in the game are randomly picked based on what time it is. It compares your opponents card to your card, but there is a problem. Time goes by before it is able to compare the cards you see and each card has a unseen different value than the value that you see. I am just wondering how to make the variables static instead of dynamic. Sorry if this makes little or no sense.
~Connor

xpi0t0s 27Oct2008 13:34

Re: Need some help
 
It doesn't make a lot of sense. How can a card have an "unseen different value than the value that you see"? When you pick the card, do you store it somewhere; is there an array of cards for each user? Is the unseen different value part of the game rules or is it something to do with your code (e.g. is this a fancy way of saying a comparison doesn't seem to be working correctly)? What do you mean by static and dynamic variables?

Maybe if you post some code it would help. Let us know what input you give the program and state what line isn't doing what you expect and what you expected it to do.

Dak914 28Oct2008 03:19

Re: Need some help
 
Ok, I already deleted the code and I am currently moving on to my next project, but I can give you snippets so as to understand it more.
Code:

srand(time(NULL));
card1=(rand()%13)+1;
getline(cin, nothing);
card2=(rand()%13)+1;

Card1 is your card while card2 is the opponents card. I have the getline() function so that time actually passes between the declarations so that both aren't the same value. This means that, in a sense, they are dynamic because they are based on what time it is and time is never the same. This is, essentially, a problem because by the time they are compared to see who wins, the values would be different than the values displayed to the user. I am wondering how I could use time() as the seed, but make the cards one value all the time, rather than only a second the same and different the very next second.
Hope that clears up things.
~Connor

xpi0t0s 28Oct2008 04:30

Re: Need some help
 
No, not really. If you set card1 to a random number from 1-13, it will stay at that number until you change it to something else. Setting card1=(rand()%13)+1 evaluates that expression ONCE at that point in time; it's not like a spreadsheet where all values are recalculated every time something changes, for example card1 doesn't get a new value when the card2 expression is evaluated. You'd really need to post complete (but minimal) code that shows what's going wrong.

Also each time you call rand() it reseeds itself, so you should get a different random number each time. Let's try:
Code:

for (int i=0; i<10; i++) printf("%d ",rand());
Output:
41 18467 6334 26500 19169 15724 11478 29358 26962 24464
Not seeded so I get the same sequence every time (Visual Studio 2005).
Adding srand(time(0)); to reseed it according to the current time (which only needs doing once, at the start of the program):
9037 8708 18282 21513 21348 21152 22071 7170 7069 8644
and a different sequence each time.

Anyway, when you move on to a new project, don't delete code, just bung it somewhere safe. That way you can always revisit it later when you have an idea about it (maybe you'll have an idea that cracks open the reason you abandoned the project).

Dak914 28Oct2008 05:39

Re: Need some help
 
Oh, well thank you. I guess it must be somewhere else in my code that is doing it. Hopefully I will not have this kind of problem again.
Thanks for the help.


All times are GMT +5.5. The time now is 18:37.