Hi, I handled the challenge in this video differently than Ben, I feel like my code is simpler and easier to read, and therefore “good code”. Can anyone who knows more than me let me know if I am correct in my thinking?
Ben’s Code:
private void CalculatePath()
{
LoadBlocks();
BreadthFirstSearch();
CreatePath();
}
private void CreatePath()
{
SetAsPath(endWaypoint);
Waypoint previous = endWaypoint.exploredFrom;
while (previous != startWaypoint)
{
previous = previous.exploredFrom;
SetAsPath(previous);
}
SetAsPath(startWaypoint);
path.Reverse();
}
private void SetAsPath(Waypoint waypoint)
{
path.Add(waypoint);
waypoint.isPlaceable = false;
}
This is the code I wrote before seeing Ben’s solution:
private void CreatePath()
{
LoadBlocks();
BreadthFirstSearch();
CalculatePath();
MarkUnplaceableBlocks();
}
private void MarkUnplaceableBlocks()
{
foreach (Waypoint waypoint in path)
{
waypoint.isPlaceable = false; // all waypoints in path cant have towers placed
}
}
I am BRAND new to programming and for the first time felt like I did a decent job and wrote clean working code on my own. Could anyone please confirm that this was good, or enlighten me on why doing it Ben’s way was better? I know that if they both work there is no wrong answer, so is there any glaring flaws in the way I handled this challenge? Thanks for any feedback!