fpchess: Adds coordinates writing to the log

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@1875 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
sekelsenmat
2011-08-30 15:05:14 +00:00
parent af711ad83f
commit f3267c88d3
5 changed files with 86 additions and 44 deletions

View File

@ -71,6 +71,21 @@ type
TChessGame = class TChessGame = class
private private
function WillKingBeInCheck(AFrom, ATo, AEnpassantToClear: TPoint): Boolean;
function IsKingInCheck(AKingPos: TPoint): Boolean;
procedure DoMovePiece(AFrom, ATo, AEnpassantToClear: TPoint);
function ValidateRookMove(AFrom, ATo: TPoint) : boolean;
procedure ResetCastleVar(AFrom : TPoint);
function ValidateKnightMove(AFrom, ATo: TPoint) : boolean;
function ValidateBishopMove(AFrom, ATo: TPoint) : boolean;
function ValidateQueenMove(AFrom, ATo: TPoint) : boolean;
function ValidateKingMove(AFrom, ATo: TPoint) : boolean;
function CheckPassageSquares(side: boolean; AFrom, ATo : TPoint) : boolean;
procedure DoCastle();
function ValidatePawnMove(AFrom, ATo: TPoint;
var AEnpassantSquare, AEnpassantSquareToClear: TPoint) : boolean;
function ValidatePawnSimpleCapture(AFrom,ATo: TPoint): Boolean;
function IsSquareOccupied(ASquare: TPoint): Boolean;
public public
Board: TChessBoard; Board: TChessBoard;
msg : String; msg : String;
@ -91,32 +106,21 @@ type
Castle:boolean;//If the move will be a castle. Castle:boolean;//If the move will be a castle.
CastleCord: TPoint; CastleCord: TPoint;
// Callbacks // Callbacks
OnMove: TOnMoveCallback; OnAfterMove: TOnMoveCallback; // For the modules
OnBeforeMove: TOnMoveCallback; // For the UI
// //
constructor Create; constructor Create;
procedure StartNewGame(APlayAsWhite: Boolean; AUseTimer: Boolean; APlayerTime: Integer); overload; procedure StartNewGame(APlayAsWhite: Boolean; AUseTimer: Boolean; APlayerTime: Integer); overload;
procedure StartNewGame(APlayAsWhite: Integer; AUseTimer: Boolean; APlayerTime: Integer); overload; procedure StartNewGame(APlayAsWhite: Integer; AUseTimer: Boolean; APlayerTime: Integer); overload;
function ClientToBoardCoords(AClientCoords: TPoint): TPoint; function ClientToBoardCoords(AClientCoords: TPoint): TPoint;
function BoardPosToChessCoords(APos: TPoint): string;
function CheckStartMove(AFrom: TPoint): Boolean; function CheckStartMove(AFrom: TPoint): Boolean;
function CheckEndMove(ATo: TPoint): Boolean; function CheckEndMove(ATo: TPoint): Boolean;
function WillKingBeInCheck(AFrom, ATo, AEnpassantToClear: TPoint): Boolean;
function IsKingInCheck(AKingPos: TPoint): Boolean;
function FindKing(): TPoint; function FindKing(): TPoint;
function MovePiece(AFrom, ATo: TPoint): Boolean; function MovePiece(AFrom, ATo: TPoint): Boolean;
procedure DoMovePiece(AFrom, ATo, AEnpassantToClear: TPoint);
function ValidateRookMove(AFrom, ATo: TPoint) : boolean;
procedure ResetCastleVar(AFrom : TPoint);
function ValidateKnightMove(AFrom, ATo: TPoint) : boolean;
function ValidateBishopMove(AFrom, ATo: TPoint) : boolean;
function ValidateQueenMove(AFrom, ATo: TPoint) : boolean;
function ValidateKingMove(AFrom, ATo: TPoint) : boolean;
function CheckPassageSquares(side: boolean; AFrom, ATo : TPoint) : boolean;
procedure DoCastle();
function ValidatePawnMove(AFrom, ATo: TPoint;
var AEnpassantSquare, AEnpassantSquareToClear: TPoint) : boolean;
function ValidatePawnSimpleCapture(AFrom,ATo: TPoint): Boolean;
function IsSquareOccupied(ASquare: TPoint): Boolean;
procedure UpdateTimes(); procedure UpdateTimes();
function GetCurrentPlayerName(): string;
function GetCurrentPlayerColor(): string;
end; end;
var var
@ -259,11 +263,14 @@ begin
// //
UpdateTimes(); UpdateTimes();
// Notify of the move
if Assigned(OnBeforeMove) then OnBeforeMove(AFrom, ATo);
// Change player // Change player
IsWhitePlayerTurn := not IsWhitePlayerTurn; IsWhitePlayerTurn := not IsWhitePlayerTurn;
// Notify of the move // Notify of the move
if Assigned(OnMove) then OnMove(AFrom, ATo); if Assigned(OnAfterMove) then OnAfterMove(AFrom, ATo);
end; end;
{ Really moves the piece without doing any check } { Really moves the piece without doing any check }
@ -693,12 +700,32 @@ begin
else BlackPlayerTime := BlackPlayerTime - lTimeDelta; else BlackPlayerTime := BlackPlayerTime - lTimeDelta;
end; end;
function TChessGame.GetCurrentPlayerName: string;
begin
if IsWhitePlayerTurn then Result := 'White'
else Result := 'Black';
end;
function TChessGame.GetCurrentPlayerColor: string;
begin
if IsWhitePlayerTurn then Result := 'White'
else Result := 'Black';
end;
function TChessGame.ClientToBoardCoords(AClientCoords: TPoint): TPoint; function TChessGame.ClientToBoardCoords(AClientCoords: TPoint): TPoint;
begin begin
Result.X := 1 + AClientCoords.X div INT_CHESSTILE_SIZE; Result.X := 1 + AClientCoords.X div INT_CHESSTILE_SIZE;
Result.Y := 1 + (INT_CHESSBOARD_SIZE - AClientCoords.Y) div INT_CHESSTILE_SIZE; Result.Y := 1 + (INT_CHESSBOARD_SIZE - AClientCoords.Y) div INT_CHESSTILE_SIZE;
end; end;
function TChessGame.BoardPosToChessCoords(APos: TPoint): string;
var
lStr: string;
begin
lStr := Char(APos.X + 96);
Result := Format('%s%d', [lStr, APos.Y]);
end;
// Check if we are moving to either an empty space or to an enemy piece // Check if we are moving to either an empty space or to an enemy piece
function TChessGame.CheckEndMove(ATo: TPoint): Boolean; function TChessGame.CheckEndMove(ATo: TPoint): Boolean;
begin begin

