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

  1. //
  2.  
  3. // File          : TicTacToe_ComputerUnit.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_ComputerUnit;
  18.  
  19.  
  20.  
  21. interface
  22.  
  23.  
  24.  
  25. uses
  26.  
  27.   // Start user include section
  28.  
  29.   // End user include section
  30.  
  31.   TicTacToe_GameUnit;
  32.  
  33.  
  34.  
  35. type
  36.  
  37.   // The computer opponent.
  38.  
  39.   TicTacToe_Computer = class
  40.  
  41.  
  42.  
  43.   private
  44.  
  45.     // User defined attributes
  46.  
  47.     id: String   (* = '' *);
  48.  
  49.  
  50.  
  51.     // Association attributes
  52.  
  53.     guiRef: Pointer;
  54.  
  55.  
  56.  
  57.     // User defined methods
  58.  
  59.  
  60.  
  61.     // Properties
  62.  
  63.  
  64.  
  65.   protected
  66.  
  67.     // User defined attributes
  68.  
  69.  
  70.  
  71.     // User defined methods
  72.  
  73.  
  74.  
  75.     // Properties
  76.  
  77.  
  78.  
  79.   public
  80.  
  81.     // User defined attributes
  82.  
  83.  
  84.  
  85.     // Association attributes
  86.  
  87.     gameRef: TicTacToe_Game;
  88.  
  89.  
  90.  
  91.     // Default constructor/destructor
  92.  
  93.     constructor Create(newgame: TicTacToe_Game);
  94.  
  95.     destructor Destroy; override;
  96.  
  97.  
  98.  
  99.     // User defined methods
  100.  
  101.     function getMaxValue(x: Integer): Integer;
  102.  
  103.     procedure checkTurn;
  104.  
  105.     procedure selectCell;
  106.  
  107.  
  108.  
  109.     // Access methods
  110.  
  111.     procedure setId(newId: String);
  112.  
  113.     function getId: String;
  114.  
  115.  
  116.  
  117.     // Association methods
  118.  
  119.     function getGui: Pointer;
  120.  
  121.     procedure removeGui;
  122.  
  123.     procedure setGame(newTicTacToe_Game: TicTacToe_Game);
  124.  
  125.     function getGame: TicTacToe_Game;
  126.  
  127.     procedure setGui(newTTicTacToe_GUI: Pointer);
  128.  
  129.  
  130.  
  131.     // Properties
  132.  
  133.  
  134.  
  135.   published
  136.  
  137.     // User defined attributes
  138.  
  139.  
  140.  
  141.     // User defined methods
  142.  
  143.  
  144.  
  145.     // Properties
  146.  
  147.   end;
  148.  
  149.  
  150.  
  151.  
  152.  
  153. implementation
  154.  
  155.  
  156.  
  157. uses
  158.  
  159.   // Start user include section
  160.  
  161.   TicTacToe_CellUnit,
  162.  
  163.   // End user include section
  164.  
  165.   TTicTacToe_GUIUnit,
  166.  
  167.   SysUtils;
  168.  
  169.  
  170.  
  171. constructor TicTacToe_Computer.Create(newgame: TicTacToe_Game);
  172.  
  173.   // Start user section
  174.  
  175.   // End user section
  176.  
  177. begin
  178.  
  179.   id := '';
  180.  
  181.  
  182.  
  183.   if (newgame <> NIL) then
  184.  
  185.   begin
  186.  
  187.     gameRef := newgame;
  188.  
  189.   end
  190.  
  191.   else
  192.  
  193.     raise EInvalidOp.Create('Object newgame has mandatory relation. NIL object reference not allowed.');
  194.  
  195.   inherited Create;
  196.  
  197.   // Start user section
  198.  
  199.   // End user section
  200.  
  201. end;
  202.  
  203.  
  204.  
  205.  
  206.  
  207. destructor TicTacToe_Computer.Destroy;
  208.  
  209.   // Start user section
  210.  
  211.   // End user section
  212.  
  213. begin
  214.  
  215.   // Start user section
  216.  
  217.   // End user section
  218.  
  219.   removeGui;
  220.  
  221.  
  222.  
  223.   inherited Destroy;
  224.  
  225. end;
  226.  
  227.  
  228.  
  229.  
  230.  
  231. // Determines the strategic value of the specified empty cell.
  232.  
  233. function TicTacToe_Computer.getMaxValue(x: Integer): Integer;
  234.  
  235. var
  236.  
  237.   i, retValue, temp: Integer;
  238.  
  239.   game: TicTacToe_Game;
  240.  
  241.   cell: TicTacToe_Cell;
  242.  
  243.  
  244.  
  245. begin
  246.  
  247.   game := getGame();
  248.  
  249.   retValue := -1;
  250.  
  251.  
  252.  
  253.   cell := game.getBoard(x);
  254.  
  255.   for i := 1 to Length(game.getPlayers()) do
  256.  
  257.   begin
  258.  
  259.     cell.setContents(game.getPlayers()[i]);
  260.  
  261.     temp := cell.getMaxLine();
  262.  
  263.     if (retValue < temp) or (retValue = -1) then
  264.  
  265.       retValue := temp;
  266.  
  267.   end;
  268.  
  269.   cell.setContents(' ');
  270.  
  271.   getMaxValue := retValue;
  272.  
  273. end;
  274.  
  275.  
  276.  
  277.  
  278.  
  279. // Checks if it's its turn.
  280.  
  281. procedure TicTacToe_Computer.checkTurn;
  282.  
  283. begin
  284.  
  285.   if getId() = TicTacToe_Game (getGame()).getActivePlayer then selectCell;
  286.  
  287. end;
  288.  
  289.  
  290.  
  291.  
  292.  
  293. // Chooses a good empty cell.
  294.  
  295. procedure TicTacToe_Computer.selectCell;
  296.  
  297. var
  298.  
  299.   i, maxi, temp: Integer;
  300.  
  301.   selection: Integer;
  302.  
  303.   game: TicTacToe_Game;
  304.  
  305.   gui: TTicTacToe_GUI;
  306.  
  307.   cell: TicTacToe_Cell;
  308.  
  309.  
  310.  
  311. begin
  312.  
  313.   game := getGame();
  314.  
  315.   gui := getGUI();
  316.  
  317.   maxi := 0;
  318.  
  319.   selection := -1;
  320.  
  321.  
  322.  
  323.   for i:= 1 to 9 do
  324.  
  325.   begin
  326.  
  327.     cell := game.getBoard(i);
  328.  
  329.     if cell.getContents() = ' ' then
  330.  
  331.     begin
  332.  
  333.       temp := getMaxValue(i);
  334.  
  335.       if (maxi < temp) or (selection = -1) then
  336.  
  337.       begin
  338.  
  339.         maxi := temp;
  340.  
  341.         selection := i;
  342.  
  343.       end;
  344.  
  345.     end;
  346.  
  347.   end;
  348.  
  349.   if selection <> -1 then
  350.  
  351.     gui.cellButtonClick(selection);
  352.  
  353. end;
  354.  
  355.  
  356.  
  357.  
  358.  
  359. // Do not delete this line -- regeneration marker
  360.  
  361.  
  362.  
  363. procedure TicTacToe_Computer.setId(newId: String);
  364.  
  365. begin
  366.  
  367.   id := newId;
  368.  
  369. end;
  370.  
  371.  
  372.  
  373.  
  374.  
  375. function TicTacToe_Computer.getId: String;
  376.  
  377. begin
  378.  
  379.   getId := id;
  380.  
  381. end;
  382.  
  383.  
  384.  
  385.  
  386.  
  387. function TicTacToe_Computer.getGui: Pointer;
  388.  
  389. begin
  390.  
  391.   getGui := guiRef;
  392.  
  393. end;
  394.  
  395.  
  396.  
  397.  
  398.  
  399. procedure TicTacToe_Computer.removeGui;
  400.  
  401. var
  402.  
  403.   oldTTicTacToe_GUI: TTicTacToe_GUI;
  404.  
  405.  
  406.  
  407. begin
  408.  
  409.   if (guiRef <> NIL) then
  410.  
  411.   begin
  412.  
  413.     oldTTicTacToe_GUI := guiRef;
  414.  
  415.     guiRef := NIL;
  416.  
  417.     oldTTicTacToe_GUI.removeComputer();
  418.  
  419.   end;
  420.  
  421. end;
  422.  
  423.  
  424.  
  425.  
  426.  
  427. procedure TicTacToe_Computer.setGame(newTicTacToe_Game: TicTacToe_Game);
  428.  
  429. begin
  430.  
  431.   if (newTicTacToe_Game <> NIL) then
  432.  
  433.   begin
  434.  
  435.     gameRef := newTicTacToe_Game;
  436.  
  437.   end;
  438.  
  439. end;
  440.  
  441.  
  442.  
  443.  
  444.  
  445. function TicTacToe_Computer.getGame: TicTacToe_Game;
  446.  
  447. begin
  448.  
  449.   getGame := gameRef;
  450.  
  451. end;
  452.  
  453.  
  454.  
  455.  
  456.  
  457. procedure TicTacToe_Computer.setGui(newTTicTacToe_GUI: Pointer);
  458.  
  459. begin
  460.  
  461.   if (newTTicTacToe_GUI <> NIL) then
  462.  
  463.   begin
  464.  
  465.     if (newTTicTacToe_GUI <> guiRef) then
  466.  
  467.     begin
  468.  
  469.       if (guiRef <> NIL) then
  470.  
  471.       begin
  472.  
  473.         TTicTacToe_GUI(guiRef).removeComputer;
  474.  
  475.       end;
  476.  
  477.       guiRef := newTTicTacToe_GUI;
  478.  
  479.       TTicTacToe_GUI(newTTicTacToe_GUI).setComputer(SELF);
  480.  
  481.     end;
  482.  
  483.   end
  484.  
  485.   else
  486.  
  487.   begin
  488.  
  489.     removeGui;
  490.  
  491.   end;
  492.  
  493. end;
  494.  
  495.  
  496.  
  497.  
  498.  
  499.  
  500.  
  501. end.
  502.  
  503.