Skip to content

Instantly share code, notes, and snippets.

@Munawwar
Created October 21, 2025 18:58
Show Gist options
  • Save Munawwar/ec250beebf5d4222f71d8bcf545b7824 to your computer and use it in GitHub Desktop.
Save Munawwar/ec250beebf5d4222f71d8bcf545b7824 to your computer and use it in GitHub Desktop.
My turbo c++ msdos ascii maze solver from 2007
/*Maze solver-By Munawwar Firoz*/
#include<fstream.h>
#include<conio.h>
#include<string.h>
#include<dos.h>
const speed=10; /*Try changing speed to 0 and run the program*/
const long rsize=41,csize=41,randsize=(rsize*csize)+2;
void mazecopy(char maze[rsize][csize],int &row,int &column)
{
row=39,column=40;
strcpy(maze[0], "###############################.#.#######");
strcpy(maze[1], "#.........#.....#.............#.#.......#");
strcpy(maze[2], "#.#.#####.#.###.###.#######.###.#####.###");
strcpy(maze[3], "#.#...#.#...#.#...#.....#.#...#.#.#.....#");
strcpy(maze[4], "#.###.#.#####.#.#####.#.#.#####.#.###.###");
strcpy(maze[5], "#...#.........#.....#.#.#.......#.#.....#");
strcpy(maze[6], "###.#.#######.#.#.#.###.#######.#.#####.#");
strcpy(maze[7], "#...#.#...#D..#.#.#...#.#.....#.#.......#");
strcpy(maze[8], "#.#####.#.#####.#.###.#.#.###.#.#######.#");
strcpy(maze[9], "#.#...#.#...#...#...#...#...#.#.........#");
strcpy(maze[10],"#.#.#.#.###.###.###.#######.#.#########.#");
strcpy(maze[11],"#...#...#.#...#.#.......#.#.#.......#...#");
strcpy(maze[12],"#########.#.#######.#.#.#.#.#######.#####");
strcpy(maze[13],"#.#.#...#.#.......#.#.#.#...#...#D#.#...#");
strcpy(maze[14],"#.#.###.#.#######.#####.#.###.#.#.#.#.#.#");
strcpy(maze[15],"#.......#.....#.#...#.#.....#.#...#...#.#");
strcpy(maze[16],"#.#####.###.###.###.#.###.###.#########.#");
strcpy(maze[17],"#.....#.#.......#.#...#.#...#.#.#.......#");
strcpy(maze[18],"#.#.#####.#######.###.#.#.#.#.#.###.###.#");
strcpy(maze[19],"#.#.....#...........#...#.#.#...#.....#.#");
strcpy(maze[20],"#.#####.###########.###.#####.#######.###");
strcpy(maze[21],"#.....#.#...#...#.#.#.#.....#.#.....#...#");
strcpy(maze[22],"#####.#.#.#.#.#.#.#.#.#####.#.#.#.#####.#");
strcpy(maze[23],"#.....#...#...#.....#.......#...#.#...#.#");
strcpy(maze[24],"###.###.#########.#.#.#########.#.#.#.#.#");
strcpy(maze[25],"#.#.#.#...#.....#.#.#.......#...#.#.#...#");
strcpy(maze[26],"#.#.#.###.###.#.#.###.###.#.#.#####.###.#");
strcpy(maze[27],"#.#.#...#...#.#.....#...#.#.#.....#.#...#");
strcpy(maze[28],"#.#.###.###.###.#######.#######.###.#####");
strcpy(maze[29],"#...#...#.#...#.#.....#...#...#.#...#.#.#");
strcpy(maze[30],"###.###.#.###.###.###.###.###.#.#.#.#.#.#");
strcpy(maze[31],"#.#.....#...#.#.#.#.#.....#.....#.#.....#");
strcpy(maze[32],"#.#####.#.###.#.#.#.#######.#####.#####.#");
strcpy(maze[33],"#.....#...#.#.....#.......#.#...#.....#.#");
strcpy(maze[34],"#.###.###.#.#######.###.#.#.#.#.#####.###");
strcpy(maze[35],"#...#...#.....#.#...#...#.#.#.#.....#.#.#");
strcpy(maze[36],"#.###.#####.#.#.#####.###.#.#.#.###.#.#.#");
strcpy(maze[37],"#...#.......#...#.#...#...#...#.#...#.#.#");
strcpy(maze[38],"###.###.#######.#.###.#.#########.###.#.#");
strcpy(maze[39],"#.....#.#.............#.........#.#.....M");
strcpy(maze[40],"###D###########################.#.#######");
}
void get_the_rc(int &row,int &column,int rc)
{
if(rc==0)
{
row++;
if(row==rsize) row=0;
}
if(rc==1)
{
column++;
if(column==csize) column=0;
}
if(rc==2)
{
row--;
if(row==-1) row=rsize-1;
}
if(rc==3)
{
column--;
if(column==-1) column=csize-1;
}
}
void main()
{
textmode(64);
clrscr();
register char maze[rsize][csize],randoms[randsize+2];
int row,column,count=0;
mazecopy(maze,row,column);
cout<<"\nTry to solve it yourself before you look at the answer";
cout<<"\nStarting point is M(in yellow) and ending at the D's(in blue)";
cout<<"\nThere are three D's.So there are three solutions.Try to find each path";
cout<<"\nPress enter to display answer\n\n";
{
for(int q=0;q<rsize;q++)
{
for(int r=0;r<csize;r++)
if(maze[q][r]!='D' && maze[q][r]!='M')
cout<<maze[q][r];
else
if(maze[q][r]=='D')
textcolor(3),putch('D'),textcolor(7);
else
textcolor(14),putch('M'),textcolor(7);
cout<<endl;
}
}
getch();
clrscr();
long double flag=0;
int pos;
{
int q=0;
for(;q<randsize;q++)
randoms[q]='0';
randoms[q]=0;
}
pos=strlen(randoms)-1;
for(int i=0;randoms[0]<'4';i++)
{
flag++;
if(count%10000==0)
{
count=0;
int crx=wherex(),cry=wherey();
gotoxy(50,25);
cout<<flag;
gotoxy(crx,cry);
}
count++;
mazecopy(maze,row,column);
maze[row][column]='M';
{
randoms[pos]++;
for(int q=pos;q>0;q--)
if(randoms[q]>'3')
{
randoms[q]='0';
randoms[q-1]++;
}
}//end of if(path[0]!='5' && path[0]!='\0')
int flagx=1;
for(int i=0;randoms[i]!='\0';i++)
{
get_the_rc(row,column,randoms[i]-48);
if(maze[row][column]=='.' && i!=randsize-2)
maze[row][column]=' ';
else
if(maze[row][column]=='D')
{
flagx=2;
pos=i;
break;
}
else
{
flagx=0;
pos=i;
break;
}
}//end of for loop
if(flagx==2)
{
int q,r;
int crx=wherex(),cry=wherey();
for(q=0;q<rsize;q++)
{
for(r=0;r<csize;r++)
if(maze[q][r]==' ')
cout<<' ';
else
if(maze[q][r]=='D')
textcolor(3),putch('D'),textcolor(7);
else
cout<<maze[q][r];
cout<<endl;
}
mazecopy(maze,row,column);
gotoxy(crx+column,cry+row);
for(int i=0;randoms[i]!='\0';i++)
{
get_the_rc(row,column,randoms[i]-48);
gotoxy(crx+column,cry+row);
if(maze[row][column]=='.')
textcolor(2),putch('x'),delay(speed),textcolor(7);
if(maze[row][column]=='D')
{
gotoxy(50,25);
cout<<i/2<<" moves made";
getch(),clrscr();
break;
}
}
}
}//end of primary for loop
gotoxy(35,25);
cout<<flag<<" attempts made";
getch();
}
@Munawwar
Copy link
Author

Munawwar commented Oct 21, 2025

M = Mouse
D = Cheese? (there are 3 in the maze)

In this one it is allowed to move through gaps in walls to the opposite side.

This is how it looks like
Screenshot from 2025-10-21 22-50-04

This code can be run from dosbox (tried on linux) and copy of borland turbo C

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment