2010-09-21 06:33:18 +00:00
|
|
|
unit mainform;
|
2010-09-16 11:52:47 +00:00
|
|
|
|
|
|
|
{$mode objfpc}{$H+}
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
2010-09-23 10:43:52 +00:00
|
|
|
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs,
|
2010-09-23 15:40:21 +00:00
|
|
|
ExtCtrls, ComCtrls, StdCtrls, Buttons, Spin,
|
2010-09-21 06:33:18 +00:00
|
|
|
//
|
2011-04-07 06:26:28 +00:00
|
|
|
IDelphiChess_Intf,
|
|
|
|
//
|
2010-10-06 13:34:34 +00:00
|
|
|
chessdrawer, chessgame, chessconfig, chesstcputils;
|
2010-09-16 11:52:47 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
|
2010-09-23 10:43:52 +00:00
|
|
|
{ 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;
|
|
|
|
|
2010-09-21 06:33:18 +00:00
|
|
|
{ TformChess }
|
2010-09-16 11:52:47 +00:00
|
|
|
|
2010-09-21 06:33:18 +00:00
|
|
|
TformChess = class(TForm)
|
2010-10-06 15:59:48 +00:00
|
|
|
btnConnect: TBitBtn;
|
2011-04-07 06:26:28 +00:00
|
|
|
btnAI: TBitBtn;
|
2010-09-16 11:52:47 +00:00
|
|
|
btnSinglePlayer: TBitBtn;
|
2010-09-21 06:33:18 +00:00
|
|
|
btnDirectComm: TBitBtn;
|
2010-09-16 11:52:47 +00:00
|
|
|
BitBtn3: TBitBtn;
|
2010-09-23 15:40:21 +00:00
|
|
|
btnHotSeat: TBitBtn;
|
2011-04-07 06:26:28 +00:00
|
|
|
btnPlayAgainstAI: TButton;
|
2010-09-23 15:40:21 +00:00
|
|
|
checkTimer: TCheckBox;
|
2010-09-21 06:33:18 +00:00
|
|
|
comboStartColor: TComboBox;
|
2011-04-07 06:57:02 +00:00
|
|
|
editWebserviceURL: TLabeledEdit;
|
2010-09-16 11:52:47 +00:00
|
|
|
Label1: TLabel;
|
2011-04-07 06:57:02 +00:00
|
|
|
labelTime: TLabel;
|
2010-09-16 11:52:47 +00:00
|
|
|
Label2: TLabel;
|
|
|
|
Label3: TLabel;
|
|
|
|
Label4: TLabel;
|
2010-09-21 06:33:18 +00:00
|
|
|
Label5: TLabel;
|
|
|
|
Label6: TLabel;
|
2010-09-23 15:40:21 +00:00
|
|
|
Label7: TLabel;
|
2010-10-06 15:59:48 +00:00
|
|
|
Label8: TLabel;
|
2011-04-07 06:26:28 +00:00
|
|
|
Label9: TLabel;
|
|
|
|
editWebServiceAI: TLabeledEdit;
|
2010-09-23 10:43:52 +00:00
|
|
|
labelPos: TLabel;
|
2010-10-05 20:05:41 +00:00
|
|
|
editRemoteID: TLabeledEdit;
|
|
|
|
editLocalIP: TLabeledEdit;
|
2010-09-21 06:33:18 +00:00
|
|
|
editPlayerName: TLabeledEdit;
|
2011-04-04 16:09:05 +00:00
|
|
|
pageStart: TPage;
|
|
|
|
pageConfigConnection: TPage;
|
|
|
|
notebookMain: TNotebook;
|
|
|
|
pageConnecting: TPage;
|
2010-09-16 11:52:47 +00:00
|
|
|
ProgressBar1: TProgressBar;
|
2011-04-04 16:09:05 +00:00
|
|
|
pageGame: TPage;
|
2010-09-23 15:40:21 +00:00
|
|
|
spinPlayerTime: TSpinEdit;
|
|
|
|
timerChessTimer: TTimer;
|
2011-04-04 16:09:05 +00:00
|
|
|
pageWebservice: TPage;
|
2010-10-06 15:59:48 +00:00
|
|
|
procedure btnConnectClick(Sender: TObject);
|
2011-04-07 06:26:28 +00:00
|
|
|
procedure btnPlayAgainstAIClick(Sender: TObject);
|
2010-09-21 06:33:18 +00:00
|
|
|
procedure FormCreate(Sender: TObject);
|
2010-09-16 11:52:47 +00:00
|
|
|
procedure HandleMainScreenButton(Sender: TObject);
|
2011-04-04 16:09:05 +00:00
|
|
|
procedure pageBeforeShow(Sender: TObject; ANewPage: TPage; ANewIndex: Integer);
|
2010-09-23 15:40:21 +00:00
|
|
|
procedure timerChessTimerTimer(Sender: TObject);
|
2010-09-16 11:52:47 +00:00
|
|
|
private
|
|
|
|
{ private declarations }
|
2011-04-07 06:57:02 +00:00
|
|
|
function FormatTime(ATimeInMiliseconds: Integer): string;
|
2010-09-16 11:52:47 +00:00
|
|
|
public
|
|
|
|
{ public declarations }
|
2010-09-23 10:43:52 +00:00
|
|
|
procedure UpdateCaptions;
|
2011-04-07 06:26:28 +00:00
|
|
|
procedure InitializeGameModel;
|
|
|
|
end;
|
2010-09-16 11:52:47 +00:00
|
|
|
|
|
|
|
var
|
2010-09-21 06:33:18 +00:00
|
|
|
formChess: TformChess;
|
2010-09-23 10:43:52 +00:00
|
|
|
vFormDrawerDelegate: TFormDrawerDelegate;
|
2010-09-16 11:52:47 +00:00
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
{$R *.lfm}
|
|
|
|
|
2010-10-06 13:34:34 +00:00
|
|
|
const
|
|
|
|
INT_PAGE_START = 0;
|
|
|
|
INT_PAGE_CONFIGCONNECTION = 1;
|
|
|
|
INT_PAGE_CONNECTING = 2;
|
|
|
|
INT_PAGE_GAME = 3;
|
2011-04-07 06:26:28 +00:00
|
|
|
INT_PAGE_AI = 4;
|
2010-10-06 13:34:34 +00:00
|
|
|
|
2010-09-21 06:33:18 +00:00
|
|
|
{ TformChess }
|
2010-09-16 11:52:47 +00:00
|
|
|
|
2010-09-21 06:33:18 +00:00
|
|
|
procedure TformChess.HandleMainScreenButton(Sender: TObject);
|
2010-09-16 11:52:47 +00:00
|
|
|
begin
|
|
|
|
if Sender = btnSinglePlayer then
|
|
|
|
begin
|
2010-10-06 13:34:34 +00:00
|
|
|
notebookMain.PageIndex := INT_PAGE_GAME;
|
2011-04-07 06:26:28 +00:00
|
|
|
InitializeGameModel();
|
2010-09-23 15:40:21 +00:00
|
|
|
end
|
|
|
|
else if Sender = btnHotSeat then
|
|
|
|
begin
|
2010-10-06 13:34:34 +00:00
|
|
|
notebookMain.PageIndex := INT_PAGE_GAME;
|
2011-04-07 06:26:28 +00:00
|
|
|
InitializeGameModel();
|
2010-09-21 06:33:18 +00:00
|
|
|
end
|
2011-04-07 06:26:28 +00:00
|
|
|
else if Sender = btnDirectComm then notebookMain.PageIndex := INT_PAGE_CONFIGCONNECTION
|
|
|
|
else if Sender = btnAI then notebookMain.PageIndex := INT_PAGE_AI;
|
2010-09-21 06:33:18 +00:00
|
|
|
end;
|
|
|
|
|
2011-04-04 16:09:05 +00:00
|
|
|
procedure TformChess.pageBeforeShow(Sender: TObject; ANewPage: TPage; ANewIndex: Integer);
|
2010-10-05 20:05:41 +00:00
|
|
|
begin
|
2010-10-06 13:34:34 +00:00
|
|
|
if ANewIndex = INT_PAGE_CONFIGCONNECTION then
|
2010-10-05 20:05:41 +00:00
|
|
|
begin
|
2010-10-06 13:34:34 +00:00
|
|
|
editLocalIP.Text := ChessGetLocalIP();
|
2010-10-05 20:05:41 +00:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2010-09-23 15:40:21 +00:00
|
|
|
procedure TformChess.timerChessTimerTimer(Sender: TObject);
|
|
|
|
begin
|
|
|
|
vChessGame.UpdateTimes();
|
|
|
|
UpdateCaptions();
|
|
|
|
end;
|
|
|
|
|
2011-04-07 06:57:02 +00:00
|
|
|
function TformChess.FormatTime(ATimeInMiliseconds: Integer): string;
|
|
|
|
var
|
|
|
|
lTimePart: Integer;
|
|
|
|
begin
|
|
|
|
Result := '';
|
|
|
|
|
|
|
|
// Hours
|
|
|
|
lTimePart := ATimeInMiliseconds div (60*60*1000);
|
|
|
|
if lTimePart > 0 then
|
|
|
|
Result := IntToStr(lTimePart) + 'h';
|
|
|
|
|
|
|
|
// Minutes
|
|
|
|
lTimePart := (ATimeInMiliseconds div (60*1000)) mod 60;
|
|
|
|
if (lTimePart > 0) or (Result <> '') then
|
|
|
|
Result := Result + IntToStr(lTimePart) + 'm';
|
|
|
|
|
|
|
|
// Seconds
|
|
|
|
lTimePart := (ATimeInMiliseconds div (1000)) mod 60;
|
|
|
|
Result := Result + IntToStr(lTimePart) + 's';
|
|
|
|
|
|
|
|
// Miliseconds
|
|
|
|
lTimePart := ATimeInMiliseconds mod (1000);
|
|
|
|
Result := Result + IntToStr(lTimePart);
|
|
|
|
end;
|
|
|
|
|
2010-09-23 10:43:52 +00:00
|
|
|
procedure TformChess.UpdateCaptions;
|
|
|
|
var
|
2010-09-23 15:40:21 +00:00
|
|
|
lStr: string;
|
2010-09-23 10:43:52 +00:00
|
|
|
begin
|
2010-09-23 15:40:21 +00:00
|
|
|
if vChessGame.CurrentPlayerIsWhite then lStr := 'White playing'
|
|
|
|
else lStr := 'Black playing';
|
2010-09-23 10:43:52 +00:00
|
|
|
|
2010-09-23 15:40:21 +00:00
|
|
|
lStr := lStr + Format(' X: %d Y: %d',
|
2010-09-23 10:43:52 +00:00
|
|
|
[vChessGame.MouseMovePos.X, vChessGame.MouseMovePos.Y]);
|
2010-09-23 15:40:21 +00:00
|
|
|
|
|
|
|
formChess.labelPos.Caption := lStr;
|
2011-04-07 06:57:02 +00:00
|
|
|
|
|
|
|
lStr := Format('White time: %s Black time: %s',
|
|
|
|
[FormatTime(vChessGame.WhitePlayerTime), FormatTime(vChessGame.BlackPlayerTime)]);
|
|
|
|
|
|
|
|
formChess.labelTime.Caption := lStr;
|
2010-09-23 10:43:52 +00:00
|
|
|
end;
|
|
|
|
|
2011-04-07 06:26:28 +00:00
|
|
|
procedure TformChess.InitializeGameModel;
|
|
|
|
begin
|
|
|
|
vChessGame.StartNewGame(comboStartColor.ItemIndex, checkTimer.Checked, spinPlayerTime.Value);
|
|
|
|
end;
|
|
|
|
|
2010-09-21 06:33:18 +00:00
|
|
|
procedure TformChess.FormCreate(Sender: TObject);
|
|
|
|
begin
|
|
|
|
// Creation of internal components
|
|
|
|
vChessDrawer := TChessDrawer.Create(Self);
|
|
|
|
vChessDrawer.Parent := pageGame;
|
2010-09-22 15:45:23 +00:00
|
|
|
vChessDrawer.Top := 50;
|
2010-09-21 06:33:18 +00:00
|
|
|
vChessDrawer.Left := 20;
|
|
|
|
vChessDrawer.Height := INT_CHESSBOARD_SIZE;
|
|
|
|
vChessDrawer.Width := INT_CHESSBOARD_SIZE;
|
2010-09-23 10:43:52 +00:00
|
|
|
vChessDrawer.SetDelegate(vFormDrawerDelegate);
|
2010-09-21 06:33:18 +00:00
|
|
|
|
|
|
|
// Loading of resources
|
|
|
|
vChessDrawer.LoadImages();
|
2010-09-16 11:52:47 +00:00
|
|
|
end;
|
|
|
|
|
2010-10-06 15:59:48 +00:00
|
|
|
procedure TformChess.btnConnectClick(Sender: TObject);
|
|
|
|
begin
|
|
|
|
notebookMain.PageIndex := INT_PAGE_CONNECTING;
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
2011-04-07 06:26:28 +00:00
|
|
|
procedure TformChess.btnPlayAgainstAIClick(Sender: TObject);
|
|
|
|
begin
|
|
|
|
InitializeGameModel();
|
|
|
|
|
|
|
|
notebookMain.PageIndex := INT_PAGE_GAME;
|
|
|
|
|
|
|
|
if comboStartColor.ItemIndex = 0 then
|
|
|
|
GetNextMoveFromBorlandWS();
|
|
|
|
end;
|
|
|
|
|
2010-09-23 10:43:52 +00:00
|
|
|
{ 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;
|
|
|
|
|
2010-09-16 11:52:47 +00:00
|
|
|
end.
|
|
|
|
|