home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / games / volume9 / othello2 / part03 / boardstuff.c next >
Encoding:
C/C++ Source or Header  |  1990-04-27  |  6.9 KB  |  287 lines

  1. /*LINTLIBRARY*/
  2.  
  3. /*  @(#)boardstuff.c 1.4 90/04/05
  4.  *
  5.  *  Various board routines used by the othello game.
  6.  *
  7.  *  Original SunView version by Ed Falk - Sun Microsystems Inc.
  8.  *
  9.  *  Rewritten for independent use by
  10.  *          Rich Burridge, Sun Microsystems, Australia
  11.  *
  12.  *  Copyright (c) Ed Falk & Rich Burridge - Sun Microsystems.
  13.  *                                          All rights reserved.
  14.  *
  15.  *  Permission is given to distribute these sources, as long as the
  16.  *  introductory messages are not removed, and no monies are exchanged.
  17.  *
  18.  *  No responsibility is taken for any errors on inaccuracies inherent
  19.  *  either to the comments or the code of this program, but if reported
  20.  *  to me, then an attempt will be made to fix them.
  21.  */
  22.  
  23. #include "color.h"
  24. #include "othello.h"
  25. #include "extern.h"
  26.  
  27.  
  28. animate_move(move)
  29. int move ;
  30. {
  31.   int x0, y0, x1, y1, x, y, dx, dy, ctr ;
  32.  
  33.   lock(IS_ON) ;
  34.   get_xy(move, &x1, &y1) ;
  35.   dx = x1 ;
  36.   dy = y1 ;
  37.   if (x1 > y1)
  38.     {
  39.       ctr = dx / 2 ;
  40.       x = BBORDER ;
  41.       y = BBORDER ;
  42.       draw_piece(WHITE, x, CY+y, RINV) ;
  43.       while (x < x1)
  44.         {
  45.           set_timer() ;
  46.           x0 = x ;
  47.           y0 = y ;
  48.           x += move_delta ;
  49.           if ((ctr -= dy) < 0)
  50.             {
  51.               ctr += dx ;
  52.               y += move_delta ;
  53.             }
  54.           draw_piece(WHITE, x, CY+y, RINV) ;
  55.           draw_piece(WHITE, x0, CY+y0, RINV) ;
  56.           nap_upto(1) ;
  57.         }
  58.       draw_piece(WHITE, x, CY+y, RINV) ;
  59.     }
  60.   else
  61.     { 
  62.       ctr = dy / 2 ;
  63.       x = BBORDER ;
  64.       y = BBORDER ;
  65.       draw_piece(WHITE, x, CY+y, RINV) ;
  66.       while (y < y1)
  67.         {
  68.           set_timer() ;
  69.           x0 = x ;
  70.           y0 = y ;
  71.           y += move_delta ;
  72.           if ((ctr -= dx) < 0)
  73.             {
  74.               ctr += dy ;
  75.               x += move_delta ;
  76.             }
  77.           draw_piece(WHITE, x, CY+y, RINV) ;
  78.           draw_piece(WHITE, x0, CY+y0, RINV) ;
  79.           nap_upto(1) ;
  80.         }
  81.       draw_piece(WHITE, x, CY+y, RINV) ;
  82.     }
  83.   lock(IS_OFF) ;
  84. }
  85.  
  86.  
  87. /*  This routine checks to see if a move can be made for this player.
  88.  *  If not, various checks are made to see if the game is finished.
  89.  *  Return value indicates if a move can be made.
  90.  */
  91.  
  92. check(player, who, be)
  93. int player ;
  94. char *who[], *be[] ;
  95. {
  96.   if ((!count(board, BLACK)) || (!count(board, WHITE)) ||
  97.       ((count(board, BLACK) + count(board, WHITE)) == 64))
  98.     {
  99.       who_wins() ;
  100.       cmode = GAME_OVER ;
  101.       message(PANEL_MES, "Game over") ;
  102.       return(FALSE) ;
  103.     }
  104.   if ((move = makemove(board, player, 0)) == NOMOVE)
  105.     {
  106.       message(PANEL_MES, sprintf(line,"%s %s forced to pass",
  107.                         who[(int) play_mode], be[(int) play_mode])) ;
  108.       if ((move = makemove(board, OPPONENT(player), 0)) == NOMOVE)
  109.         {
  110.           who_wins() ;
  111.           cmode = GAME_OVER ;
  112.           message(PANEL_MES, "Game over") ;
  113.         }
  114.       return(FALSE) ;
  115.     }
  116.   return(TRUE) ;
  117. }
  118.  
  119.  
  120. count(board, player)    /* Count the number of player pieces on the board. */
  121. int player ;
  122. BOARD board ;
  123. {
  124.   int i, n ;
  125.  
  126.   n = 0 ;
  127.   FOR_BOARD(i)
  128.     if (board.square[i] == player) n++ ;
  129.   return(n) ;
  130. }
  131.  
  132.  
  133. do_move(player, who)
  134. int player ;
  135. char *who[] ;
  136. {
  137.   int taken ;                /* Number of pieces flipped this go. */
  138.  
  139.   taken = formfliplist(move, player) ;
  140.   update_board_image() ;
  141.   if (taken == 1)
  142.     message(PANEL_MES, sprintf(line, "%s took 1 piece", who[(int) play_mode])) ;
  143.   else
  144.     message(PANEL_MES, sprintf(line, "%s took %d pieces",
  145.                                       who[(int) play_mode],taken)) ;
  146. }
  147.  
  148.  
  149. formfliplist(move, player)
  150. int move, player ;
  151. {        
  152.   int cnt, i, old_cnt ;
  153.  
  154.   old_cnt = count(board, player) ;
  155.   FOR_BOARD(i) old_board.square[i] = board.square[i] ;
  156.   old_board.moves_left = board.moves_left ;
  157.   domove(&old_board, move, &board, player) ;
  158.   FOR_BOARD(i) moves[63 - board.moves_left].square[i] = board.square[i] ;
  159.   moves[63 - board.moves_left].moves_left = board.moves_left ;
  160.   cnt = count(board, player) ;
  161.   return(cnt - old_cnt - 1) ;
  162. }
  163.  
  164.  
  165. initboard()    /* Initialise the othello board. */
  166. {
  167.   static int ivals[4] = { 27, 28, 35, 36 } ;
  168.   static int icolors[4] = { BLACK, WHITE, WHITE, BLACK } ;
  169.   int i, j , n ;
  170.  
  171.   for (n = 0; n < 4; n++)
  172.     {
  173.       FOR_BOARD(i) moves[n].square[i] = FREE ;
  174.       for (j = 0; j <= n; j++) moves[n].square[ivals[j]] = icolors[j] ;
  175.       moves[n].moves_left = 63 - n ;
  176.     }
  177.  
  178.   FOR_BOARD(i) old_board.square[i] = board.square[i] = FREE ;
  179.   board.square[27] = BLACK ;
  180.   board.square[28] = WHITE ;
  181.   board.square[35] = WHITE ;
  182.   board.square[36] = BLACK ;
  183.   board.moves_left = 60 ;
  184. }
  185.  
  186.  
  187. move_and_check(player, who, be, opp_who, opp_be)
  188. int player ;
  189. char *who[], *be[], *opp_who[], *opp_be[] ;
  190. {
  191.   do_move(player, who) ;
  192.   for (;;)
  193.     if (check(OPPONENT(player), opp_who, opp_be) == TRUE)
  194.       {
  195.         think(OPPONENT(player), opp_who) ;
  196.         if (check(player, who,be) == TRUE) break ;
  197.         if (cmode == GAME_OVER) break ;
  198.       }
  199.     else
  200.       {
  201.         if (cmode != GAME_OVER) cmode = (enum cantype) ((int) cmode - 1) ;
  202.         return ;
  203.       }
  204. }
  205.  
  206.  
  207. nap_upto(n)          /* Sleep upto n microseconds from start of timer. */
  208. int n ;
  209. {
  210.   struct timeval ctp ;
  211.   struct timezone ctzp ;
  212.   long elapsed ;     /* Number of microseconds since timer started. */
  213.  
  214.   GETTIMEOFDAY(&ctp, &ctzp) ;
  215.   elapsed = ((ctp.tv_usec - tp.tv_usec) +
  216.             (ctp.tv_sec - tp.tv_sec) * 1000000L) / 1000 ;
  217.   if (elapsed > n) return ;
  218.   usleep((unsigned) (n - elapsed)) ;
  219. }
  220.  
  221.  
  222. set_timer()
  223. {
  224.   struct timezone tzp ;
  225.  
  226.   GETTIMEOFDAY(&tp, &tzp) ;
  227. }
  228.  
  229.  
  230. show_suggestion()
  231. {
  232.   enum optype rop ;
  233.  
  234.   if (suggestion != -1)
  235.     {
  236.       rop = RCLR ;
  237.       color = (iscolor) ? C_LBROWN : C_WHITE ;
  238.       if (iscolor) rop = RSRC ;
  239.       draw_line(suggest_x-5, CY+suggest_y-5,
  240.                 suggest_x+5, CY+suggest_y+5, rop, color) ;
  241.       draw_line(suggest_x-5, CY+suggest_y+5,
  242.                 suggest_x+5, CY+suggest_y-5, rop, color) ;
  243.       suggestion = -1 ;
  244.     }
  245. }
  246.  
  247.  
  248. update_board_image()
  249. {
  250.   int flips, i, piece, x, y ;
  251.  
  252.   show_suggestion() ;
  253.   for (flips = 0; flips < 4; flips++)
  254.     {
  255.       batch(IS_ON) ;
  256.       FOR_BOARD(i)
  257.         {
  258.           if (board.square[i] != old_board.square[i])
  259.             {
  260.               get_xy(i, &x, &y) ;
  261.               if (i == move) piece = board.square[i] ;
  262.               else
  263.                 piece = (flips % 2) ? board.square[i] : board.square[i] * -1 ;
  264.               draw_piece(piece, x, CY+y, RSRC) ;
  265.             } 
  266.         }
  267.       batch(IS_OFF) ;
  268.       PAUSE ;
  269.     }
  270.   message(SCORE_MES, sprintf(line, "White: %2d, Black: %2d",
  271.                           count(board, WHITE), count(board, BLACK))) ;
  272. }
  273.  
  274.  
  275. who_wins()
  276. {
  277.   int cs, ps ;
  278.  
  279.   ps = count(board, WHITE) ;
  280.   cs = count(board, BLACK) ;
  281.   if (ps > cs) message(SCORE_MES, sprintf(line, "White wins %d-%d", ps, cs)) ;
  282.   else if (ps == cs) message(SCORE_MES, sprintf(line,"A tie %d-%d", ps, cs)) ;
  283.   else message(SCORE_MES, sprintf(line,"Black wins %d-%d", cs, ps)) ;
  284.   if (cretin_flag) message(REMARK_MES, "*** CRETIN! ***") ;
  285.   else message(REMARK_MES, "") ;
  286. }
  287.