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

  1. //
  2.  
  3. // File          : TicTacToe_CellUnit.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_CellUnit;
  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.  
  34.  
  35. type
  36.  
  37.   // Cell of the board.
  38.  
  39.   TicTacToe_Cell = class
  40.  
  41.  
  42.  
  43.   private
  44.  
  45.     // User defined attributes
  46.  
  47.     contents: String   (* = ' ' *);
  48.  
  49.  
  50.  
  51.     // Association attributes
  52.  
  53.     neighbourDict: TClassDict;
  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.     // Default constructor/destructor
  86.  
  87.     constructor Create;
  88.  
  89.     destructor Destroy; override;
  90.  
  91.  
  92.  
  93.     // User defined methods
  94.  
  95.     function getLine(dir: String; x: String): Integer;
  96.  
  97.     function getMaxLine: Integer;
  98.  
  99.  
  100.  
  101.     // Access methods
  102.  
  103.     function getContents: String;
  104.  
  105.     procedure setContents(newContents: String);
  106.  
  107.  
  108.  
  109.     // Association methods
  110.  
  111.     procedure setNeighbour(Direction: String; newTicTacToe_Cell: TicTacToe_Cell);
  112.  
  113.     function getNeighbour(Direction: String): TicTacToe_Cell;
  114.  
  115.     procedure removeNeighbour(Direction: String);
  116.  
  117.  
  118.  
  119.     // Properties
  120.  
  121.  
  122.  
  123.   published
  124.  
  125.     // User defined attributes
  126.  
  127.  
  128.  
  129.     // User defined methods
  130.  
  131.  
  132.  
  133.     // Properties
  134.  
  135.   end;
  136.  
  137.  
  138.  
  139.  
  140.  
  141. implementation
  142.  
  143.  
  144.  
  145.   // Start user include section
  146.  
  147.   // End user include section
  148.  
  149.  
  150.  
  151. constructor TicTacToe_Cell.Create;
  152.  
  153.   // Start user section
  154.  
  155.   // End user section
  156.  
  157. begin
  158.  
  159.   contents := ' ';
  160.  
  161.  
  162.  
  163.   inherited Create;
  164.  
  165.   neighbourDict := TClassDict.Create;
  166.  
  167.   // Start user section
  168.  
  169.   // End user section
  170.  
  171. end;
  172.  
  173.  
  174.  
  175.  
  176.  
  177. destructor TicTacToe_Cell.Destroy;
  178.  
  179.   // Start user section
  180.  
  181.   // End user section
  182.  
  183. begin
  184.  
  185.   // Start user section
  186.  
  187.   // End user section
  188.  
  189.   neighbourDict.Destroy;
  190.  
  191.  
  192.  
  193.   inherited Destroy;
  194.  
  195. end;
  196.  
  197.  
  198.  
  199.  
  200.  
  201. // Returns the highest number of connected cells in a line in a specific
  202.  
  203. // direction, where each cell contains the same specified symbol.
  204.  
  205. function TicTacToe_Cell.getLine(dir: String; x: String): Integer;
  206.  
  207. var
  208.  
  209.    temp : TicTacToe_Cell;
  210.  
  211.    retValue : Integer;
  212.  
  213.  
  214.  
  215. begin
  216.  
  217.   retValue := 0;
  218.  
  219.   if getContents() = 'X' then
  220.  
  221.   begin
  222.  
  223.     retValue := 1;
  224.  
  225.     temp := getNeighbour(dir);
  226.  
  227.     if (temp <> NIL) then
  228.  
  229.       retValue := retValue + temp.getLine(dir, x);
  230.  
  231.   end;
  232.  
  233.   getLine := retValue;
  234.  
  235. end;
  236.  
  237.  
  238.  
  239.  
  240.  
  241. // Returns the highest number of connected cells in a line,
  242.  
  243. // where each cell contains the same symbol as this cell.
  244.  
  245. function TicTacToe_Cell.getMaxLine: Integer;
  246.  
  247. var
  248.  
  249.   MaxValue: Integer;
  250.  
  251.   LineValue: Integer;
  252.  
  253.  
  254.  
  255. begin
  256.  
  257.   MaxValue := getLine('N', getContents()) + getLine('S', getContents()) - 1;
  258.  
  259.   LineValue := getLine('E', getContents()) + getLine('W', getContents()) - 1;
  260.  
  261.   if (MaxValue < LineValue) then MaxValue := LineValue;
  262.  
  263.  
  264.  
  265.   LineValue := getLine('NE', getContents()) + getLine('SW', getContents()) - 1;
  266.  
  267.   if (MaxValue < LineValue) then MaxValue := LineValue;
  268.  
  269.  
  270.  
  271.   LineValue := getLine('NW', getContents()) + getLine('SE', getContents()) - 1;
  272.  
  273.   if (MaxValue < LineValue) then MaxValue := LineValue;
  274.  
  275.  
  276.  
  277.   getMaxLine := MaxValue;
  278.  
  279. end;
  280.  
  281.  
  282.  
  283.  
  284.  
  285. // Do not delete this line -- regeneration marker
  286.  
  287.  
  288.  
  289. function TicTacToe_Cell.getContents: String;
  290.  
  291. begin
  292.  
  293.   getContents := contents;
  294.  
  295. end;
  296.  
  297.  
  298.  
  299.  
  300.  
  301. procedure TicTacToe_Cell.setContents(newContents: String);
  302.  
  303. begin
  304.  
  305.   contents := newContents;
  306.  
  307. end;
  308.  
  309.  
  310.  
  311.  
  312.  
  313. procedure TicTacToe_Cell.setNeighbour(Direction: String; newTicTacToe_Cell: TicTacToe_Cell);
  314.  
  315. begin
  316.  
  317.   if (newTicTacToe_Cell <> NIL) then
  318.  
  319.   begin
  320.  
  321.     neighbourDict.Add(Direction, newTicTacToe_Cell);
  322.  
  323.   end;
  324.  
  325. end;
  326.  
  327.  
  328.  
  329.  
  330.  
  331. function TicTacToe_Cell.getNeighbour(Direction: String): TicTacToe_Cell;
  332.  
  333. begin
  334.  
  335.   getNeighbour := neighbourDict.Item(Direction);
  336.  
  337. end;
  338.  
  339.  
  340.  
  341.  
  342.  
  343. procedure TicTacToe_Cell.removeNeighbour(Direction: String);
  344.  
  345. begin
  346.  
  347.   neighbourDict.RemoveUsingKey(Direction)
  348.  
  349. end;
  350.  
  351.  
  352.  
  353.  
  354.  
  355.  
  356.  
  357. end.
  358.  
  359.