home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / walnut-creek-CDROM / SIMTEL / CPMUG / CPMUG048.ARK / TTT.C < prev    next >
Text File  |  1984-04-29  |  6KB  |  255 lines

  1.  
  2. /*
  3.     BD Software presents:
  4.  
  5.     Tic Tac Toe  (by exhaustive search)
  6.  
  7.     written by Leor Zolman
  8.            September 1979
  9.  
  10. */
  11.  
  12.  
  13. #define X 1                /* The pieces on the board */
  14. #define O 5
  15. #define EMPTY 0
  16.  
  17. char board[9];                /* the playing board */
  18. char BEST;                /* move returned by "ttteval" */
  19. int wins[8][3];                /* table of  winning positions */
  20.  
  21.  
  22. main()
  23. {
  24.     int i;
  25.     int mefirst;            /* 1: computer goes first; 0: human */
  26.     int turn;            /* counts number of moves played */
  27.     int mypiece, hispiece;        /* who has x or o */
  28.     int mywins, hiswins, catwins;    /* game count */
  29.     int t;
  30.     srand(0);            /* initialize random generator */
  31.     initw(wins,"0,1,2,3,4,5,6,7,8,0,3,6,1,4,7,2,5,8,0,4,8,2,4,6");
  32.     mywins = hiswins = catwins = 0;
  33.     mefirst = 1;            /* let human go first */
  34.     printf("\n\nWelcome to BDS Tic Tac Toe!!\n");
  35.     printf("(In this version, X always goes first.\n");
  36.     printf("The board is arranged as follows:");
  37.     do {
  38.       mefirst = !mefirst;        /* reverse who goes first */
  39.       turn = 0;
  40.       display(1);
  41.       printf(mefirst ? "I go first...\n":
  42.                "You go first...\n");
  43.       clear();            /* clear the game board */
  44.            mypiece = mefirst ? X : O;     /* set who has got what */
  45.       hispiece  = mefirst ? O : X;
  46.       if (!mefirst) goto hismove;
  47.  
  48.  mymove: if (turn == 0)            /* Opening move may be random */
  49.         BEST = rand() % 9;
  50.       else if (turn == 1) {      /* Response to opener: */
  51.         if (board[4])
  52.             BEST = rand() % 2 * 6 +
  53.             rand() % 2 * 2;
  54.         else BEST = 4;
  55.        }
  56.       else {            /* OK, we're into the game... */
  57.         t = ttteval(mypiece,hispiece);
  58.         if (t == 1) printf("I've got ya!\n");
  59.         }
  60.        board[BEST] = mypiece;      /* make the move */
  61.        ++turn;
  62.        display(0);
  63.        if (win(mypiece)) {
  64.         ++mywins;
  65.         printf("\nI win!!!\n");
  66.         continue;
  67.         }
  68.        if (cats()) {
  69.         ++catwins;
  70.         printf("\nMeee-ow!\n");
  71.         continue;
  72.         }
  73.  
  74.   hismove: printf("Your move (1-9) ? ");
  75.        i = getchar() - 0x30;
  76.        if (i < 1 || i > 9 || board[i-1]) {
  77.         printf("\nIllegal!\n");
  78.         goto hismove;
  79.         }
  80.        board[i-1] = hispiece;
  81.        ++turn;
  82.        display(0);
  83.        if (win(hispiece)) {
  84.         ++hiswins;
  85.         printf("\nYou beat me!!\n");
  86.         continue;
  87.         }
  88.        if (cats()) {
  89.         ++catwins;
  90.         printf("\nOne for Morris.\n");
  91.         continue;
  92.         }
  93.        goto mymove;
  94.  
  95.     } while (ask("\nAnother game (y/n) ? "));
  96.  
  97.     printf("\n\nOK...Final score:\n");
  98.     printf("You won %d game",hiswins);
  99.     if (hiswins != 1) putchar('s');
  100.     printf(";\nI won %d game",mywins);
  101.     if (mywins != 1) putchar('s');
  102.     printf(";\nThe Cat got %d game",catwins);
  103.     if (catwins != 1) putchar('s');
  104.     printf(".\nSee ya later!!\n");
  105. }
  106.  
  107.  
  108. /*
  109.     The function "ttteval" returns an evaluation of
  110.     player x's position on the tic-tac-toe board. If he's
  111.     in a winning position, 1 is returned. If the best he
  112.     can hope for is a cat's game, 0 is returned. And, if
  113.     he's sure to lose if player y plays correctly, -1 is
  114.     returned. In any case, the best move available to 
  115.     player x is left in external variable BEST upon return
  116.     from ttteval.
  117.  
  118.     Note that a value of -1 is often returned while
  119.     recursive searching branches down into all possible
  120.     wins and losses, but with the program in the shape
  121.     it appears here, the outermost-level call to ttteval
  122.     (from the main program) will never produce a return
  123.     value of -1, since the computer has decided not to be
  124.     able to lose (the obviously logical choice of any
  125.     self-respecting problem-solver.)
  126.     In any case, the main program still bothers to 
  127.     consider the possibility of losing, so that if you
  128.     want to try inserting your own "ttteval" routine
  129.     in place of the one given, it dosn't have to be
  130.     perfect in order to work with the main program.
  131.     Tic-tac-toe is, of course, just about the only game
  132.     in which it is feasable to do an exhaustive search;
  133.     most game evaluation algorithms only go so deep and
  134.     then make a "static" evaluation, or estimate of the
  135.     player's status at the terminal position. That is how
  136.     all decent chess playing programs today work.
  137. */
  138.  
  139. ttteval(me,him)
  140. {
  141.     char i,safemove;
  142.     int v,loseflag;
  143.         /* first check for the 3 simple terminal
  144.            conditions:                */
  145.     if (win(me)) return 1;
  146.     if (win(him)) return -1;
  147.     if (cats()) return 0;
  148.         /* OK...now try all possible moves and see
  149.            our honorable opponent can do with each: */
  150.     loseflag = 1;
  151.     for (i=0; i<9; ++i) {
  152.         if (board[i]) continue; /* ignore non-moves! */
  153.         board[i] = me;        /* try the move...*/
  154.         v = ttteval(him,me);
  155.         board[i] = 0;        /* restore the empty space */
  156.         if (v == -1) {         /* if we force a loss, yey! */
  157.             BEST = i;
  158.             return 1;
  159.          }
  160.         if (v) continue;       /* uh oh! we shouldn't get beaten! */
  161.         loseflag = 0;        /* cat's game guaranteed at least */
  162.         safemove = i;
  163.      }
  164.     BEST = safemove;
  165.     return -loseflag;        /* if loseflag is true, this returns
  166.                        -1 to indicate a loss; else zero
  167.                        is returned (cat's game.)    */
  168. }
  169.  
  170. /*
  171.     This function returns true (non-zero) if player p
  172.     has three in a row on the board:
  173. */
  174.  
  175. win(p)
  176. {
  177.     char i;
  178.     for (i=0; i<8; ++i)
  179.         if (board[wins[i][0]] == p &&
  180.             board[wins[i][1]] == p &&
  181.             board[wins[i][2]] == p) return 1;
  182.     return 0;
  183. }
  184.  
  185.  
  186. /*
  187.     This function returns true if all nine squares are
  188.     taken (usually called after checking for a win, so
  189.     that all spaces filled indicates a cat's game)
  190. */
  191.  
  192. cats()
  193. {
  194.     char i;
  195.     for (i=0; i<9; ++i)
  196.         if (!board[i]) return 0;
  197.     return 1;
  198. }
  199.  
  200. /*
  201.     Function to clear the game board
  202. */
  203.  
  204. clear()
  205. {
  206.     char i;
  207.     for (i=0; i<9; ++i)
  208.         board[i] = EMPTY;
  209. }
  210.  
  211. /*
  212.     This one returns true if the player responds
  213.     positively to the prompt string, else returns false
  214.     (a good general-purpose yes/no asking function!)
  215. */
  216.  
  217. ask(s)
  218. char *s;
  219. {
  220.     char *gets(), c;
  221.     printf(s);
  222.     c = toupper(getchar());
  223.     return (c == 'Y');
  224. }
  225.  
  226. /*
  227.     Display the playing board with pieces if flag is 0,
  228.     or else with each square numbered from 1-9.
  229. */
  230.  
  231. display(flag)
  232. {
  233.     int i,j;
  234.     printf("\n\n");
  235.     for (i=0; i<9; i+=3) {
  236.         for (j=i; j< i+3; ++j) {
  237.             putchar(' ');
  238.             if (!flag)
  239.               switch(board[j]) {
  240.                 case EMPTY: putchar(' ');
  241.                     break;
  242.                 case X:    putchar('x');
  243.                     break;
  244.                 case O:    putchar('o');
  245.                }
  246.             else printf("%1d",j+1);
  247.             putchar(' ');
  248.             if (j != i+2) putchar('|');
  249.          }
  250.         putchar('\n');
  251.         if (i != 6) printf("---+---+---\n");
  252.      }
  253.     putchar('\n');
  254. }
  255.