Ugly, Redundant Code that Makes My Eyes Hurt:
public override void Attack(Direction direction, Random random)
{
bool hit;
if (direction == Direction.Up)
{
hit = DamageEnemy(direction, 20, 6, random);
if (!hit)
{
hit = DamageEnemy(Direction.Left, 20, 6, random);
if (!hit)
{
hit = DamageEnemy(Direction.Down, 20, 6, random);
if (!hit)
{
DamageEnemy(Direction.Right, 20, 6, random);
}
}
}
}
else if (direction == Direction.Left)
{
hit = DamageEnemy(direction, 20, 6, random);
if (!hit)
{
hit = DamageEnemy(Direction.Down, 20, 6, random);
if (!hit)
{
hit = DamageEnemy(Direction.Right, 20, 6, random);
if (!hit)
{
DamageEnemy(Direction.Up, 20, 6, random);
}
}
}
}
else if (direction == Direction.Down)
{
hit = DamageEnemy(direction, 20, 6, random);
if (!hit)
{
hit = DamageEnemy(Direction.Right, 20, 6, random);
if (!hit)
{
hit = DamageEnemy(Direction.Up, 20, 6, random);
if (!hit)
{
DamageEnemy(Direction.Left, 20, 6, random);
}
}
}
}
else
{
hit = DamageEnemy(Direction.Right, 20, 6, random);
if (!hit)
{
hit = DamageEnemy(Direction.Up, 20, 6, random);
if (!hit)
{
hit = DamageEnemy(Direction.Left, 20, 6, random);
if (!hit)
{
DamageEnemy(Direction.Down, 20, 6, random);
}
}
}
}
}It seems like a more elegant solution should be possible. I thought about it for a minute and came up with another solution. It allows me to use the same block of code for all the different directions. But one problem is the annotation in the book says something about making the mace swing in a nice circle. My solution could cause the mace to swing otherwise. Also, this solution seems over complicated, and kind of janky. I'm creating a list and using it in a weird way. It's kind of a creative solution I guess, but just feels wrong to me and feels like it might be hard to decipher.
Creative Solution that's Overcomplicated and Bogus:
public override void Attack(Direction direction, Random random)
{
List<int> directions = new List<int>();
for (int i = 0; i < 4; i++)
{
directions.Add(i);
}
DamageEnemy(direction, 20, 6, random);
directions.RemoveAt((int)direction);
while (directions.Count > 0)
{
DamageEnemy((Direction)directions[directions.Count - 1], 20, 6, random);
directions.RemoveAt(directions.Count - 1);
}
}Finally I came up with something that feels right. I organized my Direction enum so the directions followed a clockwise order starting at the top with 'Up':
enum Direction
{
Up,
Right,
Down,
Left,
} Then I implemented the following solution that allows me to use the same block of code for all 4 directions and hit each direction with a nice arcing motion:
Nifty Solution that's Easy to Read:
public override void Attack(Direction direction, Random random)
{
int attackDirection = (int)direction;
while (true)
{
DamageEnemy((Direction)attackDirection, 20, 6, random);
attackDirection++;
if (attackDirection > 3)
attackDirection = 0;
if (attackDirection == (int)direction)
break;
}
}I haven't totally tested it yet (except for on some scrap paper) so if you spot any holes please let me know! Also, I'd be really interested to hear if anyone came up with some other solutions. This one took me a few minutes to figure out, so I'm just wondering if anyone else had to spend some time on it as well.
Thanks!
-Justin
This post has been edited by JJJenkins: 29 May 2011 - 10:06 AM











