Mazer, the Maze Walking Snake in MakeCode for Minecraft

worm1

snake in maze 1

snake in maze 2

big Mazze Walked.png

Using Mazer

Generate a Maze

Obviously, Mazer needs a maze to walk. Our program Amazing Maze Maker  builds mazes.  Run it to generate one or more mazes before using Mazer.

If Necessary, Change Mazer Blocks to Match the Maze

The blocks from which the maze is constructed are the first three in the code below. They should match the settings in the maze generation program.  The last two blocks are he blocs from which the snake is constructed including the snake body block (black here) and the head block (red here).

init blocks code

Stand in Front of the Maze Facing East

To use Mazer, the player should stand a few blocks south (right) of the sunflower on he ground in front of the maze. Sunflowers always face east. The player can stand several blocks in front of the maze, but must be in front of it — not to the side.  Mazer first finds the opening near the sunflower, then moves to the opening and starts moving through the maze until it finds the exit, which is on the opposite side of the maze.

sunflower

Run Chat Command to Start the Program

Mazer’s run command has one parameter, the length of the snake, which defaults to 5 blocks. Longer snakes are fun to watch because in some mazes the snake’s tail may block an opening that the snake must go through. Mazer drops blocks from the end of its tail until it can get through the opening. Life often requires some sacrifice, and so it is with Mazer.

Erase the Snake and Its Trail to Start Over

The chat command “erase” is used to erase the snake and its trail so that the maze can be used again.

The Algorithm

Mazer implements the best know of the maze solving algorithms: the wall-following algorithm. Such an algorithm only works with simple mazes; that is, mazes that have no loops. A wall-following algorithm can be left or right. In a right-following maze algorithm, the actor is thought of as moving through the maze with their right hand always touching a wall. The actor will exit the maze at another opening or, if none, it will exit where it enterd. Mazer implements a right wall-following algorithm.

A snake in a maze presents in interesting challenge because it occupies more than one space. What if the snake’s tail is blocking an opening within the maze through which the snake must pass. The algorithm implemented here drops tail blocks from the snake’s end until the opening is no longer blocked. We explored other options; e.g., making all openings two bloicks wide.

The Code

Run — the Main Control of the Program

The “run” chat command does the following:

  1. Initialize b_stop to false. The chat_command “stop” can set this to true to cause the snake to stop its movement and the program to be exited.
  2. snake_len is set to the argument num1 or if it is greater than 5 or to 5 which is the defalt.  In other words, a snake must be at least 5 blocks long. I often set the length to 20 so that I can watch the tail drop off when the snake gets stuck.
  3. Calls a function to set the origin and move the player.
  4. Initialize constants used by the program.
  5. Make the snake, wchih is placed  where the player was initially standing with its head towards the wall of the maze.
  6. Search for the maze door on the wall the snake is facing.
  7. If the door is not found, exit the program. Otherwise, move the snake to the door. The snake’s head will be just inside the door so that it is not on the grass.
  8.  Enter a loop the will continue until the snake’s head is on grass, which means that it has found the exit door. This loop can be stoped if the player enters the chat command “stop.”
  9. For each pass through the loop, the following is done:
    • calls a function to get the next move, whcih depends on the snakes surroundings.
    • call a function to move the snake one block as specified by the get_next_move function.
    • Tests if snake’s head is on grass.
  10. When grass is found, the snake is moved completely out of the maze and the program ends — one more maze walked.

run code

Initialization

init code

Make the Snake

make snake code

Test If the Snake’s Head Is on Grass

test if grass code

Geet Next Move

get next move code

Left and Right Turns

turn code

Move the Snake

move snake code

Test Snake Surroundings

test for air code

Find the Door into the Maze

search for door

Search East to Find the Wall

find wall code

Search Left or Right to Find Door

search for dor in one direction code

Move the Snake to the Door

move snake to dor

Erase Snake and Trail

erase code

Code Requires Flat World

This code assumes a flat world such as that described in MakeCode for Minecraft Sandbox World: Make It Flat and Simple.

Get the Code

Mazer code is shared on Code Connection at this URL

https://makecode.com/_g7t6TVJJC0p6

To get and use the code, follow these steps.

Click the Import button import button , which is upper right in the Code Connection window just below the banner. This will open the window shown below.
Import choices

Click the Import URL button Import URL, which is on the right, to open the window shown below.

Import Copy link

Paste the URL supplied for the program you want to use in the space under the text “Copy the URL …”
import url with url

Click the Go ahead! button go ahead button.

The next window you will see will be the MakeCode window with the code downloaded from the URL. At this point, you can treat it like any other code, e.g., run it, save it locally, modify it, publish your changes or whatever else your heat desires.

We have tested several other methods of downloading the code using the URL, for example, pasting the URL in a browser. No joy. For more detailed instruction see our post How to Use Shared MakeCode on Microsoft Code Connection for Minecraft.

One thought on “Mazer, the Maze Walking Snake in MakeCode for Minecraft

  1. Pingback: Amazing Maze Maker in MakeCode for Minecraft | We Code MakeCode