📄 pawn.pas
字号:
unit Pawn;
interface
uses
SysUtils, WinTypes, Classes, Graphics, Piece;
type
// TPawnPtr = ^TPawn;
TPawn = class(TPiece)
private
{ Private declarations }
protected
{ Protected declarations }
procedure ChangeColour(NewVal: TColour); override;
function GetStartingLocation(NumItems: Byte): TPoint; override;
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
function ValidMove(Where: TPoint): Boolean; override;
published
{ Published declarations }
end;
implementation
constructor TPawn.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
if FileExists(FStartDir + 'Pictures\PawnW.bmp') then
begin
Picture.LoadFromFile(FStartDir + 'Pictures\PawnW.bmp');
Picture.Graphic.Transparent := True;
end;
Hint := 'Pawn';
FPrefix := 'P';
FValue := 1;
end;
function TPawn.ValidMove(Where: TPoint): Boolean;
var
DiffY: Integer;
begin
Result := False;
DiffY := FY - Where.Y;
if Colour = cWhite then DiffY := DiffY * -1;
if (Where.X in [1..8]) and (Where.Y in [1..8]) then
begin
{ Check for moving vertically, i.e. possibly correct non-capture }
if (Where.X = FX) then
begin
{ Obviously, a move to the current square is not valid }
if (Where.Y = FY) then Exit;
{ Check if moved 1 square. If so, it is valid. The board itself
can be responsible for checking there is nothing in the way. }
if (DiffY = 1) then
begin
Result := True;
Exit;
end
else
begin
{ Check for moving two squares on starting location }
if (DiffY = 2) then
begin
if FNumberMoves = 0 then Result := True;
Exit;
end
else
{ Moving for other than one or two moves - invalid }
Exit;
end;
end
else
begin
{ Moving diagonally? }
if (abs(FX - Where.X) <> 1) then
Result := False
else
Result := DiffY = 1;
{ NOTE! The board will be responsible for checking that the pawn
can actually capture in the square specified. The pawn itself
would not know. So, any possible move diagonally should be valid
until further checked. Also, be sure to check en passant (spelling?) }
end;
end;
end;
function TPawn.GetStartingLocation(NumItems: Byte): TPoint;
begin
if Colour = cWhite then
begin
Result.Y := 2;
Result.X := NumItems;
end
else
begin
Result.Y := 7;
Result.X := NumItems - 8;
end;
end;
procedure TPawn.ChangeColour(NewVal: TColour);
var
FileName: String;
begin
inherited ChangeColour(NewVal);
FileName := FStartDir + 'Pictures\Pawn';
if FPlayer = cBlack then
FileName := FileName + 'B.bmp'
else
FileName := FileName + 'W.bmp';
if FileExists(FileName) then
begin
Picture.LoadFromFile(FileName);
Picture.Graphic.Transparent := True;
end;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -