Getting the right approach to programming

paul2978

  • Posts: 12
Hi

Iv just completed the first 9 levels of the google blocky code which is similar to Stencyl's code

http://blockly-demo.appspot.com/static/apps/maze/maze.html?lang=en&level=10&skin=0

The problem is I am stuck on level 10 - I think the problem is how I approach the problem - can anyone assist me in on how they would tackle this? As a new programmer I belive my approach is wrong and once I have got the "nack" of it things should start to slot in better while programming?

Does anyone have any suggestions on the methods that they use to approach a programming problem.

So for example in the situation above do you sit there and write out you have two lefts to navigate or do you have a set piece of coding that you just modify to suit your needs.

I think the main problem for me is how I approach a coding issue, Ive tried many combinations but they are a bit "haphazard" any pointers in the right direction would be great.



Thanks

Paul

« Last Edit: July 22, 2013, 01:28:30 am by paul2978 »

froz

  • Posts: 250
You need to think what exactly the programm needs to check and do to achieve the goal. As you can see, on this map, the robot needs to turn left and move forward whenever there is a path to the left, if there isn't, he needs to go forward if there is a path ahead, if it also isn't there, he needs to turn right and move forward. He needs to repeat those steps till hi finds the exit.

Now you need to figure out how to put that into code blocks. I don't want to spoil it for you, but remember you can put code blocks inside another code blocks and it will be needed here.

letmethink

  • *
  • Posts: 2545
Here is my approach to solving this and other problems.

1. Check for any hints or ideas of ideal solutions.
2. Utilise and analyse those.
3. Come up with a solution in your mind
4. Work out how to do this in code blocks
5. Implement
6. Test.
7. Simplify or improve (Here I was able to do it with three blocks spare)

Following these steps, this was my workflow for this challenge.

1. Oh! A nice hint. It clearly says there to follow the left wall.
2. Quick test of following my finger, that works.
3. So what I have to do is get the man to follow the left wall which means that if you can turn left you do, if not, you just move on and if you can't do either of these you turn right.
4. So, to start off you want a repeat until touching sign block as a clue is given. Then  you want to turn left it is possible so then use an 'if path to the left' turn left and then move ahead and in the else 'if path ahead'.
In the 'if path ahead' you put move forward and in the else of that you put if path to the right, turn right and the move ahead

This is quite interesting as it actually leaves us with one block spare.
5. Implemented
6. Works it seems
7. Right now to the interesting part, making it work while using the least blocks as possible. I managed to make it work with four spare, here's how.

Okay first analysis:

From this I noticed that In every block you move ahead so it would save blocks to just get rid of three move aheads and just put one outside the ifs: Saves 2 blocks

Also, more complicated but worth it. Get rid of the 'if path to the right' block. This block is checking whether there is a path to the right or not and as we have already established that there is not one ahead or to the left we can assume that there will be one to the right (unless the hint about following the left wall was lying). Getting rid of this block may be hard to spot at first but pretty easy to understand afterwards: Saves 1 block

Overall, with this optimisation we have 4 blocks spare, good work!
~Letmethink

paul2978

  • Posts: 12
Thanks guys this is good advice :)