📄 main.cpp
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "thdCalcQ.h"
#include "About.h"
#include "Main.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TfrmQueen *frmQueen;
//---------------------------------------------------------------------------
//===========================================================================
// PRIVATE FUNCTION DEFINITION
//===========================================================================
QPoint __fastcall TfrmQueen::TellGrid(const int X,const int Y)
{
QPoint Grid;
for(int i=0;i<GRIDCOUNT;i++)
{
for(int j=0;j<GRIDCOUNT;j++)
{
int gLeft = i*(IMGLEN+1);
int gTop = j*(IMGLEN+1);
int gRight = (i+1)*(IMGLEN+1);
int gBottom = (j+1)*(IMGLEN+1);
if(X>=gLeft && X<gRight && Y>=gTop && Y<gBottom)
{
Grid.X = gLeft+1;
Grid.Y = gTop+1;
Grid.ColPos = i;
if((i%2) == (j%2))
Grid.COLOR = clGreen;
else
Grid.COLOR = clWhite;
return Grid;
}
}
}
return Grid;
}
//---------------------------------------------------------------------------
void __fastcall TfrmQueen::ShowBoard(void)
{
const int BRDGRID = IMGLEN + 1;
const int PBWIDTH = BRDGRID * GRIDCOUNT;
//Draw the lines of the chessboard
pbBoard->Canvas->Pen->Color = clBlack;
for(int i=0;i<=GRIDCOUNT;++i)
{
pbBoard->Canvas->MoveTo(i*BRDGRID,0);
pbBoard->Canvas->LineTo(i*BRDGRID,PBWIDTH);
pbBoard->Canvas->MoveTo(0,i*BRDGRID);
pbBoard->Canvas->LineTo(PBWIDTH,i*BRDGRID);
}
//Fill the grid of the chessboard with different color
for(int i=0;i<GRIDCOUNT/2;++i)
{
for(int j=0;j<GRIDCOUNT/2;++j)
{
TRect rect;
pbBoard->Canvas->Brush->Color = clGreen;
rect.Left = i*BRDGRID*2+1;
rect.Top = j*BRDGRID*2+1;
rect.Right = rect.Left+IMGLEN;
rect.Bottom = rect.top+IMGLEN;
pbBoard->Canvas->FillRect(rect);
rect.Left = (i*2+1)*BRDGRID+1;
rect.Top = (j*2+1)*BRDGRID+1;
rect.Right = rect.Left+IMGLEN;
rect.Bottom = rect.top+IMGLEN;
pbBoard->Canvas->FillRect(rect);
pbBoard->Canvas->Brush->Color = clWhite;
rect.Left = (i*2+1)*BRDGRID+1;
rect.Top = j*2*BRDGRID+1;
rect.Right = rect.Left+IMGLEN;
rect.Bottom = rect.top+IMGLEN;
pbBoard->Canvas->FillRect(rect);
rect.Left = i*2*BRDGRID+1;
rect.Top = (j*2+1)*BRDGRID+1;
rect.Right = rect.Left+IMGLEN;
rect.Bottom = rect.top+IMGLEN;
pbBoard->Canvas->FillRect(rect);
}
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmQueen::PutChessMan(const QPoint &Grid,const QImage myImage,bool isQ)
{
TGraphic *Image;
switch(myImage)
{
case TQUEEN:
{
Image = imgTQueen->Graphic;
break;
}
case QUEEN:
{
if(Grid.COLOR == clWhite)
Image = imgWQueen->Graphic;
else
Image = imgGQueen->Graphic;
break;
}
case ERRQUEEN:
{
if(Grid.COLOR == clWhite)
Image = imgWErrQ->Graphic;
else
Image = imgGErrQ->Graphic;
break;
}
case QPOINT:
{
if(Grid.COLOR == clWhite)
Image = imgWPoint->Graphic;
else
Image = imgGPoint->Graphic;
break;
}
}
pbBoard->Canvas->Draw(Grid.X,Grid.Y,Image);
if(isQ)
{
pbBoard->Canvas->Pen->Color = clRed;
pbBoard->Canvas->MoveTo(Grid.X+1,Grid.Y+1);
pbBoard->Canvas->LineTo(Grid.X+IMGLEN-1,Grid.Y+1);
pbBoard->Canvas->LineTo(Grid.X+IMGLEN-1,Grid.Y+IMGLEN-1);
pbBoard->Canvas->LineTo(Grid.X+1,Grid.Y+IMGLEN-1);
pbBoard->Canvas->LineTo(Grid.X+1,Grid.Y+1);
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmQueen::ShowChessMan(void)
{
if(!Queens.isEmpty())
for(int i=1;i<=Queens.length();++i)
PutChessMan(Queens[i],QUEEN);
if(Queens.GetFQueenPos() != 0)
PutChessMan(Queens.GetFQueen(),QUEEN,true);
}
//---------------------------------------------------------------------------
void __fastcall TfrmQueen::ClearChessMan(void)
{
if(!Queens.isEmpty())
{
QPoint QGrid;
if(Queens.GetFQueenPos() != 0)
{
QGrid = Queens.GetFQueen();
TRect rect;
rect.Left = QGrid.X;
rect.Top = QGrid.Y;
rect.Right = rect.Left + IMGLEN;
rect.Bottom = rect.Top + IMGLEN;
pbBoard->Canvas->Brush->Color = QGrid.COLOR;
pbBoard->Canvas->FillRect(rect);
}
for(int i=1;i<=Queens.length();++i)
{
QGrid = Queens[i];
TRect rect;
rect.Left = QGrid.X;
rect.Top = QGrid.Y;
rect.Right = rect.Left + IMGLEN;
rect.Bottom = rect.Top + IMGLEN;
pbBoard->Canvas->Brush->Color = QGrid.COLOR;
pbBoard->Canvas->FillRect(rect);
}
Queens.clear();
}
}
//===========================================================================
// DEFINITION OF THE FUNCTION OF THE FORM
//===========================================================================
//---------------------------------------------------------------------------
__fastcall TfrmQueen::TfrmQueen(TComponent* Owner)
: TForm(Owner)
{
CanPut = false;
OGrid.X = 1;
OGrid.Y = 1;
OGrid.ColPos = -1;
OGrid.COLOR = clGreen;
AnsiString AppPath = ExtractFilePath(Application->ExeName) + "BitMaps\\";
imgWQueen = new TPicture;
imgGQueen = new TPicture;
imgWErrQ = new TPicture;
imgGErrQ = new TPicture;
imgWPoint = new TPicture;
imgGPoint = new TPicture;
imgTQueen = new TPicture;
imgWQueen->LoadFromFile(AppPath+"WQueen.bmp");
imgGQueen->LoadFromFile(AppPath+"GQueen.bmp");
imgWErrQ->LoadFromFile(AppPath+"WErrQ.bmp");
imgGErrQ->LoadFromFile(AppPath+"GErrQ.bmp");
imgWPoint->LoadFromFile(AppPath+"WPoint.bmp");
imgGPoint->LoadFromFile(AppPath+"GPoint.bmp");
imgTQueen->LoadFromFile(AppPath+"TQueen.bmp");
}
//---------------------------------------------------------------------------
void __fastcall TfrmQueen::FormShow(TObject *Sender)
{
pbBoard->Left = 2;
pbBoard->Top = 2;
pbBoard->Width = (IMGLEN+1)*GRIDCOUNT+1;
pbBoard->Height = (IMGLEN+1)*GRIDCOUNT+1;
}
//---------------------------------------------------------------------------
void __fastcall TfrmQueen::FormDestroy(TObject *Sender)
{
delete imgWQueen;
delete imgGQueen;
delete imgWErrQ;
delete imgGErrQ;
delete imgWPoint;
delete imgGPoint;
delete imgTQueen;
}
//===========================================================================
// DEFINITION OF THE FUNCTION OF THE BUTTONS
//===========================================================================
//---------------------------------------------------------------------------
void __fastcall TfrmQueen::btnQueenClick(TObject *Sender)
{
if(btnQueen->Caption == "摆皇后(&Q)")
{
ClearChessMan();
CanPut = true;
lblDescription->Caption = "请在棋盘的任意位置摆上皇后,单击鼠标左键确定。要取消摆位请单击右键!";
}
else if(btnQueen->Caption == "暂 停(&P)")
{
lblDescription->Caption = "用户暂停!";
btnQueen->Caption = "继 续(&R)";
CalcQueen->Suspend();
Queens = CalcQueen->Queens;
}
else
{
lblDescription->Caption = "正在摆皇后,请稍候...";
btnQueen->Caption = "暂 停(&P)";
CalcQueen->Resume();
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmQueen::btnAboutClick(TObject *Sender)
{
//Display the about dialog form
TfrmAbout *AboutForm;
try
{
if(btnQueen->Caption == "暂 停(&P)")
{
CalcQueen->Suspend();
Queens = CalcQueen->Queens;
}
AboutForm = new TfrmAbout(Application);
AboutForm->ShowModal();
delete AboutForm;
if(btnQueen->Caption == "暂 停(&P)")
CalcQueen->Resume();
}
catch(Exception &e)
{
delete AboutForm;
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmQueen::btnQuitClick(TObject *Sender)
{
//Quit the program
Close();
}
//===========================================================================
// DEFINITION OF THE FUNCTION OF THE PAINTBOX
//===========================================================================
//---------------------------------------------------------------------------
void __fastcall TfrmQueen::pbBoardPaint(TObject *Sender)
{
ShowBoard();
ShowChessMan();
}
//---------------------------------------------------------------------------
void __fastcall TfrmQueen::pbBoardMouseUp(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
if(CanPut)
{
CanPut = false;
btnQueen->Caption = "暂 停(&P)";
lblDescription->Caption = "";
QPoint Grid = TellGrid(X,Y);
if(Button == mbLeft)
{
lblDescription->Caption = "正在摆皇后,请稍候...";
isCalc = true;
gpbOptions->Enabled = false;
PutChessMan(Grid,QUEEN,true);
Queens.SetFQueen(Grid,(Grid.Y-1)/(IMGLEN+1)+1);
CalcQueen = new TthdCalc(true);
CalcQueen->Resume();
}
else if(Button == mbRight)
{
btnQueen->Caption = "摆皇后(&Q)";
TRect rect;
rect.Left = Grid.X;
rect.Top = Grid.Y;
rect.Right = rect.Left + IMGLEN;
rect.Bottom = rect.Top + IMGLEN;
pbBoard->Canvas->Brush->Color = Grid.COLOR;
pbBoard->Canvas->FillRect(rect);
}
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmQueen::pbBoardMouseMove(TObject *Sender,
TShiftState Shift, int X, int Y)
{
if(CanPut)
{
QPoint CurPoint = TellGrid(X,Y);
if(CurPoint.X != OGrid.X || CurPoint.Y != OGrid.Y)
{
TRect rect;
rect.Left = OGrid.X;
rect.Top = OGrid.Y;
rect.Right = rect.Left + IMGLEN;
rect.Bottom = rect.Top + IMGLEN;
pbBoard->Canvas->Brush->Color = OGrid.COLOR;
pbBoard->Canvas->FillRect(rect);
PutChessMan(CurPoint,TQUEEN);
OGrid = CurPoint;
}
}
}
//===========================================================================
// OTHER FUNCTION DEFINITION
//===========================================================================
//---------------------------------------------------------------------------
void __fastcall TfrmQueen::chkAnimationClick(TObject *Sender)
{
if(chkAnimation->Checked)
{
chkError->Enabled = true;
if(chkError->Checked)
pnlSpeed->Visible = true;
}
else
{
chkError->Enabled = false;
if(chkError->Checked)
pnlSpeed->Visible = false;
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmQueen::chkErrorClick(TObject *Sender)
{
if(chkError->Checked)
pnlSpeed->Visible = true;
else
pnlSpeed->Visible =false;
}
//---------------------------------------------------------------------------
void __fastcall TfrmQueen::TreadSuspend(TWMShowWindow& msg)
{
if(btnQueen->Caption == "暂 停(&P)")
{
if(msg.Status == SW_PARENTCLOSING)
CalcQueen->Suspend();
else if(msg.Status == SW_PARENTOPENING)
CalcQueen->Resume();
}
TForm::Dispatch(&msg);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -