w_Case.~pas
资源名称:八数码问题的源代码.rar [点击查看]
上传用户:lhkj2008
上传日期:2010-01-04
资源大小:185k
文件大小:5k
源码类别:
其他智力游戏
开发平台:
Visual C++
- unit w_Case;
- interface
- type
- TWStatus=(Up,Down,Left,Right);
- TWNum = class(TObject)
- private
- FValue: Integer;
- public
- constructor Create(intNum:integer); reintroduce; overload;
- property Value: Integer read FValue;
- end;
- TWCell = class(TObject)
- private
- FCenter: TWNum;
- FDown: TWCell;
- FLeft: TWCell;
- FRight: TWCell;
- FUp: TWCell;
- public
- constructor Create(Num:Integer); reintroduce; overload;
- destructor Destroy; override;
- property Center: TWNum read FCenter;
- property Down: TWCell read FDown write FDown;
- property Left: TWCell read FLeft write FLeft;
- property Right: TWCell read FRight write FRight;
- property Up: TWCell read FUp write FUp;
- end;
- TWCells = class(TObject)
- private
- FActiveCell: TWCell;
- FRowStart: Integer;
- FRowEnd: Integer;
- FColStart: Integer;
- FColEnd: Integer;
- procedure CreateCellsAndNum(Row, Col: integer);
- procedure InitCellsStatus;
- procedure SwapCellPos(SCell,DCell:TWCell);
- public
- Cells: Array of array of TWCell;
- constructor Create(intRow,intCol:integer); reintroduce; overload;
- procedure InitCells(Row,Col:integer);
- procedure Move(Status:TWStatus);
- property RowStart: Integer read FRowStart;
- property RowEnd: Integer read FRowEnd;
- property ColStart: Integer read FColStart;
- property ColEnd: Integer read FColEnd;
- end;
- implementation
- constructor TWCells.Create(intRow,intCol:integer);
- begin
- // TODO -cMM: TWCells.Create default body inserted
- inherited Create;
- InitCells(intRow,intcol);
- FActiveCell:=Cells[0,0];
- end;
- procedure TWCells.CreateCellsAndNum(Row, Col: integer);
- var
- intI,intJ :integer;
- intNum:integer;
- begin
- SetLength(Cells,Row,col);
- intNum:=0;
- for intI :=Low(Cells) to High(Cells) do
- begin
- for intJ :=Low(Cells[intI]) to High(Cells[intI]) do
- begin
- Cells[inti,intj]:=TWCell.Create(intNum);
- intNum:=intNum+1;
- end;
- end;
- end;
- procedure TWCells.InitCells(Row,Col:integer);
- begin
- // TODO -cMM: TWCells.InitCells default body inserted
- assert(Row<>0,'行数不能为空');
- assert(Col<>0,'列数不能为空');
- //构建单元格及数值
- CreateCellsAndNum(Row, Col);
- //初始化每个格子周边状态
- InitCellsStatus;
- end;
- procedure TWCells.InitCellsStatus;
- var
- intI,intJ :integer;
- begin
- FRowStart:=Low(Cells);
- FRowEnd:=High(Cells);
- FColStart:=Low(Cells[Low(Cells)]);
- FColEnd:=High(Cells[High(Cells)]);
- for intI :=FRowStart to FRowEnd do
- begin
- for intJ :=FColStart to FColEnd do
- begin
- if intI=FRowStart then
- begin
- Cells[inti,intJ].Up:=nil;
- end
- else
- begin
- Cells[intI,intJ].Up:=Cells[intI-1,intJ];
- end;
- if intI=FRowEnd then
- begin
- Cells[inti,intJ].Down:=nil
- end
- else
- begin
- Cells[intI,intJ].Down:=cells[intI+1,intJ];
- end;
- if intJ=FColStart then
- begin
- Cells[intI,intJ].Left:=nil
- end
- else
- begin
- Cells[intI,intJ].Left:=Cells[intI,intJ-1];
- end;
- if intJ=FColEnd Then
- begin
- Cells[intI,intJ].Right:=nil
- end
- else
- begin
- Cells[intI,intJ].Right :=Cells[intI,intJ+1];
- end;
- end;
- end;
- end;
- procedure TWCells.Move(Status:TWStatus);
- begin
- // TODO -cMM: TWCells.Move default body inserted
- case Status of
- Up:
- begin
- if FActiveCell.Up <>nil then
- begin
- SwapCellPos(FActiveCell,FactiveCell.Up);
- FactiveCell:=FActiveCell.Up;
- end;
- end;
- Down:
- begin
- if FActiveCell.Down <>nil then
- begin
- SwapCellPos(FActiveCell,FActiveCell.Down);
- FactiveCell:=FActiveCell.Down;
- end;
- end;
- Left:
- begin
- if FActiveCell.Left <>nil then
- begin
- SwapCellPos(FActiveCell,FactiveCell.Left );
- FactiveCell:=FActiveCell.Left;
- end;
- end;
- Right:
- begin
- if FActiveCell.Right <>nil then
- begin
- SwapCellPos(FactiveCell,FactiveCell.Right);
- FactiveCell:=FActiveCell.Right;
- end;
- end;
- end;
- end;
- procedure TWCells.SwapCellPos(SCell,DCell:TWCell);
- var
- TempNum:TWNum;
- begin
- // TODO -cMM: TWCells.SwapCell default body inserted
- TempNum:=SCell.Center;
- SCell.FCenter :=DCell.FCenter;
- DCell.FCenter :=TempNum;
- end;
- constructor TWCell.Create(Num:Integer);
- begin
- // TODO -cMM: TWCell.Create default body inserted
- inherited create;
- FCenter:=TWNum.Create(Num);
- end;
- destructor TWCell.Destroy;
- begin
- // TODO -cMM: TWCell.Destroy default body inserted
- FCenter.Free;
- inherited;
- end;
- constructor TWNum.Create(intNum:integer);
- begin
- // TODO -cMM: TWNum.Create default body inserted
- inherited Create;
- FValue:=intNum;
- end;
- end.