home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Entertainment / Concentration / ScoreBoard.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-25  |  3.7 KB  |  200 lines  |  [TEXT/KAHL]

  1. /*
  2.  * ScoreBoard.c - ScoreBoard operations
  3.  */
  4.  
  5. # include    "TransSkel.h"
  6.  
  7. # include    "Concentration.h"
  8.  
  9.  
  10. /*
  11.  * Calculate rectangle for player slot.  Slot -1 is for the title.
  12.  */
  13.  
  14. static void
  15. ScoreBoardPlayerRect (short i, Rect *rp)
  16. {
  17.     SetRect (rp, 0, 0, sbWidth, sbSlotHeight - 10);
  18.     OffsetRect (rp, sbHOffset, sbVOffset + (i + 1) * sbSlotHeight);
  19. }
  20.  
  21.  
  22. /*
  23.  * Calculate rectangle for player name.
  24.  */
  25.  
  26. static void
  27. ScoreBoardPlayerNameRect (short i, Rect *rp)
  28. {
  29.     SetRect (rp, 0, 0, sbNameWidth, sbSlotHeight - 10);
  30.     OffsetRect (rp, sbHOffset, sbVOffset + (i + 1) * sbSlotHeight);
  31. }
  32.  
  33.  
  34. /*
  35.  * Calculate rectangle for player score.
  36.  */
  37.  
  38. static void
  39. ScoreBoardPlayerScoreRect (short i, Rect *rp)
  40. {
  41.     SetRect (rp, 0, 0, sbScoreWidth, sbSlotHeight - 10);
  42.     OffsetRect (rp, sbHOffset + sbNameWidth, sbVOffset + (i + 1) * sbSlotHeight);
  43. }
  44.  
  45.  
  46. /*
  47.  * Draw player info (player number and name).  If n is current player,
  48.  * draw a frame around name and score.
  49.  *
  50.  * If n >= nPlayers, erase the scoreboard slot.  This allows scoreboard
  51.  * redrawing easily, when the number of players is reduced in the Players
  52.  * dialog.
  53.  */
  54.  
  55. void
  56. DrawPlayerInfo (short n)
  57. {
  58. Str255    s;
  59. Rect    r;
  60. short    i;
  61.  
  62.     if (n >= sb->nPlayers)
  63.     {
  64.         ScoreBoardPlayerRect (n, &r);
  65.         EraseRect (&r);
  66.         return;
  67.     }
  68.     ScoreBoardPlayerNameRect (n, &r);
  69.     TextBox (sb->name[n] + 1, (long) sb->name[n][0], &r, teJustLeft);
  70.     NumToString ((long) sb->score[n], s);
  71.     ScoreBoardPlayerScoreRect (n, &r);
  72.     TextBox (s + 1, (long) s[0], &r, teJustRight);
  73.     ScoreBoardPlayerRect (n, &r);
  74.     if (n != sb->player)
  75.         PenMode (patBic);
  76.     InsetRect (&r, -1, -1);
  77.     FrameRect (&r);
  78.     InsetRect (&r, -2, -2);
  79.     FrameRect (&r);
  80.     PenNormal ();
  81. }
  82.  
  83.  
  84. void
  85. DrawScoreBoard (void)
  86. {
  87. StringPtr    str = "\pScoreboard";
  88. Rect    r, r2;
  89. short    i;
  90.  
  91.     ScoreBoardPlayerRect (-1, &r);
  92.     ScoreBoardPlayerRect (maxPlayers - 1, &r2);
  93.     r2.top = r.top;
  94.     InsetRect (&r2, -8, -8);
  95.     FrameRect (&r2);
  96.     InsetRect (&r2, 2, 2);
  97.     PenSize (2, 2);
  98.     FrameRect (&r2);
  99.     PenNormal ();
  100.     
  101.     TextBox (str + 1, (long) str[0], &r, teJustCenter);
  102.  
  103.     for (i = 0; i < maxPlayers; i++)
  104.         DrawPlayerInfo (i);
  105. }
  106.  
  107.  
  108. void
  109. FlashPlayerFrame (short n)
  110. {
  111. Rect    r;
  112. short    i;
  113.  
  114.     ScoreBoardPlayerRect (n, &r);
  115.     InsetRect (&r, -1, -1);
  116.     for (i = 0; i < 2; i++)
  117.     {
  118.         SkelPause (8L);
  119.         PenMode (patBic);
  120.         FrameRect (&r);
  121.         InsetRect (&r, -2, -2);
  122.         FrameRect (&r);
  123.         SkelPause (8L);
  124.         PenMode (patCopy);
  125.         FrameRect (&r);
  126.         InsetRect (&r, 2, 2);
  127.         FrameRect (&r);
  128.     }
  129.     PenNormal ();
  130. }
  131.  
  132.  
  133. /*
  134.  * Initialize player scores to zero and select first player.  The first
  135.  * player rotates to the next player after the previous first player
  136.  * unless no moves had been made in the previous game.
  137.  */
  138.  
  139. void
  140. ResetScoreBoard (Boolean advanceFirstPlayer)
  141. {
  142. short    i;
  143.  
  144.     for (i = 0; i < sb->nPlayers; ++i)
  145.         sb->score[i] = 0;
  146.  
  147.     if (advanceFirstPlayer)
  148.     {
  149.         if (++sb->firstPlayer >= sb->nPlayers)
  150.             sb->firstPlayer = 0;
  151.     }
  152.     sb->player = sb->firstPlayer;
  153. }
  154.  
  155.  
  156. /*
  157.  * Call this when the game is over.  It determines which player has the highest
  158.  * score (if not a tie), and makes that the current player in order to frame the
  159.  * score.
  160.  */
  161.  
  162. /* THIS FUNCTION IS NOT ACTUALLY USED */
  163.  
  164. void
  165. HiliteWinner (void)
  166. {
  167. short    i, n, highScore, highScorePlayer;
  168.  
  169.     n = sb->player;
  170.     sb->player = -1;
  171.     DrawPlayerInfo (n);        /* erase frame around current player */
  172.     /*
  173.      * determine high score
  174.      */
  175.     highScore = 0;
  176.     for (i = 0; i < sb->nPlayers; i++)
  177.     {
  178.         if (highScore < sb->score[i])
  179.             highScore = sb->score[i];
  180.     }
  181.     /*
  182.      * determine how many players have the high score and the last
  183.      * player having that score.
  184.      */
  185.     n = 0;
  186.     for (i = 0; i < sb->nPlayers; i++)
  187.     {
  188.         if (sb->score[i] == highScore)
  189.         {
  190.             ++n;
  191.             highScorePlayer = i;
  192.         }
  193.     }
  194.     if (n == 1)                /* frame player if only one with high score */
  195.     {
  196.         sb->player = highScorePlayer;
  197.         DrawPlayerInfo (sb->player);
  198.         FlashPlayerFrame (sb->player);
  199.     }
  200. }