home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cenvi23.zip / TTT.CMM < prev    next >
Text File  |  1994-09-26  |  5KB  |  177 lines

  1. /* ---------- ttt.cmm ----------- */
  2. /*                                */
  3. /* A simple game of tic-tac-toe   */
  4. /* written in Cmm                 */
  5. /* contributed by Al Stevens      */
  6. /*                                */
  7. #define TRUE 1
  8. #define FALSE 0
  9. #define BELL 7
  10. /* ---- board markers ---- */
  11. #define PLAYER 'X'
  12. #define COMPUTER 'O'
  13. #define FREE ' '
  14. /* --- game position on screen --- */
  15. #define LEFT 10
  16. #define TOP   5
  17. /* --- game board --- */
  18. board = "         ";
  19. /* --- winning combinations --- */
  20. wins = {
  21.     /* --- winning rows --- */
  22.     { 1,2,3 },
  23.     { 4,5,6 },
  24.     { 7,8,9 },
  25.     /* --- winning columns --- */
  26.     { 1,4,7 },
  27.     { 2,5,8 },
  28.     { 3,6,9 },
  29.     /* --- winning diagonals --- */
  30.     { 1,5,9 },
  31.     { 3,5,7 }
  32. };
  33. main()
  34. {
  35.     ch = 'y';
  36.     while (ch == 'y')    {
  37.         memset(board, FREE, 9);
  38.         displayboard();
  39.         /* --- get player's first move --- */
  40.         if ((mv = getmove()) == 0)
  41.             break;
  42.         /* --- set computer's first move --- */
  43.         if (mv != 5)
  44.             setpiece(5, COMPUTER);    /* center if available  */
  45.         else
  46.             setpiece(1, COMPUTER);  /* upper left otherwise */
  47.         moves = 2;
  48.         while (moves < 9) {
  49.             getmove();            /* player's next move */
  50.             moves++;
  51.             if (won())    {
  52.                 message(1, "You win");
  53.                 break;
  54.             }
  55.             if (moves == 9)
  56.                 message(1, "Tie");
  57.             else    {
  58.                 /* --- find computer's next move --- */
  59.                 if ((mv = canwin(COMPUTER)) != 0)
  60.                     /* --- win if possible --- */
  61.                     setpiece(=mv, COMPUTER);
  62.                 else if ((mv = canwin(PLAYER)) != 0)
  63.                     /* --- block player's win potential --- */
  64.                     setpiece(=mv, COMPUTER);
  65.                 else
  66.                     nextmove();
  67.                 if (won())    {
  68.                     message(1, "I win");
  69.                     break;
  70.                 }
  71.                 moves++;
  72.             }
  73.         }
  74.         message(2, "Play again? (y/n) ");
  75.         ch = getch();
  76.     }
  77. }
  78. /* --- find next available open space for a dumb move --- */
  79. nextmove()
  80. {
  81.     lmv = -1;
  82.     for (i = 0; i < 9; i++)
  83.         if (board[i] == FREE) {
  84.             lmv = i+1;
  85.             setpiece(=lmv, COMPUTER);
  86.             if (canwin(COMPUTER))
  87.                 return;
  88.             setpiece(=lmv, FREE);
  89.         }
  90.     if (lmv != -1)
  91.         setpiece(=lmv, COMPUTER);
  92. }
  93. /* --- get the player's move and post it --- */
  94. getmove()
  95. {
  96.     mv = 0;
  97.     while (mv == 0)    {
  98.         message(0, "Move (1-9)? ");
  99.         mv = getch();
  100.         mv -= '0';
  101.         if (mv < 1 || mv > 9 || board[mv-1] != FREE)    {
  102.             putchar(BELL);
  103.             mv = 0;
  104.         }
  105.     }
  106.     setpiece(=mv, PLAYER);
  107.     return mv;
  108. }
  109. /* ------ test to see if the game has been won ------- */
  110. won()
  111. {
  112.     for (i = 0; i < 8; i++) {
  113.         pl = wins[i][0]-1;
  114.         if (board[pl] == FREE)
  115.             continue;
  116.         for (k = 1; k < 3; k++)
  117.             if (board[pl] != board[wins[i][k]-1])
  118.                 break;
  119.         if (k == 3)
  120.             return TRUE;
  121.     }
  122.     return FALSE;
  123. }
  124. /* --- test to see if a player (n) can win this time; 
  125.        return 0 or winning board position --- */
  126. canwin(n)
  127. {
  128.     for (i = 0; i < 8; i++)
  129.         if ((w = trywin(n, i)) != 0)
  130.             return w;
  131.     return 0;
  132. }
  133. /* ---- test a row, column, or diagonal for a win;
  134.         return 0 or winning board position --- */
  135. trywin(n, wn)
  136. {
  137.     nct = 0;
  138.     zct = 0;
  139.     for (i = 0; i < 3; i++)    {
  140.         pl = wins[wn][i]-1;
  141.         if (board[pl] == FREE)
  142.             zct = i+1;
  143.         else if (board[pl] == n)
  144.             nct++;
  145.     }
  146.     if (nct == 2 && zct)
  147.         return wins[wn][zct-1];
  148.     return 0;
  149. }
  150. /* ------ display the tic-tac-toe board ------ */
  151. displayboard()
  152. {
  153.     ln1 = "   \xb3   \xb3";
  154.     ln2 = "\xc4\xc4\xc4\xc5\xc4\xc4\xc4\xc5\xc4\xc4\xc4";
  155.     ScreenClear();
  156.     for (y = 0; y < 5; y++)    {
  157.         ScreenCursor(LEFT,TOP+y);
  158.         printf((y&1) ? ln2 : ln1);
  159.     }
  160. }
  161. /* ---- set a players mark (O or X) on the board ---- */
  162. setpiece(pos, mark)
  163. {
  164.     board[--pos] = mark;
  165.     col = pos / 3;
  166.     row = pos % 3;
  167.     ScreenCursor(LEFT+row*4+1, TOP+col*2);
  168.     putchar(mark);
  169. }
  170. /* ---- message to opponent ---- */
  171. message(y, msg)
  172. {
  173.     ScreenCursor(LEFT, TOP+8+y);
  174.     printf(msg);
  175. }
  176.  
  177.