w_Case.~pas
上传用户:lhkj2008
上传日期:2010-01-04
资源大小:185k
文件大小:5k
源码类别:

其他智力游戏

开发平台:

Visual C++

  1. unit w_Case;
  2. interface
  3. type
  4.   TWStatus=(Up,Down,Left,Right);
  5.   TWNum = class(TObject)
  6.   private
  7.     FValue: Integer;
  8.   public
  9.     constructor Create(intNum:integer); reintroduce; overload;
  10.     property Value: Integer read FValue;
  11.   end;
  12.   TWCell = class(TObject)
  13.   private
  14.     FCenter: TWNum;
  15.     FDown: TWCell;
  16.     FLeft: TWCell;
  17.     FRight: TWCell;
  18.     FUp: TWCell;
  19.   public
  20.     constructor Create(Num:Integer); reintroduce; overload;
  21.     destructor Destroy; override;
  22.     property Center: TWNum read FCenter;
  23.     property Down: TWCell read FDown write FDown;
  24.     property Left: TWCell read FLeft write FLeft;
  25.     property Right: TWCell read FRight write FRight;
  26.     property Up: TWCell read FUp write FUp;
  27.   end;
  28.   TWCells = class(TObject)
  29.   private
  30.     FActiveCell: TWCell;
  31.     FRowStart: Integer;
  32.     FRowEnd: Integer;
  33.     FColStart: Integer;
  34.     FColEnd: Integer;
  35.     procedure CreateCellsAndNum(Row, Col: integer);
  36.     procedure InitCellsStatus;
  37.     procedure SwapCellPos(SCell,DCell:TWCell);
  38.   public
  39.     Cells: Array of array of TWCell;
  40.     constructor Create(intRow,intCol:integer); reintroduce; overload;
  41.     procedure InitCells(Row,Col:integer);
  42.     procedure Move(Status:TWStatus);
  43.     property RowStart: Integer read FRowStart;
  44.     property RowEnd: Integer read FRowEnd;
  45.     property ColStart: Integer read FColStart;
  46.     property ColEnd: Integer read FColEnd;
  47.   end;
  48. implementation
  49. constructor TWCells.Create(intRow,intCol:integer);
  50. begin
  51.   // TODO -cMM: TWCells.Create default body inserted
  52.   inherited Create;
  53.   InitCells(intRow,intcol);
  54.   FActiveCell:=Cells[0,0];
  55. end;
  56. procedure TWCells.CreateCellsAndNum(Row, Col: integer);
  57. var
  58.   intI,intJ :integer;
  59.   intNum:integer;
  60. begin
  61.   SetLength(Cells,Row,col);
  62.   intNum:=0;
  63.   for intI :=Low(Cells)  to High(Cells) do
  64.   begin
  65.     for intJ :=Low(Cells[intI])  to High(Cells[intI])  do
  66.     begin
  67.       Cells[inti,intj]:=TWCell.Create(intNum);
  68.       intNum:=intNum+1;
  69.     end;
  70.   end;
  71. end;
  72. procedure TWCells.InitCells(Row,Col:integer);
  73. begin
  74.   // TODO -cMM: TWCells.InitCells default body inserted
  75.   assert(Row<>0,'行数不能为空');
  76.   assert(Col<>0,'列数不能为空');
  77.   //构建单元格及数值
  78.   CreateCellsAndNum(Row, Col);
  79.   //初始化每个格子周边状态
  80.   InitCellsStatus;
  81. end;
  82. procedure TWCells.InitCellsStatus;
  83. var
  84.   intI,intJ :integer;
  85. begin
  86.   FRowStart:=Low(Cells);
  87.   FRowEnd:=High(Cells);
  88.   FColStart:=Low(Cells[Low(Cells)]);
  89.   FColEnd:=High(Cells[High(Cells)]);
  90.   for intI :=FRowStart  to FRowEnd do
  91.   begin
  92.     for intJ :=FColStart  to FColEnd  do
  93.     begin
  94.       if intI=FRowStart then
  95.       begin
  96.         Cells[inti,intJ].Up:=nil;
  97.       end
  98.       else
  99.       begin
  100.         Cells[intI,intJ].Up:=Cells[intI-1,intJ];
  101.       end;
  102.       if intI=FRowEnd then
  103.       begin
  104.         Cells[inti,intJ].Down:=nil
  105.       end
  106.       else
  107.       begin
  108.         Cells[intI,intJ].Down:=cells[intI+1,intJ];
  109.       end;
  110.       if intJ=FColStart then
  111.       begin
  112.         Cells[intI,intJ].Left:=nil
  113.       end
  114.       else
  115.       begin
  116.         Cells[intI,intJ].Left:=Cells[intI,intJ-1];
  117.       end;
  118.       if intJ=FColEnd Then
  119.       begin
  120.         Cells[intI,intJ].Right:=nil
  121.       end
  122.       else
  123.       begin
  124.         Cells[intI,intJ].Right :=Cells[intI,intJ+1];
  125.       end;
  126.     end;
  127.   end;
  128. end;
  129. procedure TWCells.Move(Status:TWStatus);
  130. begin
  131.   // TODO -cMM: TWCells.Move default body inserted
  132.     case Status of
  133.       Up:
  134.       begin
  135.         if FActiveCell.Up <>nil then
  136.         begin
  137.          SwapCellPos(FActiveCell,FactiveCell.Up);
  138.          FactiveCell:=FActiveCell.Up;
  139.         end;
  140.       end;
  141.       Down:
  142.       begin
  143.         if FActiveCell.Down <>nil then
  144.         begin
  145.           SwapCellPos(FActiveCell,FActiveCell.Down);
  146.           FactiveCell:=FActiveCell.Down;
  147.         end;
  148.       end;
  149.       Left:
  150.       begin
  151.         if FActiveCell.Left <>nil then
  152.         begin
  153.           SwapCellPos(FActiveCell,FactiveCell.Left );
  154.           FactiveCell:=FActiveCell.Left;
  155.         end;
  156.       end;
  157.       Right:
  158.       begin
  159.         if FActiveCell.Right <>nil then
  160.         begin
  161.          SwapCellPos(FactiveCell,FactiveCell.Right);
  162.          FactiveCell:=FActiveCell.Right;
  163.         end;
  164.       end;
  165.     end;
  166. end;
  167. procedure TWCells.SwapCellPos(SCell,DCell:TWCell);
  168. var
  169.   TempNum:TWNum;
  170. begin
  171.   // TODO -cMM: TWCells.SwapCell default body inserted
  172.   TempNum:=SCell.Center;
  173.   SCell.FCenter :=DCell.FCenter;
  174.   DCell.FCenter :=TempNum;
  175. end;
  176. constructor TWCell.Create(Num:Integer);
  177. begin
  178.   // TODO -cMM: TWCell.Create default body inserted
  179.   inherited create;
  180.   FCenter:=TWNum.Create(Num);
  181. end;
  182. destructor TWCell.Destroy;
  183. begin
  184.   // TODO -cMM: TWCell.Destroy default body inserted
  185.   FCenter.Free;
  186.   inherited;
  187. end;
  188. constructor TWNum.Create(intNum:integer);
  189. begin
  190.   // TODO -cMM: TWNum.Create default body inserted
  191.   inherited Create;
  192.   FValue:=intNum;
  193. end;
  194. end.