Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) MazeCreatorVcl

 

MazeCreator (VCL) is a tool to demonstrate my simple maze generation algorithm.

 

MazeCreator (VCL) is the predecessor of MazeCreator.

 

Technical facts

 

 

 

 

 

 

./ToolMazeCreatorVcl/ProjectMazeCreater.cpp

 

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
//---------------------------------------------------------------------------
USEFORM("UnitFormMazeCreaterMain.cpp", FormMazeCreaterMain);
USEFORM("UnitFormMazeCreaterAbout.cpp", FormMazeCreaterAbout);
//---------------------------------------------------------------------------
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
        try
        {
                 Application->Initialize();
                 Application->Title = "Maze Creater";
                 Application->CreateForm(__classid(TFormMazeCreaterMain), &FormMazeCreaterMain);
                 Application->Run();
        }
        catch (Exception &exception)
        {
                 Application->ShowException(&exception);
        }
        catch (...)
        {
                 try
                 {
                         throw Exception("");
                 }
                 catch (Exception &exception)
                 {
                         Application->ShowException(&exception);
                 }
        }
        return 0;
}
//---------------------------------------------------------------------------

 

 

 

 

 

./ToolMazeCreatorVcl/UnitFormMazeCreaterAbout.h

 

//---------------------------------------------------------------------------
/*
  Maze Creater, creates a maze and displays it on screen
  Copyright (C) 2007  Richel Bilderbeek

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
// From http://www.richelbilderbeek.nl
//---------------------------------------------------------------------------
#ifndef UnitFormMazeCreaterAboutH
#define UnitFormMazeCreaterAboutH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ComCtrls.hpp>
#include <ExtCtrls.hpp>
#include <Graphics.hpp>
//---------------------------------------------------------------------------
class TFormMazeCreaterAbout : public TForm
{
__published: // IDE-managed Components
        TImage *ImageRichelBilderbeek;
        TRichEdit *RichEdit1;
        TPanel *PanelTop;
        TPanel *PanelTopLeft;
        TPanel *Panel2;
        TPanel *Panel3;
        TPanel *Panel4;
        TPanel *Panel5;
        TPanel *Panel6;
private: // User declarations
public: // User declarations
        __fastcall TFormMazeCreaterAbout(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TFormMazeCreaterAbout *FormMazeCreaterAbout;
//---------------------------------------------------------------------------
#endif

 

 

 

 

 

./ToolMazeCreatorVcl/UnitFormMazeCreaterAbout.cpp

 

//---------------------------------------------------------------------------
/*
  Maze Creater, creates a maze and displays it on screen
  Copyright (C) 2007  Richel Bilderbeek

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
// From http://www.richelbilderbeek.nl
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "UnitFormMazeCreaterAbout.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFormMazeCreaterAbout *FormMazeCreaterAbout;
//---------------------------------------------------------------------------
__fastcall TFormMazeCreaterAbout::TFormMazeCreaterAbout(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

 

 

 

 

 

./ToolMazeCreatorVcl/UnitFormMazeCreaterMain.h

 

//---------------------------------------------------------------------------
/*
  Maze Creater, creates a maze and displays it on screen
  Copyright (C) 2007  Richel Bilderbeek

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
// From http://www.richelbilderbeek.nl
//---------------------------------------------------------------------------
#ifndef UnitFormMazeCreaterMainH
#define UnitFormMazeCreaterMainH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
#include <Graphics.hpp>
//---------------------------------------------------------------------------
class TFormMazeCreaterMain : public TForm
{
__published: // IDE-managed Components
        TImage *ImageBackground;
        void __fastcall OnAnyClick(TObject *Sender);
private: // User declarations
public: // User declarations
        __fastcall TFormMazeCreaterMain(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TFormMazeCreaterMain *FormMazeCreaterMain;
//---------------------------------------------------------------------------
void SetPixel(
  TImage * const image,
  const int x,
  const int y,
  const unsigned char red,
  const unsigned char green,
  const unsigned char blue);
void GetPixel(
  const TImage * const image,
  const int x,
  const int y,
  unsigned char& red,
  unsigned char& green,
  unsigned char& blue);


#endif

 

 

 

 

 

./ToolMazeCreatorVcl/UnitFormMazeCreaterMain.cpp

 

//---------------------------------------------------------------------------
/*
  Maze Creater, creates a maze and displays it on screen
  Copyright (C) 2007  Richel Bilderbeek

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
// From http://www.richelbilderbeek.nl
//---------------------------------------------------------------------------
#include <ctime>
#include <cassert>
#include <vector>
#include <memory>
#include <vcl.h>
#pragma hdrstop

#include "UnitFormMazeCreaterMain.h"
#include "UnitMazeCreater.h"
#include "UnitFormMazeCreaterAbout.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFormMazeCreaterMain *FormMazeCreaterMain;
//---------------------------------------------------------------------------
__fastcall TFormMazeCreaterMain::TFormMazeCreaterMain(TComponent* Owner)
        : TForm(Owner)
{
  const int size = 33;
  std::srand( std::clock() );

  ImageBackground->Picture->Bitmap->Width  = size;
  ImageBackground->Picture->Bitmap->Height = size;

  const std::vector<std::vector<int> > maze = CreateMaze(size);
  assert(maze.size() == size);
  assert(maze[0].size() == size);

  for (int y=0; y!=size; ++y)
  {
    for (int x=0; x!=size; ++x)
    {
      switch(maze[x][y])
      {
        case 0: //Road
          SetPixel(ImageBackground,x,y,255,255,255);
          break;
        case 1: //Wall
          SetPixel(ImageBackground,x,y,0,0,0);
          break;
      }
    }
  }
}
//---------------------------------------------------------------------------
void __fastcall TFormMazeCreaterMain::OnAnyClick(TObject *Sender)
{
  //
  std::auto_ptr<TFormMazeCreaterAbout> f(new TFormMazeCreaterAbout(0));
  f->ShowModal();
}
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppGetPixel.htm
void GetPixel(
  const TImage * const image,
  const int x,
  const int y,
  unsigned char& red,
  unsigned char& green,
  unsigned char& blue)
{
  assert(image!=0 && "Image is NULL");
  assert(image->Picture->Bitmap!=0 && "Bitmap is NULL");
  assert(image->Picture->Bitmap->PixelFormat == pf24bit && "Bitmap must be 24 bit");
  assert( x >= 0 && "x coordinat is below zero");
  assert( y >= 0 && "y coordinat is below zero");
  assert( x < image->Picture->Bitmap->Width  && "x coordinat is beyond image width");
  assert( y < image->Picture->Bitmap->Height && "y coordinat is beyond image height");
  red   = (static_cast<unsigned char*>(image->Picture->Bitmap->ScanLine[y])[x*3+2]);
  green = (static_cast<unsigned char*>(image->Picture->Bitmap->ScanLine[y])[x*3+1]);
  blue  = (static_cast<unsigned char*>(image->Picture->Bitmap->ScanLine[y])[x*3+0]);
}
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppSetPixel.htm
void SetPixel(
  TImage * const image,
  const int x,
  const int y,
  const unsigned char red,
  const unsigned char green,
  const unsigned char blue)
{
  assert(image!=0 && "Image is NULL");
  assert(image->Picture->Bitmap!=0 && "Bitmap is NULL");
  assert(image->Picture->Bitmap->PixelFormat == pf24bit && "Bitmap must be 24 bit");
  assert( x >= 0 && "x coordinat is below zero");
  assert( y >= 0 && "y coordinat is below zero");
  assert( x < image->Picture->Bitmap->Width  && "x coordinat is beyond image width");
  assert( y < image->Picture->Bitmap->Height && "y coordinat is beyond image height");

  static_cast<unsigned char*>(image->Picture->Bitmap->ScanLine[y])[x*3+2] = red;
  static_cast<unsigned char*>(image->Picture->Bitmap->ScanLine[y])[x*3+1] = green;
  static_cast<unsigned char*>(image->Picture->Bitmap->ScanLine[y])[x*3+0] = blue;
}
//---------------------------------------------------------------------------

 

 

 

 

 

./ToolMazeCreatorVcl/UnitMazeCreater.h

 

//---------------------------------------------------------------------------
/*
  Maze Creater, creates a maze and displays it on screen
  Copyright (C) 2007  Richel Bilderbeek

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
// From http://www.richelbilderbeek.nl
//---------------------------------------------------------------------------
#ifndef UnitMazeCreaterH
#define UnitMazeCreaterH
//---------------------------------------------------------------------------
#include <vector>

//From http://www.richelbilderbeek.nl/CppCreateMaze.htm
std::vector<std::vector<int> > CreateMaze(const int size);

#endif

 

 

 

 

 

./ToolMazeCreatorVcl/UnitMazeCreater.cpp

 

//---------------------------------------------------------------------------
/*
  Maze Creater, creates a maze and displays it on screen
  Copyright (C) 2007  Richel Bilderbeek

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
// From http://www.richelbilderbeek.nl
//---------------------------------------------------------------------------
#include <cassert>
#pragma hdrstop

#include "UnitMazeCreater.h"
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppCreateMaze.htm
std::vector<std::vector<int> > CreateMaze(const int size)
{
  //Size must be odd
  assert( size % 2 == 1);

  std::vector<std::vector<int> > maze(size, std::vector<int>(size,0));

  //Draw outer walls
  for (int i=0; i!=size; ++i)
  {
    maze[0]     [i     ] = 1;
    maze[size-1][i     ] = 1;
    maze[i]     [0     ] = 1;
    maze[i]     [size-1] = 1;
  }

  //Draw pillars
  for (int y=2; y!=size-1; y+=2)
  {
    for (int x=2; x!=size-1; x+=2)
    {
      maze[y][x] = 1;
    }
  }

  //Check around pillars
  const int nWallsToAdd = ((size / 2) - 1) * ((size / 2) - 1);
  for (int i=0; i!=nWallsToAdd; )
  {

    for (int y=2; y!=size-1; y+=2)
    {
      for (int x=2; x!=size-1; x+=2)
      {
        const int nWalls
          = (maze[y-1][x] == 0 ? 0 : 1)
          + (maze[y+1][x] == 0 ? 0 : 1)
          + (maze[y][x+1] == 0 ? 0 : 1)
          + (maze[y][x-1] == 0 ? 0 : 1);
        if ( nWalls == 0)
        {
          switch(std::rand() % 4)
          {
            case 0: maze[y-1][x] = 1; break;
            case 1: maze[y+1][x] = 1; break;
            case 2: maze[y][x+1] = 1; break;
            case 3: maze[y][x-1] = 1; break;
          }
          ++i;
        }
        else if (nWalls == 1)
        {
          switch(std::rand() % 6)
          {
            case 0: std::swap(maze[y-1][x], maze[y+1][x]); break;
            case 1: std::swap(maze[y-1][x], maze[y][x+1]); break;
            case 2: std::swap(maze[y-1][x], maze[y][x-1]); break;
            case 3: std::swap(maze[y+1][x], maze[y][x+1]); break;
            case 4: std::swap(maze[y+1][x], maze[y][x-1]); break;
            case 5: std::swap(maze[y][x+1], maze[y][x-1]); break;
          }
        }
      }
    }
  }
  return maze;
}

#pragma package(smart_init)

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml