king.pas

来自「用于开发税务票据管理的软件」· PAS 代码 · 共 102 行

PAS
102
字号
unit King;

interface

uses
  SysUtils, WinTypes, Classes, Graphics, Piece;

type
//  TKingPtr = ^TKing;
  TKing = class(TPiece)
  private
    { Private declarations }
  protected
    { Protected declarations }
    procedure ChangeColour(NewVal: TColour); override;
    function GetStartingLocation(NumberOfItems: Byte): TPoint; override;
  public
    { Public declarations }
    constructor Create(AOwner: TComponent); override;

    function IsInCheck(Where: TPoint): Boolean;
    function ValidMove(Where: TPoint): Boolean; override;
  published
    { Published declarations }
  end;

implementation

constructor TKing.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  if FileExists(FStartDir + 'Pictures\KingW.bmp') then
  begin
    Picture.LoadFromFile(FStartDir + 'Pictures\KingW.bmp');
    Picture.Graphic.Transparent := True;
  end;

  Hint := 'King';
  FPrefix := 'K';
  FValue := 10;
end;

function TKing.GetStartingLocation(NumberOfItems: byte): TPoint;
begin
  Result.X := 5;
  if Colour = cWhite then Result.Y := 1 else Result.Y := 8;
end;

function TKing.ValidMove(Where: TPoint): Boolean;
begin
  Result := False;

  { Check the following conditions:

    (1)  The x & y are in the range 1 - 8
    (2)  The king is not moving more than one square in x or y direction
    (3)  The new square is not the current square }

  if (Where.X in [1..8]) and (Where.Y in [1..8]) then
  begin
     { 1 }
     if (abs(Where.X - FX) <= 1) and (abs(Where.Y - FY) <= 1) and       { 2 }
         not ((Where.X = FX) and (Where.Y = FY)) then                    { 3 }
            Result := true
     else
       { Check for castling moves }
       if (NumberMoves = 0) and ((Where.X = 3) or (Where.X = 7)) then
         Result := ((Colour = cBlack) and (Where.Y = 8)) or
                   ((Colour = cWhite) and (Where.Y = 1));
  end;
end;

function TKing.IsInCheck(Where: TPoint): Boolean;
begin
     { I will have to figure out how to implement a check function --
       or should this be checked (hoho!) for in the main code?? The problem
       is that the king as it stands won't actually have knowledge of the
       other pieces.  Hmm.  Almost certainly will be in board code instead. 
       Ignore this code }
     result := false;
end;

procedure TKing.ChangeColour(NewVal: TColour);
var
  FileName: String;
begin
  inherited ChangeColour(NewVal);
  FileName := FStartDir + 'Pictures\King';
  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 + =
减小字号Ctrl + -
显示快捷键?