Car Animated Python Left From Right Code

Task

Maze generation
You are encouraged to solve this task according to the task description, using any language you may know.

a maze

Task

Generate and show a maze, using the simple Depth-first search algorithm.

  1. Start at a random cell.
  2. Mark the current cell as visited, and get a list of its neighbors. For each neighbor, starting with a randomly selected neighbor:
    If that neighbor hasn't been visited, remove the wall between this cell and that neighbor, and then recurse with that neighbor as the current cell.
Related tasks
  • Maze solving.

Contents

  • 1 11l
  • 2 Action!
  • 3 Ada
  • 4 Aime
  • 5 AutoHotkey
    • 5.1 Alternative Version
  • 6 AWK
  • 7 BASIC
  • 8 Batch File
  • 9 BBC BASIC
  • 10 Befunge
  • 11 C
  • 12 C#
  • 13 C++
  • 14 Clojure
  • 15 Commodore BASIC
  • 16 Common Lisp
  • 17 D
  • 18 Delphi
  • 19 EasyLang
  • 20 EGL
  • 21 Elixir
  • 22 Elm
  • 23 Emacs Lisp
  • 24 Erlang
    • 24.1 Using multiple processes
    • 24.2 Using 2 digraphs
  • 25 F#
  • 26 Forth
  • 27 FreeBASIC
  • 28 Fōrmulæ
  • 29 Go
  • 30 Haskell
  • 31 Huginn
  • 32 Icon and Unicon
  • 33 J
  • 34 Java
  • 35 JavaScript
    • 35.1 HTML Table
  • 36 Julia
  • 37 Kotlin
  • 38 Lua
  • 39 M2000 Interpreter
    • 39.1 Random Generation
    • 39.2 Depth-first search
  • 40 Mathematica/Wolfram Language
    • 40.1 Graph
  • 41 MATLAB / Octave
  • 42 Nim
  • 43 Node.js
  • 44 OCaml
  • 45 Ol
  • 46 Perl
  • 47 Phix
  • 48 PHP
  • 49 PicoLisp
  • 50 PL/I
  • 51 Processing
    • 51.1 Processing Python mode
  • 52 Prolog
  • 53 PureBasic
  • 54 Python
  • 55 Racket
  • 56 Raku
  • 57 Rascal
  • 58 Red
  • 59 REXX
    • 59.1 prettified maze version
    • 59.2 simpler version of above
    • 59.3 version 3
  • 60 Ruby
  • 61 Rust
  • 62 Scala
  • 63 Sidef
  • 64 SuperCollider
  • 65 Swift
  • 66 Tcl
  • 67 TXR
    • 67.1 Simple, Depth-First
    • 67.2 Quality Breadth-First
  • 68 XPL0
  • 69 Wren
  • 70 zkl

11l [edit]

Translation of: Python

F make_maze(w = 16, h = 8)
V vis = [[0] * w [+] [1]] * h [+] [[1] * (w + 1)]
V ver = [['| '] * w [+] [String('|')]] * h [+] [[String]()]
V hor = [['+--'] * w [+] [String('+')]] * (h + 1)

  F walk(Int x, Int y) -> N
@vis[y][x] = 1
V d = [(x - 1, y), (x, y + 1), (x + 1, y), (x, y - 1)]
random:shuffle(&d)
L(=xx, =yy) d
I yy == -1
yy = @vis.len - 1
I xx == -1
xx = @vis[0].len - 1
I @vis[yy][xx]
L.continue
I xx == x
@hor[max(y, yy)][x] = '+ '
I yy == y
@ver[y][max(x, xx)] = ' '
@walk(xx, yy)

  walk(random:(w), random:(h))

  V s = ''
L(a, b) zip(hor, ver)
s ''= (a [+] [String("\n")] + b [+] [String("\n")]).join('')
R s

 print(make_maze())

+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |                                |        |     | +  +--+--+--+--+--+--+--+--+--+  +  +--+  +  +--+ |              |           |     |     |  |     | +--+--+--+--+--+--+  +  +--+  +--+  +  +  +  +  + |           |        |  |     |     |  |  |  |  | +  +  +--+--+  +  +--+  +  +--+  +--+  +  +--+  + |  |           |     |  |  |     |     |        | +  +--+--+--+--+--+  +  +  +  +--+  +  +--+--+--+ |  |        |     |  |  |  |  |     |  |        | +  +--+--+  +  +  +  +--+  +  +  +--+--+  +--+  + |     |     |  |     |     |  |  |        |     | +--+  +  +--+  +--+  +  +--+  +  +  +--+--+  +  + |  |  |     |     |  |     |  |           |  |  | +  +  +--+  +--+  +--+--+  +--+--+--+--+--+  +  + |              |                             |  | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+        

Action! [edit]

Action! language does not support recursion. Therefore an iterative approach with a stack has been proposed.

DEFINE TOP="0"
DEFINE RIGHT="1"
DEFINE BOTTOM="2"
DEFINE LEFT="3"
DEFINE WIDTH="160"
DEFINE HEIGHT="96"

 DEFINE STACK_SIZE="5000"
BYTE ARRAY stack(STACK_SIZE)
INT stackSize

 PROC InitStack()
stackSize=0
RETURN

 BYTE FUNC IsEmpty()
IF stackSize=0 THEN
RETURN (1)
FI
RETURN (0)

 BYTE FUNC IsFull()
IF stackSize>=STACK_SIZE THEN
RETURN (1)
FI
RETURN (0)

 PROC Push(BYTE x,y)
IF IsFull() THEN Break() RETURN FI
stack(stackSize)=x stackSize==+1
stack(stackSize)=y stackSize==+1
RETURN

 PROC Pop(BYTE POINTER x,y)
IF IsEmpty() THEN Break() RETURN FI
stackSize==-1 y^=stack(stackSize)
stackSize==-1 x^=stack(stackSize)
RETURN

 PROC FillScreen()