View File

@ -48,7 +48,10 @@ var
lModule: TChessModule; lModule: TChessModule;
begin begin
lModule := GetChessModule(gSelectedModuleIndex); lModule := GetChessModule(gSelectedModuleIndex);
lModule.HandleOnMove(AFrom, ATo);
// If we are getting notified by a computer move, don't notify the module yet again
if not lModule.IsMovingAllowedNow() then
lModule.HandleOnMove(AFrom, ATo);
end; end;
procedure RegisterChessModule(AModule: TChessModule); procedure RegisterChessModule(AModule: TChessModule);
@ -98,7 +101,7 @@ end;
initialization initialization
gChessModules := TList.Create; gChessModules := TList.Create;
vChessGame.OnMove := @HandleOnMove; vChessGame.OnAfterMove := @HandleOnMove;
finalization finalization
gChessModules.Free; gChessModules.Free;
end. end.

View File

@ -278,9 +278,6 @@ var
Escape: boolean; Escape: boolean;
Score: Integer; Score: Integer;
begin begin
// If we are getting notified by a computer move, exit immediately
if IsMovingAllowedNow() then Exit;
// initialization // initialization
Escape := False; Escape := False;
Score := 0; Score := 0;

View File

@ -45,17 +45,17 @@ object formChess: TformChess
end end
object Label6: TLabel object Label6: TLabel
Left = 28 Left = 28
Height = 17 Height = 18
Top = 128 Top = 128
Width = 52 Width = 56
Caption = 'Start as:' Caption = 'Start as:'
ParentColor = False ParentColor = False
end end
object Label7: TLabel object Label7: TLabel
Left = 80 Left = 80
Height = 17 Height = 18
Top = 175 Top = 175
Width = 150 Width = 152
Caption = 'minutes for each player' Caption = 'minutes for each player'
ParentColor = False ParentColor = False
end end
@ -70,7 +70,7 @@ object formChess: TformChess
end end
object editPlayerName: TLabeledEdit object editPlayerName: TLabeledEdit
Left = 88 Left = 88
Height = 22 Height = 25
Top = 104 Top = 104
Width = 120 Width = 120
EditLabel.AnchorSideLeft.Control = editPlayerName EditLabel.AnchorSideLeft.Control = editPlayerName
@ -78,10 +78,10 @@ object formChess: TformChess
EditLabel.AnchorSideTop.Side = asrCenter EditLabel.AnchorSideTop.Side = asrCenter
EditLabel.AnchorSideRight.Control = editPlayerName EditLabel.AnchorSideRight.Control = editPlayerName
EditLabel.AnchorSideBottom.Control = editPlayerName EditLabel.AnchorSideBottom.Control = editPlayerName
EditLabel.Left = 6 EditLabel.Left = 2
EditLabel.Height = 17 EditLabel.Height = 18
EditLabel.Top = 107 EditLabel.Top = 107
EditLabel.Width = 79 EditLabel.Width = 83
EditLabel.Caption = 'Player Name' EditLabel.Caption = 'Player Name'
EditLabel.ParentColor = False EditLabel.ParentColor = False
LabelPosition = lpLeft LabelPosition = lpLeft
@ -89,7 +89,7 @@ object formChess: TformChess
end end
object comboStartColor: TComboBox object comboStartColor: TComboBox
Left = 88 Left = 88
Height = 21 Height = 27
Top = 128 Top = 128
Width = 120 Width = 120
ItemHeight = 0 ItemHeight = 0
@ -103,9 +103,9 @@ object formChess: TformChess
end end
object checkTimer: TCheckBox object checkTimer: TCheckBox
Left = 24 Left = 24
Height = 18 Height = 21
Top = 152 Top = 152
Width = 212 Width = 220
Caption = 'Set a time limit for each Player' Caption = 'Set a time limit for each Player'
Checked = True Checked = True
State = cbChecked State = cbChecked
@ -113,7 +113,7 @@ object formChess: TformChess
end end
object spinPlayerTime: TSpinEdit object spinPlayerTime: TSpinEdit
Left = 21 Left = 21
Height = 16 Height = 25
Top = 176 Top = 176
Width = 50 Width = 50
TabOrder = 4 TabOrder = 4
@ -121,7 +121,7 @@ object formChess: TformChess
end end
object comboGameMode: TComboBox object comboGameMode: TComboBox
Left = 8 Left = 8
Height = 21 Height = 27
Top = 74 Top = 74
Width = 346 Width = 346
ItemHeight = 0 ItemHeight = 0
@ -140,7 +140,7 @@ object formChess: TformChess
end end
object editLocalIP: TLabeledEdit object editLocalIP: TLabeledEdit
Left = 120 Left = 120
Height = 22 Height = 25
Top = 200 Top = 200
Width = 120 Width = 120
EditLabel.AnchorSideLeft.Control = editLocalIP EditLabel.AnchorSideLeft.Control = editLocalIP
@ -148,10 +148,10 @@ object formChess: TformChess
EditLabel.AnchorSideTop.Side = asrCenter EditLabel.AnchorSideTop.Side = asrCenter
EditLabel.AnchorSideRight.Control = editLocalIP EditLabel.AnchorSideRight.Control = editLocalIP
EditLabel.AnchorSideBottom.Control = editLocalIP EditLabel.AnchorSideBottom.Control = editLocalIP
EditLabel.Left = 12 EditLabel.Left = 15
EditLabel.Height = 17 EditLabel.Height = 18
EditLabel.Top = 203 EditLabel.Top = 203
EditLabel.Width = 105 EditLabel.Width = 102
EditLabel.Caption = 'Your IP Address:' EditLabel.Caption = 'Your IP Address:'
EditLabel.ParentColor = False EditLabel.ParentColor = False
LabelPosition = lpLeft LabelPosition = lpLeft
@ -173,15 +173,15 @@ object formChess: TformChess
ClientHeight = 500 ClientHeight = 500
object labelPos: TLabel object labelPos: TLabel
Left = 8 Left = 8
Height = 17 Height = 18
Top = 392 Top = 392
Width = 53 Width = 54
Caption = 'labelPos' Caption = 'labelPos'
ParentColor = False ParentColor = False
end end
object labelTime: TLabel object labelTime: TLabel
Left = 8 Left = 8
Height = 17 Height = 18
Top = 408 Top = 408
Width = 62 Width = 62
Caption = 'labelTime' Caption = 'labelTime'
@ -199,6 +199,7 @@ object formChess: TformChess
Anchors = [akTop, akLeft, akRight, akBottom] Anchors = [akTop, akLeft, akRight, akBottom]
BorderSpacing.Right = 5 BorderSpacing.Right = 5
BorderSpacing.Bottom = 5 BorderSpacing.Bottom = 5
ScrollBars = ssVertical
TabOrder = 0 TabOrder = 0
end end
object btnResign: TBitBtn object btnResign: TBitBtn
@ -229,8 +230,8 @@ object formChess: TformChess
end end
end end
object pageWebservice: TPage object pageWebservice: TPage
ClientWidth = 1440 ClientWidth = 2880
ClientHeight = 1732 ClientHeight = 3464
object Label8: TLabel object Label8: TLabel
Left = 0 Left = 0
Height = 32 Height = 32

View File

@ -77,6 +77,8 @@ var
formChess: TformChess; formChess: TformChess;
vFormDrawerDelegate: TFormDrawerDelegate; vFormDrawerDelegate: TFormDrawerDelegate;
procedure HandleOnMove(AFrom, ATo: TPoint);
implementation implementation
{$R *.lfm} {$R *.lfm}
@ -130,6 +132,15 @@ begin
GetChessModule(ANewIndex).ShowUserInterface(panelModules); GetChessModule(ANewIndex).ShowUserInterface(panelModules);
end; end;
procedure HandleOnMove(AFrom, ATo: TPoint);
var
lStr: String;
begin
lStr := vChessGame.GetCurrentPlayerColor();
lStr := Format('%s executed the move %s-%s', [lStr, vChessGame.BoardPosToChessCoords(AFrom), vChessGame.BoardPosToChessCoords(ATo)]);
formChess.MemoDebug.Lines.Add(lStr);
end;
procedure TformChess.UpdateCaptions; procedure TformChess.UpdateCaptions;
var var
lStr: string; lStr: string;
@ -178,6 +189,9 @@ begin
gSelectedModuleIndex := 0; gSelectedModuleIndex := 0;
end; end;
gChessModulesDebugOutputDestiny := memoDebug; gChessModulesDebugOutputDestiny := memoDebug;
// Prepare the move callback
vChessGame.OnBeforeMove := @HandleOnMove;
end; end;
procedure TformChess.btnQuitClick(Sender: TObject); procedure TformChess.btnQuitClick(Sender: TObject);