12 Replies - 1051 Views - Last Post: 05 January 2013 - 02:31 PM Rate Topic: -----

#1 AnimeWarrior  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 44
  • Joined: 28-December 12

Overflow Error in Battle Simulator

Posted 28 December 2012 - 10:25 AM

I Have spent the past few hours getting a battle simulator finished and I am having a problem with it throwing an overflow error. The battle lasts way to many rounds and every time a type of troop dies, it gets replaced some how. This makes the attack a never-ending loop. If anyone can find the problem I would be grateful.

Below is the Battle Module.
Option Explicit
Public Target As Integer
Public Round As Integer
Public DistToTarget
Public Pos(1 To 2, 1 To 10) As Integer
Dim Sent(1 To 10) As Integer
Dim Defenders(1 To 10) As Integer
Public Sub Send(ID, N)
Sent(ID) = N
End Sub
Public Sub Set_Defender(ID, N)
Defenders(ID) = N
End Sub
Public Sub Battle_Start()
Round = 1
Dim X
For X = 1 To 10
Pos(1, X) = 0 - TroopHandler.Get_Range(X)
Next X
For X = 1 To 10
Pos(2, X) = TroopHandler.Get_Range(X) + 50
Next X

PlayerAttack
End Sub

Public Sub PlayerAttack()
Dim T, R, C, S
For T = 1 To 10
If Sent(T) > 0 Then
'Troops found with ID T
'Get range
R = TroopHandler.Get_Range(T)
'get closest enemy
C = Get_ClosestEnemy
Debug.Print "Closest Enemy: " & C
'Check if closest is in range
If R <= Pos(2, C) Then
        'Get stats
        Dim PAtt, ELife, DefEff As Integer
        Dim PCount, ECount As Integer
        PAtt = TroopHandler.Get_Att(T)
        ELife = TroopHandler.Get_Life(C)
        PCount = Sent(T)
        ECount = Defenders(C)
        'Calculate
        Dim AttEff
        AttEff = PAtt * PCount
        DefEff = (0.5 * ELife) * ECount
        Debug.Print "Round: " & Round & " AttEff: " & AttEff
        Debug.Print "DefEff: " & DefEff
        Debug.Print "Att-DEf: " & AttEff - DefEff
        Debug.Print "Def-Att: " & DefEff - AttEff
        'ATTACK!
        Debug.Print "Defenders: " & Defenders(C)
        Debug.Print "Defense: " & (1 / 2 * ELife)
        Debug.Print "Life: " & ELife
        Debug.Print "Total Defense: " & (ECount * (1 / 2 * ELife))
        Debug.Print "Killed: " & ((ECount * (1 / 2 * ELife) - AttEff))
        'Defenders(C) = Defenders(C) - ((ECount * (1 / 2 * ELife) - AttEff))
        Defenders(C) = (DefEff - AttEff)
        Debug.Print "Lived:" & Defenders(C)
End If
End If
Next T

'Move forward
For T = 1 To 10
If Pos(1, T) < 0 Then
S = TroopHandler.Get_Speed(T)
    If Abs(Pos(1, T)) < S Then
        Pos(1, T) = 0
    Else
        Pos(1, T) = Pos(1, T) + S
    End If
End If
Next T

Dim O
O = Check_Outcome
If O = 0 Then
Round = Round + 1
PCAttack
Else
    MsgBox "Player Won."
    Dim L
    'Give reward
    For L = 1 To 6
        Call ResHandler.Set_Res(L, Map.Get_Reward(L))
    Next L
End If
End Sub

