I think I did pretty well with Lab #2. Got the program running just fine. Only problem I got is: My weapons are hitting in all directions all the time, not only in the specified direction. Which sense because the code provided in the book doesn't check the direction and there's some other aspects that just don't make sense to me. That's the code for the DamageEnemy method in Weapon.cs provided by the book:
protected bool DamageEnemy(Direction direction, int radius, Random random, int damage)
{
Point target = game.PlayerLocation;
for (int distance = 0; distance < radius; distance++)
{
foreach (Enemy enemy in game.Enemies)
{
if (Nearby(enemy.Location, target, radius))
{
enemy.Hit(damage, random);
return true;
}
}
target = Move(direction, target, game.Boundaries);
}
return false;
}
First: Why is there a For-loop? It Counts up int distance but it never actually uses it because the following foreach-loop uses radius instead. To my believe this outer loop does nothing which is supported by the fact that removing it doesn't do any harm, the code works like before.
Second: Although the direction is passed to the method, it is never achtually checked or used for the attack. It's just passed to the overloaded move method which brings me to:
Third: What is calling the overloaded Move-method supposed to achieve? It can't be moving the enemies after the attack, because that is already taken care in the attack method in the game class. I never wrote the overloaded Move method and commented out the line that's calling it and the code is working just fine.
I don't know if my Problems result from an error in the book's code of from the fact that I maybe solved a few tasks differently than the book suggested. Eg I'm removing used potions or dead enemys from the lists instead of just making them invisible as the book suggests. But that shouldn't be the source of the Problems I'm having I would guess.
Any hints anyone? My believe would be I'd have to rewrite the DamageEnemy method to check for the direction of the enemy before calling enemy.hit but maybe I'm missing something? Any Input is greatly appreciated.












