home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / CHASE.ZIP / CHASE.C
Text File  |  1989-03-19  |  17KB  |  665 lines

  1. /***************************************************************************
  2. *
  3. *
  4. *        chase appeared in Creative Computing
  5. *        magazine way back in 1976.  According
  6. *        to the book "More BASIC Computer Games"
  7. *        it was written by Mac Oglesby, with
  8. *        improvements by Bill Cotter and Arnold
  9. *        Loveridge.
  10. *        This version, in C, was written by
  11. *        Ken Brown.        7/25/84
  12. *        Code specific to the Microsoft C compiler
  13. *        is noted.
  14. *        The cursor movement routines assume that
  15. *        the DOS 2.x ANSI.SYS driver has been
  16. *        loaded.
  17. *
  18. ***************************************************************************/
  19.  
  20.  
  21. #include <stdio.h>
  22. #include <ctype.h>
  23. #include <conio.h>        /*  Microsoft direct console I/O routines */
  24.  
  25.  
  26.                 /* constant definitions */
  27. #define TRUE 1
  28. #define FALSE 0
  29. #define XDIM 60
  30. #define YDIM 20
  31. #define MAX_ROBOTS 120
  32. #define MAX_FENCES 120
  33. #define MIN_FENCES 5
  34. #define MIN_ROBOTS 5
  35. #define MY_CHAR '\002'
  36. #define ROB_CHAR '\004'
  37. #define JUNK_CHAR '@'
  38. #define FENCE_CHAR 'X' 
  39.  
  40.  
  41.                 /* global variable definitions */
  42. char        field[YDIM][XDIM], 
  43.             field_save[YDIM][XDIM]; 
  44.  
  45. int         robot_y[MAX_ROBOTS], 
  46.             robot_x[MAX_ROBOTS], 
  47.             robot_y_save[MAX_ROBOTS], 
  48.             robot_x_save[MAX_ROBOTS],
  49.         my_x, 
  50.             my_y, 
  51.         last_stand,
  52.         my_x_save,
  53.         my_y_save,
  54.         no_robots,
  55.         no_fences;
  56.  
  57. unsigned int    seed;
  58.  
  59. main() 
  60.         int     i_won,
  61.         game_over,
  62.         set_up,
  63.         c;
  64.          
  65.         initial();
  66.         instruct();
  67.  
  68.         set_up=TRUE;
  69.         while(TRUE) {
  70.                 if(set_up)
  71.                         new_board();
  72.                 else 
  73.                         old_board();
  74.  
  75.                 last_stand=FALSE; 
  76.         game_over=FALSE;
  77.         i_won=FALSE;
  78.  
  79.                 while(!game_over) {
  80.                         if(!last_stand)
  81.                                 game_over=move_me();
  82.  
  83.                         if(!game_over)
  84.                                 game_over=move_robots();
  85.             else
  86.                 break;
  87.  
  88.                         if(!game_over) {
  89.                 if(more_robots()==TRUE)
  90.                     continue;
  91.                 else {
  92.                     i_won=TRUE;
  93.                     break;
  94.                                 } 
  95.             }
  96.                 }
  97.                  
  98.                 if (i_won) {
  99.             move(21,0);
  100.                         printf("You have destroyed all your opponents  --  the game is yours"); 
  101.                 }
  102.                 move(22,0);
  103.                 printf("another game? (y/n)");
  104.                 c=getchar();
  105.         c=tolower(c);
  106.                 if(c=='n')
  107.             break;
  108.  
  109.                 move(23,0);
  110.                 printf("same set-up? (y/n)");
  111.                 c=getchar();
  112.                 c=tolower(c);
  113.         if(c=='y')
  114.                         set_up=FALSE;
  115.                 else
  116.                         set_up=TRUE;
  117.         }
  118.         clear();
  119.         move(22,0);
  120.  
  121.  
  122.  
  123. initial()
  124. /*
  125. *        get a seed for the random number generator
  126. */
  127. {
  128.  
  129.     clear();
  130.         do {
  131.                 clrtoeol(10,0);
  132.                 printf("Enter a seed for the random number generator? ");
  133.                 getnum(&seed);
  134.         } while(seed<=0);
  135.  
  136.     move(18,0);
  137.     printf("Be sure to lock the NUM LOCK KEY to use the numeric keypad");
  138.  
  139. }
  140.  
  141. instruct()
  142. /*
  143. *        print instructions
  144. */
  145. {
  146.         int c;
  147.  
  148.         move(20,0);
  149.         printf("do you want instructions? (y/n)");
  150.         c=getchar();
  151.         c=tolower(c);
  152.     if (c=='y') {
  153.                 clear();
  154.                 move(2,0);
  155.                 printf("you are within the walls of a high voltage maze\r\n"); 
  156.                 printf("the areas marked '%c' are high voltage fences\r\n",FENCE_CHAR); 
  157.         printf("being chased by robots\r\n");
  158.                 printf("your only chance for survival is to maneuver each\r\n"); 
  159.                 printf("robot into a fence\r\n"); 
  160.                 printf("you are the '%c'  the robots are the '%c'\r\n",MY_CHAR,ROB_CHAR); 
  161.                 printf("moves are:    7.8.9\r\n"); 
  162.                 printf("              4.*.6\r\n"); 
  163.                 printf("              1.2.3\r\n"); 
  164.                 printf("\r\nj = a tremendous (but unfortunately random leap)\r\n"); 
  165.                 printf("s = no move for rest of the game\r\n"); 
  166.                 printf("q = gave up, situation hopeless.\r\n"); 
  167.                 printf("\r\n\r\ngood luck!!");
  168.                 move(22,0);
  169.                 printf("press any key to begin");
  170.                 c=getchar();
  171.         }
  172. }
  173.  
  174. new_board()
  175. /*
  176. *        create and display a new playing board
  177. */
  178. {
  179.         int     y_coor,
  180.         x_coor,
  181.         fen_count,
  182.         rob_count;
  183.  
  184.         clear();
  185.         do {
  186.                 clrtoeol(10,0);
  187.                 printf("how many robots? (5 to 120) ");
  188.                 getnum(&no_robots);
  189.         } while(no_robots<MIN_ROBOTS||no_robots>MAX_ROBOTS);
  190.  
  191.         do {
  192.                 clrtoeol(12,0);
  193.                 printf("how many fences? (5 to 120) ");
  194.                 getnum(&no_fences);
  195.         } while(no_fences<MIN_FENCES||no_fences>MAX_FENCES);
  196.  
  197.         clear();
  198.         move(10,34);
  199.         printf("working.....");
  200.  
  201.         for(y_coor=0; y_coor<YDIM; y_coor++) {
  202.                 for(x_coor=0; x_coor<XDIM; x_coor++)
  203.                         field[y_coor][x_coor]=' ';
  204.         }
  205.  
  206.         for (y_coor=0; y_coor<YDIM; y_coor++) { 
  207.                 field[y_coor][0]=FENCE_CHAR; 
  208.                 field[y_coor][XDIM-1]=FENCE_CHAR; 
  209.         } 
  210.         for(x_coor=0; x_coor<XDIM; x_coor++) { 
  211.                 field[0][x_coor]=FENCE_CHAR; 
  212.                 field[YDIM-1][x_coor]=FENCE_CHAR; 
  213.         } 
  214.  
  215.         for(fen_count=0; fen_count<no_fences; fen_count++) {
  216.                 findspot(&y_coor, &x_coor);
  217.                 field[y_coor][x_coor]=FENCE_CHAR;
  218.         }
  219.  
  220.         findspot(&my_y, &my_x); 
  221.         field[my_y][my_x]=MY_CHAR; 
  222.  
  223.         for (rob_count=0; rob_count<no_robots; rob_count++) { 
  224.                 findspot(&robot_y[rob_count], &robot_x[rob_count]); 
  225.                 field[robot_y[rob_count]][robot_x[rob_count]]=ROB_CHAR; 
  226.         } 
  227.  
  228.         for (y_coor=0; y_coor<YDIM; y_coor++) { 
  229.                 for (x_coor=0; x_coor<XDIM; x_coor++) { 
  230.                         field_save[y_coor][x_coor]=field[y_coor][x_coor]; 
  231.                 } 
  232.         } 
  233.  
  234.         for (rob_count=0; rob_count<no_robots; rob_count++) { 
  235.                 robot_y_save[rob_count]=robot_y[rob_count]; 
  236.                 robot_x_save[rob_count]=robot_x[rob_count]; 
  237.         } 
  238.         my_y_save=my_y;
  239.         my_x_save=my_x;
  240.         draw_board();
  241. }
  242.  
  243. old_board()
  244. /*
  245. *        reload and display the previous board
  246. */
  247. {
  248.         int     y_coor,
  249.         x_coor,
  250.         rob_count;
  251.  
  252.         for(y_coor=0; y_coor<YDIM; y_coor++) {
  253.                 for(x_coor=0; x_coor<XDIM; x_coor++) {
  254.                         field[y_coor][x_coor]=field_save[y_coor][x_coor];
  255.                 }
  256.         }
  257.         for(rob_count=0; rob_count<no_robots; rob_count++) {
  258.                 robot_y[rob_count]=robot_y_save[rob_count];
  259.                 robot_x[rob_count]=robot_x_save[rob_count];
  260.         }
  261.         my_y=my_y_save;
  262.         my_x=my_x_save;
  263.         draw_board();
  264. }
  265.  
  266. move_me()
  267. /*
  268. *        player's movement routine
  269. */
  270. {
  271.         int     c,
  272.         my_x_old,
  273.         my_y_old;
  274.  
  275.     my_x_old=my_x;
  276.     my_y_old=my_y;
  277.         move(22,69);
  278.     putchar('*');
  279.     putchar('\b');
  280.     while(TRUE) {
  281.         c=getchar();
  282.         c=tolower(c);
  283.         if(isdigit(c)||c=='j'||c=='s'||c=='q')
  284.             break;
  285.         if(c==0)    /* trap for funtion keys */
  286.             c=getchar();
  287.         putchar('\007');
  288.     }
  289.     putchar(c);
  290.         switch(c) { 
  291.  
  292.                 case '1': 
  293.                         my_y++; 
  294.                         my_x--; 
  295.                         break; 
  296.                 case '2': 
  297.                         my_y++; 
  298.                         break; 
  299.                 case '3': 
  300.                         my_y++; 
  301.                         my_x++; 
  302.                         break; 
  303.                 case '4': 
  304.                         my_x--; 
  305.                         break; 
  306.                 case '6': 
  307.                         my_x++; 
  308.                         break; 
  309.                 case '7': 
  310.                         my_y--; 
  311.                         my_x--; 
  312.                         break; 
  313.                 case '8': 
  314.                         my_y--; 
  315.                         break; 
  316.                 case '9': 
  317.                         my_y--; 
  318.                         my_x++; 
  319.                         break; 
  320.                 case 'q': 
  321.                         clear();
  322.                         move(10,36);
  323.                         printf("chicken\n\n\n"); 
  324.                         move(22,0);
  325.                         exit(0); 
  326.                 case 'j': 
  327.                         my_y=1+rnd(YDIM-2); 
  328.                         my_x=1+rnd(XDIM-2); 
  329.                         break; 
  330.                 case 's': 
  331.                         last_stand=TRUE; 
  332.                         break; 
  333.                 default: 
  334.                         break; 
  335.         }        
  336.         field[my_y_old][my_x_old]=' '; 
  337.         move(my_y_old,my_x_old);
  338.         putchar(' ');
  339.         c=field[my_y][my_x]; 
  340.         switch(c) {
  341.                 case ROB_CHAR:
  342.                         flash(my_y,my_x);
  343.                         flash(my_y,my_x);
  344.                         move(my_y,my_x);
  345.                         printf("**MUNCH**");
  346.             move(21,0);
  347.                         printf("You have been eaten by robots");
  348.             return(TRUE);
  349.                         break;
  350.  
  351.                 case FENCE_CHAR:
  352.                 case JUNK_CHAR:
  353.                         flash(my_y,my_x);
  354.                         flash(my_y,my_x);
  355.                         move(my_y,my_x);
  356.                         printf("**ZAP**");
  357.             move(21,0);
  358.                         printf("High Voltage!!!  Zap, you're dead!"); 
  359.             return(TRUE);
  360.                         break;
  361.  
  362.                 default:
  363.                         field[my_y][my_x]=MY_CHAR; 
  364.                         move(my_y,my_x);
  365.             print_me();
  366.                         break;
  367.         }
  368.  
  369. move_robots()
  370. /*
  371. *        movement routine for robots
  372. */
  373. {
  374.         int     rob_count,
  375.         c;
  376.  
  377.         for(rob_count=0; rob_count<no_robots; rob_count++) { 
  378.                 if((c=field[robot_y[rob_count]][robot_x[rob_count]])!=ROB_CHAR)
  379.                         continue;
  380.                 move(robot_y[rob_count],robot_x[rob_count]);
  381.                 putchar(' ');
  382.                 field[robot_y[rob_count]][robot_x[rob_count]]=' ';
  383.                 robot_y[rob_count]=robot_y[rob_count]+sgn(my_y-robot_y[rob_count]); 
  384.                 robot_x[rob_count]=robot_x[rob_count]+sgn(my_x-robot_x[rob_count]); 
  385.                 c=field[robot_y[rob_count]][robot_x[rob_count]]; 
  386.         switch(c) {
  387.             case MY_CHAR:
  388.                             move(robot_y[rob_count],robot_x[rob_count]);
  389.                 print_robot();
  390.                             field[robot_y[rob_count]][robot_x[rob_count]]=ROB_CHAR;
  391.                             flash(robot_y[rob_count],robot_x[rob_count]);
  392.                             flash(robot_y[rob_count],robot_x[rob_count]);
  393.                             move(robot_y[rob_count],robot_x[rob_count]);
  394.                             printf("**MUNCH**");
  395.                 move(21,0);
  396.                             printf("you have been eaten by robots");
  397.                             return(TRUE);
  398.                 break;
  399.  
  400.             case ROB_CHAR:
  401.                             move(robot_y[rob_count],robot_x[rob_count]);
  402.                             putchar(JUNK_CHAR);
  403.                             field[robot_y[rob_count]][robot_x[rob_count]]=JUNK_CHAR;
  404.                             flash(robot_y[rob_count],robot_x[rob_count]);
  405.                 break;
  406.  
  407.             case JUNK_CHAR:
  408.                             flash(robot_y[rob_count],robot_x[rob_count]);
  409.                 break;
  410.  
  411.             case FENCE_CHAR:
  412.                             flash(robot_y[rob_count],robot_x[rob_count]);
  413.                 break;
  414.  
  415.             default:
  416.                         move(robot_y[rob_count],robot_x[rob_count]);
  417.                 print_robot();
  418.                         field[robot_y[rob_count]][robot_x[rob_count]]=ROB_CHAR; 
  419.                 break;
  420.             }
  421.     }
  422.     return(FALSE);
  423. }
  424.  
  425. more_robots()
  426. /*
  427. *        check whether more robots are still alive
  428. */
  429. {
  430.     int    rob_count;
  431.  
  432.     for(rob_count=0; rob_count<no_robots; rob_count++) { 
  433.         if(field[robot_y[rob_count]][robot_x[rob_count]]==ROB_CHAR)
  434.             return(TRUE);
  435.     }
  436.     return(FALSE);
  437.  
  438. draw_board()
  439. /*
  440. *        draw the board from the array board[][]
  441. */
  442. {
  443.         int     y_coor,
  444.         x_coor,
  445.         rob_count;
  446.  
  447.         clear();
  448.         move(0,0);
  449.         for(y_coor=0; y_coor<YDIM; y_coor++) { 
  450.                 for(x_coor=0; x_coor<XDIM; x_coor++) { 
  451.                         putchar(field[y_coor][x_coor]); 
  452.                 } 
  453.                 move(y_coor+1,0);
  454.         } 
  455.  
  456.     move(my_y,my_x);
  457.     print_me();
  458.  
  459.     for(rob_count=0; rob_count<no_robots; rob_count++) {
  460.         move(robot_y[rob_count],robot_x[rob_count]);
  461.         print_robot();
  462.     }
  463.  
  464.         move(1,63);
  465.         printf("Move Control:");
  466.         move(2,67);
  467.         printf("7 8 9");
  468.         move(3,68);
  469.         printf("\\|/");
  470.         move(4,67);
  471.         printf("4-5-6");
  472.         move(5,68);
  473.         printf("/|\\");
  474.         move(6,67);
  475.         printf("1 2 3");
  476.         move(9,66);
  477.         printf("Commands:");
  478.         move(10,63);
  479.         printf("j - a giant jump");
  480.         move(11,63);
  481.         printf("s - last stand");
  482.         move(12,63);
  483.         printf("q - quit");
  484.         move(15,66);
  485.         printf("Key:");
  486.         move(16,63);
  487.         printf("* - you");
  488.         move(17,63);
  489.         printf("+ - robot");
  490.         move(18,63);
  491.         printf("@ - junked robot");
  492.         move(19,63);
  493.         printf("X - electic fence");
  494.         move(22,63);
  495.         printf("Move [*]");
  496.  
  497. }
  498.  
  499. getnum(number)
  500. int *number;
  501. /*
  502. *        get a numeric input from the player
  503. *        non-numberic characters are locked out
  504. *        and the maximum length is arbitrarily set
  505. *        at 5 digits
  506. */
  507. {
  508.  
  509.         int     c,
  510.         count;
  511.         char     string[6];
  512.  
  513.         count=0;
  514.         while((c=getchar())!='\n'&&c!='\r') {
  515.                 switch(c) {
  516.                         case '1':
  517.                         case '2':
  518.                         case '3':
  519.                         case '4':
  520.                         case '5':
  521.                         case '6':
  522.                         case '7':
  523.                         case '8':
  524.                         case '9':
  525.                         case '0':
  526.                                 if(count<5) {
  527.                     string[count++]=c;
  528.                     putchar(c);
  529.                 }
  530.                                 break;
  531.                         case '\b':
  532.                                 if(count>0) {
  533.                                         count--;
  534.                     putchar('\b');
  535.                     putchar(' ');
  536.                     putchar('\b');
  537.                                 }
  538.                                 else {
  539.                                         putchar('\007');
  540.                                 }
  541.                                 break;
  542.                         default:
  543.                                 putchar('\007');
  544.                                 break;
  545.                 }
  546.         }
  547.         string[count]='\0';
  548.     stcd_i(string,number);    /* Microsoft string to numeric conversion
  549.                     (equiv. to atoi) */
  550. }
  551.  
  552.  
  553.  
  554. findspot(new_y,new_x) 
  555. /*
  556. *        locate a empty board position
  557. */
  558. int *new_y, *new_x; 
  559.  
  560.         do { 
  561.                 *new_y=1+rnd(YDIM-2); 
  562.                 *new_x=1+rnd(XDIM-2); 
  563.         } while(field[*new_y][*new_x] != ' '); 
  564.  
  565.  
  566. rnd(range) 
  567. /*
  568. *        generate a random number in the
  569. *        range 0<= number < range
  570. */
  571. int range; 
  572.         float  x; 
  573.  
  574.         seed  *= 15625;                  /* Multiplication mod 65536     */
  575.         x=(seed/65536.0)*(float)range; 
  576.         return((int)x); 
  577.  
  578.  
  579. sgn(number) 
  580. int(number); 
  581. /*
  582. *        return the sign of a number
  583. */
  584.  
  585.         if (number<0) 
  586.                 return(-1); 
  587.         if(number>0) 
  588.                 return(1); 
  589.         return(0); 
  590.  
  591. /*****************************************************************************
  592. *
  593. *        these routines assume that the ANSI.SYS driver
  594. *        has been loaded
  595. *
  596. *****************************************************************************/
  597.  
  598. move(y,x)
  599. int y,x;
  600. {
  601.     printf("\033[%d;%dH",y+1,x+1);     /* cursor postions are incremented
  602.                        by one because playing board
  603.                        starts at (0,0) and screen
  604.                        starts at (1,1) */    
  605. }
  606.  
  607. clrtoeol(y,x)
  608. int y,x;
  609. {
  610.     move(y,x);
  611.     printf("\033[K");
  612. }
  613.  
  614. clear()
  615. {
  616.     printf("\033[2J");
  617. }
  618.  
  619. print_me()
  620. {
  621.     printf("\033[1m");
  622.     putchar(MY_CHAR);
  623.     printf("\033[0m");
  624. }
  625.  
  626. print_robot()
  627. {
  628.     printf("\033[1m");
  629.     putchar(ROB_CHAR);
  630.     printf("\033[0m");
  631. }
  632.  
  633. flash(y,x)
  634. int x,y;
  635. {
  636.     int    count,
  637.         c;
  638.  
  639.         c=field[y][x];
  640.     move(y,x);
  641.         for(count=0; count<20; count++) {
  642.         printf("\033[1m");
  643.         putchar('\080');
  644.         putchar('\b');
  645.         printf("\033[0m");
  646.         putchar(c);
  647.         putchar('\b');
  648.         }
  649. }
  650. printf("\033[1m");
  651.         putchar('\080');
  652.         putchar('\b');
  653.         printf("\033[0m");
  654.         putchar(c);
  655.