Click here to Skip to main content
15,893,486 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: C++ Program without main() Pin
Richard MacCutchan7-Jun-12 4:53
mveRichard MacCutchan7-Jun-12 4:53 
AnswerRe: C++ Program without main() Pin
Aescleal7-Jun-12 0:12
Aescleal7-Jun-12 0:12 
GeneralRe: C++ Program without main() Pin
Aescleal13-Jun-12 16:50
Aescleal13-Jun-12 16:50 
QuestionDump analysis Pin
HungryCPPDev6-Jun-12 21:22
HungryCPPDev6-Jun-12 21:22 
AnswerRe: Dump analysis Pin
«_Superman_»6-Jun-12 22:23
professional«_Superman_»6-Jun-12 22:23 
GeneralRe: Dump analysis Pin
HungryCPPDev11-Jun-12 20:23
HungryCPPDev11-Jun-12 20:23 
AnswerRe: Dump analysis Pin
Erudite_Eric6-Jun-12 22:32
Erudite_Eric6-Jun-12 22:32 
QuestionWhat's problem of my code?( create a random maze) Pin
jiuyejii6-Jun-12 0:45
jiuyejii6-Jun-12 0:45 
C++
#include <iostream>
#include <stdlib.h>
#include<ctime>
using namespace std;

void makebg(char**&);

bool Is(int ,int );

void cmaze(int,int);


void count(int,int);

int M=0;
int N=0;
int in=0;
int out=0;
char  **maze; 
int c=0;
int *cti;
int *ctj;
int dir =1;

int main()
{

    cin>>M>>N;
    maze =new char*[M];
    for(int i=0;i<M;i++)
        maze[i]=new char[N];
    cti =new int[M*N];
    ctj =new int[M*N];
    for(int i=0;i<M*N;i++) 
    {
        cti[i]=0;
        ctj[i]=0;
    }

    makebg(maze);

    return 0;
}

void makebg(char **&maze)
{ 
    int   i,j; 


    for(i=0;i <M;i++) 
        for(j=0;j <N;j++) 
            maze[i][j]='#';

    srand((unsigned)time(0));

    in=rand()%(M-2)+1;       // Create entrance or exit 
    maze[in][0]='.';

    out=rand()%(M-2)+1;

         cti[0]=in;
         ctj[0]=1;
    cmaze(in,1);

} 

void cmaze(int i,int j){

    int ran=rand()%4; 


    if((i==out&&j==N-2))      //if then out the recursion
    {
        maze[i][j]='.';
        maze[i][j+1]='.';
    }


    if(maze[out][N-1]=='.')
        return;

    else{

        maze[i][j]='.';

        for (int s=0;s<M;s++)
        {
            for (int t=0;t<N;t++)
                cout<<maze[s][t]<<' ';
            cout<<'\n';
        }
        //system("cls");


        switch(ran)
        {
        case 0:{ //down
                
            if(Is(i+2,j)&&Is(i+1,j-1)&&Is(i+2,j+1)){ //To ensure that the next three sides wall and not backtrack
                i++;
                count(i,j);
                dir=2;
            } 

            else if(i+1==M-1) {               //step return
                c--;
                i=cti[c];
                j=ctj[c];
                cti[c+1]=0;     

                ctj[c+1]=0;

            }
            break;
               }   
        case 1:{ //left
            if(Is(i,j-2)&&Is(i+1,j-1)&&Is(i-1,j-1)){ 
                j--;
                count(i,j);
                dir=3;

            } 

            else if(j-1==0){          
                c--;
                i=cti[c];
                j=ctj[c];
                cti[c+1]=0;

                ctj[c+1]=0;

            }
            break;
               } 
        case 2:{ //up
            if(Is(i-2,j)&&Is(i-1,j-1)&&Is(i-1,j+1)){ 
                i--;
                count(i,j);
                dir=0;

            } 

            else if(i-1==0){             
                c--;
                i=cti[c];
                j=ctj[c];
                cti[c+1]=0;

                ctj[c+1]=0;
            

            }
            break;
               } 
        case 3:{ //right
            if(Is(i,j+2)&&Is(i-1,j+1)&&Is(i+1,j+1)){ 
                j++; 
                count(i,j);
                dir=1;
            }

            else if(j+1==N-1){              
                c--;
                i=cti[c];
                j=ctj[c];
                cti[c+1]=0;

                ctj[c+1]=0;
    
            }
            break;
               } 

        }
        dir=ran;
        cmaze(i,j);

    }

}
void count(int i,int j)
{
    c++;        //       
    cti[c]=i;   //
    ctj[c]=j;   //Record the location

}


bool Is(int i,int j)        
{
    if(i>=0&&i<=M-1&&j>=0&&j<=N-1&&maze[i][j]=='#')
        return true;
    else
        return false;
}



what's the problem of my code?? can't create the maze.
may be problem comes from back to previous step,but how to correct it?
AnswerRe: What's problem of my code?( create a random maze) Pin
Chris Losinger6-Jun-12 1:14
professionalChris Losinger6-Jun-12 1:14 
GeneralRe: What's problem of my code?( create a random maze) Pin
jiuyejii6-Jun-12 1:36
jiuyejii6-Jun-12 1:36 
SuggestionRe: What's problem of my code?( create a random maze) Pin
David Crow6-Jun-12 2:36
David Crow6-Jun-12 2:36 
AnswerRe: What's problem of my code?( create a random maze) Pin
Maximilien6-Jun-12 2:46
Maximilien6-Jun-12 2:46 
AnswerRe: What's problem of my code?( create a random maze) Pin
Aescleal6-Jun-12 6:47
Aescleal6-Jun-12 6:47 
GeneralRe: What's problem of my code?( create a random maze) Pin
Peter_in_27806-Jun-12 17:37
professionalPeter_in_27806-Jun-12 17:37 
GeneralRe: What's problem of my code?( create a random maze) Pin
jiuyejii6-Jun-12 18:49
jiuyejii6-Jun-12 18:49 
GeneralRe: What's problem of my code?( create a random maze) Pin
Aescleal6-Jun-12 23:52
Aescleal6-Jun-12 23:52 
AnswerRe: What's problem of my code?( create a random maze) Pin
Stefan_Lang8-Jun-12 4:19
Stefan_Lang8-Jun-12 4:19 
GeneralRe: What's problem of my code?( create a random maze) Pin
jiuyejii8-Jun-12 17:57
jiuyejii8-Jun-12 17:57 
QuestionMFC application being compiled on VS2012 Pin
YaronNir5-Jun-12 23:03
YaronNir5-Jun-12 23:03 
QuestionRe: MFC application being compiled on VS2012 Pin
David Crow6-Jun-12 2:37
David Crow6-Jun-12 2:37 
AnswerRe: MFC application being compiled on VS2012 Pin
YaronNir6-Jun-12 2:40
YaronNir6-Jun-12 2:40 
GeneralRe: MFC application being compiled on VS2012 Pin
tldcolli4-Aug-12 3:31
tldcolli4-Aug-12 3:31 
AnswerRe: MFC application being compiled on VS2012 Pin
tldcolli4-Aug-12 3:28
tldcolli4-Aug-12 3:28 
QuestionCode Executed Twice - OnHScroll() Pin
AmbiguousName5-Jun-12 21:42
AmbiguousName5-Jun-12 21:42 
AnswerRe: Code Executed Twice - OnHScroll() Pin
Chris Losinger6-Jun-12 1:17
professionalChris Losinger6-Jun-12 1:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.