Public Sub PCAttack()
Debug.Print "ENEMY ATTACK"
Dim T, R, C, S
For T = 1 To 10
If Defenders(T) > 0 Then
'Troops found with ID T
'Get range
R = TroopHandler.Get_Range(T)
Debug.Print "Range found"
'get closest enemy
C = Get_ClosestPlayer
Debug.Print "Closest: " & C
'Check if closest is in range
Debug.Print "Range: " & R
Debug.Print "Pos: " & Pos(1, C)
If R <= Abs(Pos(1, C)) Then
        'Get stats
        Dim EAtt, PLife, DefEff As Integer
        Dim PCount, ECount As Integer
        EAtt = TroopHandler.Get_Att(T)
        PLife = TroopHandler.Get_Life(C)
        PCount = Sent(T)
        ECount = Defenders(C)
        Debug.Print "EAtt: " & EAtt
        Debug.Print "PCount: " & PCount
        'Calculate
        Dim AttEff
        AttEff = EAtt * ECount
        DefEff = (0.5 * PLife) * PCount
        Debug.Print "AttEff: " & AttEff
        Debug.Print "DefEff: " & DefEff
        'ATTACK!
        If Sent(C) <= 0 Then GoTo 1
        Sent(C) = DefEff - AttEff
        Debug.Print "Player's Troops: " & Sent(C)
        'Sent(C) = ((PCount * (1 / 2 * PLife) - AttEff))
End If
End If
1:
Next T

'Move forward
For T = 1 To 10
If Pos(1, T) > 0 Then
S = TroopHandler.Get_Speed(T)
    If Pos(1, T) < S Then
        Pos(1, T) = 0
    Else
        Pos(1, T) = Pos(1, T) - S
    End If
End If
Next T

Dim O
O = Check_Outcome
If O = 0 Then
Round = Round + 1
PlayerAttack
Else
    MsgBox "Enemy Won."
End If
End Sub
Public Function Check_Etroops()

End Function
Public Function Check_Outcome()
Dim X
'Check Enemy
For X = 1 To 10
If Defenders(X) > 0 Then
GoTo CheckP
End If
Next X
Check_Outcome = 1 'Player won
Debug.Print "OUTCOME...PLAYER WON!"
Exit Function
CheckP:
For X = 1 To 10
If Sent(X) > 0 Then
Check_Outcome = 0 'None
Exit Function
End If
Next X

Check_Outcome = 2 'Enemy won
Debug.Print "OUTCOME...ENEMY WON!"
End Function
Public Function Get_ClosestEnemy()
Dim X
Dim Closest
Closest = 200
Dim C(1 To 10)
For X = 1 To 10
C(X) = Pos(2, X)
Next X
For X = 1 To 10
If C(X) < Closest Then
    Closest = X
End If
Next X
Get_ClosestEnemy = Closest
End Function

Public Function Get_ClosestPlayer()
Dim X
Dim Closest
Closest = 200
Dim C(1 To 10)
For X = 1 To 10
C(X) = Pos(1, X)
Next X
For X = 1 To 10
If C(X) < Closest Then
    Closest = X
End If
Next X
Get_ClosestPlayer = Closest
End Function



Is This A Good Question/Topic? 0
  • +

Replies To: Overflow Error in Battle Simulator

#2 tlhIn`toq  Icon User is offline

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

Reputation: 5436
  • View blog
  • Posts: 11,656
  • Joined: 02-June 10

Re: Overflow Error in Battle Simulator

Posted 28 December 2012 - 11:09 AM

View PostAnimeWarrior, on 28 December 2012 - 11:25 AM, said:

every time a type of troop dies, it gets replaced some how


"Some how" ? You don't know where you wrote the code to make a new troop, or when you call it?
Have you placed breakpoints at the start of the method that makes the troop so you can see where it got called from?

AnimeWarrior: 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 DEC 2012
Spoiler



Was This Post Helpful? 0
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4313
  • View blog
  • Posts: 12,090
  • Joined: 18-April 07

Re: Overflow Error in Battle Simulator

Posted 28 December 2012 - 11:11 AM

Your code is a maintenance nightmare. You should probably stop using single letter variable and array names, use more descriptive names, use comments that actually say something about the code other than "calculate" or "attack" and try some better indenting and use of white space.

If you can clean this up a bit it will be easier for all of us to actually see what might be happening here.

:)
Was This Post Helpful? 3
  • +
  • -

#4 tlhIn`toq  Icon User is offline

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