BYTE POINTER ptr ;pointer to the screen memory
INT screenSize=[3840]

  ptr=PeekC(88)
SetBlock(ptr,screenSize,$55)

  Color=0
Plot(0,HEIGHT-1) DrawTo(WIDTH-1,HEIGHT-1) DrawTo(WIDTH-1,0)
RETURN

 PROC GetNeighbors(BYTE x,y BYTE ARRAY n BYTE POINTER count)
DEFINE WALL="1"

  count^=0
IF y>2 AND Locate(x,y-2)=WALL THEN
n(count^)=TOP count^==+1
FI
IF x<WIDTH-3 AND Locate(x+2,y)=WALL THEN
n(count^)=RIGHT count^==+1
FI
IF y<HEIGHT-3 AND Locate(x,y+2)=WALL THEN
n(count^)=BOTTOM count^==+1
FI
IF x>2 AND Locate(x-2,y)=WALL THEN
n(count^)=LEFT count^==+1
FI
RETURN

 PROC Maze(BYTE x,y)
BYTE ARRAY stack,neighbors
BYTE dir,nCount

  FillScreen()

  Color=2
InitStack()
Push(x,y)
WHILE IsEmpty()=0
DO
Pop(@x,@y)
GetNeighbors(x,y,neighbors,@nCount)
IF nCount>0 THEN
Push(x,y)
Plot(x,y)
dir=neighbors(Rand(nCount))
IF dir=TOP THEN
y==-2
ELSEIF dir=RIGHT THEN
x==+2
ELSEIF dir=BOTTOM THEN
y==+2
ELSE
x==-2
FI
DrawTo(x,y)
Push(x,y)
FI
OD
RETURN

 PROC Main()
BYTE CH=$02FC,COLOR0=$02C4,COLOR1=$02C5
BYTE x,y

  Graphics(7+16)
COLOR0=$0A
COLOR1=$04

  x=Rand((WIDTH RSH 1)-1) LSH 1+1
y=Rand((HEIGHT RSH 1)-1) LSH 1+1
Maze(x,y)

  DO UNTIL CH#$FF OD
CH=$FF
RETURN

Screenshot from Atari 8-bit computer

Ada [edit]

mazes.ads:

          generic          
Height : Positive;
Width : Positive;
package Mazes is

  type Maze_Grid is private;

  procedure Initialize (Maze : in out Maze_Grid);

  procedure Put (Item : Maze_Grid);

private

  type Directions is (North, South, West, East);

  type Cell_Walls is array (Directions) of Boolean;
type Cells is record
Walls  : Cell_Walls := ( others => True);
Visited : Boolean  := False;
end record;

  subtype Height_Type is Positive range 1 .. Height;
subtype Width_Type is Positive range 1 .. Width;

  type Maze_Grid is array (Height_Type, Width_Type) of Cells;

end Mazes;

mazes.adb:

          with          Ada.Numerics.Discrete_Random;
with Ada.Text_IO;

package body Mazes is
package RNG is new Ada.Numerics.Discrete_Random (Positive);
package Random_Direction is new Ada.Numerics.Discrete_Random (Directions);

  Generator  : RNG.Generator;
Dir_Generator : Random_Direction.Generator;

  function "-" (Dir : Directions) return Directions is
begin
case Dir is
when North =>
return South;
when South =>
return North;
when East =>
return West;
when West =>
return East;
end case;
end "-";

  procedure Move
(Row  : in out Height_Type;
Column  : in out Width_Type;
Direction  : Directions;
Valid_Move : out Boolean)
is
begin
Valid_Move := False;
case Direction is
when North =>
if Row > Height_Type'First then
Valid_Move := True;
Row  := Row - 1;
end if;
when East =>
if Column < Width_Type'Last then
Valid_Move := True;
Column  := Column + 1;
end if;
when West =>
if Column > Width_Type'First then
Valid_Move := True;
Column  := Column - 1;
end if;
when South =>
if Row < Height_Type'Last then
Valid_Move := True;
Row  := Row + 1;
end if;
end case;
end Move;

  procedure Depth_First_Algorithm
(Maze  : in out Maze_Grid;
Row  : Height_Type;
Column : Width_Type)
is
Next_Row  : Height_Type;
Next_Column  : Width_Type;
Next_Direction  : Directions;
Valid_Direction : Boolean;
Tested_Wall  : array (Directions) of Boolean := ( others => False);
All_Tested  : Boolean;
begin
-- mark as visited
Maze (Row, Column).Visited := True;
loop
-- use random direction
loop
Next_Direction := Random_Direction.Random (Dir_Generator);
exit when not Tested_Wall (Next_Direction);
end loop;
Next_Row  := Row;
Next_Column  := Column;
Move (Next_Row, Next_Column, Next_Direction, Valid_Direction);
if Valid_Direction then
if not Maze (Next_Row, Next_Column).Visited then
-- connect the two cells
Maze (Row, Column).Walls (Next_Direction) :=
False;
Maze (Next_Row, Next_Column).Walls (-Next_Direction) :=
False;
Depth_First_Algorithm (Maze, Next_Row, Next_Column);
end if;
end if;
Tested_Wall (Next_Direction) := True;
-- continue as long as there are unvisited neighbours left
All_Tested := True;
for D in Directions loop
All_Tested := All_Tested and Tested_Wall (D);
end loop;
-- all directions are either visited (from here,
-- or previously visited), or invalid.
exit when All_Tested;
end loop;
end Depth_First_Algorithm;

  procedure Initialize (Maze : in out Maze_Grid) is
