home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / ROBOTS3.ZIP / robots / robots3.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-12  |  7.2 KB  |  232 lines

  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/timeb.h>
  4. #include "console.h"
  5. #include "sound.h"
  6.  
  7. unsigned Level, Peak, Bombs, dB;
  8. int Row, Col;
  9. unsigned Reds, Yellows;
  10.  
  11. byte Image[40][60];
  12. int
  13.    dR[9] = { -1, 0, +1, -1, 0, +1, -1, 0, +1 },
  14.    dC[9] = { -1, -1, -1, 0, 0, 0, +1, +1, +1 };
  15.  
  16. void NewLevel(int dL) {
  17.    long L;
  18.    if (dL > 0) {
  19.       L = Level + ((0x4000L + (0x8000L - (long)Level)*dL) >> 15);
  20.    } else {
  21.       L = Level - ((0x4000L + (long)Level*(-dL)) >> 15);
  22.    }
  23.    if (L == Level) return;
  24.    Level = (unsigned)L, PutString(28, 32, WhiteC, "%5u", Level);
  25.    if (Level >= Peak)
  26.       Peak = Level, PutString(27, 32, WhiteC, "%5u", Peak);
  27. }
  28.  
  29. void SetRobot(void) {
  30.    struct timeb T;
  31.    ftime(&T); srand((unsigned int)(T.time ^ T.millitm)&0xffff);
  32.    Peak = Level = 100, Bombs = 1; dB = 0;
  33.    ScrInit();
  34. }
  35.  
  36. int SafeColor(int Row, int Col) {
  37.    int I, J, R, C, R1, C1;
  38.    if (Row < 0 || Row >= 40 || Col < 0 || Col >= 60) return BlackC;
  39.    for (I = 0; I < 9; I++) {
  40.       R = Row + dR[I], C = Col + dC[I];
  41.       if (R < 0 || R >= 40 || C < 0 || C >= 60) continue;
  42.       switch (Image[R][C]) {
  43.          case YellowC: case RedC: return BrownC;
  44.          case BrownC: continue;
  45.       }
  46.       for (J = 0; J < 9; J++) {
  47.          R1 = R + dR[J], C1 = C + dC[J];
  48.          if (R1 < 0 || R1 >= 40 || C1 < 0 || C1 >= 60) continue;
  49.          switch (Image[R1][C1]) {
  50.             case RedC: return BrownC;
  51.          }
  52.       }
  53.    }
  54.    return CyanC;
  55. }
  56.  
  57. void Show(int Row, int Col, int Hue) {
  58.    unsigned R = 10*(Row + 1), C = 10*(Col + 2);
  59.    Box(R + 1, R + 8, C + 1, C + 8, Hue);
  60.    Image[Row][Col] = Hue;
  61.    switch (Hue) {
  62.       case YellowC: Sound(150, 1); break;
  63.       case RedC: Sound(300, 1); break;
  64.       case CyanC: Sound(600, 1); break;
  65.       case BrownC: Sound(1200, 1); break;
  66.    }
  67. }
  68.  
  69. void SafeKeys(void) {
  70.    PutString(27, 70, SafeColor(Row - 1, Col - 1), "Y");
  71.    PutString(27, 72, SafeColor(Row - 1, Col), "U");
  72.    PutString(27, 74, SafeColor(Row - 1, Col + 1), "I");
  73.    PutString(28, 70, SafeColor(Row, Col - 1), "H");
  74.    PutString(28, 72, SafeColor(Row, Col), "W");
  75.    PutString(28, 74, SafeColor(Row, Col + 1), "K");
  76.    PutString(29, 70, SafeColor(Row + 1, Col - 1), "B");
  77.    PutString(29, 72, SafeColor(Row + 1, Col), "N");
  78.    PutString(29, 74, SafeColor(Row + 1, Col + 1), "M");
  79. }
  80.  
  81. void RobotScreen(main) {
  82.    int R, C;
  83.    Box(10, 409, 20, 619, BlackC);
  84.    Border(9, 410, 19, 620, BlueC);
  85.    Border(7, 412, 17, 622, GreenC);
  86.    Border(5, 414, 15, 624, YellowC);
  87.    Border(3, 416, 13, 626, RedC);
  88.    PutString(28, 5, RedC, "R");
  89.    PutString(28, 7, YellowC, "O");
  90.    PutString(28, 9, GreenC, "B");
  91.    PutString(28, 11, CyanC, "O");
  92.    PutString(28, 13, BlueC, "T");
  93.    PutString(28, 15, PinkC, "S");
  94.    PutString(27, 25, YellowC, "Peak:");
  95.    PutString(28, 25, YellowC, "Level:");
  96.    PutString(29, 25, YellowC, "Bombs:");
  97.    PutString(27, 32, WhiteC, "%5d", Peak);
  98.    PutString(28, 32, WhiteC, "%5d", Level);
  99.    PutString(29, 32, WhiteC, "%5d", Bombs);
  100.    Row = rand()%40, Col = rand()%60, Show(Row, Col, CyanC);
  101.    PutString(28, 60, RedC, "RADAR");
  102.    Reds = Yellows = 0;
  103.    for (R = 0; R < 40; R++) for (C = 0; C < 60; C++) {
  104.       unsigned L;
  105.       if (R == Row && C == Col) continue;
  106.       L = rand();
  107.       if (L < Level >> 2) Show(R, C, RedC), Reds++;
  108.       else if (L < Level) Show(R, C, YellowC), Yellows++;
  109.       else Show(R, C, BlackC);
  110.    }
  111. }
  112.  
  113. main() {
  114.    int I, Hue, dR, dC, R, C, R1, C1;
  115.    unsigned Pitch;
  116.    SetRobot();
  117. RESTART:
  118.    RobotScreen();
  119. NEXT:
  120.    if (Reds == 0 && Yellows == 0) { dB += Level; goto COMPLETE; }
  121. KEY:
  122.    SafeKeys();
  123.    if (!KeyHit()) goto UPDATE;
  124.    switch (Keyboard()) {
  125.       case 3: ScrReset(); exit(1);
  126.       case 'y': case 'Y': dR = -1, dC = -1; goto GO;
  127.       case 'u': case 'U': case Up: dR = -1, dC = 0; goto GO;
  128.       case 'i': case 'I': dR = -1, dC = +1; goto GO;
  129.       case 'h': case 'H': case Left: dR = 0, dC = -1; goto GO;
  130.       case 't':
  131.          for (Pitch = 100; Pitch <= 1200; Pitch += 100) Sound(Pitch, 1);
  132.          Show(Row, Col, BlackC);
  133.          Row = rand()%40, Col = rand()%60;
  134.          if (Image[Row][Col] != BlackC && Image[Row][Col] != CyanC) {
  135.             Image[Row][Col] = WhiteC; goto CRASH;
  136.          }
  137.       goto MOVE;
  138.       case 'k': case 'K': case Right: dR = 0, dC = +1; goto GO;
  139.       case 'b': case 'B': dR = +1, dC = -1; goto GO;
  140.       case 'n': case 'N': case Down: dR = +1, dC = 0; goto GO;
  141.       case 'm': case 'M': dR = +1, dC = +1; goto GO;
  142.       case 'a': case 'A':
  143.          if (Bombs == 0) break;
  144.          NewLevel(-2048);
  145.          PutString(29, 32, WhiteC, "%5d", --Bombs);
  146.          for (Pitch = 1000; Pitch >= 100; Pitch -= 100) Sound(Pitch, 1);
  147.          for (R = Row - 3; R <= Row + 3; R++)
  148.          for (C = Col - 3; C <= Col + 3; C++) {
  149.             if (R < 0 || R >= 40 || C < 0 || C >= 60) continue;
  150.             switch (Image[R][C]) {
  151.                case RedC: Reds--; NewLevel(8); Show(R, C, BlackC); break;
  152.                case YellowC: Yellows--; NewLevel(4); Show(R, C, BlackC); break;
  153.             }
  154.          }
  155.       default:
  156.       goto UPDATE;
  157.    }
  158. GO:
  159.    R = Row + dR, C = Col + dC;
  160.    if (R < 0 || R >= 40 || C < 0 || C >= 60) goto UPDATE;
  161.    if (Image[R][C] == BrownC) {
  162.       int R1 = R + dR, C1 = C + dC;
  163.       if (R1 < 0 || R1 >= 40 || C1 < 0 || C1 >= 60) goto UPDATE;
  164.       if (Image[R1][C1] != BlackC) goto UPDATE;
  165.       Show(R1, C1, BrownC);
  166.       Show(Row, Col, BlackC);
  167.       Row = R, Col = C; goto MOVE;
  168.    } else {
  169.       Show(Row, Col, BlackC);
  170.       switch (Image[R][C]) {
  171.          case RedC: Reds--; NewLevel(8); Show(R, C, WhiteC); goto CRASH;
  172.          case YellowC: Yellows--; NewLevel(4); Show(R, C, WhiteC); goto CRASH;
  173.          case BlackC: Row = R, Col = C; goto MOVE;
  174.       }
  175.    }
  176. MOVE:
  177.    Show(Row, Col, CyanC);
  178. UPDATE:
  179.    for (I = 0; I < 240; I++) {
  180.       R = rand()%40, C = rand()%60; Hue = Image[R][C];
  181.       if (Hue == RedC || Hue == YellowC) goto FOUND;
  182.    }
  183.    goto NEXT;
  184. FOUND:
  185.    Show(R, C, BlackC);
  186.    R1 = R + (R < Row? +1: R > Row? -1: 0);
  187.    C1 = C + (C < Col? +1: C > Col? -1: 0);
  188.    switch (Image[R1][C1]) {
  189.       case CyanC: Show(R1, C1, WhiteC); goto CRASH;
  190.       case RedC: Reds--; NewLevel(8); goto BROWN;
  191.       case YellowC: Yellows--; NewLevel(4);
  192.       case BrownC: BROWN:
  193.          Show(R1, C1, BrownC);
  194.          if (Hue == RedC) Reds--, NewLevel(8);
  195.          else Yellows--, NewLevel(4);
  196.       goto NEXT;
  197.       case BlackC: R = R1, C = C1, Show(R, C, Hue); break;
  198.    }
  199.    if (Hue != RedC) goto NEXT;
  200.    Show(R, C, BlackC);
  201.    R1 = R + (R < Row? +1: R > Row? -1: 0);
  202.    C1 = C + (C < Col? +1: C > Col? -1: 0);
  203.    switch (Image[R1][C1]) {
  204.       case CyanC: Show(R1, C1, WhiteC); goto CRASH;
  205.       case RedC: Reds--; NewLevel(8); goto BROWN2;
  206.       case YellowC: Yellows--; NewLevel(4);
  207.       case BrownC: BROWN2:
  208.          Show(R1, C1, BrownC); Reds--, NewLevel(8);
  209.       goto NEXT;
  210.       case BlackC: Show(R1, C1, Hue); break;
  211.    }
  212. goto NEXT;
  213. CRASH:
  214.    NewLevel(-8192);
  215.    PutString(27, 55, WhiteC, "MUNCH!!!");
  216.    dB = 0;
  217. COMPLETE:
  218.    PutString(29, 55, WhiteC, "ENDED");
  219.    while (1) switch (Keyboard()) {
  220.       case 3: case 0x1b: ScrReset(); exit(0);
  221.       case '\r':
  222.          if (dB > 500) {
  223.             unsigned dd = dB/500;
  224.             Bombs += dd, dB -= 500*dd;
  225.             PutString(29, 32, WhiteC, "%5d", Bombs);
  226.          }
  227.          PutString(27, 55, BlackC, "MUNCH!!!");
  228.          PutString(29, 55, BlackC, "ENDED");
  229.       goto RESTART;
  230.    }
  231. }
  232.