This questions is similar to a question I asked about how to use walls, but since I think you have a more general focus- I'll give you a more general answer :) .Constructing a good defense is complicated. Arguably its the most challenging aspect of the game. It is hard to define a "best" defense, since attackers will use different counter-strategies, the defensive options available to you vary by level, and new strategies are evolving all the time. That said, here are some tips I find helpful.
Step 1 - Decide to protect trophies or resources
This is an easy decision if you don't have much of one or the other, but you will eventually have to choose. Protecting your resources will be important when you are saving up for the late game upgrades - some of which will require days or even weeks of stockpiling to afford. Protecting your trophies means protecting your town hall and minimizing collateral damage - so as to minimize or eliminate "cheap" one star victories for your opponents.
Step 2a - Protecting resources
The best protection for resources, is to spend them - but of course sometimes that is not an option. Given that, it is best to put your storage containers deep inside your base, ideally behind concentric walls. Put your most powerful defensive buildings close your your storage containers, and consider putting traps near them as well.
Also, and this may seem unintuitive - make your Town Hall vulnerable. It's up to you whether you want to leave it totally undefended, or with minimum defenses - but make sure its the target that attackers focus on. When it is destroyed you get an 8 hour shield for free. If it is well protected it is actually possible to get attacked over and over - resulting in massive resource losses, without ever getting a shield.
Note: as of the 10 December 2015 update, trophy farming has been massively nerfed. Town halls now contain much more loot which can be stolen easily if left outside your base. Also shields are no longer awarded on TH destruction. Hence it is highly recommended to keep your Town Hall inside your base.
Step 2b - Protecting trophies
If you are going this route, the most important thing is to keep your Town Hall well protected - using the same strategy mentioned in Step 2a for your storage containers. Opponents can also get a 1 star victory by destroying 50% of your buildings, so keep your buildings close together, and fit whatever you can behind walls. Since you are less focused on resources, it is also good to spend as much as you can on walls and simple upgrades, which will make you less of an attractive target.
Step 3 - General Tips
- Get a Clan Castle, and join a clan that is active enough to keep it filled. Units from the Clan Castle are free to you, and are a nice boost to defenses.
- Use "less valuable" buildings like Builder Huts and Barracks to distract attackers. Note that these buildings create a "keep out" which can be used to control where the enemy can drop troops - forcing a long walk under fire, as opposed to getting dropped right where they want. Decorations can also be used in this way - but note that they do not create a keepout (other than the space they occupy).
- The edge of the world is no protection. Don't use it like a wall. It is in fact better to build a little away from it - see previous point.
- Mortars have a close in "blind spot". Use walls and building to "fill" this blind spot so enemy units can't stand in it.
- Defensive buildings (cannons, mortars, archer towers, etc) should all overlap, to protect each other.
- Any time you upgrade defensive buildings, remember they are out of commission. Try to cover the "hole" they make with other defensive buildings, and avoid upgrading too many at the same time.
Well.. rarely do you get a more definitive answer than from the developers themselves:
How does the Wall Breaker choose its target?
Steps 1, 2, 3
1. It will look for the nearest building/turret that is trapped (fully or partially) by walls
2. It will choose the shortest path towards that building and blow up walls that are in its way
3. After the wall is blown up, it will choose the next building that is still trapped by walls, run straight towards it, and blow up walls that are in its way
So Wall Breakers don’t target walls anymore?!?!!1!!!!?
No. Instead of targeting walls, they aim for the main prize, a building that is trapped by walls. The wall just happens to be in its way, and has to be blown up.
That building is the reason why you need that wall taken down, right?
Do Wall Breakers attack buildings?
No. They aim for the building and blow up the wall that is in their path.
Do Wall Breakers favorite junctions?
No. The Wall Breaker will run in a straight line towards the nearest building. When it encounters a wall, it blows it up.
Can I fool Wall Breakers by using spikes or short decoy wall sections?
No. They won't be fooled by those.
The two main takeaways from this that are interesting:
1. You can no longer lure Wall Breakers with "vestigial" walls.
2. Wall Breakers target the nearest enclosed building. Some have found that you can confuse them near the edge of your main village by enclosing something like a builder hut or tesla. ¡Viva the AI abuse!
Edit:
As added info, I've found this video from Flammy extremely informative. He shows the Wall Breaker aggro in action, and even has some crude measurements of aggro distance.
Best Answer
This is because the troop's targeting AI has a spec of "random" (actually determined by the attack's timestamp) placed into it.
What this does is that if there are two targets, of similar distance, as you've shown there, the troop may pick one over the other.
You can see this visible, if you were to extract a replay, change its timestamp and play it. This time, you'll see a different outcome as all a replay does is store the times which troop is deployed and not their full path.
Another comment to it: You may have calculated the path to the cannon incorrectly; it's a tiny bit closer than you think.