Thread: Novice programmer looking for help with Random Numbers

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    26

    Novice programmer looking for help with Random Numbers

    I am looking to create a random number generator that can be used to calculate a 1 time use per calculation int that will determine if an attack hits or misses as well as another for the damage within a set range for turn based attacks with ACC% and atk/defense.

    If you can help please provide an example and explain how the code works as I want to learn to be able to use the code on my own. Thanks

    Here is an example of what I have come up with so far.

    do
    {
    if (PlayerRound)
    {
    Random rnd = new Random();
    int PlayerHitChance = 100 - PlayerAcc;
    int PlayerHit = rnd.Next(1, PlayerHitChance);

    if (PlayerHit >= 76)
    {
    //Player Hits
    Random rnd2 = new Random();
    int PlayerDmg = PlayerAtk - TargetMonsterDefense;
    int PlayerHitDmg = rnd2.Next(1, PlayerDmg);
    TargetMonsterHP = TargetMonsterHP - PlayerHitDmg;
    }
    else if (PlayerHit <= 75)
    {
    // Player Misses
    }

    PlayerRound = false;
    TargetMonsterRound = true;
    }
    else if (TargetMonsterRound)
    {

    TargetMonsterRound = false;
    PlayerRound = true;
    }
    else
    {

    }
    } while ((PlayerHP > 0) && (IsTargetMonsterAlive));
    Last edited by Snaef98; 1 Week Ago at 05:38 PM.

  2. #2
    Registered User
    Join Date
    Aug 2010
    Posts
    26
    I don't want to just print out a number on the screen but to assign it to a temp int variable that can be used within a loop.



    For example:

    Player has 25% ACC

    Roll number between 1-100 if number greater then 75 it hits if less then 75 it misses.

    Player attack = 6 and monster defense = 2 so damage so it would be atk - defense = 4 so damage would be 1-4. (random would be calculated off this number 1-4)
    Last edited by Snaef98; 1 Week Ago at 05:07 PM.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    26
    Here is the updated code

    Code:
    do
                        {
                            if (PlayerRound)
                            {
                                // Checks To See If Player Hits
                                Random rnd = new Random();
                                int PlayerHitChance = rnd.Next(1, 100);
                                if (PlayerHitChance <= PlayerAcc)
                                {
                                    // Player Hits
                                    Random rnd2 = new Random();
                                    int PlayerDmg = PlayerAtk - TargetMonsterDefense;
                                    int PlayerHitDmg = rnd2.Next(1, PlayerDmg);
                                    TargetMonsterHP = TargetMonsterHP - PlayerHitDmg;
    
                                    CombatRoundDisplayDmg = PlayerHitDmg;
                                    CombatRoundDisplay = "You hit the target!";
                                    PlayerRound = false;
                                    TargetMonsterRound = true;
                                }
                                else if (PlayerHitChance > PlayerAcc)
                                {
                                    // Player Misses
                                    CombatRoundDisplay = "You missed the target!";
                                    PlayerRound = false;
                                    TargetMonsterRound = true;
                                }
                                // Ends Player Round
                                PlayerRound = false;
                                TargetMonsterRound = true;
                            }
                            else if (TargetMonsterRound)
                            {
                                // Checks To See If Monster Hits
                                Random rnd3 = new Random();
                                int MonsterHitChance = rnd3.Next(1, 100);
                                if (MonsterHitChance <= 25)
                                {
                                    // Monster Hits
                                    Random rnd4 = new Random();
                                    int MonsterDamage = TargetMonsterAttack - PlayerDef;
                                    int MonsterHitDmg = rnd4.Next(1, MonsterDamage);
                                    CombatRoundDisplay = "The monster has hit you!";
                                    PlayerHP = PlayerHP - MonsterHitDmg;
                                    TargetMonsterRound = false;
                                    PlayerRound = true;
                                }
                                else if (MonsterHitChance > 25)
                                {
                                    // Monster Misses
                                    CombatRoundDisplay = "The monsters attack missed you!";
                                    TargetMonsterRound = false;
                                    PlayerRound = true;
                                }
                                // Ends Monster Round
                                TargetMonsterRound = false;
                                PlayerRound = true;
                            }
                            else
                            {
                                if (PlayerHP >= 0)
                                {
                                    CombatRoundDisplay = "Player has died. Game over!";
                                }
                                else if ((PlayerHP <= 1) && (!IsTargetMonsterAlive))
                                {
                                    CombatRoundDisplay = "Victory! The monster has been defeated!";
                                }
                            }
                        } while ((PlayerHP > 0) && (IsTargetMonsterAlive));

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    26
    Well now I got the code working but having trouble separating rounds it just goes through the entire battle to the finish with no display (Draw timing issues) Here is the code for combat

    Code:
     public void Combat()
            {
                if (IsPlayerInCombat)
                {
                    if (TargetMonsterID == 1)
                    {
                        while ((PlayerHP > 0) && (IsTargetMonsterAlive) && (IsPlayerInCombat))
                        {
                            if (PlayerRound)
                            {
                                 // Checks To See If Player Hits
                                 Random rnd = new Random();
                                 int PlayerHitChance = rnd.Next(1, 100);
                                 if (PlayerHitChance <= PlayerAcc)
                                 {
                                     // Player Hits
                                     Random rnd2 = new Random();
                                     int PlayerDmg = PlayerAtk - TargetMonsterDefense;
                                     int PlayerHitDmg = rnd2.Next(1, PlayerDmg);
                                     TargetMonsterHP = TargetMonsterHP - PlayerHitDmg;
    
                                     CombatRoundDisplayDmg = PlayerHitDmg;
                                     CombatRoundDisplay = "You hit the target!";
                                     PlayerRound = false;
                                     TargetMonsterRound = true;
                                     if (TargetMonsterHP <= 0)
                                     {
                                            IsTargetMonsterAlive = false;
                                            PlayerExp = PlayerExp + TargetExpGranted;
                                            PlayerGold = PlayerGold + TargetGoldGranted;
                                            CombatRoundDisplay = "Victory! Monster defeated!";
                                            IsPlayerInCombat = false;
                                     }
                                 }
                                 else if (PlayerHitChance > PlayerAcc)
                                 {
                                        // Player Misses
                                        CombatRoundDisplay = "You missed the target!";
                                        PlayerRound = false;
                                        TargetMonsterRound = true;
                                 }
                           }
                           else if (TargetMonsterRound)
                           {
                                // Checks To See If Monster Hits
                                Random rnd3 = new Random();
                                int MonsterHitChance = rnd3.Next(1, 100);
                                if (MonsterHitChance <= 75)
                                {
                                     // Monster Hits
                                     Random rnd4 = new Random();
                                     int MonsterHitDmg = rnd4.Next(1, TargetMonsterAttack);
    
                                     if (MonsterHitDmg <= 0)
                                     {
                                         // Monser Hits For Zero Damage
                                         CombatRoundDisplay = "The monster hit you but your armor absorbed the damage!";
                                         TargetMonsterRound = false;
                                         PlayerRound = true;
                                     }
                                     else if (MonsterHitDmg >= 1)
                                     {
                                         // Monster Hits For Damage
                                         CombatRoundDisplay = "The monster has hit you!";
                                         PlayerHP = PlayerHP - MonsterHitDmg;
                                         TargetMonsterRound = false;
                                         PlayerRound = true;
    
                                         if (PlayerHP <= 0)
                                         {
                                             CombatRoundDisplay = "Player has died. Game over!";
                                             gameState = GameState.Menu;
                                         }
                                     }
                                 }
                                 else if (MonsterHitChance > 75)
                                 {
                                     // Monster Misses
                                     CombatRoundDisplay = "The monsters attack missed you!";
                                     TargetMonsterRound = false;
                                     PlayerRound = true;
                                 } 
                           }
                        }
                    }
                    else if (TargetMonsterID == 2)
                    {
    
                    }
                    else if (TargetMonsterID == 3)
                    {
    
                    }
    
                }
         
    
            }

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,240
    For battle calculations I would do
    Code:
     
    Random rnd = new Random(); // you only really need one of these. Seed once per game instance.
    
    int getDamage(Object player, Object enemy) {
        int dmg = -1;
        bool hit = (1.0 - player.acc / 100.0) > rnd.NextDouble();
        if (hit)
        {
          if (player.atk > enemy.def) {
             dmg = rnd.Next(1, player.atk - enemy.def);
          }
          else {
             dmg = 0;
          }
       }
       return dmg;
    }
    
    // elsewhere
    int roundDamage = getDamage(player, monster);
    if (roundDamage != -1)
    {
       Console.WriteLine("You hit for {0}!", roundDamage);
    }
    else
    {
       Console.WriteLine("Miss!");
    }
    I factored this out to its own method. The only difference is who is the enemy, the monster or the player. The calculation may get more complex if you add things like crit rate, so it is helpful to contain the mess.

    Also, it's hard to tell, but maybe you think the combat is going too fast because you aren't interacting with the console? I don't see an input loop, and you're not even writing to the console, so all that you are doing is crunching numbers. That will happen very fast. One of the best ways to craft a battle game like this is to give the player plenty of opportunity to type commands.

    If I am wrong, and you are simply making strings to be displayed later, I recommend taking a look at say, this, to generate more interesting output: String.Format Method (System)
    Last edited by whiteflags; 1 Week Ago at 01:00 AM.

  6. #6
    Registered User
    Join Date
    Aug 2010
    Posts
    26
    Thanks for the help, im not using a windows console, im using an XNA 4.0 game application with openGL. I need a way to pause between each round and wait for user to press a key to continue because the draw keeps getting overwritten until the entire battle is over.


    I'm also only updating part of the screen during combat. My draw code is separated based on location on the game world map.

    GUI is separated like this

    [ Character Stats ] [Action / Combat Window]
    [ ----------------Text Dialog --------------------- ]


    also using separate gamestate / window for map, inventory, quests etc.
    Last edited by Snaef98; 1 Week Ago at 07:42 PM.

  7. #7
    Registered User
    Join Date
    Aug 2010
    Posts
    26
    The last code that I posted does the entire combat process. I know it works as when I boosted the static stats for the monster encounter and my guy was defeated it went back to main menu. Also upon character victory it updated monster hp to 0 and printed the victory message. the issue Is that I want each combat round to be displayed and updated under the Draw method. I'm not sure how to make the while loop pause for input from player. If I try and put an if statement for keyboard check the game just either locks up or doesn't do anything at all.

  8. #8
    Registered User
    Join Date
    Aug 2010
    Posts
    26
    Places that will need forced player input to continue would be:

    #1 Each Player Round
    #2 Each Monster Round
    #3 Player Defeat
    #4 Player Victory

  9. #9
    Registered User
    Join Date
    Aug 2010
    Posts
    26
    Here are 2 sample images first is the game screen with action menu. Second one is attempting to load in combat the second screenshot is the part im trying to work on.

    Novice programmer looking for help with Random Numbers-sample1-jpgNovice programmer looking for help with Random Numbers-sample2-jpg

    Here is the map also, as you can see this is text based with some image display and its tile by tile.

    Novice programmer looking for help with Random Numbers-sample3-jpg
    Last edited by Snaef98; 1 Week Ago at 08:10 PM.

  10. #10
    Registered User
    Join Date
    Aug 2010
    Posts
    26
    So what im trying to achieve is this>

    User presses key, encounters monster.
    Display changes to combat in upper right.
    User enters into round based combat using while loop.
    User must hit enter at end of each round during combat so draw has time to update.
    If user dies they have to hit enter and sent to game over screen.
    if user lives and defeats enemy user has to hit enter, and draw screen updates back to drawing regular screen in upper right.

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,240
    I see, well, I would go back to the drawing board a little bit. You will have to plan out your battle system a little more. Something like what I wrote can be called on demand for basic hits, but you will need something similar for everything else. Say, for example you have items that ignore defense and deal flat damage; or attacks that damage all of the enemies; or damage over rounds, like poison. You should design all of that in little chunks. I mean, if it were me I would have all these smaller pieces before I attempt to implement the larger battle loop.

    I'm only saying this because I think you actually have a good outline of what needs to happen. I think your working combat loop will wind up like this:
    Code:
    User presses key, encounters monster.
    Display changes to combat in upper right.
    while (monsters not dead && player not dead) {
       if (player goes) {
          Gather input for this round
          Calculate
       }
       else { // monster goes
          Monster AI decides what to do
          Calculate
      }
      Draw new graphics
      Switch turns
    }
    Draw new graphics for end of combat
    If user dies they have to hit enter and sent to game over screen.
    if user lives and defeats enemy user has to hit enter, and draw screen updates back to drawing regular screen in upper right.
    Your combat loop will be interleaved with graphics updates. I would just be sure that your graphics have blitted before the math portion continues working.

  12. #12
    Registered User
    Join Date
    Aug 2010
    Posts
    26
    That's the problem I'm not sure how to pause the math portion to allow for the drawing to be seen.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Novice Programmer, having trouble. Netbeans/Cygwin
    By oninchowder in forum C Programming
    Replies: 7
    Last Post: 09-13-2011, 10:30 AM
  2. Novice Programmer Humbly Requests Job Seeking Advice
    By steals10304 in forum General Discussions
    Replies: 7
    Last Post: 10-20-2009, 12:12 PM
  3. Replies: 10
    Last Post: 06-17-2005, 10:00 PM
  4. [C++] Segmentation Fault {Novice C++ Programmer}
    By INFERNO2K in forum C++ Programming
    Replies: 24
    Last Post: 06-08-2005, 07:44 PM
  5. Replies: 4
    Last Post: 11-16-2004, 07:29 AM

Tags for this Thread