CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Help! Robot in a maze (beginner)

  1. #1
    Join Date
    Oct 2016
    Posts
    3

    Help! Robot in a maze (beginner)

    Hi!
    I'm doing a project where a robot is in a "corridor" with 4 sides and 4 corners. The robot starts in the top right corner and could be facing either South or West (it could move in a clockwise or counterclockwise direction) and should go one lap around the corridor and end up in the same spot.

    I have created a method where the robot checks in every corner if it should turn left or right (see below) but it is very clumpsy. Is there
    any easier way to code this?

    My instructions says: "When the robot is at the end of one corridor and is facing the wall, it is easy to find out if it is moving in a
    clockwise or counterclockwise direction. If there is a wall on the robots left side the movement is clockwise and vice versa.

    But I don't understand how it can check if there is a wall on the sides? I only know the "frontIsClear"-method.



    public void checkIfClear() {

    if (!robot.frontIsClear()) {
    robot.turnLeft();
    counter++;
    }
    if (!robot.frontIsClear()){
    turnAround();
    counter++;
    }
    }

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,154

    Re: Help! Robot in a maze (beginner)

    What methods are provided by the robot class?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.6.4)

  3. #3
    Join Date
    Oct 2016
    Posts
    3

    Re: Help! Robot in a maze (beginner)

    Quote Originally Posted by 2kaud View Post
    What methods are provided by the robot class?

    void move()
    void turnLeft()
    boolean frontIsClear()
    boolean atEndOfWorld()
    Location getLocation() r
    int getDirection()
    void setDelay(int t)

  4. #4
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,154

    Re: Help! Robot in a maze (beginner)

    Given just those methods it's not obvious if there is another way to check if left or right is blocked as the only useful test in this case seems to be frontIsClear().

    Is there anything returned by getLocation() or getDirection() which would be of use? The instructions refer to movement, so perhaps getDirection() is helpful?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.6.4)

  5. #5
    Join Date
    Oct 2016
    Posts
    3

    Re: Help! Robot in a maze (beginner)

    I've thought about that. Since the start direction is either south or west i thought that maybe one could use getDirection in the beginning, and then have one method for south and one for west.

    But then the instruction I have says "When the robot is at the end of one corridor and is facing the wall, it is *easy* to find out if it is moving in a
    clockwise or counterclockwise direction. If there is a wall on the robots left side the movement is clockwise and vice versa.

    I don't get this because there is no method for checking what is to the left/right.

  6. #6
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,154

    Re: Help! Robot in a maze (beginner)

    Since the start direction is either south or west i thought that maybe one could use getDirection in the beginning, and then have one method for south and one for west.
    Yes. If the start direction is south, then proceed forward until front is not clear. At which point you are at the bottom right, so you need to turn right. Then when way forward is not clear, turn right again. Basically when the way forward is not clear, turn right go forward and repeat until at starting point. As there is no direct turn right command, this can be done with three turn lefts.

    Similar for start west. But in this case when way is not clear turn left.

    When the robot is at the end of one corridor and is facing the wall, it is *easy* to find out if it is moving in a
    clockwise or counterclockwise direction
    The initial start direction determines if moving clockwise or counterclockwise. Start south then it is moving clockwise, start west and it is moving anti-clockwise.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.6.4)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)