How to resolve the algorithm Maze generation step by step in the Action! programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Maze generation step by step in the Action! programming language

Table of Contents

Problem Statement

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

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Maze generation step by step in the Action! programming language

Source code in the action! programming language

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
    n(count^)=RIGHT count^==+1
  FI
  IF y
    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

  

You may also check:How to resolve the algorithm Quine step by step in the Crystal programming language
You may also check:How to resolve the algorithm Abstract type step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Write float arrays to a text file step by step in the Erlang programming language
You may also check:How to resolve the algorithm Window creation/X11 step by step in the Go programming language
You may also check:How to resolve the algorithm Binary search step by step in the Tcl programming language