Jump to content




Making Complex Program Goals Manageable


4 replies to this topic

#1 Lupus590

  • Members
  • 1,962 posts
  • LocationUK

Posted 27 December 2015 - 09:15 PM

"I want to do something BIG! Where do I start?"

This may help.

This tutorial will hopefully teach you how to break down a large problem into manageable smaller problems which should make your programming life easier.

Due to the multitude of programs one could write, I'm not going to be able to cover every use case. However, the steps will be very similar for most projects.

For my own sanity, I'm going to run through an example problem (programming a turtle to be an ore miner), but the guide is also applicable to other tasks (Off the top of my head: tree farms, wheat/crop farms, reactor control and power monitoring, etc. - I've specifically avoided Operating Systems as they have a host of other complexities, you may need to adapt this guide more than other projects).

My example problem to solve with a program
So I have managed to make a turtle I would like it to get more resources for me. Rather than download someone else's program, I have decided to make my own.

But mining is a surprisingly complex task.

How would a human do it?
First off think of the ways that you can do the task. Note down the kind of things you do.

Mining can be done 3 main ways by a player, you can go down caves, dig a large hole, or dig long tunnels.

Can a program do it?
Now that you have your list of how you do it, cross off the one which a program will not be easily made to do.

Back in my mining example, turtles can't see more than the block in front of them, so caving is not ideal. Digging a large hole will use a lot of fuel and will get us a lot of stone, but is doable. The last one is strip mining, which is a bit more complicated to automate, but quite efficient.

How does a dishwasher wash dishes?
How do you wash the dishes in a sink? Is it different to how a dishwashing machine does it? Perhaps there is a way which your computer can do which just isn't easy or safe for a player to do? Computers and humans have different abilities. A computer can check a reactor's temperature a lot more frequently than you can, and can react to changes sooner. This means that the computer may have a smaller error margin or reaction buffer, which may allow you to make your reactor more efficient.

Remember the rule "don't dig straight down"? Well, turtles can fly, so that's not an issue for them. Combine this with their immunity to lava and you have a good reason to consider having them mine in a way which is not safe players.

Add to your list things which are not possible as a human but the computer can do.

Divide and conquer
So I've decided that my turtle is going to mine via vertical tunnels, but how do I get it do that? It's time to try and break the problem task down into smaller tasks. This step can be replicated several times until the smaller tasks are manageable. Some of these smaller tasks could end up as functions, or parts of a loop, or even an if statement - don't worry about the specifics yet, in fact, you don't need to be programming quite yet.

So my turtle will start on the surface, mines down until it gets to bedrock, then returns to the surface. Sounds easy enough, but how do I know when I get to bedrock? I can't mine bedrock, so if I try to mine and turtle.digDown returns false then I know that whatever is below me must be bedrock. If I get that to bedrock then I need to start going up. But how will I know that I'm back on the surface? Well, computers are really good with numbers, so why don't I get my turtle to count how many times it has gone down, it could then use this number to know how many times to go back up. My program now will have two states, it will start on the surface and go down (descending state). Once it tries to mine bedrock it will change to the ascending state and start returning to the surface.

Maximising efficiency
So my turtle miner would work (if I wrote a program for it, which you could at this stage), but it could be better. Remember when I said that turtles can see only the block in front of them? (Which is why we opted out of automating cave mining.) Well, there are blocks which the turtle can see as it is going down. Why don't we get it to mine those too?

Are there any improvments that you can make to your program so that it solves your task better?

Returning to my turtle, I recall that there are four sides to a hole in Minecraft, If I make the assumption that I've already done the level above me then I only have to worry about the block in front, behind, and to my left and right. But turtle can only see from in front. I can turn round so that my side becomes my front, do this enough times and I would have face every side of the hole. But I don't want to mine is unless I want it....

While not programWritten() do writeProgram() end
I said that sometimes you may need to repeat steps, keep breaking your tasks down until you can program a solution for them, keep an eye out for situations where your program can be made more efficient or is limited due to how computers work.

In its current state, my turtle program could dig a plus-shaped hole. I could get 1/5th of the cobblestone which I would by having my turtle be more intelligent on what to mine. I want my turtle to scan and dig the walls of the tunnel, I've already compensated for the fact that the turtle can only see the block ahead. Turtles can compare items in their inventory with blocks in the world, but they also can inspect blocks and get the name of the block. Compare requires that I have the blocks with me which will use inventory space, blocks which drop different items will also cause problems (such as coal), but silk touch can fix that. Inspect however requires me to know the names of the blocks, telling the program these names is another point to consider...

Sometimes a solution will bring with it another problem. In my above example, I have two solutions to my current task, both bring with them other problems. If you find yourself in a similar situation, I would encourage you to weigh the pros and cons of each option.

Sometimes the more efficient option can bring problems which are above your current programming ability, that's fine, go for the simpler option and maybe make a note of the more efficient one to swap it in later.

Again, this step will likely assist in the creation of functions, for example identifying whether a block on the wall of the tunnel is something I want to mine will almost certainly be a function, turning the turtle to face each wall will also be a function. A lot of the rest of the program will probably be two loops, one for going down and the other for back up.

This example program could be a bit more efficient, it could start another tunnel from the bottom as it returns to the surface, but for now, I want more resources.

Don't be afraid to start using your less efficient program, in fact, it may help you find a bug you didn't know about or even a more efficient method of performing your task.

Edited by Lupus590, 11 December 2017 - 10:59 PM.


#2 Lupus590

  • Members
  • 1,962 posts
  • LocationUK

Posted 20 February 2016 - 07:28 PM

OP edit: Spelling corrections and title rename

#3 caza1112

  • Members
  • 14 posts

Posted 21 February 2016 - 01:10 AM

Nice work lupus, makes i sound much easier and you have to appreciate that :')

#4 Waitdev_

  • Members
  • 432 posts
  • LocationWherever you want me to be

Posted 21 February 2016 - 10:07 AM

wow, nice job :D

also, you can use this video to help you start making complex programs:
https://www.youtube....h?v=ZXsQAXx_ao0

Edited by Waitdev_, 23 February 2016 - 06:52 AM.


#5 Lupus590

  • Members
  • 1,962 posts
  • LocationUK

Posted 11 December 2017 - 11:00 PM

OP Edit:
Turtle miner example is now in italics to better separate it from the rest of the guide.
Some section title changes and rewrites of parts.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users