home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / Pascal / BPASCAL.700 / D12 / CHESSOWL.ZIP / CHPIECES.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-10-01  |  5.6 KB  |  197 lines

  1. unit chpieces;
  2.  
  3. interface
  4.  
  5. uses WinProcs, WinTypes, OWindows, Pieces, Chessdll, ChConst;
  6.  
  7. type
  8.  
  9.   PChessPiece = ^TChessPiece;
  10.   TChessPiece = object (TGamePiece)
  11.     Piece: TPiece;
  12.     Color: TColor;
  13.     LastValidSquare: TLocation;
  14.     SquareIsValid: Boolean;
  15.     InJeopardy: Boolean;
  16.     ValidMoves: array [0..27] of TMove;  { 28 = max moves per piece (Queen)}
  17.     constructor Init(AParent: PWindowsObject;
  18.                      ASquare: TSquare;
  19.                      Loc: TLocation);
  20.     procedure GetSquare(var Sq: TLocation);
  21.     procedure SetSquare(Sq: TLocation);
  22.     procedure Paint(DC: HDC); virtual;
  23.     function  GetCursor: HCursor; virtual;
  24.     function  CanDrag: Boolean; virtual;
  25.     procedure DragBegin(DC: HDC; Mouse: TPoint); virtual;
  26.     procedure DragContinue(DC: HDC; Mouse: TPoint; Sq: TLocation); virtual;
  27.     function  DragEnd(DC: HDC; Mouse: TPoint;
  28.                       Sq: TLocation; var Move): Boolean; virtual;
  29.     procedure ResetValidMoves(var MoveArray: array of TMove);
  30.     procedure CheckJeopardy(var MoveArray: array of TMove);
  31.     function  ValidSquare(Sq: TLocation): Boolean;
  32.   end;
  33.  
  34.  
  35. implementation
  36.  
  37. constructor TChessPiece.Init(AParent: PWindowsObject;
  38.                              ASquare: TSquare;
  39.                              Loc: TLocation);
  40. begin
  41.   Piece := ASquare.Piece;
  42.   Color := ASquare.Color;
  43.   inherited Init(AParent,
  44.     PChar(bmChessPiecesBaseID + Ord(Piece) + Ord(High(Piece))*Ord(Color)),
  45.     PChar(bmChessPiecesBaseID + Ord(Piece) + Ord(High(Piece))*2), Loc);
  46.   FillChar(ValidMoves, SizeOf(ValidMoves), 0);
  47.   Word(LastValidSquare) := 0;   
  48.   SquareIsValid := True;
  49.   InJeopardy := False;
  50. end;
  51.  
  52. procedure TChessPiece.GetSquare(var Sq: TLocation);
  53. begin
  54.   Sq := Square;
  55. end;
  56.  
  57. procedure TChessPiece.SetSquare(Sq: TLocation);
  58. begin
  59.   Square := Sq;
  60. end;
  61.  
  62. function TChessPiece.GetCursor: HCursor;
  63. begin
  64.   if Dragging then
  65.     if SquareIsValid then
  66.       GetCursor := LoadCursor(HInstance, PChar(curInvisible))
  67.     else
  68.       GetCursor := LoadCursor(GetModuleHandle('User'), PChar(idc_No))
  69.   else
  70.     GetCursor := LoadCursor(HInstance, PChar(curGrabHandOpen));
  71. end;
  72.  
  73. procedure TChessPiece.Paint(DC: HDC);
  74. var
  75.   OldBrush: HBrush;
  76.   OldPen: HPen;
  77.   OldRop: Integer;
  78.   R: TRect;
  79. begin
  80.   inherited Paint(DC);
  81.   if InJeopardy and not DragHidden then
  82.   begin
  83.     R := Rect;
  84.     OldBrush := SelectObject(DC, GetStockObject(Null_Brush));
  85.     OldPen := SelectObject(DC, GetStockObject(White_Pen));
  86.     OldRop := SetRop2(DC, R2_XORPEN);
  87.     InflateRect(R, -1, -1);
  88.     Rectangle(DC, R.Left, R.Top, R.Right, R.Bottom);
  89.     InflateRect(R, -2, -2);
  90.     Rectangle(DC, R.Left, R.Top, R.Right, R.Bottom);
  91.     SelectObject(DC, OldPen);
  92.     SelectObject(DC, OldBrush);
  93.     SetRop2(DC, OldRop);
  94.   end;      
  95. end;
  96.  
  97. function  TChessPiece.CanDrag: Boolean;
  98. begin
  99.   CanDrag := ValidMoves[0].Change.Piece <> pEmpty;
  100. end;
  101.  
  102. procedure TChessPiece.DragBegin(DC: HDC; Mouse: TPoint);
  103. begin
  104.   SquareIsValid := True;
  105.   inherited DragBegin(DC, Mouse);
  106.   Word(LastValidSquare) := 0;   
  107. end;
  108.  
  109. procedure TChessPiece.DragContinue(DC: HDC; Mouse: TPoint; Sq: TLocation);
  110. begin
  111.   SquareIsValid :=(Word(Sq) = Word(Square)) or
  112.                   (Word(Sq) = Word(LastValidSquare)) or
  113.                    ValidSquare(TLocation(Sq));
  114.   inherited DragContinue(DC, Mouse, Sq);
  115. end;
  116.  
  117. function  TChessPiece.DragEnd(   DC: HDC;
  118.                               Mouse: TPoint;
  119.                                  Sq: TLocation;
  120.                            var Move        ): Boolean;
  121. begin
  122.   DragEnd := inherited DragEnd(DC, Mouse, Sq, Move)
  123.              and ValidSquare(Sq);
  124.   with TChange(Move) do
  125.   begin
  126.     Piece := Self.Piece;
  127.     Source:= Square;
  128.     Dest  := Sq;
  129.   end;
  130. end;
  131.  
  132. { ResetValidMoves takes the valid move list of the current player
  133.   and copies moves for this piece into a local array.  The valid moves
  134.   are used in setting the cursor to indicate whether a piece is
  135.   draggable or whether the dragging piece can move to the current square. }
  136. procedure TChessPiece.ResetValidMoves(var MoveArray: array of TMove);
  137. var
  138.   X: Integer;
  139.   Y: Integer;
  140. begin
  141.   X := 0;
  142.   Y := 0;
  143.   ValidMoves[X].Change.Piece := pEmpty;
  144.   while (X <= High(ValidMoves)) and
  145.         (Y <= High(MoveArray))  and
  146.         (MoveArray[Y].Change.Piece <> pEmpty) do
  147.   begin
  148.     if Word(MoveArray[Y].Change.Source) = Word(Square) then
  149.     begin
  150.       Move(MoveArray[Y], ValidMoves[X], SizeOf(TMove));
  151.       Inc(X);
  152.       ValidMoves[X].Change.Piece := pEmpty;
  153.     end;
  154.     Inc(Y);
  155.   end;
  156. end;
  157.  
  158. { CheckJeopardy takes the valid move list of the opponent and looks
  159.   for any move that will capture this piece. }
  160. procedure TChessPiece.CheckJeopardy(var MoveArray: array of TMove);
  161. var
  162.   X: Integer;
  163.   OldState: Boolean;
  164. begin
  165.   OldState := InJeopardy;
  166.   InJeopardy := False;
  167.   X := 0;
  168.   while (not InJeopardy) and
  169.         (X <= High(MoveArray)) and
  170.         (MoveArray[X].Change.Piece <> pEmpty) do
  171.   begin
  172.     InJeopardy := (Word(MoveArray[X].Change.Dest) = Word(Square));
  173.     Inc(X);
  174.   end;
  175.   if OldState xor InJeopardy then  { If state has changed, redraw }
  176.     RequestRedraw;
  177. end;
  178.  
  179. function  TChessPiece.ValidSquare(Sq: TLocation): Boolean;
  180. var
  181.   X: Integer;
  182. begin
  183.   ValidSquare := False;
  184.   X := 0;
  185.   while (X <= High(ValidMoves)) and
  186.         (ValidMoves[X].Change.Piece <> pEmpty) and
  187.         (Word(ValidMoves[X].Change.Dest) <> Word(Sq)) do
  188.     Inc(X);
  189.   if (X <= High(ValidMoves)) and
  190.      (Word(ValidMoves[X].Change.Dest) = Word(Sq)) then
  191.   begin
  192.     ValidSquare := True;
  193.     LastValidSquare := Sq;
  194.   end
  195. end;
  196.  
  197. end.