I wrote a Turtle Maze Game 1.56 (XHL585-5) as a Small Basic program.  In this article, I'd like to introduce how to create this kind of program.

I wrote this program with three phases.  Firstly I wrote a program just creates and shows a maze.  Secondly I wrote an AI (Artificial Intelligence) which solve the maze.  At last, I wrote an UI (user interface) to battle with the AI.

Following description shows the detail of these three phases.

1. Creating a maze

1.1 How to create a maze?

There are many ideas to create mazes in Wikipedia.  At first, I decided to use Randomized Prim's algorithm.  Then I decided the data structure of a maze as using an 2d array cell[row][col].  This array contains 1 for wall or "" for passage.

2. Creating AI which solves the maze

2.1 How to solve the maze?

There are also many ideas to solve mazes in Wikipedia.  Actually, I did not check web site because I did know Wall Follower algorithm.  This algorithm is very easy so the AI is little slow...

My first program was Maze 0.3 (PNC833).  This program was published as an example of Graphical Challenge 3 in Challenge of the Month - August 2012.  But this program runs properly only in local environment.

2.2 What are needed for Silverlight environment?

Maze 0.3 doesn't work properly in web browser with Silverlight.  So, I rewrote it to work in browser and the output is Maze 1.2 (PNC833-12).  This program was introduced as "Turtle Maze" in Small Basic Blog.  You can find the difference as commented Silverlight in this program.  I am also updating these difference as an article titled "Small Basic: Difference Between Local and Remote" on TechNet Wiki.

3. Creating UI for another turtle

3.1 Create another turtle

I made a plan to make another turtle for human player.  Following steps was the plan to create it.

(1) Make a program to show turtle.

' Show Turtle to Capture
tx = Turtle.X
ty = Turtle.Y
s = 18            ' set frame size
x1 = tx - s / 2
x2 = tx + s / 2
y1 = ty - s / 2
y2 = ty + s / 2
GraphicsWindow.BrushColor = "Gray"
GraphicsWindow.PenWidth = 0
GraphicsWindow.FillRectangle(x1, y1, 1, s)      ' draw frame
GraphicsWindow.FillRectangle(x1, y1, s, 1)
GraphicsWindow.FillRectangle(x2, y1, 1, s)
GraphicsWindow.FillRectangle(x1, y2, s, 1)
GraphicsWindow.FillRectangle(tx, y2, 1, s / 2)  ' draw center
GraphicsWindow.FillRectangle(x2, ty, s / 2, 1)
GraphicsWindow.FillRectangle(tx, y1 - s / 2, 1, s / 2)
GraphicsWindow.FillRectangle(x1 - s / 2, ty, s / 2, 1)

(2) Capture original turtle using [Alt]+[Print Screen] key.  Or you can use Snipping Tool also.

(3) Create a bitmap of original turtle using Paint program.

(4) Make a program to change turtle color.  --> I made a tool (SDH367) for this.

(5) Capture new turtle using [Alt]+[Print Screen] key.  Or you can use Snipping Tool also.

(6) Create a bitmap of another color turtle using Paint and GIMP2.

(7) Use the bitmap as image shape.  --> I made a sample program (CQV406-0).

3.2 How to control turtle

I decided to use arrow keys to control turtle.  Arrow keys are recognized by using GraphicsWindow.KeyDown = OnKeyDown.  (OnKeyDown is a key event handling subroutine.)

3.3 How to save score

Score will be saved in setting file.  Setting file name can be get with File.GetSettingsFilePath().  But file operation cannot be run in remote, so this is limitation for remote environment.

3.4 Turtle disappears after clear

Small Basic Turtle disappears after calling GraphicsWindow.Clear() and never appears again.  But we need GraphicsWindow.Clear() to remove lines drawn by Turtle.  So, I think only the way to restart Turtle for the game is not to use pen of the Turtle.

Other Languages