Row, Column : Positive;
begin
-- initialize random generators
RNG.Reset (Generator);
Random_Direction.Reset (Dir_Generator);
-- choose starting cell
Row  := RNG.Random (Generator) mod Height + 1;
Column := RNG.Random (Generator) mod Width + 1;
Ada.Text_IO.Put_Line
( "Starting generation at " &
Positive'Image (Row) &
" x" &
Positive'Image (Column) );
Depth_First_Algorithm (Maze, Row, Column);
end Initialize;

  procedure Put (Item : Maze_Grid) is
begin
for Row in Item'Range ( 1 ) loop
if Row = Item'First ( 1 ) then
Ada.Text_IO.Put ('+');
for Col in Item'Range ( 2 ) loop
if Item (Row, Col).Walls (North) then
Ada.Text_IO.Put ( "---+" );
else
Ada.Text_IO.Put ( " +" );
end if;
end loop;
Ada.Text_IO.New_Line;
end if;
for Col in Item'Range ( 2 ) loop
if Col = Item'First ( 2 ) then
if Item (Row, Col).Walls (West) then
Ada.Text_IO.Put ('|');
else
Ada.Text_IO.Put (' ');
end if;
elsif Item (Row, Col).Walls (West)
and then Item (Row, Col - 1 ).Walls (East)
then
Ada.Text_IO.Put ('|');
elsif Item (Row, Col).Walls (West)
or else Item (Row, Col - 1 ).Walls (East)
then
Ada.Text_IO.Put ('>');
else
Ada.Text_IO.Put (' ');
end if;
if Item (Row, Col).Visited then
Ada.Text_IO.Put ( " " );
else
Ada.Text_IO.Put ( "???" );
end if;
end loop;
if Item (Row, Item'Last ( 2 ) ).Walls (East) then
Ada.Text_IO.Put_Line ( "|" );
else
Ada.Text_IO.Put_Line ( " " );
end if;
Ada.Text_IO.Put ('+');
for Col in Item'Range ( 2 ) loop
if Item (Row, Col).Walls (South) then
Ada.Text_IO.Put ( "---+" );
else
Ada.Text_IO.Put ( " +" );
end if;
end loop;
Ada.Text_IO.New_Line;
end loop;
end Put;
end Mazes;

Example main.adb:

          with          Mazes;
procedure Main is
package Small_Mazes is new Mazes (Height => 8, Width => 11 );
My_Maze : Small_Mazes.Maze_Grid;
begin
Small_Mazes.Initialize (My_Maze);
Small_Mazes.Put (My_Maze);
end Main;
Starting generation at  3 x 7 +---+---+---+---+---+---+---+---+---+---+---+ |   |               |   |                   | +   +   +   +---+   +   +   +---+---+---+   + |       |       |       |   |       |       | +   +---+---+   +---+---+   +   +   +   +---+ |           |           |   |   |   |   |   | +   +---+---+---+---+   +---+   +   +   +   + |   |           |       |       |       |   | +   +   +---+   +   +   +   +---+---+---+   + |   |   |           |   |       |           | +   +   +---+---+---+---+---+   +---+   +   + |   |   |                   |           |   | +---+   +   +---+---+---+   +---+---+---+   + |       |   |           |                   | +   +---+   +---+---+   +---+---+---+---+---+ |                                           | +---+---+---+---+---+---+---+---+---+---+---+

Aime [edit]

grid_maze(data b, integer N)
{
data d;

  N.times(bb_cast, d, "+---");
bb_cast(d, "+\n");

  N.times(bb_cast, d, "| * ");
bb_cast(d, "|\n");

  N.times(bb_copy, b, d);

  b_size(d, N * 4 + 2);

  bb_copy(b, d);
}

 void
walk_cell(data b, integer N, line_size, x, y, list x_offsets, y_offsets)
{
integer i, p, q, r;

  b_replace(b, y + x, ' ');

  r = drand(3);

  i = 0;
while (i < 4) {
p = x + x_offsets[q = (r + i) & 3];
q = y + y_offsets[q];

  if (-1 < p && p < line_size
&& -1 < q && q < line_size * (N * 2 + 1)) {
if (b[q + p] == '*') {
walk_cell(b, N, line_size, p, q, x_offsets, y_offsets);
b[(q + y) / 2 + (p + x) / 2] = ' ';
if (p == x) {
b[(q + y) / 2 + p - 1] = ' ';
b[(q + y) / 2 + p + 1] = ' ';
}
}
}

  i += 1;
}
}

 walk_maze(data b, integer N)
{
integer line_size, x, y;
list x_offsets, y_offsets;

  line_size = N * 4 + 1 + 1;

  l_bill(x_offsets, 0, 4, 0, -4, 0);
l_bill(y_offsets, 0, 0, line_size * 2, 0, line_size * -2);

  x = drand(N - 1) * 4 + 2;
y = line_size * (drand(N - 1) * 2 + 1);

  walk_cell(b, N, line_size, x, y, x_offsets, y_offsets);
}

 main(void)
{
data b;

  grid_maze(b, 10);
walk_maze(b, 10);

  o_(b);

  0;
}

+---+---+---+---+---+---+---+---+---+---+ |                       |               | +   +---+---+---+---+   +   +---+---+   + |   |       |       |   |   |           | +   +   +   +   +   +   +   +   +---+   + |   |   |       |       |   |   |   |   | +   +---+---+   +---+---+---+   +   +   + |           |               |   |   |   | +---+---+---+---+---+---+   +   +   +   + |                       |   |   |       | +   +---+---+---+---+   +   +   +---+---+ |   |               |   |   |           | +   +---+---+---+   +   +   +---+   +   + |       |       |       |       |   |   | +   +   +   +   +   +---+---+   +---+   + |   |       |   |           |       |   | +---+---+---+   +---+---+---+---+   +   + |               |       |       |       | +   +---+---+---+   +   +   +   +---+   + |                   |       |           | +---+---+---+---+---+---+---+---+---+---+        

AutoHotkey [edit]

For a challenge, this maze generation is entirely string based. That is to say, all operations including the wall removal and retrieval of cell states are done on the output string.

          ; Initially build the board          
Width := 11
Height := 8
Loop % height* 2 + 1
{
Outer := A_Index
Loop % Width
maze .= Outer & 1 ? "+-" : "|0"
maze .= (Outer & 1 ? "+" : "|" ) "`n"
}
StringTrimRight , maze, maze, 1 ; removes trailing newline
Clipboard := Walk(maze)

 Walk(S, x= 0 , y= 0 ) {
If !x{ ; --Start at a random cell...
StringReplace , junk, S, `n,,UseErrorLevel ; Calculate rows
Random , y, 1 , ErrorLevel // 2
Random , x, 1 , InStr (S, "`n" ) // 2 - 1 ; Calculate height
}

  ; --Obtain a list of its neighbors...
neighbors := x "," y+ 1 "`n" x "," y- 1 "`n" x+ 1 "," y "`n" x- 1 "," y
; --Randomize the list...
Sort neighbors, random

  ; --Then for each neighbor...
Loop Parse, neighbors, `n
{
pC := InStr ( A_LoopField , "," ) , x2 := SubStr ( A_LoopField , 1 , pC- 1 ) , y2 := SubStr ( A_LoopField , pC+ 1 )
; If it has not been visited...
If GetChar(S, 2 *x2, 2 *y2) = "0" {
; Mark it as visited...
S := ChangeChar(s, 2 *x2, 2 *y2, " " )
; Remove the wall between this cell and the neighbor...
S := ChangeChar(S, x+x2, y+y2, " " )
; Then recurse with the neighbor as the current cell
S := Walk(S, x2, y2)
}
}
return S
}

; Change a character in a string using x and y coordinates
ChangeChar(s, x, y, c) {
Loop Parse, s, `n
{
If ( A_Index = Y)
Loop Parse, A_LoopField
If ( A_Index = x)
out .= c
Else out .= A_LoopField
Else out .= A_LoopField
out .= "`n"
}
StringTrimRight , out, out, 1
return out
}

; retrieve a character in a string using x and y coordinates
GetChar(s, x, y, n= 1 ) {
x*=n, y*=n
Loop Parse, s, `n
If ( A_Index = Y)
return SubStr ( A_LoopField , x, 1 )
}

+-+-+-+-+-+-+-+-+-+-+-+ |         |     |     | +-+ +-+-+ +-+ + + +-+-+ |   |         | |     | + +-+ +-+ +-+-+ +-+ + + | |     | |   |   | | | + + +-+-+ + + +-+ +-+ + | |   |   | |     |   | + +-+ + +-+-+-+ +-+ + + | |   |       |     | | + +-+-+-+-+-+ +-+-+-+ + | |   |       |   |   | + + + + +-+-+-+ + + +-+ |   |   |   |   | |   | +-+-+-+-+ +-+ + +-+-+ + |             |       | +-+-+-+-+-+-+-+-+-+-+-+

Alternative Version [edit]

http://rosettacode.org/wiki/Maze_solving#AutoHotkey

Generator and solver combined.

AWK [edit]

          #!/usr/bin/awk -f          

# Remember: AWK is 1-based, for better or worse.

BEGIN {
# The maze dimensions.
width = 20; # Global
height = 20; # Global
resetMaze( );

  # Some constants.
top = 1;
bottom = 2;
left = 3;
right = 4;

  # Randomize the PRNG.
randomize( );

  # Visit all the cells starting at a random point.
visitCell(getRandX( ), getRandY( ) );

  # Show the result.
printMaze( );
}

# Wander through the maze removing walls as we go.
function visitCell(x, y, dirList, dir, nx, ny, ndir, pi) {
setVisited(x, y); # This cell has been visited.

  # Visit neighbors in a random order.
dirList = getRandDirList( );
for (dir = 1; dir <= 4; dir++ ) {
# Get coordinates of a random neighbor (next in random direction list).
ndir = substr (dirList, dir, 1 );
nx = getNextX(x, ndir);
ny = getNextY(y, ndir);

  # Visit an unvisited neighbor, removing the separating walls.
if (wasVisited(nx, ny) == 0 ) {
rmWall(x, y, ndir);
rmWall(nx, ny, getOppositeDir(ndir) );
visitCell(nx, ny)
}
}
}

# Display the text-mode maze.
function printMaze( x, y, r, w) {
for (y = 1; y <= height; y++ ) {
for (pass = 1; pass <= 2; pass++ ) { # Go over each row twice: top, middle
for (x = 1; x <= width; x++ ) {
if (pass == 1 ) { # top
printf ( "+" );
printf (hasWall(x, y, top) == 1 ? "---" : " " );
if (x == width) printf ( "+" );
}
else if (pass == 2 ) { # left, right
printf (hasWall(x, y, left) == 1 ? "|" : " " );
printf ( " " );
if (x == width) printf (hasWall(x, y, right) == 1 ? "|" : " " );
}
}
print;
}
}
for (x = 1; x <= width; x++ ) printf ( "+---" ); # bottom row
print ( "+" ); # bottom right corner
}

# Given a direction, get its opposite.
function getOppositeDir(d) {
if (d == top) return bottom;
if (d == bottom) return top;
if (d == left) return right;
if (d == right) return left;
}

# Build a list (string) of the four directions in random order.
function getRandDirList( dirList, randDir, nx, ny, idx) {
dirList = "";
while ( length (dirList) < 4 ) {
randDir = getRandDir( );
if ( ! index (dirList, randDir) ) {
dirList = dirList randDir;
}
}
return dirList;
}

# Get x coordinate of the neighbor in a given a direction.
function getNextX(x, dir) {
if (dir == left) x = x - 1;
if (dir == right) x = x + 1;
if ( !isGoodXY(x, 1 ) ) return - 1; # Off the edge.
return x;
}

# Get y coordinate of the neighbor in a given a direction.
function getNextY(y, dir) {
if (dir == top) y = y - 1;
if (dir == bottom) y = y + 1;
if ( !isGoodXY( 1, y) ) return - 1; # Off the edge.
return y;
}

# Mark a cell as visited.
function setVisited(x, y, cell) {
cell = getCell(x, y);
if (cell == - 1 ) return;
cell = substr (cell, 1, 4 ) "1"; # walls plus visited
setCell(x, y, cell);
}

# Get the visited state of a cell.
function wasVisited(x, y, cell) {
cell = getCell(x, y);
if (cell == - 1 ) return 1; # Off edges already visited.
return substr (getCell(x,y), 5, 1 );
}

# Remove a cell's wall in a given direction.
function rmWall(x, y, d, i, oldCell, newCell) {
oldCell = getCell(x, y);
if (oldCell == - 1 ) return;
newCell = "";
for (i = 1; i <= 4; i++ ) { # Ugly as concat of two substrings and a constant?.
newCell = newCell (i == d ? "0" : substr (oldCell, i, 1 ) );
}
newCell = newCell wasVisited(x, y);
setCell(x, y, newCell);
}

# Determine if a cell has a wall in a given direction.
function hasWall(x, y, d, cell) {
cell = getCell(x, y);
if (cell == - 1 ) return 1; # Cells off edge always have all walls.
return substr (getCell(x, y), d, 1 );
}

# Plunk a cell into the maze.
function setCell(x, y, cell, idx) {
if ( !isGoodXY(x, y) ) return;
maze[x, y] = cell
}

# Get a cell from the maze.
function getCell(x, y, idx) {
if ( !isGoodXY(x, y) ) return - 1; # Bad cell marker.
return maze[x, y];
}

# Are the given coordinates in the maze?
function isGoodXY(x, y) {
if (x < 1 || x > width) return 0;
if (y < 1 || y > height) return 0;
return 1;
}

# Build the empty maze.
function resetMaze( x, y) {
delete maze;
for (y = 1; y <= height; y++ ) {
for (x = 1; x <= width; x++ ) {
maze[x, y] = "11110"; # walls (up, down, left, right) and visited state.
}
}
}

# Random things properly scaled.

function getRandX( ) {
return 1 + int ( rand ( ) * width);
}

function getRandY( ) {
return 1 + int ( rand ( ) * height);
}

function getRandDir( ) {
return 1 + int ( rand ( ) * 4 );
}

function randomize( ) {
"echo $RANDOM" | getline t;
srand (t);
}

Example output:

+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ |                       |                   |                       |           | +---+   +---+   +---+---+   +---+   +---+---+   +---+   +---+---+   +   +---+   + |       |   |   |           |   |           |       |   |   |       |       |   | +   +---+   +   +   +---+---+   +---+---+   +   +---+   +   +   +---+---+---+   + |       |       |   |                   |       |       |       |               | +   +   +   +---+   +---+   +   +---+   +---+---+   +---+---+   +---+   +---+   + |   |   |   |   |       |   |   |       |       |   |       |           |       | +---+   +   +   +---+   +---+   +   +---+---+   +   +   +   +---+---+---+   +---+ |       |       |       |       |               |       |   |       |       |   | +   +   +---+---+   +---+   +---+---+---+---+   +---+---+   +---+   +   +---+   + |   |   |       |   |           |           |   |       |       |   |   |       | +   +---+   +   +   +---+---+   +---+   +   +   +   +   +---+   +   +   +   +   + |   |       |       |       |       |   |   |   |   |       |   |   |       |   | +   +   +---+---+---+   +   +---+   +   +   +   +---+---+   +   +   +---+---+   + |   |   |               |           |   |   |               |   |               | +   +   +---+---+---+   +---+---+---+   +   +---+---+---+   +   +---+---+   +---+ |       |               |   |           |           |       |   |       |       | +   +---+   +---+---+---+   +   +---+---+---+---+   +   +---+   +   +   +---+   + |   |       |           |   |   |       |       |   |   |   |       |   |       | +   +   +   +   +---+   +   +   +   +   +   +   +   +   +   +---+---+   +   +---+ |       |   |       |   |           |   |   |   |   |   |           |   |       | +   +---+---+---+   +   +---+---+---+   +   +   +   +   +   +---+   +   +---+   + |   |               |           |   |       |   |           |   |   |       |   | +---+   +---+---+---+---+---+   +   +---+   +---+---+---+---+   +   +---+   +   + |   |   |       |           |   |       |   |           |       |       |   |   | +   +   +   +---+   +---+   +   +---+   +   +   +---+   +---+   +---+   +   +   + |   |   |           |       |       |       |   |           |           |   |   | +   +   +   +---+---+   +---+---+   +   +---+   +---+---+   +---+---+   +   +---+ |   |   |   |   |       |           |       |       |   |           |   |       | +   +   +   +   +   +---+   +---+---+---+---+---+   +   +---+---+   +   +---+   + |       |   |   |           |                       |               |       |   | +---+---+   +   +---+---+---+---+   +   +---+---+---+   +---+---+---+---+   +   + |       |       |               |   |       |       |           |           |   | +   +   +---+   +---+---+   +   +   +---+   +   +   +---+---+   +---+---+---+   + |   |       |       |       |   |       |   |   |   |       |           |       | +   +   +---+---+   +   +---+   +   +---+   +---+   +   +   +---+---+   +   +---+ |   |           |   |   |       |   |       |       |   |           |   |       | +   +---+   +---+   +   +   +---+---+   +---+   +---+   +---+---+   +   +---+   + |       |               |               |                       |               | +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+        

BASIC [edit]

This implementation was written using QB64. It should also be compatible with Qbasic, as it uses no QB64-exclusive features.

OPTION BASE 0
RANDOMIZE TIMER

 REM must be even
width% = 40
height% = 20

 REM make array and fill
DIM maze$(width%, height%)
FOR x% = 0 TO width%
FOR y% = 0 TO height%
maze$(x%, y%) = "#"
NEXT y%
NEXT x%

 REM initial start location
currentx% = INT(RND * (width% - 1))
currenty% = INT(RND * (height% - 1))
REM value must be odd
IF currentx% MOD 2 = 0 THEN currentx% = currentx% + 1
IF currenty% MOD 2 = 0 THEN currenty% = currenty% + 1
maze$(currentx%, currenty%) = " "

 REM generate maze
done% = 0
DO WHILE done% = 0
FOR i% = 0 TO 99
oldx% = currentx%
oldy% = currenty%

  REM move in random direction
SELECT CASE INT(RND * 4)
CASE 0
IF currentx% + 2 < width% THEN currentx% = currentx% + 2
CASE 1
IF currenty% + 2 < height% THEN currenty% = currenty% + 2
CASE 2
IF currentx% - 2 > 0 THEN currentx% = currentx% - 2
CASE 3
IF currenty% - 2 > 0 THEN currenty% = currenty% - 2
END SELECT

  REM if cell is unvisited then connect it
IF maze$(currentx%, currenty%) = "#" THEN
maze$(currentx%, currenty%) = " "
maze$(INT((currentx% + oldx%) / 2), ((currenty% + oldy%) / 2)) = " "
END IF
NEXT i%

  REM check if all cells are visited
done% = 1
FOR x% = 1 TO width% - 1 STEP 2
FOR y% = 1 TO height% - 1 STEP 2
IF maze$(x%, y%) = "#" THEN done% = 0
NEXT y%
NEXT x%
LOOP

 REM draw maze
FOR y% = 0 TO height%
FOR x% = 0 TO width%
PRINT maze$(x%, y%);
NEXT x%
PRINT
NEXT y%

 REM wait
DO: LOOP WHILE INKEY$ = ""

This used a slightly modified version that outputs to a text file. (You can't copy from a QB64 window.)

######################################### # #   #     #     #   #   #       # #   # # ### # # # # ##### # # ### # # # # # ### # #   # # #   #   # #     # # # #     # # # # # ####### # ####### ####### ##### # # #   #             # #     #     #   #   # # ##### ### ### # # ### # # ##### ### ### #     # # # #   #   # # #     # # #     # ### ##### # ##### ### ### # ### # # ##### #       # #   #     #     # # #   # #   # # # # ### # # ##### ### # # # # ##### ### # # # #     # #         # #     # #     # # ### # # ######### ### ####### # ##### # # #   # # #   # #     #   # # # #   # # # # ### ####### # ### # ##### # # ### # # # #   #         # #   # # #     #       # # ##### # ### ### ### ### # # # # # # # # # #     # #   #     #   #   # #   # # #   # # # # # ### # ### ### ### # ### ### ### # # # # #   #   #   #   #   #   #   #   # # #########################################

Batch File [edit]

:amaze          Rows Cols [wall char]
:: A stack-less, iterative, depth-first maze generator in native WinNT batch.
:: Rows and Cols must each be >1 and Rows*Cols cannot exceed 2096.
:: Default wall character is #, [wall char] is used if provided.

@ ECHO OFF
SETLOCAL EnableDelayedExpansion

:: check for valid input, else GOTO :help

IF /I "% ~2" EQU "" GOTO :amaze_help
FOR /F "tokens=* delims=0123456789" %% A IN ("% ~1 %~2") DO IF "% %~A" NEQ "" GOTO :amaze_help
SET /A "rows=% ~1, cols=% ~2, mTmp=rows*cols"
IF ! rows ! LSS 2 GOTO :amaze_help
IF ! cols ! LSS 2 GOTO :amaze_help
IF ! mTmp ! GTR 2096 GOTO :amaze_help

:: set map characters and use 1st character of %3 for wall, if defined

SET "wall=#"
SET "hall= "
SET "crumb=."
IF "% ~3" NEQ "" SET "wall=% ~3"
SET "wall=! wall:~0,1 !"

:: assign width, height, cursor position, loop count, and offsets for NSEW

SET /A "cnt=0, wide=cols*2-1, high=rows*2-1, size=wide*high, N=wide*-2, S=wide*2, E=2, W=-2"

:: different random entrance points

:: ...on top
:: SET /A "start=(!RANDOM! %% cols)*2"
:: ...on bottom
:: SET /A "start=size-(!RANDOM! %% cols)*2-1"
:: ...on top or bottom
:: SET /A ch=cols*2, ch=!RANDOM! %% ch
:: IF !ch! GEQ !cols! ( SET /A "start=size-(ch-cols)*2-1"
:: ) ELSE SET /A start=ch*2
:: random entrance inside maze
SET /A "start=(! RANDOM !  %% cols*2)+( ! RANDOM ! % % rows*2)*wide"
SET /A "curPos=start, cTmp=curPos+1, loops=cols*rows*2+1"

:: fill the maze with 8186 wall characters, clip to size, and open 1st cell

SET "mz=! wall !"
FOR /L %% A IN (1,1,6) DO SET mz=! mz !! mz !! mz !! mz !
SET bdr=! mz:~-%wide% !
SET mz=! mz:~3 !! mz:~3 !
SET mz=! mz:~-%size% !
SET mz=! mz:~0,%curPos% !! hall !! mz:~%cTmp% !

:: iterate #cells*2+1 steps of random depth-first search

FOR /L % %@ IN (1,1,% loops %) DO (
SET "rand=" & SET "crmPos="
REM set values for NSEW cell and wall positions
SET /A "rCnt=rTmp=0, cTmp=curPos+1, np=curPos+N, sp=curPos+S, ep=curPos+E, wp=curPos+W, wChk=curPos/wide*wide, eChk=wChk+wide, nw=curPos-wide, sw=curPos+wide, ew=curPos+1, ww=curPos-1"
REM examine adjacent cells, build direction list, and find last crumb position
FOR /F "tokens=1-8" %% A IN ("! np ! ! sp ! ! ep ! ! wp ! ! nw ! ! sw ! ! ew ! ! ww !") DO (
IF ! np ! GEQ 0 IF "! mz:~%%A,1 !" EQU "! wall !" ( SET /A rCnt+=1 & SET "rand=n ! rand !"
) ELSE IF "! mz:~%%E,1 !" EQU "! crumb !" SET /A crmPos=np, cw=nw
IF ! sp ! LEQ ! size ! IF "! mz:~%%B,1 !" EQU "! wall !" ( SET /A rCnt+=1 & SET "rand=s ! rand !"
) ELSE IF "! mz:~%%F,1 !" EQU "! crumb !" SET /A crmPos=sp, cw=sw
IF ! ep ! LEQ ! eChk ! IF "! mz:~%%C,1 !" EQU "! wall !" ( SET /A rCnt+=1 & SET "rand=e ! rand !"
) ELSE IF "! mz:~%%G,1 !" EQU "! crumb !" SET /A crmPos=ep, cw=ew
IF ! wp ! GEQ ! wChk ! IF "! mz:~%%D,1 !" EQU "! wall !" ( SET /A rCnt+=1 & SET "rand=w ! rand !"
) ELSE IF "! mz:~%%H,1 !" EQU "! crumb !" SET /A crmPos=wp, cw=ww
)
IF DEFINED rand ( REM adjacent unvisited cell is available
SET /A rCnt=! RANDOM ! % % rCnt
FOR %% A IN (! rand !) DO ( REM pick random cell + wall
IF ! rTmp ! EQU ! rCnt ! SET /A "curPos=! %%Ap ! , cTmp=curPos+1, mw= !%%Aw !, mTmp=mw+1"
SET /A rTmp+=1
)
REM write the 2 new characters into the maze
FOR /F "tokens=1-4" %% A IN ("! mw ! ! mTmp ! ! curPos ! ! cTmp !") DO (
SET "mz=! mz:~0,%%A !! crumb !! mz:~%%B !"
SET "mz=! mz:~0,%%C !! hall !! mz:~%%D !"
)
) ELSE IF DEFINED crmPos ( REM follow the crumbs backward
SET /A mTmp=cw+1
REM erase the crumb character and set new cursor position
FOR /F "tokens=1-2" %% A IN ("! cw ! ! mTmp !") DO SET "mz=! mz:~0,%%A !! hall !! mz:~%%B !"
SET "curPos=! crmPos !"
)
)
SET /A open=cols/2*2, mTmp=open+1
ECHO ! wall !! bdr:~0,%open% !! hall !! bdr:~%mTmp% !! wall !
FOR /L %% A IN (0,! wide ! , ! size !) DO IF %% A LSS ! size ! ECHO ! wall !! mz:~%%A,% wide% !! wall !
ECHO ! wall !! bdr:~0,%open% !! hall !! bdr:~%mTmp% !! wall !
ENDLOCAL
EXIT /B 0

 :amaze_help
ECHO Usage: % ~0 Rows Cols [wall char]
ECHO Rows^>1, Cols^>1, and Rows*Cols^<=2096
ECHO Example: % ~0 11 39 @
ENDLOCAL
EXIT /B 0

Example output:

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @                   @     @   @         @     @     @       @   @         @   @ @ @@@@@@@@@@@ @@@@@@@ @ @ @ @ @ @@@@@ @ @ @ @ @@@ @ @@@ @@@ @ @@@ @ @@@@@ @@@ @ @ @       @   @       @ @ @ @   @     @ @ @ @ @   @     @     @   @ @         @ @ @ @@@@@@@ @@@ @@@@@@@ @ @ @@@@@ @@@@@ @@@ @ @ @@@@@@@@@@@@@@@ @@@ @@@@@@@@@@@ @ @ @     @ @   @   @   @ @ @   @   @       @   @   @         @ @ @     @     @ @ @ @ @@@ @ @ @@@@@ @ @ @@@ @ @ @@@ @@@@@@@ @@@@@ @ @ @@@@@@@ @ @ @@@@@ @ @@@ @ @   @   @ @ @     @ @ @ @   @ @   @   @   @ @     @ @ @ @     @   @   @     @ @ @@@ @@@ @ @ @@@@@ @ @ @ @ @@@ @ @ @@@ @ @ @@@ @@@@@ @ @ @ @@@ @@@ @ @@@@@@@@@ @ @     @ @ @     @   @ @ @   @ @ @   @   @       @   @   @ @   @   @       @   @ @ @@@@@ @ @@@ @ @@@ @ @@@@@ @ @ @@@@@@@@@@@@@@@@@ @@@@@ @ @@@@@ @@@ @@@@@ @ @@@ @   @   @ @   @   @ @ @     @ @ @   @       @   @   @   @ @     @ @ @   @ @   @ @@@@@ @@@ @ @ @@@@@ @ @ @@@@@ @ @ @ @ @@@@@ @ @ @@@ @ @@@ @ @@@@@ @ @ @ @ @@@@@ @     @     @ @     @ @     @ @   @     @   @ @     @ @ @   @       @ @ @   @ @ @ @@@@@@@@@@@ @ @@@@@ @@@@@ @ @@@@@@@ @@@ @ @ @@@@@ @ @ @@@@@@@@@ @@@ @@@ @ @ @ @ @         @ @     @     @ @   @   @ @   @ @     @ @ @         @ @   @   @ @ @ @ @ @@@@@@@ @@@@@@@ @@@ @@@ @@@ @ @ @@@ @@@ @@@@@ @@@ @@@@@@@ @ @ @ @ @ @@@ @ @ @ @     @ @       @ @   @   @   @ @ @   @   @ @   @   @     @ @ @   @ @ @ @ @ @ @ @@@@@ @ @@@ @@@@@ @ @ @ @@@@@@@ @ @ @@@ @@@ @ @@@ @@@ @@@ @ @ @@@@@@@ @ @ @ @ @       @   @       @ @ @   @   @ @   @ @ @   @ @   @   @   @ @ @       @ @   @ @ @@@@@@@ @ @@@@@@@@@ @@@@@ @ @ @ @@@@@ @ @ @ @ @ @@@ @@@ @@@ @ @ @@@@@@@ @@@ @ @         @         @         @   @         @ @       @       @   @           @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@        

BBC BASIC [edit]

          MazeWidth% = 11
MazeHeight% = 9
MazeCell% = 50

  VDU 23,22,MazeWidth%*MazeCell%/2+3;MazeHeight%*MazeCell%/2+3;8,16,16,128
VDU 23,23,3;0;0;0; : REM Line thickness
PROCgeneratemaze(Maze&(), MazeWidth%, MazeHeight%, MazeCell%)
END

  DEF PROCgeneratemaze(RETURN m&(), w%, h%, s%)
LOCAL x%, y%
DIM m&(w%, h%)
FOR y% = 0 TO h%
LINE 0,y%*s%,w%*s%,y%*s%
NEXT
FOR x% = 0 TO w%
LINE x%*s%,0,x%*s%,h%*s%
NEXT
GCOL 15
PROCcell(m&(), RND(w%)-1, y% = RND(h%)-1, w%, h%, s%)
ENDPROC

  DEF PROCcell(m&(), x%, y%, w%, h%, s%)
LOCAL i%, p%, q%, r%
m&(x%,y%) OR= &40 : REM Mark visited
r% = RND(4)
FOR i% = r% TO r%+3
CASE i% MOD 4 OF
WHEN 0: p% = x%-1 : q% = y%
WHEN 1: p% = x%+1 : q% = y%
WHEN 2: p% = x% : q% = y%-1
WHEN 3: p% = x% : q% = y%+1
ENDCASE
IF p% >= 0 IF p% < w% IF q% >= 0 IF q% < h% IF m&(p%,q%) < &40 THEN
IF p% > x% m&(p%,q%) OR= 1 : LINE p%*s%,y%*s%+4,p%*s%,(y%+1)*s%-4
IF q% > y% m&(p%,q%) OR= 2 : LINE x%*s%+4,q%*s%,(x%+1)*s%-4,q%*s%
IF x% > p% m&(x%,y%) OR= 1 : LINE x%*s%,y%*s%+4,x%*s%,(y%+1)*s%-4
IF y% > q% m&(x%,y%) OR= 2 : LINE x%*s%+4,y%*s%,(x%+1)*s%-4,y%*s%
PROCcell(m&(), p%, q%, w%, h%, s%)
ENDIF
NEXT
ENDPROC

Sample output:
Maze bbc.gif

Befunge [edit]

Dimensions are specified by the first two values pushed onto the stack - currently 20 (45*) by 16 (28*). Note, however, that the upper limit in a standard Befunge-93 implementation will be around 38 by 40 (1520 cells) due to the constrained page size.

Also note that this requires an interpreter with working read-write memory support, which is suprisingly rare in online implementations. Padding the code page with extra blank lines or spaces can sometimes help. Using smaller dimensions might also be preferable, especially on slower implementations.

45*28*10p00p020p030p006p0>20g30g00g*+::"P"%\"P"/6+gv>$\[email protected]::\+g02+*g00+g03-\<
0_ 1!%4+1\-\0!::\-\2%2:p<pv0<< v0p+6/"P"\%"P":\+4%4<^<v-<$>+2%\1-*20g+\1+4%::v^
#| +2%\1-*30g+\1\40g1-:v0+v2?1#<v>+:00g%!55+*>:#0>#,_^>:!|>\#%"P"v#:*+*g00g0<>1
02!:++`\0\`-1g01:\+`\< !46v3<^$$<^1,g2+1%2/2,g1+1<v%g00:\<*g01,<>:30p\:20p:v^3g
0#$g#<1#<-#<`#<\#<0#<^#_^/>#1+#4<>"P"%\"P"/6+g:2%^!>,1-:#v_$55+^|$$ "JH" $$>#<0
::"P"%\"P"/6+g40p\40g+\:#^"P"%#\<^ ::$_,#!0#:<*"|"<^," _"<:g000 <> /6+g4/2%+#^_
          _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ |_  |  _ _  |_     _ _|  _ _   _ _  |_  | | |_ _|  _|_  |_|_  |  _| |  _|_  |_  | | |   |_   _  | |   |_ _   _| |  _ _ _|_  | | |_  |_|  _|_ _|_  |_ _|  _|_  |    _| | | |_ _ _  |   |  _ _|   |  _  | | | |  _| |  _ _  |_| | | |  _ _| |_|  _|  _|_ _| | | | |  _|  _|_ _|_  |_ _  | | | |  _ _  | |_  |_ _ _|  _ _  |_ _  |_ _| | |_  | | | | |_ _ _ _ _|   |_ _ _ _| |   |_  | | | | |_    |  _ _ _|_ _ _ _ _  | |_  |_ _| | | |  _|_ _|_ _   _    |  _ _|_  |_   _ _| | | |  _ _ _  | |  _|_|_ _ _  |_  |_ _  | | |  _|  _  | | |_ _ _ _ _   _ _| | |   | | |_  |_  |  _|_|   |  _  |_|  _ _ _| | | | | | | | |_|  _ _| | |  _|  _|   |  _|_| | |_ _ _|_ _ _ _ _|_ _|_ _ _ _ _|_|_ _ _ _|

C [edit]

Generation/solver in one. Requires UTF8 locale and unicode capable console. If your console font line-drawing chars are single width, define DOUBLE_SPACE to 0.

          #include <stdio.h>          
#include <stdlib.h>
#include <string.h>
#include <locale.h>

#define DOUBLE_SPACE 1

#if DOUBLE_SPACE
# define SPC " "
#else
# define SPC " "

0 Response to "Car Animated Python Left From Right Code"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel