home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / borland / cb / setup / cbuilder / data.z / FOOTMAIN.CPP < prev    next >
C/C++ Source or Header  |  1997-02-28  |  13KB  |  450 lines

  1. //----------------------------------------------------------------------------
  2. //Borland C++Builder
  3. //Copyright (c) 1987, 1997 Borland International Inc. All Rights Reserved.
  4. //----------------------------------------------------------------------------
  5. //---------------------------------------------------------------------------
  6. #include <vcl.h>
  7. #pragma hdrstop
  8.                                               
  9. #include <vcl/mmsystem.hpp>
  10. #include "footmain.h"
  11. #include "instr.h"
  12. //---------------------------------------------------------------------------
  13. #pragma resource "*.dfm"
  14. TForm1 *Form1;
  15. char numbuf[10];
  16. //---------------------------------------------------------------------------
  17. __fastcall TForm1::TForm1(TComponent* Owner)
  18.   : TForm(Owner)
  19. {
  20. }
  21. //---------------------------------------------------------------------------
  22. void __fastcall TForm1::MoveForward(TObject *Sender)
  23. {
  24.     if ((OnOffSwitch->Position == 2) || (LastSack != NULL))
  25.         return;
  26.  
  27.     if (!running)
  28.         TogglePlay(true);
  29.  
  30.     if (Runningback->Left < TACKLEROW3 - 2)
  31.     {
  32.         if (field[rbx+1][rby].player != NULL)
  33.         {
  34.             Sacked(field[rbx+1][rby].player);
  35.             return;
  36.         }
  37.         field[rbx][rby].player = NULL;
  38.         field[rbx][rby].rb = false;
  39.         Runningback->Left += 22;
  40.         rbx++;
  41.     }
  42.     else
  43.     {
  44.         if (field[0][rby].player != NULL)
  45.         {
  46.             Sacked(field[0][rby].player);
  47.             return;
  48.         }
  49.         field[rbx][rby].player = NULL;
  50.         field[rbx][rby].rb = false;
  51.         Runningback->Left = RBLEFT;
  52.         rbx = 0;
  53.     }
  54.  
  55.     YardsToGo--;
  56.     FieldPosition++;
  57.     if (FieldPosition == 100)
  58.     {
  59.         sndPlaySound("td.wav", SND_SYNC);
  60.         running = false;
  61.         ShowField(false);
  62.         Home+=7;
  63.         HomeDisplay->Caption = itoa(Home, numbuf, 10);
  64.         FieldPosition=80;  // Starting position for visitor
  65.         Down = 1;
  66.         YardsToGo = 10;
  67.         Computer->Visible = true;
  68.         return;
  69.     }
  70.     field[rbx][rby].player = Runningback;
  71.     field[rbx][rby].rb = true;
  72. }
  73. //---------------------------------------------------------------------
  74. void __fastcall TForm1::MoveUp(TObject *Sender)
  75. {
  76.     if ((OnOffSwitch->Position == 2) || (LastSack != NULL))
  77.         return;
  78.  
  79.     if (!running)
  80.         TogglePlay(true);
  81.  
  82.     if (Runningback->Top == RBTOP)
  83.         return; // already as up as it gets
  84.  
  85.     if (field[rbx][rby-1].player != NULL)
  86.     {
  87.         Sacked(field[rbx][rby-1].player);
  88.         return;
  89.     }
  90.  
  91.     if (Runningback->Top == RBBOTTOM)
  92.         Runningback->Top = RBMIDDLE;
  93.     else if (Runningback->Top == RBMIDDLE)
  94.         Runningback->Top = RBTOP;
  95.  
  96.     field[rbx][rby].player = NULL;
  97.     field[rbx][rby].rb = false;
  98.     rby--;
  99.     field[rbx][rby].player = Runningback;
  100.     field[rbx][rby].rb = true;
  101. }
  102. //---------------------------------------------------------------------
  103. void __fastcall TForm1::MoveDown(TObject *Sender)
  104. {
  105.     if ((OnOffSwitch->Position == 2) || (LastSack != NULL))
  106.         return;
  107.  
  108.     if (!running)
  109.         TogglePlay(true);
  110.  
  111.     if (Runningback->Top == RBBOTTOM)
  112.         return; // already as down as it gets
  113.  
  114.     if (field[rbx][rby+1].player != NULL)
  115.     {
  116.         Sacked(field[rbx][rby+1].player);
  117.         return;
  118.     }
  119.  
  120.     if (Runningback->Top == RBTOP)
  121.         Runningback->Top = RBMIDDLE;
  122.     else if (Runningback->Top == RBMIDDLE)
  123.         Runningback->Top = RBBOTTOM;
  124.  
  125.     field[rbx][rby].player = NULL;
  126.     field[rbx][rby].rb = false;
  127.     rby++;
  128.     field[rbx][rby].player = Runningback;
  129.     field[rbx][rby].rb = true;
  130. }
  131. //---------------------------------------------------------------------
  132. void __fastcall TForm1::Switched(TObject *Sender)
  133. {
  134.     switch (OnOffSwitch->Position)
  135.     {
  136.             case 1:    {
  137.                     Timer->Interval = 250;
  138.                     HomeDisplay->Visible = true;
  139.                     VisitorDisplay->Visible = true;
  140.                     TimeDisplay->Visible = true;
  141.                     ResetGame(NULL);
  142.                     break;
  143.                 }
  144.                 case 2:    {
  145.                     ShowField(false);
  146.                     HomeDisplay->Visible = false;
  147.                     VisitorDisplay->Visible = false;
  148.                     TimeDisplay->Visible = false;
  149.                     break;
  150.                 }
  151.                 case 3:    {
  152.                     Timer->Interval = 100;
  153.                     HomeDisplay->Visible = true;
  154.                     VisitorDisplay->Visible = true;
  155.                     TimeDisplay->Visible = true;
  156.                     ResetGame(NULL);
  157.                     break;
  158.                 }
  159.         }
  160. }
  161. //---------------------------------------------------------------------
  162. void __fastcall TForm1::ShowField(bool visible)
  163. {
  164.     Runningback->Visible = visible;
  165.     Tackler1->Visible = visible;
  166.     Tackler2->Visible = visible;
  167.     Tackler3->Visible = visible;
  168.     Tackler4->Visible = visible;
  169.     Tackler5->Visible = visible;
  170.  
  171. }
  172. //---------------------------------------------------------------------
  173. void __fastcall TForm1::TimerTimer(TObject *Sender)
  174. {
  175.     int x, y, newx, newy, direction;
  176.  
  177.     newy = y = random(3);
  178.     newx = x = random(10);
  179.     direction = random(2); // 0 is for x, 1 is for y
  180.  
  181.     if (!running)
  182.     {
  183.         if (LastSack != NULL)
  184.             LastSack->Visible = !LastSack->Visible;
  185.         return;
  186.     }
  187.  
  188.     if (field[x][y].rb == true)
  189.         return; // can't move the runningback!
  190.  
  191.     if (field[x][y].player == NULL)
  192.         return; // no tacker at this spot
  193.  
  194.     if (field[x][y].player->Left <= RBLEFT)
  195.         return; // already at the endzone
  196.  
  197.     if (direction == 0)  // we're moving horizontal
  198.     {
  199.         if (x < rbx)
  200.             newx = x +1;
  201.         else if (x > rbx)
  202.             newx = x -1;
  203.         else return;
  204.         //  we're already horizontally lined up with rb
  205.     }
  206.     else if (direction == 1)  // we're moving vertical
  207.     {
  208.         if (y < rby)
  209.             newy = y +1;
  210.         else if (y > rby)
  211.             newy = y -1;
  212.         else return;
  213.         //  we're already vertically lined up with rb
  214.     }
  215.  
  216.     if (field[newx][newy].rb == true) // got him!
  217.     {
  218.         Sacked(field[x][y].player);
  219.         return;
  220.     }
  221.  
  222.     if (field[newx][newy].player == NULL) // not blocked
  223.     {
  224.         field[x][y].player->Left -= (22 * (x - newx));
  225.         field[x][y].player->Top -= (16 * (y - newy));
  226.         field[newx][newy].player = field[x][y].player;
  227.         field[x][y].player = NULL;
  228.         return;
  229.     }
  230. }
  231. //---------------------------------------------------------------------
  232. void __fastcall TForm1::Sacked(TLabel *player)
  233. {
  234.     sndPlaySound("whistle.wav", SND_SYNC);
  235.     running = false;
  236.     LastSack = player;
  237.     if (YardsToGo <=0)
  238.     {
  239.         Down = 1;
  240.         YardsToGo = 10;
  241.     }
  242.     else
  243.     {
  244.         Down++;
  245.         if (Down > 4)
  246.         {
  247.             sndPlaySound("whistle.wav", SND_SYNC);
  248.             Down = 1;  // First down for visitor
  249.             YardsToGo = 10;
  250.             Computer->Visible = true;
  251.         }
  252.     }
  253. }
  254. //---------------------------------------------------------------------
  255. void __fastcall TForm1::ResetGame(TObject *Sender)
  256. {
  257.     YardsToGo=10;
  258.     Down=1;
  259.     FieldPosition=20;
  260.     Home=0;
  261.     Visitor=0;
  262.     Quarter = 1;
  263.     TimeLeft=15;
  264.     TimeDisplay->Caption = FloatToStrF(TimeLeft, ffGeneral, 4, 4);
  265.     HomeDisplay->Caption = itoa(Home, numbuf, 10);
  266.     VisitorDisplay->Caption = itoa(Visitor, numbuf, 10);
  267.     randomize;
  268.     ResetField();
  269. }
  270. //---------------------------------------------------------------------
  271. void __fastcall TForm1::ResetField()
  272. {
  273.     TogglePlay(false);
  274.     LastSack = NULL;
  275.  
  276.     // empty the field
  277.     for (int y=0; y<3; y++)
  278.         for (int x=0; x<10; x++)
  279.         {
  280.             field[x][y].player = NULL;
  281.             field[x][y].rb = false;
  282.         }
  283.  
  284.     // initial locations of players
  285.     field[0][1].player=Runningback;
  286.     field[0][1].rb=true;
  287.     rbx = 0;
  288.     rby = 1;
  289.     field[3][0].player=Tackler1;
  290.     field[3][1].player=Tackler2;
  291.     field[3][2].player=Tackler3;
  292.     field[5][1].player=Tackler4;
  293.     field[9][1].player=Tackler5;
  294.  
  295.     Runningback->Left = RBLEFT;
  296.     Runningback->Top = RBMIDDLE;
  297.     Tackler1->Left = TACKLEROW1;
  298.     Tackler1->Top = TACKLETOP;
  299.     Tackler2->Left = TACKLEROW1;
  300.     Tackler2->Top = TACKLEMIDDLE;
  301.     Tackler3->Left = TACKLEROW1;
  302.     Tackler3->Top = TACKLEBOTTOM;
  303.     Tackler4->Left = TACKLEROW2;
  304.     Tackler4->Top = TACKLEMIDDLE;
  305.     Tackler5->Left = TACKLEROW3;
  306.     Tackler5->Top = TACKLEMIDDLE;
  307.  
  308.     ShowField(true);
  309. }
  310. //---------------------------------------------------------------------
  311. void __fastcall TForm1::Exit1Click(TObject *Sender)
  312. {
  313.     Application->Terminate();  
  314. }
  315. //---------------------------------------------------------------------
  316. void __fastcall TForm1::About1Click(TObject *Sender)
  317. {
  318.     About->ShowModal();
  319. }
  320. //---------------------------------------------------------------------
  321. void __fastcall TForm1::ShowStats(TObject *Sender, TMouseButton Button,
  322.       TShiftState Shift, int X, int Y)
  323. {
  324.     if ((OnOffSwitch->Position == 2) || (running))
  325.         return;
  326.  
  327.     if (Quarter == 5)
  328.         return;
  329.  
  330.     YTGDisplay->Caption = itoa(YardsToGo, numbuf, 10);
  331.  
  332.     if (FieldPosition <= 50)
  333.     {
  334.         itoa(FieldPosition, numbuf, 10);
  335.         strcat(numbuf, " <");
  336.     }
  337.     else
  338.     {
  339.         strcpy(numbuf, "> ");
  340.         itoa(100 - FieldPosition, numbuf + 2, 10);
  341.     }
  342.     FieldPosDisplay->Caption = numbuf;
  343.  
  344.     DownDisplay->Caption = itoa(Down, numbuf, 10);
  345.  
  346.     ShowField(false);
  347.     ShowDisplay(true);
  348. }
  349. //---------------------------------------------------------------------
  350. void __fastcall TForm1::HideStats(TObject *Sender, TMouseButton Button,
  351.       TShiftState Shift, int X, int Y)
  352. {
  353.     if ((OnOffSwitch->Position == 2) || (running))
  354.         return;
  355.  
  356.     ShowDisplay(false);
  357.     if (!Computer->Visible)
  358.         ResetField();
  359. }
  360. //---------------------------------------------------------------------
  361. void __fastcall TForm1::ShowDisplay(bool visible)
  362. {
  363.     DownLabel->Visible = visible;
  364.     FieldPosLabel->Visible = visible;
  365.     YTGLabel->Visible = visible;
  366.     DownDisplay->Visible = visible;
  367.     FieldPosDisplay->Visible = visible;
  368.     YTGDisplay->Visible = visible;
  369. }
  370. //---------------------------------------------------------------------
  371. void __fastcall TForm1::Kick(TObject *Sender)
  372. {
  373.     if ((running) || (Down != 4))
  374.         return;  // button only valid just before fourth down
  375.  
  376.     ShowField(false);
  377.     FieldPosition += random(100);
  378.     if (FieldPosition >= 100)
  379.     {
  380.         sndPlaySound("td.wav", SND_SYNC);
  381.         Home+=3;
  382.         HomeDisplay->Caption = itoa(Home, numbuf, 10);
  383.         FieldPosition=80;  // Starting position for visitor
  384.       }
  385.     else
  386.     {
  387.         sndPlaySound("whistle.wav", SND_SYNC);
  388.         sndPlaySound("whistle.wav", SND_SYNC);
  389.     }
  390.     Down = 1;  // first down for visitor
  391.     YardsToGo = 10;
  392.     Computer->Visible = true;
  393.     LastSack = Runningback; // hack to keep movement keys disabled
  394. }
  395. //---------------------------------------------------------------------
  396. void __fastcall TForm1::ClockTick(TObject *Sender)
  397. {
  398.     if (!running)
  399.         return;
  400.  
  401.     sndPlaySound("tick.wav", SND_ASYNC);
  402.     TimeLeft -= 0.1;
  403.     TimeDisplay->Caption = FloatToStrF(TimeLeft, ffGeneral, 4, 4);
  404.  
  405.     if (TimeLeft <= 0)
  406.     {
  407.         Quarter++;
  408.         TimeLeft = 15;
  409.     }
  410.     if (Quarter >= 5)
  411.     {
  412.         // game over
  413.         sndPlaySound("whistle.wav", SND_SYNC);
  414.         sndPlaySound("whistle.wav", SND_SYNC);
  415.         LastSack = Runningback; // hack to keep movement keys disabled
  416.         TogglePlay(false);
  417.         ShowField(false);
  418.         ShowDisplay(true);
  419.     }
  420. }
  421. //---------------------------------------------------------------------
  422. void __fastcall TForm1::TogglePlay(bool toggle)
  423. {
  424.         running = toggle;
  425.         Timer->Enabled = toggle;
  426.         Clock->Enabled = toggle;
  427. }
  428. //---------------------------------------------------------------------
  429. void __fastcall TForm1::ComputerClick(TObject *Sender)
  430. {
  431.  
  432.     ShowField(false);
  433.     FieldPosition -= random(100);
  434.     if (FieldPosition <= 0)
  435.     {
  436.         sndPlaySound("td.wav", SND_ASYNC);
  437.         Visitor+=7;
  438.         VisitorDisplay->Caption = itoa(Visitor, numbuf, 10);
  439.         FieldPosition=20;
  440.     }
  441.     else
  442.     {
  443.         sndPlaySound("whistle.wav", SND_SYNC);
  444.         sndPlaySound("whistle.wav", SND_SYNC);
  445.     }
  446.     Computer->Visible = false;
  447.     LastSack = Runningback; // hack to keep movement keys disabled
  448. }
  449. //---------------------------------------------------------------------
  450.