Implements support for piece moving in fpchess

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@1326 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
sekelsenmat
2010-09-23 10:43:52 +00:00
parent e52bf638d2
commit 5898609891
6 changed files with 242 additions and 77 deletions

View File

@ -11,6 +11,15 @@ uses
type type
TChessDrawerDelegate = class
public
procedure HandleMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); virtual; abstract;
procedure HandleMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer); virtual; abstract;
procedure HandleMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer); virtual; abstract;
end;
{ TChessDrawer } { TChessDrawer }
TChessDrawer = class(TCustomControl) TChessDrawer = class(TCustomControl)
@ -31,6 +40,7 @@ type
AImage: TFPImageBitmap); AImage: TFPImageBitmap);
function GetChessTileImage(ATile: TChessTile): TPortableNetworkGraphic; function GetChessTileImage(ATile: TChessTile): TPortableNetworkGraphic;
procedure LoadImages(); procedure LoadImages();
procedure SetDelegate(ADelegate: TChessDrawerDelegate);
end; end;
var var
@ -224,5 +234,13 @@ begin
bmpWKnight.Assign(imgBKing); } bmpWKnight.Assign(imgBKing); }
end; end;
procedure TChessDrawer.SetDelegate(ADelegate: TChessDrawerDelegate);
begin
// Events
OnMouseMove := @ADelegate.HandleMouseMove;
OnMouseUp := @ADelegate.HandleMouseUp;
OnMouseDown := @ADelegate.HandleMouseDown;
end;
end. end.

View File

