⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 maze.c

📁 Many C samples. It is a good sample for students to learn C language.
💻 C
📖 第 1 页 / 共 3 页
字号:
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <graphics.h>
#include <dos.h>
#include <stdlib.h>
#include <alloc.h>

#define TRUE 1
#define FALSE 0

typedef struct stack_1_rec {
  unsigned char      index_1;
  struct stack_1_rec *next_ptr;
} *stack_1_rec_ptr;

typedef struct stack_2_rec {
  unsigned char      index_1;
  unsigned char      index_2;
  struct stack_2_rec *next_ptr;
} *stack_2_rec_ptr;

static void generate_maze(int *,int *,int *,int *,int *,int *,int *,
             int *,int *,int *,int *,int *,int *,int *,int *);
static void initialize(int *,int *,int *,int *,int *,int *,int *,int *,
             int *,int *,int *,int *,int *,int *,int *,int *);
static void let_user_try_to_solve(int *,int *,int *,int *,int *,int *,
             int *,int *,int *,int *,int *);
void main(void);
static void optionally_have_computer_solve(int *,int *,int *,int *,
             int *,int *,int *,int *,int *);
static void remove_rejected_attempts(int *,int *,int *,int *,
             stack_1_rec_ptr *,stack_1_rec_ptr *,int *,int *,int *,
             int *,int *);

void main()
  {
    int delta_x [4] [24];
    int delta_y [4] [24];
    int erase;
    int fatal_error;
    int key_pressed;
    int magnitude_delta_x;
    int magnitude_delta_y;
    int num_columns;
    int num_rows;
    int passage;
    int path;
    int r_n [8];
    int twice_magnitude_delta_x;
    int twice_magnitude_delta_y;
    int wall;
    int x_max;
    int y_max;

    fatal_error=FALSE;

    initialize(&delta_x[0][0],&delta_y[0][0],&erase,&fatal_error,
    &magnitude_delta_x,&magnitude_delta_y,&num_columns,&num_rows,
    &passage,&path,&r_n[0],&twice_magnitude_delta_x,
    &twice_magnitude_delta_y,&wall,&x_max,&y_max);
    if (! fatal_error)
      generate_maze(&delta_x[0][0],&delta_y[0][0],&magnitude_delta_x,
       &magnitude_delta_y,&num_columns,&num_rows,&passage,&path,
       &r_n[0],&twice_magnitude_delta_x,&twice_magnitude_delta_y,
       &wall,&x_max,&y_max,&fatal_error);
    if (! fatal_error)
      let_user_try_to_solve(&delta_x[0][0],&delta_y[0][0],&erase,
       &key_pressed,&magnitude_delta_x,&magnitude_delta_y,&passage,
       &path,&wall,&y_max,&fatal_error);
    if (! fatal_error)
      optionally_have_computer_solve(&delta_x[0][0],&delta_y[0][0],
       &key_pressed,&magnitude_delta_x,&magnitude_delta_y,&passage,
       &path,&y_max,&fatal_error);
    if (! fatal_error)
      closegraph();
    return;
}

