board.pas

来自「用于开发税务票据管理的软件」· PAS 代码 · 共 1,705 行 · 第 1/5 页

PAS
1,705 行
字号
      FOldHeight := Height;
      FSquareSize := Min(Width, Height) shr 3;
//      FBitmap.ClipRect := Rect(0,0,Width,Height);
      FPicChanged := True;
    end;
  end;

  // Only recalculate position if some board display thing changed.
  // Otherwise, skip this and use the old bitmap
  if FPicChanged then
  begin
    FBitmap.Width := Width;
    FBitmap.Height := Height;

    if (FPic.Graphic <> nil) then
    begin
      if FStretchPic then
        FBitmap.Canvas.StretchDraw(FBitmap.Canvas.ClipRect, FPic.Graphic)
      else
        FBitmap.Canvas.Draw(0,0, FPic.Graphic);
    end
    else
    begin
      FBitmap.Canvas.Brush.Color := FBackgroundC;
      FBitmap.Canvas.FillRect(Bounds(0,0,FBitmap.Width,FBitmap.Height));//ClearAll;
    end;

    { This fills in the sqaures of the board in alternating BlackC, WhiteC }
    for j := 0 to 7 do
    begin
      for i := 0 to 7 do
      begin
        FBitmap.Canvas.Brush.Color := BrushColours[SquareIsWhite];

        FBitmap.Canvas.FillRect(Bounds(i * FSquareSize,
                                       j * FSquareSize,
                                       FSquareSize,
                                       FSquareSize));

        SquareIsWhite := not SquareIsWhite;
      end;

      SquareIsWhite := not SquareIsWhite;
    end;

    // Now draw all the pieces
    for i := 0 to High(Board) do
    begin
      CurrPiecePtr := Board[i];
      if (CurrPiecePtr <> nil) and (CurrPiecePtr.Captured = False) and
         (InBounds(CurrPiecePtr.Location)) then
        CurrPiecePtr.DrawToBitmap(FBitmap, FSquareSize);
    end;

    { Finally, draw board lines if needed }
    if FDrawLines then
      DrawGridLines(FBitmap.Canvas);

    FPicChanged := False;
  end;

  if FGameRunning then
    Canvas.Draw(0,0,FBitmap)
  else
    BitBlt(Canvas.Handle,0,0,Width,Height,FBitmap.Canvas.Handle,0,0,SRCAND);
end;

{ Draw the board grid lines, whose width is determined by FGridLineWidth.  See
  also its opposite procedure, UndrawGridLines }
procedure TChessboard.DrawGridlines(DrawCanvas: TCanvas);
var
  i: Integer;
  OldWidth: Cardinal;
begin
  with DrawCanvas do
  begin
    Pen.Color := FGridLineColour;
    OldWidth := Pen.Width;
    Pen.Width := FGridlineWidth;

    for i := 0 to 8 do
    begin
      MoveTo(i * FSquareSize, 0);
      LineTo(i * FSquareSize, FSquareSize * 8);
      MoveTo(0, i * FSquareSize);
      LineTo(8 * FSquareSize, i * FSquareSize);
    end;
    Pen.Width := OldWidth;
  end;
end;

{procedure TChessboard.DrawToBitmap(P: TPiecePtr; _X, _Y: Cardinal); register;
begin
     Bitmap.Canvas.Draw(_X * p^.Width, _Y * p^.Height, p^.Picture.Graphic);
end;}

{ This function converts the X & y coordinates from the DragOver
  and DragDrop procedures into the FX and FY points that the piece
  has. }
function TChessboard.FindNewLocation(const X,Y: Cardinal): TPoint; register;
begin
  if FSquareSize = 0 then Exit;
  Result.X := (X div FSquareSize) + 1;
  Result.Y := 8 - (y div FSquareSize);
end;