@ -29,6 +29,11 @@ type
ctBPawn, ctBKnight, ctBBishop, ctBRook, ctBQueen, ctBKing ctBPawn, ctBKnight, ctBBishop, ctBRook, ctBQueen, ctBKing
); );
const
WhitePieces = [ctWPawn, ctWKnight, ctWBishop, ctWRook, ctWQueen, ctWKing];
BlackPieces = [ctBPawn, ctBKnight, ctBBishop, ctBRook, ctBQueen, ctBKing];
type
{@@ {@@
The index [1][1] refers to the left-bottom corner of the table, The index [1][1] refers to the left-bottom corner of the table,
also known as A1. also known as A1.
@ -42,8 +47,14 @@ type
TChessGame = class TChessGame = class
public public
Board: TChessBoard; Board: TChessBoard;
CurrentPlayerIsWhite: Boolean;
Dragging: Boolean;
DragStart, MouseMovePos: TPoint;
procedure StartNewGame(APlayAsWhite: Boolean); overload; procedure StartNewGame(APlayAsWhite: Boolean); overload;
procedure StartNewGame(APlayAsWhite: Integer); overload; procedure StartNewGame(APlayAsWhite: Integer); overload;
function ClientToBoardCoords(AClientCoords: TPoint): TPoint;
function CheckStartMove(AFrom: TPoint): Boolean;
function MovePiece(AFrom, ATo: TPoint): Boolean;
end; end;
var var
@ -59,6 +70,8 @@ var
i: Integer; i: Integer;
j: Integer; j: Integer;
begin begin
CurrentPlayerIsWhite := True;
// //
if APlayAsWhite then if APlayAsWhite then
begin begin
@ -114,6 +127,45 @@ begin
StartNewGame(APlayAsWhite = 0); StartNewGame(APlayAsWhite = 0);
end; end;
{
Returns: If the move is valid and was executed
}
function TChessGame.MovePiece(AFrom, ATo: TPoint): Boolean;
begin
Result := False;
if not CheckStartMove(AFrom) then Exit;
// Parameter checking
if (AFrom.X < 1) or (AFrom.X > 8) or (ATo.X < 1) or (ATo.X > 8) then Exit;
if (AFrom.Y < 1) or (AFrom.Y > 8) or (ATo.Y < 1) or (ATo.Y > 8) then Exit;
// col, row
Board[ATo.X][ATo.Y] := Board[AFrom.X][AFrom.Y];
Board[AFrom.X][AFrom.Y] := ctEmpty;
CurrentPlayerIsWhite := not CurrentPlayerIsWhite;
Result := True;
end;
function TChessGame.ClientToBoardCoords(AClientCoords: TPoint): TPoint;
begin
Result.X := 1 + AClientCoords.X div INT_CHESSTILE_SIZE;
Result.Y := 1 + (INT_CHESSBOARD_SIZE - AClientCoords.Y) div INT_CHESSTILE_SIZE;
end;
{@@
AFrom - The start move position in board coordinates
}
function TChessGame.CheckStartMove(AFrom: TPoint): Boolean;
begin
if CurrentPlayerIsWhite then
Result := Board[AFrom.X][AFrom.Y] in WhitePieces
else
Result := Board[AFrom.X][AFrom.Y] in BlackPieces;
end;
initialization initialization
vChessGame := TChessGame.Create; vChessGame := TChessGame.Create;

View File

@ -36,7 +36,7 @@
<Filename Value="fpchess.lpr"/> <Filename Value="fpchess.lpr"/>
<IsPartOfProject Value="True"/> <IsPartOfProject Value="True"/>
<UnitName Value="fpchess"/> <UnitName Value="fpchess"/>
<UsageCount Value="33"/> <UsageCount Value="37"/>
</Unit0> </Unit0>
<Unit1> <Unit1>
<Filename Value="mainform.pas"/> <Filename Value="mainform.pas"/>
@ -46,9 +46,9 @@
<UnitName Value="mainform"/> <UnitName Value="mainform"/>
<EditorIndex Value="0"/> <EditorIndex Value="0"/>
<WindowIndex Value="0"/> <WindowIndex Value="0"/>
<TopLine Value="52"/> <TopLine Value="108"/>
<CursorPos X="24" Y="70"/> <CursorPos X="26" Y="122"/>
<UsageCount Value="33"/> <UsageCount Value="37"/>
<Loaded Value="True"/> <Loaded Value="True"/>
<LoadedDesigner Value="True"/> <LoadedDesigner Value="True"/>
</Unit1> </Unit1>
@ -123,10 +123,12 @@
<Unit11> <Unit11>
<Filename Value="..\..\..\lazarussvn\lcl\controls.pp"/> <Filename Value="..\..\..\lazarussvn\lcl\controls.pp"/>
<UnitName Value="Controls"/> <UnitName Value="Controls"/>
<EditorIndex Value="4"/>
<WindowIndex Value="0"/> <WindowIndex Value="0"/>
<TopLine Value="193"/> <TopLine Value="305"/>
<CursorPos X="3" Y="225"/> <CursorPos X="61" Y="320"/>
<UsageCount Value="16"/> <UsageCount Value="18"/>
<Loaded Value="True"/>
</Unit11> </Unit11>
<Unit12> <Unit12>
<Filename Value="..\..\..\lazarussvn\lcl\include\notebook.inc"/> <Filename Value="..\..\..\lazarussvn\lcl\include\notebook.inc"/>
@ -163,9 +165,9 @@
<UnitName Value="chessdrawer"/> <UnitName Value="chessdrawer"/>
<EditorIndex Value="3"/> <EditorIndex Value="3"/>
<WindowIndex Value="0"/> <WindowIndex Value="0"/>
<TopLine Value="148"/> <TopLine Value="17"/>
<CursorPos X="26" Y="157"/> <CursorPos X="3" Y="25"/>
<UsageCount Value="31"/> <UsageCount Value="35"/>
<Loaded Value="True"/> <Loaded Value="True"/>
</Unit16> </Unit16>
<Unit17> <Unit17>
@ -222,9 +224,9 @@
<IsVisibleTab Value="True"/> <IsVisibleTab Value="True"/>
<EditorIndex Value="1"/> <EditorIndex Value="1"/>
<WindowIndex Value="0"/> <WindowIndex Value="0"/>
<TopLine Value="2"/> <TopLine Value="130"/>
<CursorPos X="76" Y="23"/> <CursorPos X="42" Y="137"/>
<UsageCount Value="23"/> <UsageCount Value="27"/>
<Loaded Value="True"/> <Loaded Value="True"/>
</Unit23> </Unit23>
<Unit24> <Unit24>
@ -235,7 +237,7 @@
<WindowIndex Value="0"/> <WindowIndex Value="0"/>
<TopLine Value="18"/> <TopLine Value="18"/>
<CursorPos X="13" Y="39"/> <CursorPos X="13" Y="39"/>
<UsageCount Value="23"/> <UsageCount Value="27"/>
<Loaded Value="True"/> <Loaded Value="True"/>
</Unit24> </Unit24>
<Unit25> <Unit25>
@ -280,119 +282,133 @@
<Unit30> <Unit30>
<Filename Value="..\..\..\fpctrunk\packages\fcl-image\src\fpimage.pp"/> <Filename Value="..\..\..\fpctrunk\packages\fcl-image\src\fpimage.pp"/>
<UnitName Value="FPimage"/> <UnitName Value="FPimage"/>
<EditorIndex Value="4"/>
<WindowIndex Value="0"/> <WindowIndex Value="0"/>
<TopLine Value="18"/> <TopLine Value="18"/>
<CursorPos X="1" Y="30"/> <CursorPos X="1" Y="30"/>
<UsageCount Value="10"/> <UsageCount Value="10"/>
<Loaded Value="True"/>
</Unit30> </Unit30>
</Units> </Units>
<JumpHistory Count="26" HistoryIndex="25"> <JumpHistory Count="30" HistoryIndex="29">
<Position1> <Position1>
<Filename Value="chessdrawer.pas"/> <Filename Value="chessdrawer.pas"/>
<Caret Line="108" Column="1" TopLine="100"/> <Caret Line="237" Column="8" TopLine="212"/>
</Position1> </Position1>
<Position2> <Position2>
<Filename Value="chessdrawer.pas"/> <Filename Value="..\..\..\lazarussvn\lcl\controls.pp"/>
<Caret Line="110" Column="1" TopLine="100"/> <Caret Line="1232" Column="30" TopLine="1218"/>
</Position2> </Position2>
<Position3> <Position3>
<Filename Value="chessdrawer.pas"/> <Filename Value="chessdrawer.pas"/>
<Caret Line="113" Column="1" TopLine="100"/> <Caret Line="238" Column="44" TopLine="212"/>
</Position3> </Position3>
<Position4> <Position4>
<Filename Value="chessdrawer.pas"/> <Filename Value="mainform.pas"/>
<Caret Line="114" Column="1" TopLine="100"/> <Caret Line="21" Column="71" TopLine="5"/>
</Position4> </Position4>
<Position5> <Position5>
<Filename Value="chessdrawer.pas"/> <Filename Value="chessgame.pas"/>
<Caret Line="116" Column="1" TopLine="100"/> <Caret Line="49" Column="40" TopLine="34"/>
</Position5> </Position5>
<Position6> <Position6>
<Filename Value="chessdrawer.pas"/> <Filename Value="chessgame.pas"/>
<Caret Line="117" Column="1" TopLine="100"/> <Caret Line="151" Column="1" TopLine="135"/>
</Position6> </Position6>
<Position7> <Position7>
<Filename Value="chessdrawer.pas"/> <Filename Value="chessgame.pas"/>
<Caret Line="119" Column="1" TopLine="100"/> <Caret Line="54" Column="15" TopLine="39"/>
</Position7> </Position7>
<Position8> <Position8>
<Filename Value="chessdrawer.pas"/> <Filename Value="mainform.pas"/>
<Caret Line="120" Column="1" TopLine="100"/> <Caret Line="127" Column="1" TopLine="111"/>
</Position8> </Position8>
<Position9> <Position9>
<Filename Value="chessdrawer.pas"/> <Filename Value="mainform.pas"/>
<Caret Line="122" Column="1" TopLine="100"/> <Caret Line="109" Column="14" TopLine="95"/>
</Position9> </Position9>
<Position10> <Position10>
<Filename Value="chessdrawer.pas"/> <Filename Value="mainform.pas"/>
<Caret Line="123" Column="1" TopLine="100"/> <Caret Line="114" Column="14" TopLine="100"/>
</Position10> </Position10>
<Position11> <Position11>
<Filename Value="chessdrawer.pas"/> <Filename Value="chessdrawer.pas"/>
<Caret Line="110" Column="61" TopLine="94"/> <Caret Line="47" Column="21" TopLine="33"/>
</Position11> </Position11>
<Position12> <Position12>
<Filename Value="chessdrawer.pas"/> <Filename Value="mainform.pas"/>
<Caret Line="123" Column="29" TopLine="102"/> <Caret Line="114" Column="14" TopLine="100"/>
</Position12> </Position12>
<Position13> <Position13>
<Filename Value="chessdrawer.pas"/> <Filename Value="mainform.pas"/>
<Caret Line="28" Column="15" TopLine="21"/> <Caret Line="117" Column="43" TopLine="100"/>
</Position13> </Position13>
<Position14> <Position14>
<Filename Value="chessdrawer.pas"/> <Filename Value="mainform.pas"/>
<Caret Line="131" Column="1" TopLine="112"/> <Caret Line="111" Column="1" TopLine="100"/>
</Position14> </Position14>
<Position15> <Position15>
<Filename Value="chessdrawer.pas"/> <Filename Value="mainform.pas"/>
<Caret Line="8" Column="73" TopLine="1"/> <Caret Line="112" Column="1" TopLine="100"/>
</Position15> </Position15>
<Position16> <Position16>
<Filename Value="chessdrawer.pas"/> <Filename Value="chessgame.pas"/>
<Caret Line="131" Column="15" TopLine="102"/> <Caret Line="133" Column="1" TopLine="119"/>
</Position16> </Position16>
<Position17> <Position17>
<Filename Value="chessdrawer.pas"/> <Filename Value="chessgame.pas"/>
<Caret Line="121" Column="21" TopLine="110"/> <Caret Line="136" Column="1" TopLine="119"/>
</Position17> </Position17>
<Position18> <Position18>
<Filename Value="chessdrawer.pas"/> <Filename Value="mainform.pas"/>
<Caret Line="32" Column="24" TopLine="9"/> <Caret Line="112" Column="1" TopLine="103"/>
</Position18> </Position18>
<Position19> <Position19>
<Filename Value="chessdrawer.pas"/> <Filename Value="mainform.pas"/>
<Caret Line="185" Column="29" TopLine="171"/> <Caret Line="122" Column="1" TopLine="103"/>
</Position19> </Position19>
<Position20> <Position20>
<Filename Value="chessdrawer.pas"/> <Filename Value="mainform.pas"/>
<Caret Line="113" Column="70" TopLine="99"/> <Caret Line="123" Column="1" TopLine="103"/>
</Position20> </Position20>
<Position21> <Position21>
<Filename Value="chessdrawer.pas"/> <Filename Value="mainform.pas"/>
<Caret Line="119" Column="21" TopLine="105"/> <Caret Line="121" Column="43" TopLine="104"/>
</Position21> </Position21>
<Position22> <Position22>
<Filename Value="chessdrawer.pas"/> <Filename Value="mainform.pas"/>
<Caret Line="106" Column="36" TopLine="99"/> <Caret Line="123" Column="1" TopLine="104"/>
</Position22> </Position22>
<Position23> <Position23>
<Filename Value="chessdrawer.pas"/> <Filename Value="chessgame.pas"/>
<Caret Line="163" Column="48" TopLine="148"/> <Caret Line="157" Column="15" TopLine="143"/>
</Position23> </Position23>
<Position24> <Position24>
<Filename Value="chessdrawer.pas"/> <Filename Value="mainform.pas"/>
<Caret Line="161" Column="1" TopLine="148"/> <Caret Line="56" Column="29" TopLine="33"/>
</Position24> </Position24>
<Position25> <Position25>
<Filename Value="chessdrawer.pas"/> <Filename Value="mainform.pas"/>
<Caret Line="142" Column="15" TopLine="136"/> <Caret Line="85" Column="1" TopLine="82"/>
</Position25> </Position25>
<Position26> <Position26>
<Filename Value="chessdrawer.pas"/> <Filename Value="mainform.pas"/>
<Caret Line="165" Column="43" TopLine="148"/> <Caret Line="87" Column="57" TopLine="72"/>
</Position26> </Position26>
<Position27>
<Filename Value="mainform.pas"/>
<Caret Line="56" Column="29" TopLine="47"/>
</Position27>
<Position28>
<Filename Value="mainform.pas"/>
<Caret Line="79" Column="36" TopLine="79"/>
</Position28>
<Position29>
<Filename Value="mainform.pas"/>
<Caret Line="111" Column="3" TopLine="97"/>
</Position29>
<Position30>
<Filename Value="mainform.pas"/>
<Caret Line="122" Column="26" TopLine="108"/>
</Position30>
</JumpHistory> </JumpHistory>
</ProjectOptions> </ProjectOptions>
<CompilerOptions> <CompilerOptions>

View File

@ -1,19 +1,19 @@
object formChess: TformChess object formChess: TformChess
Left = 181 Left = 181
Height = 300 Height = 433
Top = 209 Top = 209
Width = 240 Width = 240
Caption = 'FP Chess 0.1' Caption = 'FP Chess 0.1'
ClientHeight = 300 ClientHeight = 433
ClientWidth = 240 ClientWidth = 240
OnCreate = FormCreate OnCreate = FormCreate
LCLVersion = '0.9.29' LCLVersion = '0.9.29'
object notebookMain: TUntabbedNotebook object notebookMain: TUntabbedNotebook
Left = 0 Left = 0
Height = 300 Height = 433
Top = 0 Top = 0
Width = 240 Width = 240
PageIndex = 0 PageIndex = 3
Align = alClient Align = alClient
TabOrder = 0 TabOrder = 0
TabStop = True TabStop = True
@ -111,8 +111,8 @@ object formChess: TformChess
end end
end end
object pageConfigConnection: TUNBPage object pageConfigConnection: TUNBPage
ClientWidth = 240 ClientWidth = 480
ClientHeight = 300 ClientHeight = 600
object Label3: TLabel object Label3: TLabel
Left = 0 Left = 0
Height = 32 Height = 32
@ -176,8 +176,8 @@ object formChess: TformChess
end end
end end
object pageConnecting: TUNBPage object pageConnecting: TUNBPage
ClientWidth = 240 ClientWidth = 480
ClientHeight = 300 ClientHeight = 600
object Label4: TLabel object Label4: TLabel
Left = 0 Left = 0
Height = 32 Height = 32
@ -200,7 +200,7 @@ object formChess: TformChess
end end
object pageGame: TUNBPage object pageGame: TUNBPage
ClientWidth = 240 ClientWidth = 240
ClientHeight = 300 ClientHeight = 433
object Label5: TLabel object Label5: TLabel
Left = 0 Left = 0
Height = 32 Height = 32
@ -213,6 +213,14 @@ object formChess: TformChess
ParentColor = False ParentColor = False
ParentFont = False ParentFont = False
end end
object labelPos: TLabel
Left = 8
Height = 14
Top = 392
Width = 40
Caption = 'labelPos'
ParentColor = False
end
end end
end end
end end

View File

@ -5,13 +5,24 @@ unit mainform;
interface interface
uses uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls, Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs,
ComCtrls, StdCtrls, Buttons, ExtCtrls, ComCtrls, StdCtrls, Buttons,
// //
chessdrawer, chessgame, chessconfig; chessdrawer, chessgame, chessconfig;
type type
{ TFormDrawerDelegate }
TFormDrawerDelegate = class(TChessDrawerDelegate)
public
procedure HandleMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); override;
procedure HandleMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer); override;
procedure HandleMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer); override;
end;
{ TformChess } { TformChess }
TformChess = class(TForm) TformChess = class(TForm)
@ -26,6 +37,7 @@ type
Label4: TLabel; Label4: TLabel;
Label5: TLabel; Label5: TLabel;
Label6: TLabel; Label6: TLabel;
labelPos: TLabel;
LabeledEdit1: TLabeledEdit; LabeledEdit1: TLabeledEdit;
LabeledEdit2: TLabeledEdit; LabeledEdit2: TLabeledEdit;
editPlayerName: TLabeledEdit; editPlayerName: TLabeledEdit;
@ -41,10 +53,12 @@ type
{ private declarations } { private declarations }
public public
{ public declarations } { public declarations }
procedure UpdateCaptions;
end; end;
var var
formChess: TformChess; formChess: TformChess;
vFormDrawerDelegate: TFormDrawerDelegate;
implementation implementation
@ -62,6 +76,17 @@ begin
else if Sender = btnDirectComm then notebookMain.PageIndex := 1; else if Sender = btnDirectComm then notebookMain.PageIndex := 1;
end; end;
procedure TformChess.UpdateCaptions;
var
CurPlayerStr: string;
begin
if vChessGame.CurrentPlayerIsWhite then CurPlayerStr := 'White playing'
else CurPlayerStr := 'Black playing';
formChess.labelPos.Caption := Format(CurPlayerStr + ' X: %d Y: %d',
[vChessGame.MouseMovePos.X, vChessGame.MouseMovePos.Y]);
end;
procedure TformChess.FormCreate(Sender: TObject); procedure TformChess.FormCreate(Sender: TObject);
begin begin
// Creation of internal components // Creation of internal components
@ -71,10 +96,56 @@ begin
vChessDrawer.Left := 20; vChessDrawer.Left := 20;
vChessDrawer.Height := INT_CHESSBOARD_SIZE; vChessDrawer.Height := INT_CHESSBOARD_SIZE;
vChessDrawer.Width := INT_CHESSBOARD_SIZE; vChessDrawer.Width := INT_CHESSBOARD_SIZE;
vChessDrawer.SetDelegate(vFormDrawerDelegate);
// Loading of resources // Loading of resources
vChessDrawer.LoadImages(); vChessDrawer.LoadImages();
end; end;
{ TFormDrawerDelegate }
procedure TFormDrawerDelegate.HandleMouseMove(Sender: TObject;
Shift: TShiftState; X, Y: Integer);
begin
vChessGame.MouseMovePos := vChessGame.ClientToBoardCoords(Point(X, Y));
formChess.UpdateCaptions;
end;
procedure TFormDrawerDelegate.HandleMouseUp(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
lCoords: TPoint;
begin
vChessGame.Dragging := False;
lCoords := vChessGame.ClientToBoardCoords(Point(X, Y));
if not vChessGame.MovePiece(vChessGame.DragStart, lCoords) then Exit;
vChessDrawer.Invalidate;
formChess.UpdateCaptions;
end;
procedure TFormDrawerDelegate.HandleMouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
lCoords: TPoint;
begin
lCoords := vChessGame.ClientToBoardCoords(Point(X, Y));
if not vChessGame.CheckStartMove(lCoords) then Exit;
vChessGame.Dragging := True;
vChessGame.DragStart := lCoords;
vChessDrawer.Invalidate;
formChess.UpdateCaptions;
end;
initialization
vFormDrawerDelegate := TFormDrawerDelegate.Create;
finalization
vFormDrawerDelegate.Free;
end. end.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 12 KiB

After

Width:  |  Height:  |  Size: 6.3 KiB