static void initialize(delta_x,delta_y,erase,fatal_error,
 magnitude_delta_x,magnitude_delta_y,num_columns,num_rows,passage,path,
 r_n,twice_magnitude_delta_x,twice_magnitude_delta_y,wall,x_max,y_max)
  int *delta_x;
  int *delta_y;
  int *erase;
  int *fatal_error;
  int *magnitude_delta_x;
  int *magnitude_delta_y;
  int *num_columns;
  int *num_rows;
  int *passage;
  int *path;
  int *r_n;
  int *twice_magnitude_delta_x;
  int *twice_magnitude_delta_y;
  int *wall;
  int *x_max;
  int *y_max;
    {
      int  delta_index_1a;
      int  delta_index_1b;
      int  delta_index_1c;
      int  delta_index_1d;
      int  delta_index_2;
      int  ErrorCode;
      int  GraphDriver;
      int  GraphMode;
      int  max_num_columns;
      int  max_num_rows;
      int  max_x;
      int  max_y;
      int  r_n_index_1;
      int  r_n_index_2;
      char seed [256];
      int  tem_int;

      detectgraph(&GraphDriver,&GraphMode);
      ErrorCode=graphresult();
      if (ErrorCode != 0)
        {
          *fatal_error=TRUE;
          printf("Fatal error:  %s\n",grapherrormsg(ErrorCode));
        }
      if (! *fatal_error)
        {
          switch (GraphDriver)
            {
              case CGA:
              case MCGA:
              case ATT400:
                {
                  GraphMode=0;
                  *erase=2;
                  *wall=1;
                  *passage=0;
                  *path=3;
                  break;
                }
              case EGA:
                {
                  GraphMode=EGAHI;
                  *erase=4;
                  *wall=9;
                  *passage=0;
                  *path=2;
                  break;
                }
              case EGA64:
                {
                  GraphMode=EGA64HI;
                  *erase=2;
                  *wall=1;
                  *passage=0;
                  *path=3;
                  break;
                }
              case VGA:
                {
                  GraphMode=VGAHI;
                  *erase=4;
                  *wall=9;
                  *passage=0;
                  *path=2;
                  break;
                }
              case IBM8514:
                {
                  GraphMode=IBM8514HI;
                  *erase=4;
                  *wall=9;
                  *passage=0;
                  *path=2;
                  break;
                }
              default:
                {
                  GraphMode=0;
                  *erase=0;
                  *wall=1;
                  *passage=0;
                  *path=1;
                  break;
                }
            }
          initgraph(&GraphDriver,&GraphMode,"");
          ErrorCode=graphresult();
          if (ErrorCode == 0)
            {
              max_x=getmaxx();
              max_y=getmaxy();
              max_num_columns=max_x/2;
              max_num_rows=max_y/2;
              closegraph();
            }
          else
            {
              GraphDriver=DETECT;
              initgraph(&GraphDriver,&GraphMode,"");
              ErrorCode=graphresult();
              if (ErrorCode == 0)
                {
                  max_x=getmaxx();
                  max_y=getmaxy();
                  max_num_columns=max_x/2;
                  max_num_rows=max_y/2;
                  *erase=0;
                  *wall=1;
                  *passage=0;
                  *path=1;
                  closegraph();
                }
              else
                {
                  *fatal_error=TRUE;
                  printf("Fatal error:  %s\n",grapherrormsg(ErrorCode));
                }
            }
        }
      if (! *fatal_error)
        {
          printf("                                 Maze Generator\n");
          printf("\n");
          printf("\n");
          printf("\n");
          printf(
"     This program will generate a maze.  After the maze is generated, you");
          printf("\n");
          printf(
"may use the cursor keys to solve it.  Press Q to quit or S to have the");
          printf("\n");
          printf(
"computer solve the maze.  If the computer solves the maze, you must press");
          printf("\n");
          printf(
"some key to exit.\n");
          printf("\n");
          do
            {
              printf("     Number of columns (2 to ");
              printf("%d",max_num_columns);
              printf(")? ");
              fflush(stdin);
              scanf("%d",num_columns);
              if ((*num_columns < 2)
              ||  (*num_columns > max_num_columns))
                {
                  printf(
                   "? The number of columns must be between 2 and ");
                  printf("%d",max_num_columns);
                  printf(", inclusively\n");
                }
            }
          while ((*num_columns < 2)
          ||     (*num_columns > max_num_columns));
          printf("\n");
          do
            {
              printf("     Number of rows (2 to ");
              printf("%d",max_num_rows);
              printf(")? ");
              fflush(stdin);
              scanf("%d",num_rows);
              if ((*num_rows < 2) || (*num_rows > max_num_rows))
                {
                  printf(
                   "? The number of rows must be between 2 and ");
                  printf("%d",max_num_rows);
                  printf(", inclusively\n");
                }
            }
          while ((*num_rows < 2) || (*num_rows > max_num_rows));
          printf("\n");
          printf("     Random number seed? ");
          fflush(stdin);
          gets(&seed[0]);
          for (r_n_index_1=0;
           ((r_n_index_1 < 8) && (seed[r_n_index_1] != (char) 0));
           r_n_index_1++)
            {
              tem_int=(int) seed[r_n_index_1];
              while (tem_int >= 29) tem_int-=29;
              *(r_n+r_n_index_1)=tem_int;
            }
          r_n_index_2=7;
          while (r_n_index_1 > 0)
            {
              r_n_index_1--;
              *(r_n+r_n_index_2)=*(r_n+r_n_index_1);
              r_n_index_2--;
            }
          while (r_n_index_2 >= 0)
            {
              *(r_n+r_n_index_2)=19;
              r_n_index_2--;
            }
          initgraph(&GraphDriver,&GraphMode,"");
          *magnitude_delta_x=max_x/(*num_columns)/2;
          *twice_magnitude_delta_x
           =(*magnitude_delta_x)+(*magnitude_delta_x);
          *magnitude_delta_y=max_y/(*num_rows)/2;
          *twice_magnitude_delta_y
           =(*magnitude_delta_y)+(*magnitude_delta_y);
          *x_max=*twice_magnitude_delta_x*(*num_columns);
          *y_max=*twice_magnitude_delta_y*(*num_rows);
          *delta_x=(*magnitude_delta_x);
          *(delta_y+24)=(*magnitude_delta_y);
          *(delta_x+48)=-(*magnitude_delta_x);
          *(delta_y+72)=-(*magnitude_delta_y);
          *delta_y=0;
          *(delta_x+24)=0;
          *(delta_y+48)=0;
          *(delta_x+72)=0;
          delta_index_2=-1;
          for (delta_index_1a=0; delta_index_1a < 4; delta_index_1a++)
            for (delta_index_1b=0; delta_index_1b < 4; delta_index_1b++)
              if (delta_index_1a != delta_index_1b)
                for (delta_index_1c=0; delta_index_1c < 4;
                 delta_index_1c++)
                  if ((delta_index_1a != delta_index_1c)
                  &&  (delta_index_1b != delta_index_1c))
                    for (delta_index_1d=0; delta_index_1d < 4;
                     delta_index_1d++)
                      if ((delta_index_1a != delta_index_1d)
                      &&  (delta_index_1b != delta_index_1d)
                      &&  (delta_index_1c != delta_index_1d))
                        {
                          delta_index_2=delta_index_2+1;
                          *(delta_x+(24*delta_index_1a+delta_index_2))
                           =*delta_x;
                          *(delta_y+(24*delta_index_1a+delta_index_2))
                           =*delta_y;
                          *(delta_x+(24*delta_index_1b+delta_index_2))
                           =*(delta_x+24);
                          *(delta_y+(24*delta_index_1b+delta_index_2))
                           =*(delta_y+24);
                          *(delta_x+(24*delta_index_1c+delta_index_2))
                           =*(delta_x+48);
                          *(delta_y+(24*delta_index_1c+delta_index_2))
                           =*(delta_y+48);
                          *(delta_x+(24*delta_index_1d+delta_index_2))
                           =*(delta_x+72);
                          *(delta_y+(24*delta_index_1d+delta_index_2))
                           =*(delta_y+72);
                        }
        }
      return;
    }

static void generate_maze(delta_x,delta_y,magnitude_delta_x,
 magnitude_delta_y,num_columns,num_rows,passage,path,r_n,
 twice_magnitude_delta_x,twice_magnitude_delta_y,wall,x_max,y_max,
 fatal_error)
  int *delta_x;
  int *delta_y;
  int *magnitude_delta_x;
  int *magnitude_delta_y;
  int *num_columns;
  int *num_rows;
  int *passage;
  int *path;
  int *r_n;
  int *twice_magnitude_delta_x;
  int *twice_magnitude_delta_y;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -