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

  1. unit Pieces;
  2.  
  3. interface
  4.  
  5. {$IFDEF DLL}
  6. uses Objects, Views, Dialogs, ChessDLL, ChessCmd, Drivers;
  7. {$ELSE}
  8. uses Objects, Views, Dialogs, ChessInf, ChessCmd, Drivers;
  9. {$ENDIF}
  10.  
  11. type
  12.   PChessPiece = ^TChessPiece;
  13.   TChessPiece = object(TView)
  14.     PieceType: TSquare;
  15.     Location: TLocation;
  16.     constructor Init(var Bounds: TRect; APieceType: TSquare; ALocation: TLocation);
  17.     constructor Load(var S:TStream);
  18.     procedure CapturePiece;
  19.     procedure Draw; virtual;
  20.     function GetPromotionPiece: TPiece;
  21.     procedure HandleEvent(var Event: TEvent); virtual;
  22.     procedure MoveToSquare(ALocation: TLocation);
  23.     procedure SnapToSquare;
  24.     procedure Store(var S: TStream);
  25.   end;
  26.  
  27. const
  28.   RChessPiece: TStreamRec = (
  29.     ObjType: otChessPiece;
  30.     VmtLink: Ofs(TypeOf(TChessPiece)^);
  31.     Load:    @TChessPiece.Load;
  32.     Store:   @TChessPiece.Store);
  33.  
  34. implementation
  35. uses ChessUtl, Board;
  36.  
  37. type
  38.   TPictureType = array[0..2] of
  39.   record
  40.      x : integer;
  41.      s : string[6];
  42.   end;
  43.  
  44. const
  45.   PiecePicture: array[pKing..pPawn] of TPictureType =
  46.  
  47.         (((x : 1;   s :  '++++'),
  48.           (x : 1;   s :  '⌠ K⌠'),
  49.           (x : 1;   s :  '⌡⌡⌡⌡')),
  50.  
  51.          ((x : 1;   s :  'ΘΘΘΘ'),
  52.           (x : 1;   s :  '╞╬╬╡'),
  53.           (x : 1;   s :  '│ Q│')),
  54.  
  55.          ((x : 1;   s :  '┌╥╥┐'),
  56.           (x : 1;   s :  '│ R│'),
  57.           (x : 1;   s :  '│  │')),
  58.  
  59.          ((x : 2;   s :   '┌Ω┐'),
  60.           (x : 2;   s :   '│ │'),
  61.           (x : 2;   s :   '│B│')),
  62.  
  63.          ((x : 1;   s :  '┌──┐'),
  64.           (x : 1;   s :  '╘┐''│'),
  65.           (x : 2;   s :   '│N│')),
  66.  
  67.          ((x : 0;   s : ''     ),
  68.           (x : 3;   s :    'P' ),
  69.           (x : 2;   s :   '≡≡≡')));
  70.  
  71.  
  72. constructor TChessPiece.Init(var Bounds: TRect; APieceType: TSquare; ALocation: TLocation);
  73. begin
  74.   inherited Init(Bounds);
  75.   EventMask := EventMask or (evMove + evBroadcast);
  76.   PieceType := APieceType;
  77.   Location := ALocation;
  78. end;
  79.  
  80. constructor TChessPiece.Load(var S: TStream);
  81. begin
  82.   inherited Load(S);
  83.   S.Read(PieceType, SizeOf(PieceType) + SizeOf(TLocation));
  84. end;
  85.  
  86. procedure TChessPiece.CapturePiece;
  87. begin
  88.   Hide;
  89.   DrawView;
  90.   Free;
  91. end;
  92.  
  93. procedure TChessPiece.Draw;
  94. var
  95.   Color: Word;
  96.   I: Integer;
  97.   B: TDrawBuffer;
  98.   XOfs, XLen: Integer;
  99.   R: TRect;
  100.   WasVisible: Boolean;
  101.  
  102.   procedure DoDraws(P: PView);
  103.   var
  104.     Bounds: TRect;
  105.   begin
  106.     while P <> nil do
  107.     begin
  108.       P^.GetBounds(Bounds);
  109.       Bounds.Intersect(R);
  110.       if not Bounds.Empty then
  111.         P^.DrawView;
  112.       P := P^.NextView;
  113.     end;
  114.   end;
  115.  
  116. begin
  117.   Owner^.Lock;
  118.   WasVisible := State and sfVisible <> 0;
  119.   State := State and not sfVisible;
  120.   GetBounds(R);
  121.   DoDraws(NextView);
  122.   if not WasVisible then Exit;
  123.   State := State or sfVisible;
  124.  
  125.   if PieceType.Color = cBlack then
  126.     Color := GetColor($0404) else Color := GetColor($0505);
  127.   for I := 0 to 2 do
  128.   begin
  129.     XOfs := PiecePicture[PieceType.Piece][I].x;
  130.     XLen := Length(PiecePicture[PieceType.Piece][I].s);
  131.     if XLen > 0 then
  132.     begin
  133.       MoveStr(B, PiecePicture[PieceType.Piece][I].s, Color);
  134.       WriteBuf(XOfs, I, XLen, 1, B);
  135.     end;
  136.   end;
  137.   Owner^.Unlock;
  138. end;
  139.  
  140. function TChessPiece.GetPromotionPiece: TPiece;
  141. begin
  142.   if PieceType.Piece = pPawn then
  143.     GetPromotionPiece := pQueen
  144.   else GetPromotionPiece := PieceType.Piece;
  145. end;
  146.  
  147. procedure TChessPiece.HandleEvent(var Event: TEvent);
  148. var
  149.   E: TEvent;
  150.   R: TRect;
  151.   P: PChessPiece;
  152.   S: TSquare;
  153. begin
  154.   inherited HandleEvent(Event);
  155.   case Event.What of
  156.     evMouseDown:
  157.       if PChessBoard(Owner)^.CanMovePiece(PieceType.Color) then
  158.       begin
  159.         MakeFirst;
  160.         R.Assign(0, 0, Owner^.Size.X, Owner^.Size.Y);
  161.         DragView(Event, dmDragMove, R, Size, Size);
  162.         SnapToSquare;
  163.       end;
  164.     evMove:
  165.       case Event.Command of
  166.         cmMovePiece:
  167.           with PMove(Event.InfoPtr)^ do
  168.             if (Kind in [kNormal, kEnPassant, kPawnPromote, kCastling]) and
  169.               (Word(Change.Source) = Word(Location)) then
  170.             begin
  171.               if (Kind = kPawnPromote) and (PieceType.Piece = pPawn) then
  172.                 PieceType.Piece := Change.Piece;
  173.               MoveToSquare(Change.Dest);
  174.             end
  175.             else if (PieceType.Piece = Contents) and Capture and
  176.               (Word(Change.Dest) = Word(Location)) then
  177.               CapturePiece
  178.             else if (Kind = kCastling) and (PieceType.Piece = pRook) and
  179.               (Word(RookSource) = Word(Location)) then
  180.               MoveToSquare(RookDest)
  181.             else if (Kind = kEnPassant) and (PieceType.Piece = Contents) and
  182.               Capture and (Word(EPCapture) = Word(Location)) then
  183.               CapturePiece;
  184.         cmUndoMove:
  185.           with PMove(Event.InfoPtr)^ do
  186.             if (Word(Change.Dest) = Word(Location)) then
  187.             begin
  188.               if (Kind = kPawnPromote) and (Change.Piece = PieceType.Piece) then
  189.                 PieceType.Piece := pPawn;
  190.               MoveToSquare(Change.Source);
  191.               if Capture then
  192.               begin
  193.                 S.Piece := Contents;
  194.                 if PieceType.Color = cWhite then
  195.                   S.Color := cBlack else S.Color := cWhite;
  196.                 case Kind of
  197.                   kNormal:
  198.                     begin
  199.                       SquareToLocal(Change.Dest, R.A, Owner^.Size.Y);
  200.                       R.Assign(R.A.X, R.A.Y, R.A.X + 6, R.A.Y + 3);
  201.                       P := New(PChessPiece, Init(R, S, Change.Dest));
  202.                     end;
  203.                   kEnPassant:
  204.                     begin
  205.                       SquareToLocal(EPCapture, R.A, Size.Y);
  206.                       R.Assign(R.A.X, R.A.Y, R.A.X + 6, R.A.Y + 3);
  207.                       P := New(PChessPiece, Init(R, S, EPCapture));
  208.                     end;
  209.                 end;
  210.                 Owner^.Insert(P);
  211.               end;
  212.             end
  213.             else if (Kind = kCastling) and (PieceType.Piece = pRook) and
  214.               (Word(RookDest) = Word(Location)) then
  215.               MoveToSquare(RookSource);
  216.         cmFindPiece:
  217.           if Event.InfoWord = Word(Location) then
  218.             ClearEvent(Event);
  219.       end;
  220.     evBroadcast:
  221.       case Event.Command of
  222.         cmRegisterSave: PCollection(Event.InfoPtr)^.Insert(@Self);
  223.       end;
  224.   end;        
  225. end;
  226.  
  227. procedure TChessPiece.MoveToSquare(ALocation: TLocation);
  228. var
  229.   Point: TPoint;
  230. begin
  231.   Location := ALocation;
  232.   SquareToLocal(Location, Point, Owner^.Size.Y);
  233.   MoveTo(Point.X, Point.Y);
  234. end;
  235.  
  236. procedure TChessPiece.SnapToSquare;
  237. var
  238.   S: TLocation;
  239.   P: TPoint;
  240.   C: TChange;
  241.   Result: TChessError;
  242. begin
  243.   P.X := Origin.X + (Size.X div 2);
  244.   P.Y := Origin.Y + (Size.Y div 2);
  245.   PointInSquare(P, S);
  246.   C.Piece := PieceType.Piece;
  247.   C.Source := Location;
  248.   C.Dest := S;
  249.   Result := PChessBoard(Owner)^.ValidateMove(C);
  250.   case Result of
  251.     ceOK: Message(Owner, evMove, cmSubmitMove, @C);
  252.     ceAmbiguousMove:
  253.       begin
  254.         C.Piece := GetPromotionPiece;
  255.         Message(Owner, evMove, cmSubmitMove, @C);
  256.       end;
  257.   else
  258.     SquareToLocal(Location, P, Owner^.Size.Y);
  259.     MoveTo(P.X, P.Y);
  260.   end;
  261. end;
  262.  
  263. procedure TChessPiece.Store(var S: TStream);
  264. begin
  265.   inherited Store(S);
  266.   S.Write(PieceType, SizeOf(PieceType) + SizeOf(TLocation));
  267. end;
  268.  
  269. end.
  270.