3 Replies - 1159 Views - Last Post: 19 January 2013 - 08:08 AM Rate Topic: -----

#1 Luke JSik  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 19-January 13

Small Battle Simulator Problem

Posted 19 January 2013 - 04:50 AM

Hi there, it's difficult to explain the problem. I am VERY new to C# as it is my first language. I tried writing a little one sided battle simulator but the problem occurs that sometimes when clicking attack it does not deduct from the enemies health if they are hit, but instead adds to it. Here is the code:

 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Battle_Simulator
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
           Random attackChance = new Random();

           int health = 1000; 
           int critStrike = 200;  
           int strike = 100; 
              
           int hit = attackChance.Next(1, 100); 
            //hit can either be 1 to 100.
           if(hit >= 60) 
           {
               MessageBox.Show("You Hit the Monster for: 100");
               label6.Text = (health - strike).ToString(); 

           } 
           else if((hit > 40) && (hit < 60))
           {
               MessageBox.Show("You Critically Hit the Monster for: 200 Damage!" );
               label6.Text = (health - critStrike).ToString(); 
           } 

           else{  
               MessageBox.Show("Your Attack Missed"); 
           } 

            if(health <50) 
            {
                MessageBox.Show(" YOU HAVE SLAIN THE MONSTER!");
            }






        }
    }
} 




I hope you can help.

Is This A Good Question/Topic? 0
  • +

Replies To: Small Battle Simulator Problem

#2 pharylon  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 83
  • Joined: 01-September 12

Re: Small Battle Simulator Problem

Posted 19 January 2013 - 06:05 AM

I'm a noob myself, but I see the problem right off the bat: you aren't debugging your code. You do know how to start debugging and step through and look at the variables, right? Because if not, you need to learn how to do that first.

If you had done that, it'd be obvious to you that you're never updating the health of the monster. It starts at 1000 and stays there forever. All you're doing is updating the text in label6 (which is a terrible name), but that change goes away the next time the monster is attacked.

Instead of

label6.Text = (health - critStrike).ToString();



You should have

health = (health - critStrike);
label6.Text = health.ToString();



Also, by the way, you should really fix your names. Stuff like button1 and label6 aren't very informative. It's bad practice and you should get in the habit of doing it right. And your variables aren't very descriptive. What's the difference between a "strike" and a "hit?" If I had found you a week ago and asked you which one would represent an attempted attack on a monster and which one would represent the damage done, do you think you could have figured out which was which from the names? Call them something like "swing" and "hitDamage."

Also you shouldn't be instantiating your Random class in the event. Create it in the constructor and then just call attackChance.Next in the event.

But, mainly, you really just need to learn to debug and you won't get stumped by something this simple.

This post has been edited by pharylon: 19 January 2013 - 06:19 AM

Was This Post Helpful? 2
  • +
  • -

#3 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5571
  • View blog
  • Posts: 11,910
  • Joined: 02-June 10

Re: Small Battle Simulator Problem

Posted 19 January 2013 - 08:01 AM

What this shows us is that you aren't familiar with breakpoints and how to debug your own code.


tlhIn`toq's FAQ list

Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute.

Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.


TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated Jan 2013
Spoiler



Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5571
  • View blog
  • Posts: 11,910
  • Joined: 02-June 10

Re: Small Battle Simulator Problem

Posted 19 January 2013 - 08:08 AM

Some of my common tips (some may apply more than others to your specific style):
  • You have to program as if everything breaks, nothing works, the cyberworld is not perfect, the attached hardware is flakey, the network is slow and unreliable, the harddrive is about to fail, every method will return an error and every user will do their best to break your software. Confirm everything. Range check every value. Make no assumptions or presumptions.

  • Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)

  • You aren't paying for variable names by the byte. So instead of variables names of a, b, c go ahead and use meaningful names like index, timeOut, row, column and so on. You should avoid 'T' for the timer. Amongst other things 'T' is commonly used throughout C# for Type and this will lead to problems. There are naming guidelines you should follow so your code confirms to industry standards. It makes life much easier on everyone around you, including those of us here to help. If you start using the standards from the beginning you don't have to retrain yourself later.
    You might want to look at some of the naming guidelines. Its a lot easier to start with good habits than to break bad habits later and re-learn.



  • Try to avoid having work actually take place in GUI control event handlers. It is better to have the GUI handler call other methods so those methods can be reused and make the code more readable. This is also how you can send parameters rather than use excessive global variables. Get in this habit even if you are using WinForms because WPF works a lot under the idea of "commands" and this will get you working towards that. Think of each gester, control click, menu option etc. as a command to do something such as a command to SAVE. It doesn't matter where the command comes from, all sources should point at the same target to do the actual saving.
    Spoiler


  • Don't replace lines of code that don't work. Instead comment them out and put your new attempts below that. This will keep you from re-trying the same ideas over and over. Also, when you come back to us saying "I've tried this 100 different ways and still can't get it", we can actually see what you tried. So often a failed attempt is very very close and just needs a little nudge in the right direction. So if we can say "See what you did in attempt 3... blah blah" it helps a lot

    Spoiler

    If you are using Visual Studio you can select a block of lines and hit control+k control+c (Kode Comment) to comment it out. control+k control+u (Kode Uncomment) to uncomment a selected block.

This post has been edited by tlhIn`toq: 19 January 2013 - 08:09 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1