{ Note: This procedure undoes the grid lines, literally, by filling in the line
  segments with the appropriate square colour.  This is of course quicker than
  using ForceRedraw to invalidate the whole lot.  However, it is also slightly
  tricky to get the positions right, so feel free to examine the coordinates
  closely if you don't feel the image is right. }
procedure TChessboard.UndrawGridLines(DrawCanvas: TCanvas);
var
  x,y: Integer;
  OldWidth: Cardinal;
  PenColours: Array[Boolean] of TColor;
  duh: Boolean;
begin
  PenColours[False] := FWhiteC;
  PenColours[True] := FBlackC;

  with DrawCanvas do
  begin
    OldWidth := DrawCanvas.Pen.Width;
    Pen.Width := FGridLineWidth;

    y := 7;
    while y >= 0 do
    begin
      MoveTo(0, Y * FSquareSize);

      x := 7;
      while x >= 0 do
      begin    // mod 2    // mod 2
        Duh := (x and 1) + (y and 1) = 0;

        Pen.Color := PenColours[Duh];
        LineTo( (X + 1) * FSquareSize, Y * FSquareSize );

        Pen.Color := PenColours[ not Duh ];
        MoveTo( X * FSquareSize, Y * FSquareSize );
        LineTo( X * FSquareSize, (Y+1) * FSquareSize );

        MoveTo( (X + 1) * FSquareSize, Y * FSquareSize);


        Dec(x);
      end;

      Dec(y);
    end;

    // And finish off with the outside lines...
    Pen.Color := FBackgroundC;
    MoveTo( 8 * FSquareSize, 0);
    y := 7;
    while y >= 0 do
    begin
      MoveTo( 8 * FSquareSize     ,  Y * FSquareSize );
      LineTo( 8 * FSquareSize     , (Y+1) * FSquareSize);
      MoveTo( Y * FSquareSize     ,  8 * FSquareSize);
      LineTo( (Y+1) * FSquareSize ,  8 * FSquareSize);
      Dec(y);
    end;
    Pen.Width := OldWidth;
  end;
end;

{ Is a specified board square black or white? }
function TChessboard.GetSquareColour(Where: TPoint): TColor;
begin
  if Where.x and 1 = 0 then Inc(Where.Y);
  if Where.y and 1 = 0 then Result := FWhiteC else Result := FBlackC;
end;

{ REMEMBER to call SendEndTurnEvent after MovePiece if you intend the user to
  be able to use the OnEndTurn event!! }
function TChessboard.MovePiece(var P: TPiece; const WhereTo: TPoint): Boolean;
var
  KingsRank: Byte;
  WhereToPiecePtr,
  pKing: TPiece;
  KingLocation,
  From: TPoint;
  EnemyColour: TColour;
begin
  Result := False;
  From := P.Location;

  if ValidMove(From, WhereTo) then
  begin
    UpdateUndoRecords(From, WhereTo, False);

    { Validmove has signalled that the move is a capture }
    if FCaptureOn then
    begin
      if FindPiece(WhereToPiecePtr, WhereTo) = False then
        raise ECapturePieceNotThere.Create('The piece to capture was not in the given square');

        CapturePiece(WhereToPiecePtr);
    end
    else
      { Validmove has signalled that the move is castling }
      if FCastling <> csNone then
      begin
        KingsRank := 8 - (Turn * 7);  // this is faster than below line
//        if Turn = 1 then KingsRank := 1 else KingsRank := 8;

        if FCastling = csQueenSideCastling then
        begin
          if FindPiece(WhereToPiecePtr, Point(1,KingsRank)) = False then
            raise EPieceNotFoundInSquare.Create('The rook was not found while castling Queen''s side');

          MovePieceToSquare(WhereToPiecePtr, Point(4, KingsRank));

          if not FLoading then EraseSquare(Point(1, KingsRank), nil);
        end
        else
        begin
          if FindPiece(WhereToPiecePtr, Point(8, KingsRank)) = False then
            raise EPieceNotFoundInSquare.Create('The rook was not found while castling King''s side');

          MovePieceToSquare(WhereToPiecePtr, Point(6, KingsRank));

          if not FLoading then EraseSquare(Point(8, KingsRank), nil);
        end;

      if not FLoading then EraseSquare(WhereToPiecePtr.Location, WhereToPiecePtr);
    end
    else
      { En Passant is on... }
      if (FEnPassant.X <> 0) then
      begin
        if FindPiece(WhereToPiecePtr, FEnPassant) = False then
          raise ECapturePieceNotThere.Create('Attempted en passant captured piece was not there');

          CapturePiece(WhereToPiecePtr);
          FPiecesInSquares[FEnPassant.X shl 3 + FEnPassant.Y - 9] := False;

        if not FLoading then EraseSquare(FEnPassant, nil);
        FLastPawnMoved := nil;
      end
      else
      { This time, if not en passant reset everything }
      begin
        { Moved pawn two spaces, so set it up for possible en passant }
        if (P is TPawn) and not (FCaptureOn) then
        begin
          if abs(From.Y - WhereTo.Y) = 2 then
            FLastPawnMoved := TPawn(P)
          else
            FLastPawnMoved := nil;
        end;
      end;

      MovePieceToSquare(P, WhereTo);

      { Check for a pawn that needs promoted }
      if P is TPawn then
        if ((P.Colour = cWhite) and (WhereTo.Y = 8)) or
           ((P.Colour = cBlack) and (WhereTo.Y = 1)) then
          PromotePawn(TPawn(P));

      { Change the turn, update info }
      if FTurn = 1 then
      begin
        Inc(FTurnNumber);
        FTurn := 0;
      end
      else
        FTurn := 1;

      { See if king is now in check }
      KingLocation := FindKing(pKing, TColour(Integer(FTurn) xor 1));
      if pKing.Colour = cWhite then EnemyColour := cBlack else EnemyColour := cWhite;

      FCheck := PiecesAttackingSquare(KingLocation, EnemyColour) > 0;

      if not FLoading then
      begin
        { Draw piece to new location }
        if TUndoRecord(UndoList[FUndoCount]^).PromotedPawn = nil then
          EraseSquare(WhereTo, p);

        { Erase old position }
        EraseSquare(From, nil);
      end;

      Result := True;
   end;
end;

{ This moves the piece supplied to the given square.  It also updates the
  FPiecesInSquares TBits, so that the previous square is empty and the new
  square is flagged as holding a piece.}
procedure TChessboard.MovePieceToSquare(P: TPiece; const WhereTo: TPoint);
begin
  FPiecesInSquares[P.X shl 3 + P.Y - 9] := False;
  P.Move(WhereTo);
  FPiecesInSquares[WhereTo.X shl 3 + WhereTo.Y - 9] := True;
end;

{ This decides whether the given move is different from the corresponding move
  in the undo list, if moves have been undone.  If the moves are not the same,
  any undone moves from that point on are cleared, since they are no longer
  relevant. }
procedure TChessboard.UpdateUndoRecords(const From, WhereTo: TPoint; const PositionMove: Boolean);
var
  R: TUndoRecordPtr;
begin
  if FUndoCount > 0 then
  begin
    // CHECK TO SEE IF MOVE IS DIFFERENT.
    R := UndoList[ FUndoCount - 1 ];

    if (ComparePoints(From, R^.From) = False) or
       (ComparePoints(WhereTo, R^.WhereTo) = False) then
    begin
      // Clear the undone moves, since they are no longer correct
      while FUndoCount > 0 do
      begin
        Dispose(UndoList.First);
        UndoList.Delete(0);
        Dec(FUndoCount);
      end;

      AddUndoRecord(From, WhereTo);
      TUndoRecordPtr(UndoList.First)^.PositionMode := PositionMove;
    end
    else Dec(FUndoCount);
  end
  else
  begin
    AddUndoRecord(From, WhereTo);
    TUndoRecordPtr(UndoList.First)^.PositionMode := PositionMove;
  end;
end;

{ Fills in a square with the correct colour.  If "p" is nil, no further action
  is taken.  Otherwise, the piece is drawn to the square }
procedure TChessboard.EraseSqua

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?