home *** CD-ROM | disk | FTP | other *** search
/ Más de 2,500 Juegos / CD1.iso / ZIPDAT / 0032 / 0032.ZIP / CHESSCLK.CPP < prev    next >
C/C++ Source or Header  |  1996-10-23  |  13KB  |  476 lines

  1. /****************************** Chess clock program ************************
  2.       FULL-FEATURED CHESS CLOCK
  3.       Large digital displays of clocks
  4.                Move counter
  5.                White and Black clocks separately settable
  6.                Times from 1 minute to 9 hours
  7.                Low-time warning (optional)
  8.                Flashing "tick" indicator (optional)
  9.       Flag for active player's clock
  10.       Audible player changeover indicator
  11.       Pause control
  12.       NEW: Optional "overtime" tracking in Word Game Mode
  13. ****************************************************************************/
  14. // M\Cooper, PO Box 237, St. David, AZ 85630-0237
  15. // E-mail: thegrendel@theriver.com
  16. // Web:  http://personal.riverusers.com/~thegrendel/
  17.  
  18.  
  19. #include <stdio.h>
  20. #include <time.h>
  21. #include <conio.h>
  22. #include <stdlib.h>
  23. #include <graphics.h>
  24. #include <dos.h>
  25. #include "oscr.hpp"
  26. #include "chessclk.hpp"
  27.  
  28. #define XXPOS 60
  29. #define YYPOS 37
  30. // Base coordinates for time display
  31.  
  32. #define PX   197
  33. #define PY   130
  34. // Coordinates for "pause" message
  35.  
  36. #define XWIDTH 580
  37. #define YHEIGHT 25
  38. #define XXMARGIN 50
  39. #define LXMARGIN 26
  40. // *Whose_clock_is_running* flag coordinates
  41.  
  42. #define DEFAULT_TIME 2
  43. #define DEFAULT_TEST() if( hrs==0 && min == 0 ) hrs = DEFAULT_TIME
  44.  
  45. //Word Game Mode Indicator Message
  46. #define X_m 260
  47. #define Y_m 30
  48. #define wgm_color MAGENTA
  49. char wgm_message [] = "Word Game Mode";
  50.  
  51.    FLAG ScrabFLAG = OFF;
  52.    int wpsecs = 0,
  53.        bpsecs = 0;
  54.      
  55.  
  56. void main( int argc, char **argv )
  57. {
  58.       if( argc > 1 && ( *( *(argv+1) ) == 's' || *( *(argv+1) ) == 'S' ) )
  59.           ScrabFLAG = ON; //Wanna play wordgames?
  60.  
  61.    randomize();
  62.    opening_screen();
  63.    play();
  64. }
  65.  
  66. void CountdownTimer::clock_on()
  67. {
  68.    time_t prev_sec;
  69.    int ch;
  70.  
  71.    toFLAG = OFF;
  72.    pause_t = 0;
  73.  
  74.       if( p == WHITE_ )
  75.      text_color = LIGHTBLUE;
  76.       else
  77.      text_color = DARKGRAY;
  78.  
  79.       setcolor( text_color );
  80.       settextstyle( TRIPLEX_FONT, HORIZ_DIR, 5 );
  81.       settextjustify( LEFT_TEXT, TOP_TEXT );
  82.  
  83.      ShowFlag();
  84.  
  85.  
  86.       /**************INITIALIZE**********************/
  87.       startn_t = time ( NULL );  //Click on stopwatch.
  88.       /**********************************************/
  89.  
  90.       gotoxy( XXPOS * p + 1, YYPOS );
  91.       outtextxy( NAME_POS * p, 100, player [p] );
  92.       display_time();  //Otherwise initial time not displayed...
  93.  
  94.       while( !( ch = kbhit() ) )
  95.          {
  96. /**************************(1)***************************/
  97.  
  98.      prev_sec = seconds;
  99.      if( running_flag ) //Is this 2nd or later lap?
  100.         interval_t = time( NULL ) - startn_t;
  101.      else
  102.         interval_t = time( NULL ) - start_t;
  103.  
  104. /**********************XXXXX**********PAUSE-TIME**********************/
  105.     if( !overtime_flag )
  106.       running_t = total_seconds - interval_t - pause_t;
  107.     else //if overtime
  108.       {
  109.       running_t =  interval_t - total_seconds - pause_t;
  110.       text_color = LIGHTRED;
  111.       }
  112. //                                              ^^^^^^^
  113.  
  114.     if( overtime_flag && running_flag )
  115.       running_t = interval_t + total_seconds - pause_t;
  116. //            
  117.      convert( running_t );
  118.  
  119.      if( seconds - prev_sec )
  120.         {
  121.         display_time();
  122.  
  123.         if( !seconds )
  124.            if( minutes == warning )
  125.           if( !hours )
  126.              if( time_warning_flag )
  127.             blatt();
  128.  
  129.         if( visual_ticking_flag ) // Show blinking box ticks?
  130.            {
  131.            setfillstyle( random ( PATTERNS ), random ( COLORS ) );
  132.            setcolor ( random ( COLORS ) );
  133.            setlinestyle( SOLID_LINE, 0xFFF, NORM_WIDTH );
  134.            bar( X_C - RADIUS, Y_C - RADIUS,
  135.             X_C + RADIUS, Y_C + RADIUS );
  136.            }
  137.         }
  138.  
  139.      if( timeout() && !ScrabFLAG )
  140.         exit_();
  141.   
  142. /******************(2)******************/
  143.   if( timeout() && ScrabFLAG && !toFLAG )
  144.      {
  145.      overtime_flag = ON;
  146.  
  147.      startn_t = time( NULL );
  148.      toFLAG = ON;
  149.  
  150.      if( running_flag)
  151.         total_seconds = 0; /*********reset******/
  152.  
  153.      erase_numbers();
  154.      }
  155.  
  156.      }
  157.  
  158.       total_seconds = hours * 3600 + minutes * 60 + seconds;
  159.       running_flag = ON;  // Remember that clock was already running.
  160.  
  161.       
  162.       ch = getch(); //Retrieve keypress.
  163.       if( ch == ESC )
  164.           {
  165.            if( overtime_flag && p == WHITE_ )
  166.                wpsecs = total_seconds; 
  167.            if( overtime_flag && p == BLACK_ )
  168.                bpsecs = total_seconds;
  169.  
  170.            exit__(); //Quit.
  171.           }
  172.            
  173.  
  174.       if( ch == 'p' || ch == 'P' )
  175.          {
  176.            if( overtime_flag && p == WHITE_ )
  177.                wpsecs = total_seconds; 
  178.            if( overtime_flag && p == BLACK_ )
  179.                bpsecs = total_seconds;
  180.  
  181.          pause();
  182.           }
  183.  
  184.  
  185.            if( overtime_flag && p == WHITE_ )
  186.                wpsecs = total_seconds; 
  187.            if( overtime_flag && p == BLACK_ )
  188.                bpsecs = total_seconds;
  189.  
  190.  
  191.       return;
  192.  
  193. }
  194.  
  195. void CountdownTimer::display_moves()
  196. {
  197.    char buf[ 5 ];
  198.    static char ebuf[ 5 ];
  199.  
  200.       if( moves > 1 )
  201.      {
  202.      setcolor( WHITE );
  203.      settextstyle( TRIPLEX_FONT, HORIZ_DIR, 1 );
  204.      settextjustify( CENTER_TEXT, TOP_TEXT );
  205.      outtextxy( MOVES_X, MOVES_Y, ebuf );
  206.      }
  207.  
  208.       sprintf( buf, "%003d", moves );
  209.       sprintf( ebuf, buf );
  210.       setcolor( GREEN );
  211.       settextstyle( TRIPLEX_FONT, HORIZ_DIR, 1 );
  212.       settextjustify( CENTER_TEXT, TOP_TEXT );
  213.       outtextxy( MOVES_X, MOVES_Y, buf );
  214.       
  215.       return;
  216. }
  217.  
  218. void graphics_setup( int background_color )
  219. {
  220.    int grdriver = VGA,
  221.        grmode = VGAHI;
  222.  
  223.        registerfarbgidriver( EGAVGA_driver_far );
  224.        registerfarbgifont( gothic_font_far );
  225.        registerfarbgifont( triplex_font_far );
  226.        initgraph( &grdriver, &grmode, "" );
  227.        setbkcolor( background_color );
  228.  
  229. // Note: Borland BGI graphics drivers are completely
  230. //       incompatible with Microsloth Wimpdows.
  231. //       ============
  232.  
  233. }
  234.  
  235.  
  236. /*************************FINAL STATS?**************************/
  237. void exit__()
  238. {
  239.       closegraph();
  240.  
  241.       if( ScrabFLAG )
  242.          {
  243.            printf( "White has %d minute(s) and %d second(s) of overtime.\n",
  244.                     wpsecs/60, wpsecs%60 );
  245.            printf( "Penalty points for White = %d.\n\n",
  246.                      wpsecs/6 );
  247.  
  248.            printf( "Black has %d minute(s) and %d second(s) of overtime.\n",
  249.                     bpsecs/60, bpsecs%60 );
  250.            printf( "Penalty points for Black = %d.\n\n",
  251.                      bpsecs/6 );
  252.          }
  253.      
  254.       exit( QUIT );
  255. }
  256.  
  257.     /***************Routine to erase old numbers*************/
  258. void CountdownTimer::erase_numbers()
  259. {
  260.       setcolor ( WHITE ); 
  261.  
  262.       if( seconds == 59 || ( seconds == 0 && ScrabFLAG ) )
  263.      outtextxy( p * BLK_TIME, Y_TIMEPOS, line_clear );
  264.       else
  265.      if( seconds == 9 || seconds == 19 || seconds == 29
  266.          || seconds == 39 || seconds == 49
  267.       || ( seconds == 10 && ScrabFLAG )
  268.       || ( seconds == 20 && ScrabFLAG )
  269.       || ( seconds == 30 && ScrabFLAG )
  270.       || ( seconds == 40 && ScrabFLAG )
  271.       || ( seconds == 50 && ScrabFLAG )  )
  272.         outtextxy( p * BLK_TIME + POS1_OFFSET, Y_TIMEPOS,
  273.                line_clear + 6 );
  274.       else
  275.          outtextxy( p * BLK_TIME + POS_OFFSET, Y_TIMEPOS,
  276.             line_clear + 7 ); 
  277.  
  278.      return;
  279.  
  280. }
  281.  
  282.  
  283. void play()
  284. {
  285.    int hrs,
  286.        min;
  287.    char inputstr[ MAXLEN ],
  288.     inp;
  289.  
  290.       clrscr();
  291.  
  292.       textcolor ( LIGHTCYAN );
  293.       cprintf( "\n                              WHITE hours: " );
  294.       gets( inputstr );
  295.       hrs = atoi( inputstr );
  296.       cprintf( "                              WHITE minutes: " );
  297.       gets( inputstr );
  298.       min = atoi( inputstr );
  299.       DEFAULT_TEST();
  300.       CountdownTimer t1( hrs, min, WHITE_ );
  301.  
  302.       textcolor( RED );
  303.       cprintf( "\n                              BLACK hours: " );
  304.       gets( inputstr );
  305.       hrs = atoi( inputstr );
  306.       cprintf( "                              BLACK minutes: " );
  307.       gets( inputstr );
  308.       min = atoi( inputstr );
  309.       DEFAULT_TEST();
  310.       CountdownTimer t2( hrs, min, BLACK_ );
  311.  
  312.       textcolor( YELLOW );
  313.       cprintf( "\n\n                         Enable flashing clock ticks? " );
  314.