Thread: Creating combat system

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    15

    Creating combat system

    So i'm fairly new with C and i was hoping if someone could help me with my problem. When using rand() i keep getting the same value and not truely random, also is there a way to set the number parameters rand() will produce. ex. 1-100.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int
    main()
    {
          int player, attack, monster, attack2, initiate;
          
          printf("A monster appears\n");
          monster = 100;
          
                      while (monster <= 100 && monster > 0){
                      printf("1.Attack\n2.Defend\n");
                      scanf("%d", &attack2);
                      if (attack2 = 1){
                      int attack = rand();
                      monster = monster - attack;
                      printf("Monster's health: %d\n", monster);
                      }
                      else if (attack2 = 2){
                      attack2 = rand();
                      monster = monster - attack2;
                      printf("monster's health: %d\n", monster);
                      }
                      else if (monster == 0 || monster < 0){
                      printf("You have slayed the monster!\n");
                      system("pause");
                      return(0);
                      }
                      
                      
                      
                         
                         }
                         }
    Also how would i implement the players health into this? Sorry for being such a noob.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    rand() is a pseudo-random number generator. It generates a sequence of numbers that seem quite random, based on an initial seed. The problem is that if you reuse the same seed you'll get the same sequence of numbers. (If you don't set the seed at all it reuses the same default.) Typically you'll want to set the seed based on something that changes -- using the time (in seconds) is a common example:
    Code:
    srand((unsigned)time(NULL));  /* needs stdlib.h and time.h */
    With this method, if you run the program twice within the same second you'll get the same numbers, otherwise you'll get different numbers. It's possible to use more precise timing functions but they aren't part of the standard.

    Also, this
    Code:
    if (attack2 = 1){
    should use == rather than =. A single equals sign means assignment (i.e. set attack2 to 1, then check if that is nonzero!); two equals signs means comparison (is attack2 equal to 1?).

    You seem to have implemented the monster's health in the variable monster. Why not do the same with the variable player, which you have conveniently already declared?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    15
    Thanks man that solved my problems, I just used a % sign for setting the damage parameters. Is there a way where i can make this combat system its own function, ex. I can just call this function for monster #2 and #3 which would have different healths etc?

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Something like this?
    Code:
    int battle(int health, int attack) {
        int damage = rand() % attack + 1;  /* generate number in range [1,attack] */
        health -= damage;
        return health;
    }
    
    monster = battle(monster, 3);  /* player attacks monster for up to 3 damage */
    player = battle(player, 5);  /* monster attacks player for up to 5 damage */
    If you wanted to, you could have an array representing all the monsters that are present, which would let you easily increase the number of monsters (even while the program was running, for different levels or something).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Your indentation needs work




    Code:
    else if (monster == 0 || monster < 0)
    is the same as
    else if (monster <= 0)



    while (monster <= 100 && monster > 0)
    If this is false, the program has an exit where an integer is not controlled
    Also, because the variables are not initialised, that could be false before even starting.




    I think that you should change the return 0 to break and have the return 0 at the bottom of your main. That way your program isn't returning random numbers to your operating system - I've heard this type of program being discribed as a random error generator for your operating system.




    You need to use the modulus '%' operator to keep your attack/defend in a reasonalble range. When I ran your program [edit]after putting in srand[/edit], I smashed your monster down to -19082
    Fact - Beethoven wrote his first symphony in C

  6. #6
    Registered User
    Join Date
    Dec 2012
    Posts
    15
    Quote Originally Posted by dwks View Post
    Something like this?
    Code:
    int battle(int health, int attack) {
        int damage = rand() % attack + 1;  /* generate number in range [1,attack] */
        health -= damage;
        return health;
    }
    
    monster = battle(monster, 3);  /* player attacks monster for up to 3 damage */
    player = battle(player, 5);  /* monster attacks player for up to 5 damage */
    If you wanted to, you could have an array representing all the monsters that are present, which would let you easily increase the number of monsters (even while the program was running, for different levels or something).

    Cool thanks! i'm pretty lousy with arrays! ha i have my final for intro to computer science tomorrow and arrays are on it, i guess i should read up on it some before i take my final. Can i just set int values for a certain monster and have that int in an array?

  7. #7
    Registered User
    Join Date
    Dec 2012
    Posts
    15
    Quote Originally Posted by Click_here View Post
    Your indentation needs work




    Code:
    else if (monster == 0 || monster < 0)
    is the same as
    else if (monster <= 0)



    while (monster <= 100 && monster > 0)
    If this is false, the program has an exit where an integer is not controlled
    Also, because the variables are not initialised, that could be false before even starting.




    I think that you should change the return 0 to break and have the return 0 at the bottom of your main. That way your program isn't returning random numbers to your operating system - I've heard this type of program being discribed as a random error generator for your operating system.




    You need to use the modulus '%' operator to keep your attack/defend in a reasonalble range. When I ran your program, I smashed your monster down to -19082
    yeah i already added the '%' operator earlier. Okay and ill start using break should it be at the end of each battle?

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by skatenjoi922 View Post
    Cool thanks! i'm pretty lousy with arrays! ha i have my final for intro to computer science tomorrow and arrays are on it, i guess i should read up on it some before i take my final. Can i just set int values for a certain monster and have that int in an array?
    Hmm... maybe
    Code:
    int monsters[5], attacks[5];
    monsters[0] = 100; attacks[0] = 5;
    monsters[1] = 80; attacks[1] = 6;
    /* ... */
    or equivalently
    Code:
    int monsters[5] = {100, 80, /*...*/};
    int attacks[5] = {5, 6, /*...*/};
    Then
    Code:
    printf("All the monsters attack you!\n");
    for(i = 0; i < 5; i ++) {
        player = battle(player, attacks[i]);
    }
    printf("You attack a monster in return.\n");
    monster[2] = battle(monster[2], player_attack);
    where the user gets to pick which monster they attack, instead of just 2 all the time.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    Dec 2012
    Posts
    15
    Quote Originally Posted by dwks View Post
    Hmm... maybe
    Code:
    int monsters[5], attacks[5];
    monsters[0] = 100; attacks[0] = 5;
    monsters[1] = 80; attacks[1] = 6;
    /* ... */
    or equivalently
    Code:
    int monsters[5] = {100, 80, /*...*/};
    int attacks[5] = {5, 6, /*...*/};
    Then
    Code:
    printf("All the monsters attack you!\n");
    for(i = 0; i < 5; i ++) {
        player = battle(player, attacks[i]);
    }
    printf("You attack a monster in return.\n");
    monster[2] = battle(monster[2], player_attack);
    where the user gets to pick which monster they attack, instead of just 2 all the time.
    lol im having a hard time keeping up, ill try to learn from this but i think im going to temporarily stick with what i got going, ill post the code of what i make later. Thanks again for the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating a menu system
    By openwindow in forum C Programming
    Replies: 22
    Last Post: 11-25-2011, 08:33 PM
  2. Creating a menu system
    By ICool in forum C Programming
    Replies: 9
    Last Post: 09-17-2007, 12:18 PM
  3. What I've got so far, creating a menu system:
    By Shamino in forum Game Programming
    Replies: 4
    Last Post: 06-15-2007, 03:03 AM
  4. Creating a 2-D tile system...
    By Raigne in forum C++ Programming
    Replies: 14
    Last Post: 05-24-2007, 04:51 PM
  5. Creating an Instant Messenging system...
    By Fool in forum C++ Programming
    Replies: 8
    Last Post: 08-13-2001, 06:41 PM