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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Maze generation step by step in the FutureBasic 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 FutureBasic programming language

Source code in the futurebasic programming language

_rows = 9
_cols = 11
_size = 32
_mgn = 32

_t = ( 1 << 0 )
_l = ( 1 << 1 )
_b = ( 1 << 2 )
_r = ( 1 << 3 )
_a = _t + _l + _b + _r

_window = 1

void local fn BuildWindow
  window _window, @"FutureBasic - Maze generation", (0,0,_cols*_size+_mgn*2,_rows*_size+_mgn*2), NSWindowStyleMaskTitled
end fn


local fn CellAvailable( r as long, c as long ) as BOOL
  if ( r < 0 || c < 0 || r >= _rows || c >= _cols ) then exit fn
  if ( mda_integer(r,c) == _a ) then exit fn = YES
end fn = NO


void local fn ProcessCell( r as long, c as long )
  long r1 = r, c1 = c, d(3), count, dir, opp
  
  while ( 1 )
    BlockZero( @d(0), sizeof(long) * 4 )
    count = 0
    if ( fn CellAvailable( r - 1, c ) ) then d(count) = _t : count++
    if ( fn CellAvailable( r, c - 1 ) ) then d(count) = _l : count++
    if ( fn CellAvailable( r + 1, c ) ) then d(count) = _b : count++
    if ( fn CellAvailable( r, c + 1 ) ) then d(count) = _r : count++
    if ( count == 0 ) then break
    
    dir = d(rnd(count)-1)
    mda(r,c) = @(mda_integer(r,c) - dir)
    
    select ( dir )
      case _t
        r1 = r-1 : opp = _b
      case _l
        c1 = c-1 : opp = _r
      case _b
        r1 = r+1 : opp = _t
      case _r
        c1 = c+1 : opp = _l
    end select
    
    mda(r1,c1) = @(mda_integer(r1,c1) - opp)
    fn ProcessCell( r1, c1 )
  wend
end fn


void local fn DrawMaze
  long r, c, x = _mgn, y = _mgn, value
  
  pen 2,, NSLineCapStyleRound
  
  for r = 0 to _rows - 1
    for c = 0 to _cols - 1
      value = mda(r,c)
      if ( value & _t ) then line x, y to x + _size, y
      if ( value & _l ) then line x, y to x, y + _size
      if ( value & _b ) then line x, y + _size to x + _size, y + _size
      if ( value & _r ) then line x + _size, y to x + _size, y + _size
      x += _size
    next
    x = _mgn
    y += _size
  next
end fn


void local fn BuildMaze
  long r, c
  
  for r = 0 to _rows - 1
    for c = 0 to _cols - 1
      mda(r,c) = _a
    next
  next
  
  random
  r = rnd(_rows) - 1
  c = rnd(_cols) - 1
  fn ProcessCell( r, c )
  fn DrawMaze
end fn

fn BuildWindow
fn BuildMaze

HandleEvents

  

You may also check:How to resolve the algorithm Narcissist step by step in the C# programming language
You may also check:How to resolve the algorithm Table creation/Postal addresses step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Array length step by step in the Smalltalk programming language
You may also check:How to resolve the algorithm Copy stdin to stdout step by step in the OCaml programming language
You may also check:How to resolve the algorithm Loops/Infinite step by step in the Ring programming language