home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / FC20C.ZIP / TTT3D.C < prev    next >
C/C++ Source or Header  |  1990-08-20  |  5KB  |  299 lines

  1. /*
  2.  * 3D Tic Tac Toe.
  3.  *
  4.  * Original Author Unknown
  5.  */
  6. #include \MC\stdio.h
  7.  
  8. #define    EMPTY    0
  9. #define    PLAYER    1
  10. #define    BEAST    5
  11.  
  12. /*
  13.  * This is a table of all winning combinations.
  14.  * From Kilobaud, April 78.
  15.  * You can look there to see how it is ordered.
  16.  */
  17. char w[] = {
  18.     0,    1,    2,    3,
  19.     4,    5,    6,    7,
  20.     8,    9,    10,    11,
  21.     12,    13,    14,    15,
  22.     0,    4,    8,    12,
  23.     1,    5,    9,    13,
  24.     2,    6,    10,    14,
  25.     3,    7,    11,    15,
  26.     0,    5,    10,    15,
  27.     3,    6,    9,    12,
  28.     16,    17,    18,    19,
  29.     20,    21,    22,    23,
  30.     24,    25,    26,    27,
  31.     28,    29,    30,    31,
  32.     16,    20,    24,    28,
  33.     17,    21,    25,    29,
  34.     18,    22,    26,    30,
  35.     19,    23,    27,    31,
  36.     16,    21,    26,    31,
  37.     19,    22,    25,    28,
  38.     32,    33,    34,    35,
  39.     36,    37,    38,    39,
  40.     40,    41,    42,    43,
  41.     44,    45,    46,    47,
  42.     32,    36,    40,    44,
  43.     33,    37,    41,    45,
  44.     34,    38,    42,    46,
  45.     35,    39,    43,    47,
  46.     32,    37,    42,    47,
  47.     35,    38,    41,    44,
  48.     48,    49,    50,    51,
  49.     52,    53,    54,    55,
  50.     56,    57,    58,    59,
  51.     60,    61,    62,    63,
  52.     48,    52,    56,    60,
  53.     49,    53,    57,    61,
  54.     50,    54,    58,    62,
  55.     51,    55,    59,    63,
  56.     48,    53,    58,    63,
  57.     51,    54,    57,    60,
  58.     0,    16,    32,    48,
  59.     1,    17,    33,    49,
  60.     2,    18,    34,    50,
  61.     3,    19,    35,    51,
  62.     4,    20,    36,    52,
  63.     5,    21,    37,    53,
  64.     6,    22,    38,    54,
  65.     7,    23,    39,    55,
  66.     8,    24,    40,    56,
  67.     9,    25,    41,    57,
  68.     10,    26,    42,    58,
  69.     11,    27,    43,    59,
  70.     13,    29,    45,    61,
  71.     12,    28,    44,    60,
  72.     14,    30,    46,    62,
  73.     15,    31,    47,    63,
  74.     0,    21,    42,    63,
  75.     4,    21,    38,    55,
  76.     8,    25,    42,    59,
  77.     12,    25,    38,    51,
  78.     1,    21,    41,    61,
  79.     13,    25,    37,    49,
  80.     2,    22,    42,    62,
  81.     14,    26,    38,    50,
  82.     3,    22,    41,    60,
  83.     7,    22,    37,    52,
  84.     11,    26,    41,    56,
  85.     15,    26,    37,    48,
  86.     0,    20,    40,    60,
  87.     0,    17,    34,    51,
  88.     3,    18,    33,    48,
  89.     3,    23,    43,    63,
  90.     12,    24,    36,    48,
  91.     12,    29,    46,    63,
  92.     15,    30,    45,    60,
  93.     15,    27,    39,    51
  94. };
  95.  
  96. /*
  97.  * This is the board.
  98.  * Starts off all empty.
  99.  */
  100. char    b[64] = 0;
  101. char    sep[] = "  -----------  -----------  -----------  -----------\n";
  102.  
  103. /*
  104.  * The mainline is just a driver.
  105.  */
  106. main()
  107. {
  108.     char buf[20];
  109.  
  110.     printf("Do you want the rules? ");
  111.     fgets(buf, 20, stdin);
  112.     if(buf[0]=='Y' || buf[0]=='y')
  113.         rules();
  114.     printf("Do you want to go first? ");
  115.     fgets(buf,20,stdin);
  116.     if(buf[0]=='Y' || buf[0]=='y')
  117.         user();
  118.     for(;;) {
  119.         beast();
  120.         user();
  121.     }
  122. }
  123.  
  124. /*
  125.  * Print the rules of the game.
  126.  */
  127. rules()
  128. {
  129.     printf("Three dimensional tic-tac-toe is played on a 4x4x4\n");
  130.     printf("board. To win you must get 4 in a row.  Your moves\n");
  131.     printf("are specified as a 3 digit number; the first digit\n");
  132.     printf("is the level, the second the row and the third the\n");
  133.     printf("column. Levels and columns  go  from left to right\n");
  134.     printf("from 1 to 3. Rows go from top to bottom with  0 on\n");
  135.     printf("the top.\n");
  136. }
  137.  
  138. /*
  139.  * Accept a user move. Exit if he wins.
  140.  */
  141. user()
  142. {
  143.     register int i, j, t;
  144.     char buf[20];
  145.  
  146.     board();
  147.     for(;;) {
  148.         printf("Your move (L/R/C)? ");
  149.         if(!fgets(buf,20,stdin)) {
  150.             printf("Chicken.\n");
  151.             exit(0);
  152.         }
  153.         i = 16*(buf[0]-'1') + (buf[1]-'1') + 4*(buf[2]-'1');
  154.         if(i>=0 && i<=63 && b[i]==EMPTY)
  155.             break;
  156.         printf("Eh?\n");
  157.     }
  158.     b[i] = PLAYER;
  159.     for(i=0; i<4*76; i+=4) {
  160.         t = 0;
  161.         for(j=0; j<4; ++j)
  162.             t += b[w[i+j]];
  163.         if(t == 4*PLAYER) {
  164.             printf("You win.\n");
  165.             exit(0);
  166.         }
  167.     }
  168. }
  169.  
  170. /*
  171.  * Display the board. (Not as easy as it sounds)
  172.  */
  173. board()
  174. {
  175.     register int i, j;
  176.  
  177.     for(i=1; i < 5; ++i)
  178.         printf("       %d     ", i);
  179.     printf("\n");
  180.     for(i=0; i<4; ++i) {
  181.         printf(sep);
  182.         printf("%d ", i+1);
  183.         for(j=0; j<64; j+=4) {
  184.             psq(i+j);
  185.             if(j==12 || j==28 || j==44)
  186.                 printf("  ");
  187.             else if(j >= 60)
  188.                 putc('\n', stdout);
  189.             else
  190.                 putc('!', stdout);
  191.         }
  192.     }
  193.     printf(sep);
  194.     for(i=0; i < 4; ++i) {
  195.         for(j=1; j < 5; ++j)
  196.             printf("  %d", j);
  197.         printf(" "); }
  198.     printf("\n");
  199. }
  200.  
  201. /*
  202.  * Format and put out square `s' of the board.
  203.  */
  204. psq(s)
  205.     int s;
  206. {
  207.     register int v;
  208.  
  209.     v = b[s];
  210.     if(v == PLAYER)
  211.         printf("PP");
  212.     else if(v == BEAST)
  213.         printf("CC");
  214.     else
  215.         printf("  ");
  216. }
  217.  
  218. /*
  219.  * Move for the machine. Just exit on machine wins and draws.
  220.  */
  221. beast()
  222. {
  223.     register int i, j, t;
  224.     int s, bs, bt, v[76];
  225.  
  226.     for(i=0; i<4*76; i+=4) {
  227.         t = 0;
  228.         for(j=0; j<4; ++j)
  229.             t += b[w[i+j]];
  230.         v[i>>2] = t;
  231.         if(t == 3*BEAST)
  232.             break;
  233.     }
  234.     if(i < 4*76) {
  235.         for(j=0; j<4; ++j)
  236.             if(b[w[i+j]] == EMPTY) {
  237.                 b[w[i+j]] = BEAST;
  238.                 break;
  239.             }
  240.         board();
  241.         printf("I win.\n");
  242.         exit(0);
  243.     }
  244.     bt = 0;
  245.     for(s=0; s<64; ++s) {
  246.         if(b[s] != EMPTY)
  247.             continue;
  248.         t = 0;
  249.         for(i=0; i<4*76; i+=4) {
  250.             for(j=0; j<4; ++j)
  251.                 if(w[i+j] == s)
  252.                     break;
  253.             if(j != 4) {
  254.                 if(v[i>>2] == 3*PLAYER) {
  255.                     b[s] = BEAST;
  256.                     return;
  257.                 }
  258.                 t += weight(v[i>>2]);
  259.             }
  260.         }
  261.         if(t > bt) {
  262.             bt = t;
  263.             bs = s;
  264.         }
  265.     }
  266.     if(bt != 0)
  267.         b[bs] = BEAST;
  268.     else {
  269.         for(s=0; s<64; ++s)
  270.             if(b[s] == EMPTY)
  271.                 break;
  272.         if(s == 64) {
  273.             printf("Draw.\n");
  274.             exit(0);
  275.         }
  276.         b[s] = BEAST;
  277.     }
  278. }
  279.  
  280. /*
  281.  * Given a total along a winning combination, return the weight value.
  282.  */
  283. weight(at)
  284.     int at;
  285. {
  286.     register int t;
  287.  
  288.     t = at;
  289.     if(t == PLAYER)
  290.         return(1);
  291.     if(t == 2*PLAYER)
  292.         return(4);
  293.     if(t == BEAST)
  294.         return(1);
  295.     if(t == 2*BEAST)
  296.         return(2);
  297.     return(0);
  298. }
  299.