Reputation: 5436
  • View blog
  • Posts: 11,656
  • Joined: 02-June 10

Re: Overflow Error in Battle Simulator

Posted 28 December 2012 - 11:21 AM

As an aside, these variable names make it REALLY hard on everyone including yourself. This isn't algebra class where you have to have names of X, A, B etc.


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.

Was This Post Helpful? 0
  • +
  • -

#5 AnimeWarrior  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 44
  • Joined: 28-December 12

Re: Overflow Error in Battle Simulator

Posted 28 December 2012 - 11:28 AM

Yes I do know how to use breakpoints. I still have no idea why it is adding troops. There is no built in feature in this module to add a troop. I am pretty sure it is a simple math error.

The lack of neatness is because I never intended to get help on it. I have made it a bit neater.
Option Explicit
Public Target As Integer
Public Round As Integer
Public DistToTarget
Public Pos(1 To 2, 1 To 10) As Integer
Dim Sent(1 To 10) As Integer
Dim Defenders(1 To 10) As Integer


Public Sub Send(ID, N)
Sent(ID) = N
End Sub


Public Sub Set_Defender(ID, N)
Defenders(ID) = N
End Sub


Public Sub Battle_Start()
Round = 1
Dim X
For X = 1 To 10
	Pos(1, X) = 0 - TroopHandler.Get_Range(X)
Next X
For X = 1 To 10
	Pos(2, X) = TroopHandler.Get_Range(X) + 50
Next X
PlayerAttack
End Sub

Public Sub PlayerAttack()
Dim T, Range, Closest, Speed
For T = 1 To 10
	If Sent(T) > 0 Then
		'Troops found with ID T
		'Get range
		Range = TroopHandler.Get_Range(T)
		'get closest enemy
		Closest = Get_ClosestEnemy
		Debug.Print "Closest Enemy: " & Closest
		'Check if closest is in range
		If Range <= Pos(2, Closest) Then
				'Get stats
				Dim PAtt, ELife, DefEff As Integer
				Dim PCount, ECount As Integer
				PAtt = TroopHandler.Get_Att(T)
				ELife = TroopHandler.Get_Life(Closest)
				PCount = Sent(T)
				ECount = Defenders(Closest)
				'Calculate
				Dim AttEff
				AttEff = PAtt * PCount
				DefEff = (0.5 * ELife) * ECount
				Debug.Print "Round: " & Round & " AttEff: " & AttEff
				Debug.Print "DefEff: " & DefEff
				Debug.Print "Att-DEf: " & AttEff - DefEff
				Debug.Print "Def-Att: " & DefEff - AttEff
				'ATTACK!
				Debug.Print "Defenders: " & Defenders(Closest)
				Debug.Print "Defense: " & (1 / 2 * ELife)
				Debug.Print "Life: " & ELife
				Debug.Print "Total Defense: " & (ECount * (1 / 2 * ELife))
				Debug.Print "Killed: " & ((ECount * (1 / 2 * ELife) - AttEff))
				'Defenders(Closest) = Defenders(Closest) - ((ECount * (1 / 2 * ELife) - AttEff))
				Defenders(Closest) = (DefEff - AttEff)
				Debug.Print "Lived:" & Defenders(Closest)
		End If
	End If
Next T

'Move forward
For T = 1 To 10
	If Pos(1, T) < 0 Then
	Speed = TroopHandler.Get_Speed(T)
		If Abs(Pos(1, T)) < Speed Then
			Pos(1, T) = 0
		Else
			Pos(1, T) = Pos(1, T) + Speed
		End If
	End If
Next T

Dim O
O = Check_Outcome
If O = 0 Then
	Round = Round + 1
	PCAttack
	Else
		MsgBox "Player Won."
		Dim L
		'Give reward
		For L = 1 To 6
			Call ResHandler.Set_Res(L, Map.Get_Reward(L))
		Next L
End If
End Sub

Public Sub PCAttack()
Debug.Print "ENEMY ATTACK"
Dim T, Range, Closest, Speed
For T = 1 To 10
	If Defenders(T) > 0 Then
		'Troops found with ID T
		'Get range
		Range = TroopHandler.Get_Range(T)
		'get closest enemy
		Closest = Get_ClosestPlayer
		'Check if closest is in range
		If Range <= Abs(Pos(1, Closest)) Then
				'Get stats
				Dim EAtt, PLife, DefEff As Integer
				Dim PCount, ECount As Integer
				EAtt = TroopHandler.Get_Att(T)
				PLife = TroopHandler.Get_Life(Closest)
				PCount = Sent(T)
				ECount = Defenders(Closest)
				'Calculate
				Dim AttEff
				AttEff = EAtt * ECount
				DefEff = (0.5 * PLife) * PCount
				'ATTACK!
				If Sent(Closest) <= 0 Then GoTo 1
				Sent(Closest) = DefEff - AttEff
				'Sent(Closest) = ((PCount * (1 / 2 * PLife) - AttEff))
		End If
	End If
1:
Next T

'Move forward
For T = 1 To 10
	If Pos(1, T) > 0 Then
		Speed = TroopHandler.Get_Speed(T)
		If Pos(1, T) < Speed Then
			Pos(1, T) = 0
		Else
			Pos(1, T) = Pos(1, T) - Speed
		End If
	End If
Next T

Dim O
O = Check_Outcome
If O = 0 Then
	Round = Round + 1
	PlayerAttack
Else
    MsgBox "Enemy Won."
End If
End Sub


Public Function Check_Outcome()
Dim X
'Check Enemy
For X = 1 To 10
	If Defenders(X) > 0 Then
		GoTo CheckP
	End If
Next X
Check_Outcome = 1 'Player won
Exit Function
CheckP:
For X = 1 To 10
	If Sent(X) > 0 Then
		Check_Outcome = 0 'None
		Exit Function
	End If
Next X
Check_Outcome = 2 'Enemy won
End Function


Public Function Get_ClosestEnemy()
Dim X
Dim Closest
Closest = 200
Dim C(1 To 10)
For X = 1 To 10
	C(X) = Pos(2, X)
Next X
For X = 1 To 10
	If C(X) < Closest Then
		Closest = X
	End If
Next X
Get_ClosestEnemy = Closest
End Function

Public Function Get_ClosestPlayer()
Dim X
Dim Closest
Closest = 200
Dim C(1 To 10)
For X = 1 To 10
	C(X) = Pos(1, X)
Next X
For X = 1 To 10
	If C(X) < Closest Then
		Closest = X
	End If
Next X
Get_ClosestPlayer = Closest
End Function



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#6 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 572
  • View blog
  • Posts: 2,985
  • Joined: 19-May 09

Re: Overflow Error in Battle Simulator

Posted 28 December 2012 - 12:53 PM

Quote

The lack of neatness is because I never intended to get help on it.
Reminds me of when I was a little boy and came in dirty from playing outside. My mother told me to wash my hands and arms and put on a clean shirt before dinner. I washed my hands only and put on a long sleeved shirt. My mom pulled up my sleeves and told me to go wash my arms, to which I protested that nobody could see them. I didn't get very far with that argument.

Point is, as others have already said, if your code is sloppy it's hard to think neatly about it.

Now, I don't believe you've told us the line of code where you get the Overflow error. Rather than asking us to work through all of your code, it would be helpful if you would tell us.
Was This Post Helpful? 1
  • +
  • -

#7 AnimeWarrior  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 44
  • Joined: 28-December 12

Re: Overflow Error in Battle Simulator

Posted 28 December 2012 - 02:29 PM

        DefEff = (0.5 * ELife) * ECount


This line within the PlayerAttack Subroutine is showing the error. However the fact that this is a round based simulation,my problem could stem from any one of the lines.
After more debugging I have found that after the player reaches 0 troops, the simulation continues and the player then has something like 1.24E24 troops when he should have none.
As I said its probably a simple math error but I cant find it. The round based simulator is making it hard to pinpoint the lines of code adding troops and not ending the simulation.
Was This Post Helpful? 0
  • +
  • -

#8 AnimeWarrior  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 44
  • Joined: 28-December 12

Re: Overflow Error in Battle Simulator

Posted 28 December 2012 - 03:45 PM

Well this is pathetic... in the time you all took to bash my style of coding, I have found the problems myself. Everyone has their own way of coding, mine just happens to be "chaotic". Unlike some of you older folks, I can remember exactly what each of my one-letter variables do. I know each one stands for something.
Example:
S - Speed
R - Range
T - Temp, or in the Battle Module's case it is Troop
X - Temp variable used in Counting/For Loops
Was This Post Helpful? -1
  • +
  • -

#9 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8937
  • View blog
  • Posts: 33,473
  • Joined: 12-June 08

Re: Overflow Error in Battle Simulator

Posted 28 December 2012 - 03:57 PM

Quote

Well this is pathetic... in the time you all took to bash my style of coding, I have found the problems myself.

Ah.. glad to be of service?

Quote

Everyone has their own way of coding, mine just happens to be "chaotic". Unlike some of you older folks, I can remember exactly what each of my one-letter variables do.

Look.. that's great.. you know what your code does but your way of coding is not conducive to comprehension outside of anyone but you. If you want to ask for help - great, but understand you are handicapping anyone who *isn't* you by having a coding style that runs contrary to general best.. nay.. decent.. practices.
Was This Post Helpful? 2
  • +
  • -

#10 AnimeWarrior  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 44
  • Joined: 28-December 12

Re: Overflow Error in Battle Simulator

Posted 28 December 2012 - 04:10 PM

Sorry, I was expecting more positive feedback than everyone bashing my naming techniques and indenting.
Was This Post Helpful? 0
  • +
  • -

#11 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8937
  • View blog
  • Posts: 33,473
  • Joined: 12-June 08

Re: Overflow Error in Battle Simulator

Posted 28 December 2012 - 04:43 PM

Personally speaking - understand if everyone who came here asking for help had some inane coding practice that only made sense to them we would spend countless hours and days just trying to make sense of what was being written and *THEN* start the help. Remember that, though from your perspective your snowflake coding practice makes sense to you and you are one person asking a bunch of people for help.. in my shoes I am one person helping a multitude of people.

To the cut to the chase - it is more effectively to coach folks into a generally accepted coding practice that is fairly universal across the industry. Everyone wins!

Quote

Sorry, I was expecting more positive feedback than everyone bashing my naming techniques and indenting.

Another piece of advice.. and again this is just me spitball'n - but not everyone is going to coddle you and interact with you on a warm and fuzzy level. Some people are blunt and others are solving problems for you that you didn't realize you have. Everything in this thread has been positive for one reason or another.. abet it might not have directly solved the problem you had in hand, but hopefully it solved a problem for interacting with the industry in the future. ;)
Was This Post Helpful? 2
  • +
  • -

#12 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 572
  • View blog
  • Posts: 2,985
  • Joined: 19-May 09

Re: Overflow Error in Battle Simulator

Posted 05 January 2013 - 01:06 PM

View PostAnimeWarrior, on 28 December 2012 - 04:45 PM, said:

Well this is pathetic... in the time you all took to bash my style of coding, I have found the problems myself. Everyone has their own way of coding, mine just happens to be "chaotic". Unlike some of you older folks, I can remember exactly what each of my one-letter variables do. I know each one stands for something.
Example:
S - Speed
R - Range
T - Temp, or in the Battle Module's case it is Troop
X - Temp variable used in Counting/For Loops

If you want to be the sort of coder whose code is only intelligible to himself, then if that works for you, stick with it. If you want to behave like a punk, then don't post here anymore.

This post has been edited by BobRodes: 05 January 2013 - 01:08 PM

Was This Post Helpful? 0
  • +
  • -

#13 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Re: Overflow Error in Battle Simulator

Posted 05 January 2013 - 02:31 PM

It helps to have proper meaningful variable names, as the maintainer and future developers will appreciate it. Remember that this person could be you, and you've forgotten what they stand for.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1