home *** CD-ROM | disk | FTP | other *** search
/ PC World 1998 October / PCWorld_1998-10_cd.bin / software / prehled / komix / DATA.Z / TTicTacToe_GUIUnit.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1997-08-20  |  8.9 KB  |  390 lines

  1. //
  2.  
  3. // File          : TTicTacToe_GUIUnit.pas
  4.  
  5. //
  6.  
  7. // Project       : PCProject
  8.  
  9. // Configuration : PCConfig 1
  10.  
  11. // Phase         : Implementation 1
  12.  
  13. // System        : TicTacToe 1
  14.  
  15. //
  16.  
  17. unit TTicTacToe_GUIUnit;
  18.  
  19.  
  20.  
  21. interface
  22.  
  23.  
  24.  
  25. uses
  26.  
  27.   // Start user include section
  28.  
  29.   // End user include section
  30.  
  31.   Menus,
  32.  
  33.   ExtCtrls,
  34.  
  35.   StdCtrls,
  36.  
  37.   Forms,
  38.  
  39.   Classes,
  40.  
  41.   TicTacToe_GameUnit,
  42.  
  43.   TicTacToe_ComputerUnit;
  44.  
  45.  
  46.  
  47. type
  48.  
  49.   // Graphical User Interface of the game.
  50.  
  51.   TTicTacToe_GUI = class(TForm)
  52.  
  53.  
  54.  
  55.     // Visual Components
  56.  
  57.     CellButton3: TButton (* Component:TicTacToe_CellButton *);
  58.  
  59.     CellButton8: TButton (* Component:TicTacToe_CellButton *);
  60.  
  61.     Board: TPanel (* Component:TicTacToe_Board *);
  62.  
  63.     New: TMenuItem (* Component:TicTacToe_New *);
  64.  
  65.     CellButton5: TButton (* Component:TicTacToe_CellButton *);
  66.  
  67.     Result: TLabel (* Component:TicTacToe_Result *);
  68.  
  69.     ComputerX: TMenuItem (* Component:TicTacToe_Opponent *);
  70.  
  71.     CellButton2: TButton (* Component:TicTacToe_CellButton *);
  72.  
  73.     CellButton7: TButton (* Component:TicTacToe_CellButton *);
  74.  
  75.     MainMenu: TMainMenu (* Component:TicTacToe_MainMenu *);
  76.  
  77.     GameMenu: TMenuItem (* Component:TicTacToe_GameMenu *);
  78.  
  79.     OpponentMenu: TMenuItem (* Component:TicTacToe_OpponentMenu *);
  80.  
  81.     CellButton4: TButton (* Component:TicTacToe_CellButton *);
  82.  
  83.     CellButton9: TButton (* Component:TicTacToe_CellButton *);
  84.  
  85.     Human: TMenuItem (* Component:TicTacToe_Opponent *);
  86.  
  87.     CellButton1: TButton (* Component:TicTacToe_CellButton *);
  88.  
  89.     ComputerO: TMenuItem (* Component:TicTacToe_Opponent *);
  90.  
  91.     CellButton6: TButton (* Component:TicTacToe_CellButton *);
  92.  
  93.  
  94.  
  95.     // Events methods
  96.  
  97.     procedure CellButton1Click(Sender: TObject);
  98.  
  99.     procedure CellButton3Click(Sender: TObject);
  100.  
  101.     procedure HumanClick(Sender: TObject);
  102.  
  103.     procedure CellButton4Click(Sender: TObject);
  104.  
  105.     procedure CellButton5Click(Sender: TObject);
  106.  
  107.     procedure CellButton6Click(Sender: TObject);
  108.  
  109.     procedure CellButton7Click(Sender: TObject);
  110.  
  111.     procedure CellButton8Click(Sender: TObject);
  112.  
  113.     procedure CellButton9Click(Sender: TObject);
  114.  
  115.     procedure NewClick(Sender: TObject);
  116.  
  117.     procedure ComputerOClick(Sender: TObject);
  118.  
  119.     procedure ComputerXClick(Sender: TObject);
  120.  
  121.     procedure CellButton2Click(Sender: TObject);
  122.  
  123.  
  124.  
  125.   private
  126.  
  127.     // User defined attributes
  128.  
  129.     CellButtons: array [1..9] of TButton;
  130.  
  131.  
  132.  
  133.     // Association attributes
  134.  
  135.     computerRef: TicTacToe_Computer;
  136.  
  137.     gameRef: TicTacToe_Game;
  138.  
  139.  
  140.  
  141.     // User defined methods
  142.  
  143.  
  144.  
  145.     // Properties
  146.  
  147.  
  148.  
  149.   protected
  150.  
  151.     // User defined attributes
  152.  
  153.  
  154.  
  155.     // User defined methods
  156.  
  157.  
  158.  
  159.     // Properties
  160.  
  161.  
  162.  
  163.   public
  164.  
  165.     // User defined attributes
  166.  
  167.  
  168.  
  169.     // Default constructor/destructor
  170.  
  171.     constructor Create(AOwner: TComponent); override;
  172.  
  173.     destructor Destroy; override;
  174.  
  175.  
  176.  
  177.     // User defined methods
  178.  
  179.     procedure enableBoard;
  180.  
  181.     procedure displayResult(text: String);
  182.  
  183.     procedure disableBoard;
  184.  
  185.     procedure cellButtonClick(index: Integer);
  186.  
  187.     procedure clearBoard;
  188.  
  189.  
  190.  
  191.     // Association methods
  192.  
  193.     procedure setGame(newTicTacToe_Game: TicTacToe_Game);
  194.  
  195.     function getGame: TicTacToe_Game;
  196.  
  197.     procedure removeComputer;
  198.  
  199.     function getComputer: TicTacToe_Computer;
  200.  
  201.     procedure setComputer(newTicTacToe_Computer: TicTacToe_Computer);
  202.  
  203.     procedure removeGame;
  204.  
  205.  
  206.  
  207.     // Properties
  208.  
  209.  
  210.  
  211.   published
  212.  
  213.     // User defined attributes
  214.  
  215.  
  216.  
  217.     // User defined methods
  218.  
  219.  
  220.  
  221.     // Properties
  222.  
  223.   end;
  224.  
  225.  
  226.  
  227. // Class feature attributes
  228.  
  229. var
  230.  
  231.   TicTacToe_GUI: TTicTacToe_GUI   (* Form attribute *);
  232.  
  233.  
  234.  
  235.  
  236.  
  237. implementation
  238.  
  239.  
  240.  
  241.   // Start user include section
  242.  
  243.   // End user include section
  244.  
  245.  
  246.  
  247. {$R *.DFM}
  248.  
  249.  
  250.  
  251. constructor TTicTacToe_GUI.Create(AOwner: TComponent);
  252.  
  253.   // Start user section
  254.  
  255.   // End user section
  256.  
  257. begin
  258.  
  259.  
  260.  
  261.   inherited Create(AOwner);
  262.  
  263.   // Start user section
  264.  
  265.   setGame(TicTacToe_Game.Create);
  266.  
  267.   setComputer(TicTacToe_Computer.Create(getGame()));
  268.  
  269.   CellButtons[1] := CellButton1;
  270.  
  271.   CellButtons[2] := CellButton2;
  272.  
  273.   CellButtons[3] := CellButton3;
  274.  
  275.   CellButtons[4] := CellButton4;
  276.  
  277.   CellButtons[5] := CellButton5;
  278.  
  279.   CellButtons[6] := CellButton6;
  280.  
  281.   CellButtons[7] := CellButton7;
  282.  
  283.   CellButtons[8] := CellButton8;
  284.  
  285.   CellButtons[9] := CellButton9;
  286.  
  287.  
  288.  
  289.   TicTacToe_Game(getGame()).startGame;
  290.  
  291.   // End user section
  292.  
  293. end;
  294.  
  295.  
  296.  
  297.  
  298.  
  299. destructor TTicTacToe_GUI.Destroy;
  300.  
  301.   // Start user section
  302.  
  303.   // End user section
  304.  
  305. begin
  306.  
  307.   // Start user section
  308.  
  309.   // End user section
  310.  
  311.   removeComputer;
  312.  
  313.   removeGame;
  314.  
  315.  
  316.  
  317.   inherited Destroy;
  318.  
  319. end;
  320.  
  321.  
  322.  
  323.  
  324.  
  325. // Enables all the buttons of the board.
  326.  
  327. procedure TTicTacToe_GUI.enableBoard;
  328.  
  329. var
  330.  
  331.   i: Integer;
  332.  
  333.  
  334.  
  335. begin
  336.  
  337.   for i := 1 to 9 do
  338.  
  339.     if CellButtons[i].Caption = ' ' then
  340.  
  341.       CellButtons[i].Enabled := True;
  342.  
  343. end;
  344.  
  345.  
  346.  
  347.  
  348.  
  349. // Displays the specified result.
  350.  
  351. procedure TTicTacToe_GUI.displayResult(text: String);
  352.  
  353. begin
  354.  
  355.   Result.Caption := text;
  356.  
  357. end;
  358.  
  359.  
  360.  
  361.  
  362.  
  363. // Disables all the buttons of the board.
  364.  
  365. procedure TTicTacToe_GUI.disableBoard;
  366.  
  367. var
  368.  
  369.   i: Integer;
  370.  
  371. begin
  372.  
  373.   for i := 1 to 9 do
  374.  
  375.     CellButtons[i].Enabled := False;
  376.  
  377. end;
  378.  
  379.  
  380.  
  381.  
  382.  
  383. // Checks if cell is empty and sets it.
  384.  
  385. procedure TTicTacToe_GUI.cellButtonClick(index: Integer);
  386.  
  387. begin
  388.  
  389.   if CellButtons[index].Enabled then
  390.  
  391.   begin
  392.  
  393.     CellButtons[index].Enabled := False;
  394.  
  395.     CellButtons[index].Caption := gameRef.getActivePlayer();
  396.  
  397.     gameRef.selectCell(index);
  398.  
  399.     computerRef.checkTurn();
  400.  
  401.   end;
  402.  
  403. end;
  404.  
  405.  
  406.  
  407.  
  408.  
  409. // Clears the contents of all the buttons of the board.
  410.  
  411. procedure TTicTacToe_GUI.clearBoard;
  412.  
  413. var
  414.  
  415.   i: Integer;
  416.  
  417. begin
  418.  
  419.   for i := 1 to 9 do
  420.  
  421.     CellButtons[i].Caption := ' ';
  422.  
  423. end;
  424.  
  425.  
  426.  
  427.  
  428.  
  429. procedure TTicTacToe_GUI.CellButton1Click(Sender: TObject);
  430.  
  431. begin
  432.  
  433.   cellButtonClick(1);
  434.  
  435. end;
  436.  
  437.  
  438.  
  439.  
  440.  
  441. procedure TTicTacToe_GUI.CellButton3Click(Sender: TObject);
  442.  
  443. begin
  444.  
  445.   cellButtonClick(3);
  446.  
  447. end;
  448.  
  449.  
  450.  
  451.  
  452.  
  453. // Sets opponent.
  454.  
  455. procedure TTicTacToe_GUI.HumanClick(Sender: TObject);
  456.  
  457. begin
  458.  
  459.   Human.Checked := True;
  460.  
  461.   computerRef.setId('');
  462.  
  463.   computerRef.checkTurn;
  464.  
  465. end;
  466.  
  467.  
  468.  
  469.  
  470.  
  471. procedure TTicTacToe_GUI.CellButton4Click(Sender: TObject);
  472.  
  473. begin
  474.  
  475.   cellButtonClick(4);
  476.  
  477. end;
  478.  
  479.  
  480.  
  481.  
  482.  
  483. procedure TTicTacToe_GUI.CellButton5Click(Sender: TObject);
  484.  
  485. begin
  486.  
  487.   cellButtonClick(5);
  488.  
  489. end;
  490.  
  491.  
  492.  
  493.  
  494.  
  495. procedure TTicTacToe_GUI.CellButton6Click(Sender: TObject);
  496.  
  497. begin
  498.  
  499.   cellButtonClick(6);
  500.  
  501. end;
  502.  
  503.  
  504.  
  505.  
  506.  
  507. procedure TTicTacToe_GUI.CellButton7Click(Sender: TObject);
  508.  
  509. begin
  510.  
  511.   cellButtonClick(7);
  512.  
  513. end;
  514.  
  515.  
  516.  
  517.  
  518.  
  519. procedure TTicTacToe_GUI.CellButton8Click(Sender: TObject);
  520.  
  521. begin
  522.  
  523.   cellButtonClick(8);
  524.  
  525. end;
  526.  
  527.  
  528.  
  529.  
  530.  
  531. procedure TTicTacToe_GUI.CellButton9Click(Sender: TObject);
  532.  
  533. begin
  534.  
  535.   cellButtonClick(9);
  536.  
  537. end;
  538.  
  539.  
  540.  
  541.  
  542.  
  543. // Starts new game.
  544.  
  545. procedure TTicTacToe_GUI.NewClick(Sender: TObject);
  546.  
  547. begin
  548.  
  549.   gameRef.startGame;
  550.  
  551.   computerRef.checkTurn;
  552.  
  553. end;
  554.  
  555.  
  556.  
  557.  
  558.  
  559. // Sets opponent.
  560.  
  561. procedure TTicTacToe_GUI.ComputerOClick(Sender: TObject);
  562.  
  563. begin
  564.  
  565.   ComputerO.Checked := True;
  566.  
  567.   computerRef.setId('O');
  568.  
  569.   computerRef.checkTurn;
  570.  
  571. end;
  572.  
  573.  
  574.  
  575.  
  576.  
  577. // Sets opponent.
  578.  
  579. procedure TTicTacToe_GUI.ComputerXClick(Sender: TObject);
  580.  
  581. begin
  582.  
  583.   ComputerX.Checked := True;
  584.  
  585.   computerRef.setId('X');
  586.  
  587.   computerRef.checkTurn;
  588.  
  589. end;
  590.  
  591.  
  592.  
  593.  
  594.  
  595. procedure TTicTacToe_GUI.CellButton2Click(Sender: TObject);
  596.  
  597. begin
  598.  
  599.   cellButtonClick(2);
  600.  
  601. end;
  602.  
  603.  
  604.  
  605.  
  606.  
  607. // Do not delete this line -- regeneration marker
  608.  
  609.  
  610.  
  611. procedure TTicTacToe_GUI.setGame(newTicTacToe_Game: TicTacToe_Game);
  612.  
  613. begin
  614.  
  615.   if (newTicTacToe_Game <> NIL) then
  616.  
  617.   begin
  618.  
  619.     if (newTicTacToe_Game <> gameRef) then
  620.  
  621.     begin
  622.  
  623.       if (gameRef <> NIL) then
  624.  
  625.       begin
  626.  
  627.         gameRef.removeGui;
  628.  
  629.       end;
  630.  
  631.       gameRef := newTicTacToe_Game;
  632.  
  633.       newTicTacToe_Game.setGui(SELF);
  634.  
  635.     end;
  636.  
  637.   end
  638.  
  639.   else
  640.  
  641.   begin
  642.  
  643.     removeGame;
  644.  
  645.   end;
  646.  
  647. end;
  648.  
  649.  
  650.  
  651.  
  652.  
  653. function TTicTacToe_GUI.getGame: TicTacToe_Game;
  654.  
  655. begin
  656.  
  657.   getGame := gameRef;
  658.  
  659. end;
  660.  
  661.  
  662.  
  663.  
  664.  
  665. procedure TTicTacToe_GUI.removeComputer;
  666.  
  667. var
  668.  
  669.   oldTicTacToe_Computer: TicTacToe_Computer;
  670.  
  671.  
  672.  
  673. begin
  674.  
  675.   if (computerRef <> NIL) then
  676.  
  677.   begin
  678.  
  679.     oldTicTacToe_Computer := computerRef;
  680.  
  681.     computerRef := NIL;
  682.  
  683.     oldTicTacToe_Computer.removeGui();
  684.  
  685.   end;
  686.  
  687. end;
  688.  
  689.  
  690.  
  691.  
  692.  
  693. function TTicTacToe_GUI.getComputer: TicTacToe_Computer;
  694.  
  695. begin
  696.  
  697.   getComputer := computerRef;
  698.  
  699. end;
  700.  
  701.  
  702.  
  703.  
  704.  
  705. procedure TTicTacToe_GUI.setComputer(newTicTacToe_Computer: TicTacToe_Computer);
  706.  
  707. begin
  708.  
  709.   if (newTicTacToe_Computer <> NIL) then
  710.  
  711.   begin
  712.  
  713.     if (newTicTacToe_Computer <> computerRef) then
  714.  
  715.     begin
  716.  
  717.       if (computerRef <> NIL) then
  718.  
  719.       begin
  720.  
  721.         computerRef.removeGui;
  722.  
  723.       end;
  724.  
  725.       computerRef := newTicTacToe_Computer;
  726.  
  727.       newTicTacToe_Computer.setGui(SELF);
  728.  
  729.     end;
  730.  
  731.   end
  732.  
  733.   else
  734.  
  735.   begin
  736.  
  737.     removeComputer;
  738.  
  739.   end;
  740.  
  741. end;
  742.  
  743.  
  744.  
  745.  
  746.  
  747. procedure TTicTacToe_GUI.removeGame;
  748.  
  749. var
  750.  
  751.   oldTicTacToe_Game: TicTacToe_Game;
  752.  
  753.  
  754.  
  755. begin
  756.  
  757.   if (gameRef <> NIL) then
  758.  
  759.   begin
  760.  
  761.     oldTicTacToe_Game := gameRef;
  762.  
  763.     gameRef := NIL;
  764.  
  765.     oldTicTacToe_Game.removeGui();
  766.  
  767.   end;
  768.  
  769. end;
  770.  
  771.  
  772.  
  773.  
  774.  
  775.  
  776.  
  777. end.
  778.  
  779.