Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > Shorter code


Reply
 
Thread Tools Display Modes
  #1  
Old 11-20-2004, 04:07 PM
VB_MASTER VB_MASTER is offline
Freshman
 
Join Date: Oct 2004
Posts: 48
Default Shorter code


Hello, I what to be able to make better code here. Since my game is going to extendeble out side of the VB enviorment, I want to be able to check if a level is done, meaning all the enemys in that level are dead, as shown in the code below. The problem is I want to be able to check for all the enemy's dead and check which level is done or not. The code below works fine. But to expand the campain of the game you need to add more if statements.
Is there a way that I can have better code?
e_enemy() has an array of 20 but the level can be extended are far as it can go.

Code:
Function LevelDone(ByVal Level As Integer) As Boolean

If Level = 1 Then
    'The FinalDead should never change in a level or campain module!
    If e_Enemy(0).FinalDead = True And e_Enemy(1).FinalDead = True And e_Enemy(2).FinalDead = True Then 'Check to see if all the enemys are dead.
        LevelDone = True 'End the level
        fMain.ResetGame ' Reset the game.    
    End If
    
ElseIf Level = 2 Then
    If e_Enemy(0).FinalDead = True And e_Enemy(1).FinalDead = True And e_Enemy(2).FinalDead = True Then
        LevelDone = True
        fMain.ResetGame
    End If
ElseIf Level = 3 Then
    If e_Enemy(0).FinalDead = True And e_Enemy(1).FinalDead = True And e_Enemy(2).FinalDead = True Then
        LevelDone = True
        fMain.ResetGame
    End If
    
ElseIf Level = 4 Then
    If e_Enemy(0).FinalDead = True And e_Enemy(1).FinalDead = True And e_Enemy(2).FinalDead = True And e_Enemy(3).FinalDead = True Then
        LevelDone = True
        fMain.ResetGame
    End If
    
ElseIf Level = 5 Then
    If e_Enemy(0).FinalDead = True And e_Enemy(1).FinalDead = True Then
        LevelDone = True
        fMain.ResetGame
    End If
End If
End Function
Reply With Quote
  #2  
Old 11-20-2004, 04:37 PM
anhmytran anhmytran is offline
Senior Contributor

Retired Moderator
* Guru *
 
Join Date: Aug 1999
Location: Hartford, Connecticut, 06
Posts: 1,487
Default

Your code has very much chances to get better.
1- Your function accepts only 1 argument, and returns only 1 argument,
but the code inside the functions, you allow many arguments without
declaration. In this way, your function is not an ideal function. An ideal
function should have been a tightly closed environment, that allows all
arguments in and out itself through only its entrance.
2- You'd better apply the Case structure when the ElseIf statement repeats
more than 3 times.
Suggestion for improvement:
Make a class instead of your function. Class can accept many arguments
and return many arguments, while function can returns only a single argument.
In your case, the class should have following properties: Level, e_Enemy,
LevelDone, and fMain.
If the type of property fMain is Form, your function'd better to be a private
Sub in the form. Private Sub can work with access to all private objects and
variables in the form, and it can change them, too.
Reply With Quote
  #3  
Old 11-20-2004, 04:52 PM
VB_MASTER VB_MASTER is offline
Freshman
 
Join Date: Oct 2004
Posts: 48
Default

Okay sorry but that did not help any.
What I want is to be able to check if the enemys are dead, if all are dead. So far thats what the code here is doing. I want to change the code so that I don't have to keep adding if statements. This game I am making is supposed to be extendeble to a players view, meaning the player can extend it.
Reply With Quote
  #4  
Old 11-20-2004, 05:17 PM
Xiero Xiero is offline
Newcomer
 
Join Date: Sep 2003
Location: Kentucky, USA
Posts: 12
Default

If you know the number of enemies, why not try something like this?

Code:
Public Function AllEnemiesDefeated(lngEnemyAmount As Long) As Boolean

    Dim lngIndex As Long
    Dim lngEnemyDeathCount As Long
    Dim blnResult As Boolean
    
    For lngIndex = 0 To lngEnemyAmount
    
        If e_Enemy(lngIndex).FinalDead = True Then
        
            lngEnemyDeathCount = lngEnemyDeathCount + 1
        
        End If
    
    Next
    
    If lngEnemyDeathCount = lngEnemyAmount Then
    
        blnResult = True
        
        fMain.ResetGame
    
    End If
    
    AllEnemiesDefeated = blnResult

End Function
Reply With Quote
  #5  
Old 11-20-2004, 05:36 PM
anhmytran anhmytran is offline
Senior Contributor

Retired Moderator
* Guru *
 
Join Date: Aug 1999
Location: Hartford, Connecticut, 06
Posts: 1,487
Default

I do not know or understand your game (rule to play).
So, your word does not make sense to me.
You may shorten you If statement when you have a fixed number of rules.
In your code, I see that there are 3 rules in your game:
Rule 1 for level values of 1, 2, 3
Rule 2 for level values of 4
Rule 3 for level values of 5
You may make an array hard coded into the application with maximum of levels.
The array is 2-D with the first column containing all levels,
the second containing the number of rule associated with each level.
Then you write an If statement with 3 rules only .
The player can extend the levels.
When the code is executed, the level the player wants is checked
for the number of rule.
Then the number of rule is applied with your If statement.
When you have extending number of rules, you should write
Select Case structure instead of If statement.
Any way, you should write many Cases or you should hard code
a 2 dimentional array with many rows. You have no short cut.
Reply With Quote
  #6  
Old 11-20-2004, 10:03 PM
VB_MASTER VB_MASTER is offline
Freshman
 
Join Date: Oct 2004
Posts: 48
Default

Quote:
Originally Posted by Xiero
If you know the number of enemies, why not try something like this?

Code:
Public Function AllEnemiesDefeated(lngEnemyAmount As Long) As Boolean

    Dim lngIndex As Long
    Dim lngEnemyDeathCount As Long
    Dim blnResult As Boolean
    
    For lngIndex = 0 To lngEnemyAmount
    
        If e_Enemy(lngIndex).FinalDead = True Then
        
            lngEnemyDeathCount = lngEnemyDeathCount + 1
        
        End If
    
    Next
    
    If lngEnemyDeathCount = lngEnemyAmount Then
    
        blnResult = True
        
        fMain.ResetGame
    
    End If
    
    AllEnemiesDefeated = blnResult

End Function
Perfect! Thats exacly what I needed! Thank you! I have no idea why I did not think of that!!!!! Thank you!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->