Need some help

Discussion in 'C' started by Dak914, Oct 27, 2008.

  1. Dak914

    Dak914 Member

    Joined:
    May 3, 2008
    Messages:
    48
    Likes Received:
    5
    Trophy Points:
    8
    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
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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.
     
  3. Dak914

    Dak914 Member

    Joined:
    May 3, 2008
    Messages:
    48
    Likes Received:
    5
    Trophy Points:
    8
    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
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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).
     
  5. Dak914

    Dak914 Member

    Joined:
    May 3, 2008
    Messages:
    48
    Likes Received:
    5
    Trophy Points:
    8
    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.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice