 |
 |

11-20-2004, 04:07 PM
|
|
Freshman
|
|
Join Date: Oct 2004
Posts: 48
|
|
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
|
|

11-20-2004, 04:37 PM
|
|
Senior Contributor
Retired Moderator * Guru *
|
|
Join Date: Aug 1999
Location: Hartford, Connecticut, 06
Posts: 1,487
|
|
|
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.
|
|

11-20-2004, 04:52 PM
|
|
Freshman
|
|
Join Date: Oct 2004
Posts: 48
|
|
|
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.
|
|

11-20-2004, 05:17 PM
|
|
Newcomer
|
|
Join Date: Sep 2003
Location: Kentucky, USA
Posts: 12
|
|
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
|
|

11-20-2004, 05:36 PM
|
|
Senior Contributor
Retired Moderator * Guru *
|
|
Join Date: Aug 1999
Location: Hartford, Connecticut, 06
Posts: 1,487
|
|
|
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.
|
|

11-20-2004, 10:03 PM
|
|
Freshman
|
|
Join Date: Oct 2004
Posts: 48
|
|
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! 
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|
|
|
 |
|