This site requires JavaScript, please enable it in your browser!
Greenfoot back
Insomnia
Insomnia wrote ...

2012/11/26

problems to write the right code for a scenario

Insomnia Insomnia

2012/11/26

#
http://s7.directupload.net/file/d/3086/wu5drgm6_png.htm On this picture you can see my scenario. I want to collect this single battery with the robot. The robot has to go the left way, it is against the way the clock runs. My first impressions were to check with a "while-loop" if there is a battery on the field and then to check where the walls are.. Can anyone help me with the code please? I am not able to write it in a right way :(
danpost danpost

2012/11/26

#
First your robot has to find the wall. This can be accomplished by the following steps each act cycle until a wall is found: (1) move one space foward (2) if intersecting wall, move back and turn left Now that the robot has found the wall, it must be able to follow the wall. This can be accomplished by the following steps each act cycle until the battery is found: (1) turn right (2) move one space forward (3) while intersecting wall, move back, turn left and move forward (4) if battery is found, done. The battery must be placed in a cell that at minimum touches the corner of a wall cell. In other words, the eight cells around the cell that the battery is in cannot be all non-wall cells. If you allow that to be a possibility, then the movement code for the robot would be all the more complicated (there are different degrees of complexity that can occur, in that case; depending on the shape of the area). Since the movement code for the robot above is in two parts, the robot must 'know' what part it is trying to accomplish. A boolean field would suit the needs of determining which part it is on. You can call it 'foundWall' and have it initially set to 'false'. When the wall is found, set it to true. The part of the code to execute can be based on the value of this field.
Insomnia Insomnia

2012/11/26

#
Thank you very much for your answer. Your solution was first in my mind, but then there is another problem: The battery could be on another field and there can be a random wall additional. For example in my second picture... I have really problems to write the code for this :( http://s1.directupload.net/file/d/3086/y3jskrx4_png.htm
danpost danpost

2012/11/26

#
Yes, I did notice that there were two cells that do not have any walls near them. That would be problematic. The random wall, however, would only tend to help that situation (if you noticed, your first picture has two cells without walls near them; but, your second picture only has one because of the extra wall). One way to resolve your situation is to have the robot map out where it has gone while using the method given above, then programatically go to the remaining unvisited possible non-wall cells.
Insomnia Insomnia

2012/11/28

#
Do you want to write the code for me? :D I am not able to do this :(
You need to login to post a reply.