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

  1. //
  2.  
  3. // File          : TicTacToe_GameUnit.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 TicTacToe_GameUnit;
  18.  
  19.  
  20.  
  21. interface
  22.  
  23.  
  24.  
  25. uses
  26.  
  27.   // Start user include section
  28.  
  29.   // End user include section
  30.  
  31.   ClassDict,
  32.  
  33.   TicTacToe_CellUnit;
  34.  
  35.  
  36.  
  37. type
  38.  
  39.   // The Tic Tac Toe game itself.
  40.  
  41.   TicTacToe_Game = class
  42.  
  43.  
  44.  
  45.   private
  46.  
  47.     // User defined attributes
  48.  
  49.     turnNumber: Integer;
  50.  
  51.     players: String   (* = 'XO' *);
  52.  
  53.     drawText: String   (* = 'It''s a draw!!!' *);
  54.  
  55.     winText: String   (* = ' is the winner!!!' *);
  56.  
  57.  
  58.  
  59.     // Association attributes
  60.  
  61.     guiRef: Pointer;
  62.  
  63.     boardDict: TClassDict;
  64.  
  65.  
  66.  
  67.     // User defined methods
  68.  
  69.  
  70.  
  71.     // Properties
  72.  
  73.  
  74.  
  75.   protected
  76.  
  77.     // User defined attributes
  78.  
  79.  
  80.  
  81.     // User defined methods
  82.  
  83.  
  84.  
  85.     // Properties
  86.  
  87.  
  88.  
  89.   public
  90.  
  91.     // User defined attributes
  92.  
  93.  
  94.  
  95.     // Default constructor/destructor
  96.  
  97.     constructor Create;
  98.  
  99.     destructor Destroy; override;
  100.  
  101.  
  102.  
  103.     // User defined methods
  104.  
  105.     procedure selectCell(number: Integer);
  106.  
  107.     procedure connectCells;
  108.  
  109.     procedure checkResult(number: Integer);
  110.  
  111.     procedure createCells;
  112.  
  113.     procedure startGame;
  114.  
  115.     function getActivePlayer: String;
  116.  
  117.     procedure clearBoard;
  118.  
  119.  
  120.  
  121.     // Access methods
  122.  
  123.     procedure setWinText(newWinText: String);
  124.  
  125.     procedure setPlayers(newPlayers: String);
  126.  
  127.     function getDrawText: String;
  128.  
  129.     procedure setDrawText(newDrawText: String);
  130.  
  131.     function getTurnNumber: Integer;
  132.  
  133.     procedure setTurnNumber(newTurnNumber: Integer);
  134.  
  135.     function getPlayers: String;
  136.  
  137.     function getWinText: String;
  138.  
  139.  
  140.  
  141.     // Association methods
  142.  
  143.     procedure removeBoard(Index: Integer);
  144.  
  145.     function getGui: Pointer;
  146.  
  147.     function getBoard(Index: Integer): TicTacToe_Cell;
  148.  
  149.     procedure removeGui;
  150.  
  151.     procedure setGui(newTTicTacToe_GUI: Pointer);
  152.  
  153.     procedure setBoard(Index: Integer; newTicTacToe_Cell: TicTacToe_Cell);
  154.  
  155.  
  156.  
  157.     // Properties
  158.  
  159.  
  160.  
  161.   published
  162.  
  163.     // User defined attributes
  164.  
  165.  
  166.  
  167.     // User defined methods
  168.  
  169.  
  170.  
  171.     // Properties
  172.  
  173.   end;
  174.  
  175.  
  176.  
  177.  
  178.  
  179. implementation
  180.  
  181.  
  182.  
  183. uses
  184.  
  185.   // Start user include section
  186.  
  187.   // End user include section
  188.  
  189.   TTicTacToe_GUIUnit;
  190.  
  191.  
  192.  
  193. constructor TicTacToe_Game.Create;
  194.  
  195.   // Start user section
  196.  
  197.   // End user section
  198.  
  199. begin
  200.  
  201.   players := 'XO';
  202.  
  203.   drawText := 'It''s a draw!!!';
  204.  
  205.   winText := ' is the winner!!!';
  206.  
  207.  
  208.  
  209.   inherited Create;
  210.  
  211.   boardDict := TClassDict.Create;
  212.  
  213.   // Start user section
  214.  
  215.   createCells;
  216.  
  217.   connectCells;
  218.  
  219.   // End user section
  220.  
  221. end;
  222.  
  223.  
  224.  
  225.  
  226.  
  227. destructor TicTacToe_Game.Destroy;
  228.  
  229.   // Start user section
  230.  
  231.   // End user section
  232.  
  233. begin
  234.  
  235.   // Start user section
  236.  
  237.   // End user section
  238.  
  239.   removeGui;
  240.  
  241.   boardDict.Destroy;
  242.  
  243.  
  244.  
  245.   inherited Destroy;
  246.  
  247. end;
  248.  
  249.  
  250.  
  251.  
  252.  
  253. // Marks the selected cell and checks to
  254.  
  255. // see if there is a result.
  256.  
  257. procedure TicTacToe_Game.selectCell(number: Integer);
  258.  
  259. var
  260.  
  261.   cell: TicTacToe_Cell;
  262.  
  263. begin
  264.  
  265.   cell := getBoard(number);
  266.  
  267.   cell.setContents(getActivePlayer);
  268.  
  269.   checkResult(number);
  270.  
  271. end;
  272.  
  273.  
  274.  
  275.  
  276.  
  277. // Connects all the cells of the board.
  278.  
  279. procedure TicTacToe_Game.connectCells;
  280.  
  281. var
  282.  
  283.   N, E, W, S: Integer;
  284.  
  285.   i: Integer;
  286.  
  287.   cell: TicTacToe_Cell;
  288.  
  289.  
  290.  
  291. begin
  292.  
  293.   N := 3;
  294.  
  295.   W := -1;
  296.  
  297.   E := -W;
  298.  
  299.   S := -N;
  300.  
  301.  
  302.  
  303.   for i := 1 to 9 do
  304.  
  305.   begin
  306.  
  307.     cell := getBoard(i);
  308.  
  309.     cell.setNeighbour('N', getBoard(i + N));
  310.  
  311.     if ((i mod 3) <> 0) then
  312.  
  313.     begin
  314.  
  315.       cell.setNeighbour('NE', getBoard(i + N + E));
  316.  
  317.       cell.setNeighbour('E', getBoard(i + E));
  318.  
  319.       cell.setNeighbour('SE', getBoard(i + S + E));
  320.  
  321.     end;
  322.  
  323.     cell.setNeighbour('S', getBoard(i + S));
  324.  
  325.     if ((i mod 3) <> 1) then
  326.  
  327.     begin
  328.  
  329.       cell.setNeighbour('SW', getBoard(i + S + W));
  330.  
  331.       cell.setNeighbour('W', getBoard(i + W));
  332.  
  333.       cell.setNeighbour('NW', getBoard(i + N + W));
  334.  
  335.     end;
  336.  
  337.   end;
  338.  
  339. end;
  340.  
  341.  
  342.  
  343.  
  344.  
  345. // Checks if there is a winner or a draw
  346.  
  347. // and displays this result.
  348.  
  349. procedure TicTacToe_Game.checkResult(number: Integer);
  350.  
  351. var
  352.  
  353.   gui: TTicTacToe_GUI;
  354.  
  355.   cell: TicTacToe_Cell;
  356.  
  357. begin
  358.  
  359.   gui := getGUI();
  360.  
  361.   cell := getBoard(number);
  362.  
  363.  
  364.  
  365.   if cell.getMaxLine() = 3 then
  366.  
  367.   begin
  368.  
  369.     gui.disableBoard();
  370.  
  371.     gui.displayResult(getActivePlayer() + getWinText());
  372.  
  373.   end
  374.  
  375.   else
  376.  
  377.   begin
  378.  
  379.     if (getTurnNumber = 9 -1) then
  380.  
  381.     begin
  382.  
  383.       gui.disableBoard();
  384.  
  385.       gui.displayResult(getDrawText());
  386.  
  387.     end;
  388.  
  389.   end;
  390.  
  391.   setTurnNumber(getTurnNumber() + 1);
  392.  
  393. end;
  394.  
  395.  
  396.  
  397.  
  398.  
  399. // Creates all the cells of the board.
  400.  
  401. // 
  402.  
  403. procedure TicTacToe_Game.createCells;
  404.  
  405. var
  406.  
  407.   aCell: TicTacToe_Cell;
  408.  
  409.   i: Integer;
  410.  
  411.  
  412.  
  413. begin
  414.  
  415.   for i := 1 to 9 do
  416.  
  417.   begin
  418.  
  419.     aCell := TicTacToe_Cell.Create;
  420.  
  421.     setBoard(i, aCell);
  422.  
  423.   end;
  424.  
  425. end;
  426.  
  427.  
  428.  
  429.  
  430.  
  431. // Initialize everything for a new game.
  432.  
  433. procedure TicTacToe_Game.startGame;
  434.  
  435. var
  436.  
  437.   gui: TTicTacToe_GUI;
  438.  
  439.  
  440.  
  441. begin
  442.  
  443.   gui := getGUI();
  444.  
  445.  
  446.  
  447.   setTurnNumber(0);
  448.  
  449.   gui.ClearBoard();
  450.  
  451.   Self.ClearBoard();
  452.  
  453.   gui.enableBoard();
  454.  
  455.   gui.displayResult('');
  456.  
  457. end;
  458.  
  459.  
  460.  
  461.  
  462.  
  463. // Determines the active player.
  464.  
  465. function TicTacToe_Game.getActivePlayer: String;
  466.  
  467. begin
  468.  
  469.   getActivePlayer := getPlayers()[turnNumber mod Length(getPlayers()) + 1];
  470.  
  471. end;
  472.  
  473.  
  474.  
  475.  
  476.  
  477. // Clears all the cells of the board.
  478.  
  479. procedure TicTacToe_Game.clearBoard;
  480.  
  481. var
  482.  
  483.    i: Integer;
  484.  
  485.    cell: TicTacToe_Cell;
  486.  
  487. begin
  488.  
  489.   for i := 1 to boardDict.Count do
  490.  
  491.   begin
  492.  
  493.     cell := getBoard(i);
  494.  
  495.     cell.setContents(' ');
  496.  
  497.   end;
  498.  
  499. end;
  500.  
  501.  
  502.  
  503.  
  504.  
  505. // Do not delete this line -- regeneration marker
  506.  
  507.  
  508.  
  509. procedure TicTacToe_Game.setWinText(newWinText: String);
  510.  
  511. begin
  512.  
  513.   winText := newWinText;
  514.  
  515. end;
  516.  
  517.  
  518.  
  519.  
  520.  
  521. procedure TicTacToe_Game.setPlayers(newPlayers: String);
  522.  
  523. begin
  524.  
  525.   players := newPlayers;
  526.  
  527. end;
  528.  
  529.  
  530.  
  531.  
  532.  
  533. function TicTacToe_Game.getDrawText: String;
  534.  
  535. begin
  536.  
  537.   getDrawText := drawText;
  538.  
  539. end;
  540.  
  541.  
  542.  
  543.  
  544.  
  545. procedure TicTacToe_Game.setDrawText(newDrawText: String);
  546.  
  547. begin
  548.  
  549.   drawText := newDrawText;
  550.  
  551. end;
  552.  
  553.  
  554.  
  555.  
  556.  
  557. function TicTacToe_Game.getTurnNumber: Integer;
  558.  
  559. begin
  560.  
  561.   getTurnNumber := turnNumber;
  562.  
  563. end;
  564.  
  565.  
  566.  
  567.  
  568.  
  569. procedure TicTacToe_Game.setTurnNumber(newTurnNumber: Integer);
  570.  
  571. begin
  572.  
  573.   turnNumber := newTurnNumber;
  574.  
  575. end;
  576.  
  577.  
  578.  
  579.  
  580.  
  581. function TicTacToe_Game.getPlayers: String;
  582.  
  583. begin
  584.  
  585.   getPlayers := players;
  586.  
  587. end;
  588.  
  589.  
  590.  
  591.  
  592.  
  593. function TicTacToe_Game.getWinText: String;
  594.  
  595. begin
  596.  
  597.   getWinText := winText;
  598.  
  599. end;
  600.  
  601.  
  602.  
  603.  
  604.  
  605. procedure TicTacToe_Game.removeBoard(Index: Integer);
  606.  
  607. begin
  608.  
  609.   boardDict.RemoveUsingKey(Index)
  610.  
  611. end;
  612.  
  613.  
  614.  
  615.  
  616.  
  617. function TicTacToe_Game.getGui: Pointer;
  618.  
  619. begin
  620.  
  621.   getGui := guiRef;
  622.  
  623. end;
  624.  
  625.  
  626.  
  627.  
  628.  
  629. function TicTacToe_Game.getBoard(Index: Integer): TicTacToe_Cell;
  630.  
  631. begin
  632.  
  633.   getBoard := boardDict.Item(Index);
  634.  
  635. end;
  636.  
  637.  
  638.  
  639.  
  640.  
  641. procedure TicTacToe_Game.removeGui;
  642.  
  643. var
  644.  
  645.   oldTTicTacToe_GUI: TTicTacToe_GUI;
  646.  
  647.  
  648.  
  649. begin
  650.  
  651.   if (guiRef <> NIL) then
  652.  
  653.   begin
  654.  
  655.     oldTTicTacToe_GUI := guiRef;
  656.  
  657.     guiRef := NIL;
  658.  
  659.     oldTTicTacToe_GUI.removeGame();
  660.  
  661.   end;
  662.  
  663. end;
  664.  
  665.  
  666.  
  667.  
  668.  
  669. procedure TicTacToe_Game.setGui(newTTicTacToe_GUI: Pointer);
  670.  
  671. begin
  672.  
  673.   if (newTTicTacToe_GUI <> NIL) then
  674.  
  675.   begin
  676.  
  677.     if (newTTicTacToe_GUI <> guiRef) then
  678.  
  679.     begin
  680.  
  681.       if (guiRef <> NIL) then
  682.  
  683.       begin
  684.  
  685.         TTicTacToe_GUI(guiRef).removeGame;
  686.  
  687.       end;
  688.  
  689.       guiRef := newTTicTacToe_GUI;
  690.  
  691.       TTicTacToe_GUI(newTTicTacToe_GUI).setGame(SELF);
  692.  
  693.     end;
  694.  
  695.   end
  696.  
  697.   else
  698.  
  699.   begin
  700.  
  701.     removeGui;
  702.  
  703.   end;
  704.  
  705. end;
  706.  
  707.  
  708.  
  709.  
  710.  
  711. procedure TicTacToe_Game.setBoard(Index: Integer; newTicTacToe_Cell: TicTacToe_Cell);
  712.  
  713. begin
  714.  
  715.   if (newTicTacToe_Cell <> NIL) then
  716.  
  717.   begin
  718.  
  719.     boardDict.Add(Index, newTicTacToe_Cell);
  720.  
  721.   end;
  722.  
  723. end;
  724.  
  725.  
  726.  
  727.  
  728.  
  729.  
  730.  
  731. end.
  732.  
  733.