How am I doing with this...

This is a discussion on How am I doing with this... within the C Programming forums, part of the General Programming Boards category; I'm very new at C programming, and whenever I take up a language, I always write a very simple fighting ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    5

    How am I doing with this...

    I'm very new at C programming, and whenever I take up a language, I always write a very simple fighting game. I think I completed the game for now, and was just curious on what you all thought of the code, what I've done wrong, what I could do to improve it, and any other tips you could give me. Right off the bat, I used 4 globals, which I know is a no-no, but I can't think of another way to do it without adding more variables. Yes, this is pretty long for what it is, and I would very much appreciate any feedback thanks

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    int hislife = 20;    // Enemy's life
    int yourlife = 20;   // Your life
    
    int rand_damage;     // Random damage dealt.
    int damage_self;     // Damage dealt to self if superhit
    
    
    int chance() {
       int to_hit = rand() % 10 + 1;
       if (to_hit >= 6) {
          return(2);
       } else {
          return(1);
       }
    }
    
    int damage() {
       int damg = rand() % 4 + 1;
       return(damg);
    }
    
    void youswing() {
    
       if (chance() == 2) {
          rand_damage = damage();
          hislife -= rand_damage;
          printf("Hit enemy for %d damage!\n", rand_damage);
       } else {
          printf("Missed!\n");
       }
    }
    
    void heswing() {
    
       if (chance() == 1) {
          rand_damage = damage();
          yourlife -= rand_damage;
          printf("Enemy hit you for %d damage!\n", rand_damage);
       } else {
          printf("Enemy missed you!\n");
       }
    }
    
    
    int main() {
    
       char input[100];     // Data inputted
       char choice;         // Input Assignment
    
       srand((unsigned int)time(NULL));
    
       while ((choice != 'Q') && (choice != 'q')) {
    
          if (hislife <= 0) {
             printf("Good job. He's dead. Hope you're proud.\n");
             break;
          } else if (yourlife <= 0) {
             printf("You're dead, and you suck.\n");
             break;
          } else if ((yourlife <= 0) && (hislife <= 0)) {
             printf("You killed him, but you're dead too. Such is life..\n");
             break;
          }
    
    
          printf("His life: %d - Your life: %d\n", hislife, yourlife);
          printf("(A)ttack, (S)uperhit, (Q)uit: ");
          fgets(input, sizeof(input), stdin);
          sscanf(input, "%c", &choice);
    
          if ((choice == 'A') || (choice == 'a')) {
    
             printf("\n");
    
             if (chance() == 1) {
                youswing();
                heswing();
             } else {
                heswing();
                youswing();
             }
    
             printf("\n");
    
          } else if ((choice == 'S') || (choice == 's')) {
    
             if (chance() == 2) {
                rand_damage = damage() + 3;
                hislife -= rand_damage;
                damage_self = damage() - 2;
    
                if (damage_self <= 0) {
                   damage_self = 1;
                }
    
                yourlife -= damage_self;
    
                printf("\nYou swung a wreckless blow for %d damage, taking %d damage to yourself\n", rand_damage, damage_self);
             } else {
                damage_self = damage() - 2;
    
                if (damage_self <= 0) {
                   damage_self = 1;
                }
    
                yourlife -= damage_self;
                printf("\nYou threw a completely wreckless blow, hurting yourself for %d damage!\n", damage_self);
             }
    
             if (chance() == 1) {
                rand_damage = damage();
                yourlife -= rand_damage;
                printf("Enemy hit you for %d damage!\n", rand_damage);
             } else {
                printf("Enemy missed you!\n");
             }
    
          } else if ((choice == 'Q') || (choice == 'q')) {
             printf("Goodbye\n");
          } else {
             printf("Unknown: %c\n", choice);
          }
    
       }
    
       return(0);
    
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int chance() {
       int to_hit = rand() % 10 + 1;
       if (to_hit >= 6) {
          return(2);
       } else {
          return(1);
       }
    }
    You don't really need the 'else'.

    Code:
    int chance( void )
    {
        int to_hit = 1 + rand() % 10;
        if( to_hit > 5 )
            return 2;
        return 1;
    }
    Just for the record, you can even simplify this more:

    Code:
    int chance( void )
    {
        return (rand()%10)+1 > 5 ? 2 : 1;
    }
    Here is one problem though:

    Code:
      if (hislife <= 0) {
             printf("Good job. He's dead. Hope you're proud.\n");
             break;
          } else if (yourlife <= 0) {
             printf("You're dead, and you suck.\n");
             break;
          } else if ((yourlife <= 0) && (hislife <= 0)) {
    The third choice will never happen. Put it first.

    Code:
    if( yourlife < 1 && hislife < 1 )
        printf("You're both dead.\n");
    else
    if( yourlife < 1 )
        printf("You're dead.\n");
    else
    if( hislife < 1 )
        printf("He's dead.\n");
    I haven't read over all the rest (on the phone).

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    5
    Thanks for the help - I've noticed C programmers tend to avoid >= or <= operators and tend to just use < >. Any specific reason to this? Also, the following code I just don't understand in the least:

    Code:
    int chance( void )
    {
        return (rand()%10)+1 > 5 ? 2 : 1;
    }
    I see that it's comparing the rand statement and then returning either a 2 or a 1, but I've never seen the ? 2 : 1 syntax before. I suppose I could simplify the whole process by just making it go return(1 + rand()%1), in which case I wouldn't even need the function - The number following % in the rand statement is the highest number possible, -1, correct? So if I do rand() % 10, the possibilities are 0-9? Or am I off base here?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Thanks for the help - I've noticed C programmers tend to avoid >= or <= operators and tend to just use < >. Any specific reason to this?
    Just personal preference. I just bump the value by one rather than using the = sign in <= or >= comparison. Shrug.


    The way you're using rand, it just applies a modulus operator to whatever value ran normally returns. Thus, it's the remainder of whatever that number is divided by N.

    rand() % 10 = 0 to 9, since the remainder of dividing anything by 10 cannot be any thing other than 0 to 9.

    The ternary (or trinary) operator is an "if else" check.

    if a < 5 do this, else do that

    It must be used as a R-value in a statement. Consider:

    a = 5 < 4 ? 0 : 1;

    This means:

    if( 5 < 4 ) a = 0; else a = 1;

    (if this is true) ? (then this value) : (else this value)

    And so:

    return rand() % 10 < 5 ? 1 : 0;



    Quzah.
    Last edited by quzah; 06-25-2002 at 03:47 PM.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

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