Thread: mission notimpossible (maze gen)

  1. #1
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356

    Talking mission notimpossible (maze gen)

    Well after struggling for 2 hours withthis maze **** i have finnaly finished it ..It uses a maze generator to generate the maze ....
    I know i know you might be thinking okay aaze generator might be he used stacks or some algorith and so on ...No this is just a simple **** ...but works pretty fine .....

    You can use this code or get the zip file which has the exe and the code ...

    Code:
    /* Written by datainjector
       Maze generator and game */
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <windows.h>
    /*#include <conio.c> use only if needed for getch() */
    
    #define ROW 20
    #define COL 75
    
    void creatmaze (int [][COL], int *, int * );
    void printarea(int [][COL]);
    void movement( int [][COL], int , int *, int * );
    void gotoxy ( int , int );
    void kill_cursor( void );
    
    int main(void)
    {
     int maze[ROW][COL]={0};
     int col=1, row=0, trackend, trackstart;
     srand(time(0));
    
     kill_cursor();
     creatmaze(maze, &trackend, &trackstart);
     printarea(maze);
    
     while ( maze[trackend][(COL-2)] == -1 ){
           movement ( maze, tolower(getch()), &trackstart, &col );
     }
     system("cls");
    
     printf("You won\nTo lay a different mare just run the program again...\n");
    
    
    
          system("pause");
          return 0;
    }
    void creatmaze ( int maze[][COL] , int *trackend, int *trackstart)
    {
         int cnt_main, cnt_sub;
         int row, col, tf;
         int rowt, colt;
         rowt = 0, colt = 0;
         row = 2, col = 1;
    
         for (  cnt_main = 0; cnt_main <= COL-1; cnt_main+=COL-1 ){
             for ( cnt_sub = 0; cnt_sub <= ROW-1; cnt_sub++ )
                 maze[cnt_sub][cnt_main] = 2;
         }
    
         for ( cnt_main = 0; cnt_main <= ROW-1; cnt_main+=ROW-1 ){
             for ( cnt_sub = 0; cnt_sub <= COL-1; cnt_sub++ )
                 maze[cnt_main][cnt_sub] = 2;
         }
    
         for ( cnt_main = 1; cnt_main <= ROW-2; cnt_main++ ){
             for ( cnt_sub = 1; cnt_sub <= COL-2; cnt_sub++ ){
                 maze[cnt_main][cnt_sub] = 1;
             }
         }
    
         row =  1 + rand() % 19;
         *trackstart = row;
         tf = 1;
    
         maze[row][1] = -2;
         cnt_main = 0;
         while ( col < COL-2 ){
    
               if ( tf == 1 ){
                  if ( maze[row][col+=1] == 2){
                     col-=1;
                  }
                  else{
                       maze[row][col] = 0;
                  }
               }
               else if ( tf == 2 ){
                    if ( maze[row-=1][col] == 2 ){
                       row+=1;
                    }
                    else{
                       maze[row][col] = 0;
                    }
               }
               else{
                    if ( maze[row+=1][col] == 2 ){
                      row-=1;
                    }
                    else
                        maze[row][col] = 0;
               }
    
    
               tf = 1 + rand() /(RAND_MAX / 3 );
    
    
       }
    
       *trackend = row;
       cnt_main = 0;
       while ( ++cnt_main != 1000 ){
             /*rowt = 1 + rand() / (RAND_MAX / (ROW-2)); this creats an easier maze i guess
              colt = 1 + rand() / (RAND_MAX /(COL-2)); */
             rowt = 1 + rand() % (ROW-2);
             colt = 1 + rand() % (COL-2);
    
             maze[rowt][colt] = 0;
    
      }
      maze[*trackstart][1] = -2;
      maze[*trackend][(COL-2)] = -1;
    
    
    }
    void movement ( int array[ROW][COL], int choice, int *row, int *col )
    {
        int store;
    
    	switch(choice){
    		case 'w' :
                store = array[--(*row)][(*col)];
                if ( store != 2 && store != 1 ){
    				array[(*row)][*col] = -2;
                    gotoxy(*col,*row);
                    putchar(2);
    				array[++(*row)][*(col)] = 0;
                    gotoxy(*col,*row);
                    putchar(255);
    				--(*row);
    				}
    			else{
    				array[++(*row)][*col] = -2;
                    gotoxy(*col,*row);
                    putchar(2);
                }
    			break;
    	   case 's' :
                store = array[++(*row)][(*col)];
                if ( store != 2 && store != 1  ){
    				 array[*row][*col] = -2;
                     gotoxy(*col,*row);
                     putchar(2);
    				 array[--(*row)][(*col)] = 0;
                     gotoxy(*col,*row);
                     putchar(255);
    				 ++(*row);
    			}
    			else{
    				array[--(*row)][*col] = -2;
                    gotoxy(*col,*row);
                    putchar(2);
                }
    			break;
    	  case 'a' :
                store = array[(*row)][--(*col)];
                if ( store != 2 && store != 1 ){
    				array[*row][*col] = -2;
                    gotoxy(*col,*row);
                    putchar(2);
    				array[*row][++(*col)] = 0;
                    gotoxy(*col,*row);
                    putchar(255);
    				--(*col);
    			}
    			else{
    				array[*row][++(*col)] = -2;
                    gotoxy(*col,*row);
                    putchar(2);
                }
    			break;
    	  case 'd' :
                store = array[(*row)][++(*col)];
                if ( store != 2 && store != 1  ){
    				array[*row][*col] = -2;
                    gotoxy(*col,*row);
                    putchar(2);
    				array[*row][--(*col)] = 0;
                    gotoxy(*col,*row);
                    putchar(255);
    				++(*col);
    			}
    			else{
    				array[*row][--(*col)] = -2;
                    gotoxy(*col,*row);
                    putchar(2);
                }
    			break;
    	}
    }
    void printarea(int maze[][COL])
    {
     int cnt_main, cnt_sub;
    
     for ( cnt_main = 0; cnt_main < ROW; cnt_main++ ){
         for ( cnt_sub = 0; cnt_sub < COL; cnt_sub++ ){
             if ( maze[cnt_main][cnt_sub] == 2 )
                putchar(176);
             else if ( maze[cnt_main][cnt_sub] == 1 )
                putchar(178);
             else if ( maze[cnt_main][cnt_sub] == 0 )
                putchar(255);
             else if ( maze[cnt_main][cnt_sub] == -2 )
                  putchar(2);
             else
                 putchar(6);
    
         }
         printf("\n");
     }
    }
    void gotoxy(int x, int y)
    {
     COORD coord;
     coord.X = x;
     coord.Y = y;
     SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
    }
    void kill_cursor( void )
    {
    	CONSOLE_CURSOR_INFO cci;
    
    	cci.dwSize = 1;
    	cci.bVisible = FALSE;
    	SetConsoleCursorInfo( GetStdHandle( STD_OUTPUT_HANDLE ), &cci );
    }
    Please do give some feedback ...Cheers
    Last edited by datainjector; 11-21-2002 at 11:55 PM.
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Now I'm going to make you feel real bad ;) :D

    How about this for an alternative:
    Code:
    /* From http://home.planet.nl/~faase009/Signindex.html */
    /*  [email protected] (192.74.137.5) */               /* Joseph H. Allen */ 
    int a[1817];main(z,p,q,r){for(p=80;q+p-80;p-=2*a[p])for(z=9;z--;)q=3&(r=time(0)
    +r*57)/7,q=q?q-1?q-2?1-p%79?-1:0:p%79-77?1:0:p<1659?79:0:p>158?-79:0,q?!a[p+q*2
    ]?a[p+=a[p+=q]=q]=q:0:0;for(;q++-1817;)printf(q%79?"%c":"%c\n","#"[!a[q-1]]);}
    And the output
    Code:
     ######### ##### # ### ########### #   ##### # ### ####### ##### #####   ### #
     #   # #     # # # #   #   # # #   #     #   #   #   #   #   #   # #       # #
     ##### ####### ### ####### # # ### # ############# ### ##### ##### ### #######
             #   #   #       #   # # # #   # #       # #     #   # # #       #
     ####### # # # # # ####### # # # ##### # # ##### ##### ##### # # # # # # #####
       # # #   #   # #     #   #   # # # #   #     #           #     # # # #     #
     # # # ########### ### ### ### # # # ####### ####### ######### ####### ### ###
     # #     # # #     #     # # # #               # # # #   #     #       # # #
     ### # # # # # ####### ### # ### # # ######### # # ##### # ### ######### #####
       # # #         #   # #   #     # # #     #     # #       #       # #     #
     # ##### ### ####### ##### # ##### ### # # ### # # # # ### # # ##### ### #####
     #     # #     #           #   #   # # # # #   #   # # #   # #   #     #     #
     ##### ####### ### ######### # ##### # ### ####### ### ############### ##### #
     # # #       #     #         # #       # #       #   #     #       #   #   #
     # # ####### ### ##### ####### # ##### # # ##### ### ##### # ##### ### # #####
         #   #     # # #     #   # #   # #   #   #     #     #   # # # #     # #
     ### # # ####### # # ####### # # ### # ######### ### ##### ### # # # # ### # #
       # # # # #   #   # #         # #   #     # #     #   #     #   #   # #   # #
     ####### # # ####### ####### # # ### ### ### # # ### # # # ##### # ##### #####
       # # #           # # # #   # # #   #   #   # #   # # # # #     # #         #
       # # ##### ####### # # ######### ######### ### ########### ######### #######
    :D

    And don't ask me to explain it.....
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    My god Hammer thats Fugly!
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Stoned_Coder
    My god Hammer thats Fugly!
    BTW, I forgot to point out, it ain't mine. and yes, it's a mare.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356
    Now I'm going to make you feel real bad
    Jees dock when i meant by feedback i didn't mean that you should make me feel bad (that one was just for the flow)hehe rap world aintit funny lol ...The maze is better than mine ofcourse but i cant find the exit ????
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When I compile/run it, the players icon kinda screws up when you move. Here's a screen print of it after I'd pressed D for right just once. You'll notice there are now 2 smilies (the lower one being the original which doesn't move).
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356
    But it working perfectly fine on my machine tried it on the schools machine it work just fine ...I have found out a better way to improve the maze will take some time when i am finished i will post it here

    Cheers
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having trouble solving maze.
    By eurus in forum C Programming
    Replies: 3
    Last Post: 02-17-2006, 01:52 AM
  2. NAQ: Everything you never wanted to know about CPP
    By evildave in forum C Programming
    Replies: 21
    Last Post: 12-12-2005, 10:56 AM
  3. Q: Recursion to find all paths of a maze
    By reti in forum C Programming
    Replies: 7
    Last Post: 11-26-2002, 09:28 AM
  4. My Maze Game --- A Few Questions
    By TechWins in forum Game Programming
    Replies: 18
    Last Post: 04-24-2002, 11:00 PM
  5. random maze gen.
    By Goof Program in forum C Programming
    Replies: 1
    Last Post: 02-19-2002, 12:08 PM