A problem in a game I made.

This is a discussion on A problem in a game I made. within the C Programming forums, part of the General Programming Boards category; 2 weird things: 1. That it chooses 1 random number and it's like static for each compilation? What do I ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    242

    A problem in a game I made.

    2 weird things:

    1. That it chooses 1 random number and it's like static for each compilation?
    What do I mean?
    If you compile/execute it, it choose a random number between 0 to 2, and it's the same one for all of the games.
    If you close the window and open a new one, it chooses a random number again between 0 to 2, and does the same thing.
    A bit hard to explain, so just compile it.

    2. It doesn't show the ERROR::1\2\3 thing, why?
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<conio.h>
    int pw=0,cw=0,pp=0,cp=0,t=1,m=100,g;
    int main(void)
    {
    while(m>0)
    {
     randomize();
     clrscr();
     printf("---GAME #&#37;d---\n",t);
     printf("You have %d $\n",m);
     printf("Points situaton:Computer %5d\n\t\tYou      %5d\n",cp,pp);
     puts("Choose your weapon:\nFor rock     O   enter 1\nFor paper"
     "    []  enter 2\nFor scissors 8<  enter 3");
    
     scanf("%d", &pw);
     if(pw > 3 || pw < 0) puts("ERROR::Use only 1\\2\\3");
     if(pw == 1 || pw == 2 || pw == 3)
     {
      printf("\nHow much money you wanna gamble on?\n");
      scanf("%d", &g);
    
      if(g > m) g = m;
      if(g < 0) printf("\nOnly native numbers are acceptable.\n");
      if(g <= m && g > 0)
      {
    	cw = random(2);
    	if((cw==0&&pw==1)||(cw==1&&pw==2)||(cw==2&&pw==3))
    	{
    		printf("Computer: %d", cw);
    		puts("Result: equivalent");
    		 t++;
    	}
    	if((cw==1&&pw==1)||(cw==2&&pw==2)||(cw==0&&pw==3))
    	{
    	 printf("Computer: %d", cw);
    	 puts("Computer win.");
    	 cp++;
    	 m=m-g;
    	 t++;
    	}
      if((cw==2&&pw==1)||(cw==0&&pw==2)||(cw==1&&pw==3))
    	{
    	 printf("Computer: %d", cw);
    	 puts("Player win.");
    	 pp++;
    	 m=m+g;
    	 t++;
    	}
      }
     }
    }
    
    
     if(m<=0)
     {
      puts("The end!!!");
      getchar();
     }
    
    return 0;
    }
    Please help, thanks.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You probably don't want to "randomize()" every loop - that will make it start over on the same number each time (well, theoretically it shouldn't, but you definitely shouldn't call it multiple times - the whole point of setting a "random seed" is that you don't keep resetting it to another random seed all the time).

    You also clear the screen immediately after printing your "1 or 2 or 3" message, which is why you think it's not being printed.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Could you fix my code please?
    I tried to fix it with your instructions and it didn't work.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What did you do?

    It's no point in me fixing things, as I already (sort of, usually, ehm, I think...) know how to fix code.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<conio.h>
    int pw=0,cw=0,pp=0,cp=0,t=1,m=100,g;
    int main(void)
    {
    randomize();
    while(m>0)
    {
     clrscr();
     printf("---GAME #&#37;d---\n",t);
     printf("You have %d $\n",m);
     printf("Points:Computer %5d\n\t\tYou      %5d\n",cp,pp);
     puts("Choose your weapon:\nFor rock     O   type 1\nFor paper"
     "    []  type 2\nFor scissors 8<  type  3");
    
     scanf("%d", &pw);
     if(pw > 3 || pw < 0)
     {
     puts("ERROR::Use only 1\\2\\3");
     clrscr();
     }
     if(pw == 1 || pw == 2 || pw == 3)
     {
      printf("\nHow much money you wanna gamble on?\n");
      scanf("%d", &g);
    
      if(g > m) g = m;
      if(g < 0) printf("\nOnly native numbers are acceptable.\n");
      if(g <= m && g > 0)
      {
    	cw = random(2);
    	if((cw==0&&pw==1)||(cw==1&&pw==2)||(cw==2&&pw==3))
    	{
    		printf("Computer: %d", cw);
    		puts("Result: equivalent");
    		 t++;
    	}
    	if((cw==1&&pw==1)||(cw==2&&pw==2)||(cw==0&&pw==3))
    	{
    	 printf("Computer: %d", cw);
    	 puts("Computer win.");
    	 cp++;
    	 m=m-g;
    	 t++;
    	}
      if((cw==2&&pw==1)||(cw==0&&pw==2)||(cw==1&&pw==3))
    	{
    	 printf("Computer: %d", cw);
    	 puts("Player win.");
    	 pp++;
    	 m=m+g;
    	 t++;
    	}
      }
     }
    }
    
    
     if(m<=0)
     {
      puts("The end!!!");
      getchar();
     }
    
    return 0;
    }
    Both problems doesn't work.

    Should I put cw = random(2); before the while loop too?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    while(m>0)
    {
     clrscr();
     printf("---GAME #%d---\n",t);
     printf("You have %d $\n",m);
     printf("Points:Computer %5d\n\t\tYou      %5d\n",cp,pp);
     puts("Choose your weapon:\nFor rock     O   type 1\nFor paper"
     "    []  type 2\nFor scissors 8<  type  3");
    
     scanf("%d", &pw);
     if(pw > 3 || pw < 0)
     {
     puts("ERROR::Use only 1\\2\\3");
     clrscr();
     }
     if(pw == 1 || pw == 2 || pw == 3)
    Right. If you enter 4 for example, how long does it take from hitting enter until the screen clears in the red section of text?

    If we then ignore that clrscr() immediately after the puts(), how much longer until the screen is cleared again, considering we are not entering the next-if statement?

    Which by the way, leads me to say that you should replace your "if(pw == 1 || ... " with an "else", as that's really what it is. You have a few other places where you do "if (x) ...; if (!x) ...;" which is exactly a "if (x) ... ; else ...", which makes it much clearer what you want to do [not to mention that it runs faster because the same test doesn't have to be done again].

    As far as I can tell, your "random()" call is in the right place. Not sure what's up with that.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    By the way: When code doesn't work, try to step through the part you think doesn't work. You can do this by hand (with pen and paper noting for example what variable values are at each step, and so on, checking VERY CAREFULLY the conditions that apply to the code), tracking each step to "understand" what happens, or using a source-level debugger, stepping a line at a time in the code.

    Often, doing it by hand is actually better (especially when you are a novice) than doing it in the computer.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Hmm, I didn't understand what you were saying actually, sorry, my English isn't that good lol
    I kinda understood that I should replace the location of clrscr();
    Could you explain me it in an easier way?
    ------
    The random thing works! awesome.
    Last edited by eXeCuTeR; 11-16-2007 at 07:26 PM.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you do
    Code:
    puts("Something");
    clrscr();
    How long do you think you have to read the text "something"? I'd say about 2 microseconds - for most humans, that's not enough. May be more like half a millisecond, but still too short to read it properly.

    For this sort of application, why not remove ALL of the clrscr()? Just let it scroll up - at least until you have the functions working as they should.

    As to random "not working", I'm pretty sure random does work, but perhaps not as you expect it. Try writing a special test-code to test just the random function. For example ask for two numbers, one is the "how may" and one is the "range" (the 2 in your case). So you can print 10 numbers with range of 2, 5 numbers with range 1, etc, etc.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Quote Originally Posted by matsp View Post
    If you do
    Code:
    puts("Something");
    clrscr();
    How long do you think you have to read the text "something"? I'd say about 2 microseconds - for most humans, that's not enough. May be more like half a millisecond, but still too short to read it properly.

    For this sort of application, why not remove ALL of the clrscr()? Just let it scroll up - at least until you have the functions working as they should.

    As to random "not working", I'm pretty sure random does work, but perhaps not as you expect it. Try writing a special test-code to test just the random function. For example ask for two numbers, one is the "how may" and one is the "range" (the 2 in your case). So you can print 10 numbers with range of 2, 5 numbers with range 1, etc, etc.

    --
    Mats
    The random does work, sorry
    But why do I have to put randomize(); outside the while loop? you wrote it and I still can't figure why and what different does it make.

    ---
    So, there's no way to let it "update" instead of scrolling with clrscr(); or another method?

    Thanks.
    Last edited by eXeCuTeR; 11-16-2007 at 07:35 PM.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by eXeCuTeR View Post
    The random does work, sorry
    But why do I have to put randomize(); outside the while loop? you probably wrote it and I didn't figure why.
    Ah, ok. Randomize sets a "ranomly selected seed" for the random number generator, e.g. by reading the system time. The "default" behaviour is that the random numbers are generated as a sequence based on a default seed in the random number source code. This will generate the same sequence of random numbers every time. Which is good if all you really need is some numbers that vary from one take to another. For a game like yours, it's no good, because after a while, you'll "learn" that the sequence is always "stone, paper, rock, paper, stone, rock, stone, stone, rock, paper", and end up with a fortune before you know it - no challenge in that.

    So you need to set the random number generator off at a different starting point each time you start your application. That is what randomize() does.

    But if you call randomize every loop, you start over with a, most likely, similar number each time. And that may (probably will) lead to very similar numbers each time after you do randomize (even if you'd get a completely different value 3-5 random numbers later on, if you did continue).

    ---
    So, there's no way to let it "update" instead of scrolling with clrscr(); or another method?

    Thanks.
    Well, you can always use "gotoxy()" and such - but for now, I would just use scrolling - because it also allows you to "look back" and see what was happening before - helps when you want to see if your calculations are right.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Oh, OK.

    Well, here's my code:
    (used \n s, instead of clrscr, just run it you'll see lol this code now looks stupid)
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<conio.h>
    int pw=0,cw=0,pp=0,cp=0,t=1,m=100,g;
    int main(void)
    {
    randomize();
    while(m>0)
    {
     printf("\n\t*****************************************************\n");
     printf("\t\t\t---GAME #&#37;d---\n",t);
     printf("\t\t\tYou have %d $\n",m);
     printf("\t\t\tComputer %5d\n\t\t\tYou      %5d\n",cp,pp);
     puts("\t\t\tChoose your weapon:\n\t\t\tFor rock     O   type 1\n\t\t\tFor paper"
     "    []  type 2\n\t\t\tFor scissors 8<  type  3");
      printf("\n\t*****************************************************\n\n\n\n\n\n");
    
     scanf("%d", &pw);
     if(pw > 3 || pw < 0)
     {
     puts("\t\tERROR::Use only 1\\2\\3");
     }
     if(pw == 1 || pw == 2 || pw == 3)
     {
      printf("\n\t\tHow much money you wanna gamble on?\n");
      scanf("%d", &g);
    
      if(g > m) g = m;
      if(g < 0) printf("\n\n\n\n\n\n\n\n\n\n\n\t\tOnly native numbers are acceptable.\n");
      if(g <= m && g > 0)
      {
    	cw = random(2);
    	if((cw==0&&pw==1)||(cw==1&&pw==2)||(cw==2&&pw==3))
    	{
    		printf("\n\n\n\n\n\n\n\n\n\n\t\tComputer chose: %d\n", cw+1);
    		puts("\t\t\tResult: equivalent");
    		 t++;
    	}
    	if((cw==1&&pw==1)||(cw==2&&pw==2)||(cw==0&&pw==3))
    	{
    	 printf("\n\n\n\n\n\n\n\n\n\n\t\tComputer chose: %d\n", cw+1);
    	 puts("\t\t\tComputer win.");
    	 cp++;
    	 m=m-g;
    	 t++;
    	}
      if((cw==2&&pw==1)||(cw==0&&pw==2)||(cw==1&&pw==3))
    	{
    	 printf("\n\n\n\n\n\n\n\n\n\n\t\tComputer chose: %d\n", cw+1);
    	 puts("\t\t\tPlayer win.");
    	 pp++;
    	 m=m+g;
    	 t++;
    	}
      }
     }
    }
    
    
     if(m<=0)
     {
      puts("\n\t\t\t~~~GAME OVER~~~");
      getchar();
     }
    
    return 0;
    }

    Thanks so much!! You are the best
    Last edited by eXeCuTeR; 11-16-2007 at 08:14 PM.

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You still have a whole lot of "if" that could be either "else", or "else if".

    If you write something like this:
    Code:
    if (a == 0) something
    if (a != 0) someother
    The compiler may very well do both a comparison against zero to see if it's equal, and then, check again if it's not zero. This is of course not necessary [assuming a hasn't changed in between of course], and if you can HELP the compiler with the understanding that the "other part" is ALWAYS an "else" to the first if-statement, you are making the compilers life a lot easier, not to mention that the reader of the code will more easily understand the intended flow of the code. If you have the above, you have to figure out if there's anything in "something" that may modify a and make it enter the second if. If you make it an else, then the reader will KNOW FOR SURE that the else side only happens when a is not zero when the whole if-statement starts, no matter what may or may not happen to a later on - so no need to read through the "something" code [whcih may of course be half a dozen or two dozen lines in a big program [the latter is bad style, but it still happens - functions should be short, and if-statements, for-loops, etc even more so].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D RPG Online Game Project. 30% Complete. To be released and marketed.
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 10-28-2006, 12:48 AM
  2. C Programming 2d Array Question
    By jeev2005 in forum C Programming
    Replies: 3
    Last Post: 04-26-2006, 03:18 PM
  3. problem with my opegl game...
    By revelation437 in forum Game Programming
    Replies: 6
    Last Post: 11-25-2004, 10:32 AM
  4. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 04:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21