rock paper scissors

MAZES CHALLENGE

A maze is a path or collection of paths, typically from an entrance to a goal.  People of all ages enjoy solving these puzzles. These graphic designs test a person’s skill to find the exit using the shortest and perhaps only path.

Today’s Python coding challenge creates a solution for any rectangular maze. While the coding does not find the shortest solution, it does allow for a robot to find the intended target no matter where in the maze it starts or in which direction the robot is facing.

If mazes intrigue your mind, check out China’s sprawling Yancheng Dafeng Dream Maze, which is located in the city of Yancheng, it stands as the world’s largest permanent maze. The colorful attraction, which has a total size of 383,160.12 square feet, also broke the Guinness World Record for the world’s largest pathway network in a hedge maze in the summer of 2018. Its longest path stretches a whopping 5.87 miles.

Of course in the United States, every Autumn one can easily find a local corn maze complete with Halloween horror-themed events.

Life is a maze from which we never escape. Every decision takes us in a different direction and every time we turn one way we could just as easily have turned the other.― Chloe Thurlow, Katie in Love

INDENTS MATTER

This project represents Day 6 of the #100 Days of Coding Challenge.

In Python, the proper indentation of the code proves crucial. Many other coding languages mark a block of code with { }, but Python utilizes indentation. The block starts with the first line of code being indented and ends when the next block remains unindented. While many debate indenting with spaces or tabs, Python prefers using spaces. Ideally, indent each block of code with four spaces.

Therefore, indentation advantages include better readability and consistency. Indentation errors and logical errors occur when proper indentation is not followed.

FUNCTIONS SAVE

Often certain lines of code need repeating. Defining these lines of code into a function help avoid repetition and allows the code to be reusable.

In Python, a function is a group of related statements that performs a specific task. This modular approach keeps the coding more organized and manageable.

To define a function, the keyword “def” marks the set of lines for the function. Functions permit the passage of parameters (arguments) but are not necessary. A colon (:) marks the end of the function header.

In this coding challenge, the maze presents itself in Reeborg’s World, a program to help beginners to learn Python. Some functions already exist. Others the programmer needs to establish.

For instance, the function “turn_left” is predefined, but “turn_right” does not exist. By defining a function, to “turn_left” three times, the robot actually turns right. No need to clutter the lines of code with “turn_left” three times every time the robot needs to turn right. Just insert the new function turn_right().

def turn_right():
turn_left()
turn_left()
turn_left()

LOOPS REPEAT

Flowchart of while loop

Often in programming, lines of code need repeating. For this challenge, I used the “while” loop.

These lines of code repeat until a condition is met. And when the condition becomes false, the line immediately after the loop in the program is executed. In this scenario, until the robot reaches the exit location, the block of code contained in the “while” loop repeats.

“For” loops also repeat, but they are generally used when a certain amount of reiteration is needed. Perhaps you need lines of code to repeat for each item in a list or each number within a range. Visit my Password Generator for programming with “For” loops.

In this exercise, the number of turns and moves remains unknown as the robot appears randomly each time the program executes. This type of loop (the while loop) falls under the category of indefinite iteration.

 ESCAPE THE MAZE – DAY 6

Check out my “Escape the Maze” program!

The logic of this challenge did not include finding the shortest path to the maze exit. Instead, it utilizes the logic that many firefighters deploy as they search for people inside burning structures and wish to maintain their orientation. In the Firefighter-Oriented Search Method, they follow a wall. Thus, the robot in this program always follows the right wall.

The screenshot of the program (below) shows a robot randomly placed in the maze and the robot facing direction is random as well. One of the great attributes of Reeborg’s World is that you can watch the program as it steps through each line of code (right screen). Just start the play button.

If you wish to see the robot in a different position, you can copy and paste my code below into the Reeborg’s World website. Here are the instructions.

  • Click HERE to go the Reeborg’s World website
  • Exit out of the “Lost in the Maze” window
  • Paste the copy and paste the code below into the Python Code window. Make sure the indentation is correct!
  • Click the play button.
  • To reload the game click the blue and white reload arrow (far right above maze)   .
  • Have fun!
def turn_right():
turn_left()
turn_left()
turn_left()

 

while front_is_clear():
move()
turn_left()

 

while not at_goal():
if front_is_clear() andwall_on_right():
move()
elif front_is_clear() and right_is_clear():
turn_right()
move()
elif front_is_clear():
move()
elif wall_in_front() and right_is_clear():
turn_right()
move()
else:
turn_left()

If you wish to see the robot in a different position, you can copy and paste my code below into the Reeborg’s World website. Here are the instructions.

  • Click HERE to go the Reeborg’s World website
  • Exit out of the “Lost in the Maze” window
  • Paste the copy and paste the code below into the Python Code window. Make sure the indentation is correct!
  • Click the play button.
  • To reload the game click the blue and white reload arrow (far right above maze)   .
  • Have fun!
def turn_right():
turn_left()
turn_left()
turn_left()
while front_is_clear():
move()
turn_left()
while not at_goal():
if front_is_clear() and wall_on_right():
move()
elif front_is_clear() and right_is_clear():
turn_right()
move()
elif front_is_clear():
move()
elif wall_in_front() and right_is_clear():
turn_right()
move()
else:
turn_left()