?? mainform1.~cpp
字號(hào):
RBishopPosDone = false;
if (ChessBoard[sq].color == black)
if (ChessBoard[sq].piece == bishop)
if ((sq != 0x52) && (sq != 0x56) && (sq != 0x70) && (sq != 0x74)
&& (sq != 0x78) && (sq != 0x92) && (sq != 0x96))
BBishopPosDone = false;
if (ChessBoard[sq].color == red)
if (ChessBoard[sq].piece == pawn)
{
if ((sq < 0x50) && (sq >= 0x30 ))
if ((sq == 0x31) || (sq == 0x33) || (sq ==0x35) || (sq == 0x37)
|| (sq == 0x41) || (sq == 0x43) || (sq == 0x45) || (sq == 0x47))
RPawnPosDone = false;
if ((sq >> 4) < 3)
RPawnPosDone = false;
}
if (ChessBoard[sq].color == black)
if (ChessBoard[sq].piece == pawn)
{
if ((sq >= 0x50) && (sq < 0x70 ))
if ((sq == 0x51) || (sq == 0x53) || (sq == 0x55) || (sq == 0x57)
|| (sq == 0x61) || (sq == 0x63) || (sq == 0x65) || (sq == 0x67))
BPawnPosDone = false;
if (( sq >> 4) >= 7)
BPawnPosDone = false;
}
}
if (RKingPosDone == false)
ErrorPosText = ErrorPosText+ "帥";
if (BKingPosDone == false)
ErrorPosText = ErrorPosText + "將";
if (RAssistPosDone == false)
ErrorPosText = ErrorPosText + "仕";
if (BAssistPosDone == false)
ErrorPosText = ErrorPosText + "士";
if (RBishopPosDone == false)
ErrorPosText = ErrorPosText + "相";
if (BBishopPosDone == false)
ErrorPosText = ErrorPosText + "象";
if (RPawnPosDone == false)
ErrorPosText = ErrorPosText + "兵";
if (BPawnPosDone == false)
ErrorPosText = ErrorPosText + "卒";
ErrorPosText = ErrorPosText + "的位置錯(cuò)誤";
if ((TotalCount[red] <= 16) && (TotalCount[black] <= 16) &&
RKingNumDone && BKingNumDone && RAssistNumDone && BAssistNumDone
&& RBishopNumDone && BBishopNumDone && RKnightNumDone && BKnightNumDone
&& RRookNumDone && BRookNumDone && RGunnerNumDone && BGunnerNumDone
&& RPawnNumDone && BPawnNumDone)
{
ResetNewPos();
if (Attacks(Player, PieceTable[Opponent][0].isquare))
ErrorPosText = "王" + ErrorPosText;
if (!Attacks(Player, PieceTable[Opponent][0].isquare)&&
RKingPosDone && BKingPosDone && RAssistPosDone && BAssistPosDone
&& RBishopPosDone && BBishopPosDone && RPawnPosDone && BPawnPosDone)
Done = true;
else
Error(ErrorPosText.c_str());
}
else
Error(ErrorNumText.c_str());
}
else
NormalSetup();
if (Done)
NormalSetup();
}
//---------------------------------------------------------------------------
/*
主菜單項(xiàng)“提示”事件處理函數(shù)
把程序計(jì)算得出的最好走法在信息框中顯示出來(lái)。
*/
void __fastcall TMainForm::Hint1Click(TObject *Sender)
{
SearchHintMove();
DisplayHint();
}
//---------------------------------------------------------------------------
/*
主菜單項(xiàng)“對(duì)弈”事件處理函數(shù)
每點(diǎn)擊一次,程序走一步,每走完一次,程序輪流改變對(duì)手。
*/
void __fastcall TMainForm::PassClick(TObject *Sender)
{
if (Level == easygame)
HideAttacks();
CurPlayer = Opponent;
ComputerColor = Player;
ComputersTurn();
}
//---------------------------------------------------------------------------
/*
主菜單項(xiàng)“輪流”子菜單項(xiàng)”紅方“事件處理函數(shù)
選中子菜單項(xiàng)”紅方“時(shí),在該項(xiàng)前打勾。同時(shí)設(shè)置為當(dāng)前玩家。
*/
void __fastcall TMainForm::RedTurnClick(TObject *Sender)
{
CheckMenuItem(TChessEditMenu->Handle, BlackTurn->Command, MF_UNCHECKED);
CheckMenuItem(TChessEditMenu->Handle, RedTurn->Command, MF_CHECKED);
CurPlayer = red;
ComputerColor = black;
if (CurPlayer != Player)
{
Opponent = ComputerColor;
Player = ProgramColor = CurPlayer;
}
}
//---------------------------------------------------------------------------
/*
主菜單項(xiàng)“輪流”子菜單項(xiàng)”黑方“事件處理函數(shù)
選中子菜單項(xiàng)”黑方“時(shí),在該項(xiàng)前打勾。同時(shí)設(shè)置為當(dāng)前玩家。
*/
void __fastcall TMainForm::BlackTurnClick(TObject *Sender)
{
CheckMenuItem(TChessEditMenu->Handle, BlackTurn->Command, MF_CHECKED);
CheckMenuItem(TChessEditMenu->Handle, RedTurn->Command, MF_UNCHECKED);
CurPlayer = black;
ComputerColor = red;
if (CurPlayer != Player)
{
Opponent = ComputerColor;
Player = ProgramColor = CurPlayer;
}
}
//---------------------------------------------------------------------------
/*
主菜單項(xiàng)“顏色”事件處理函數(shù)
彈出顏色對(duì)話框,設(shè)置棋盤背景顏色。
Invalidate函數(shù)請(qǐng)求重畫
*/
void __fastcall TMainForm::ColorsClick(TObject *Sender)
{
ColorDlg->ShowModal();
hColorBrush = CreateSolidBrush(
RGB(ColorSquareColors[0], ColorSquareColors[1], ColorSquareColors[2]));
Invalidate();
}
//---------------------------------------------------------------------------
/*
主菜單項(xiàng)“棋子值”事件處理函數(shù)
模態(tài)彈出設(shè)置棋子值對(duì)話框,設(shè)置每個(gè)棋子(士、象、馬、車、炮、兵)的估價(jià)值。
*/
void __fastcall TMainForm::PieceValuesClick(TObject *Sender)
{
PieceValueDlg->ShowModal();
}
//---------------------------------------------------------------------------
/*
主菜單項(xiàng)“最好路線”事件處理函數(shù)
翻轉(zhuǎn)子菜單項(xiàng)”最好路線“時(shí),改變?cè)擁?xiàng)前的勾號(hào)。
勾住 ”最好路線“時(shí),顯示對(duì)弈時(shí)的最好路線。
去掉勾時(shí),則不顯示對(duì)弈時(shí)的最好路線 。
*/
void __fastcall TMainForm::BestPathClick(TObject *Sender)
{
ShowBestPath = !ShowBestPath;
if (ShowBestPath)
CheckMenuItem(TChessMenu->Handle, BestPath->Command, MF_CHECKED);
else
{
CheckMenuItem(TChessMenu->Handle, BestPath->Command, MF_UNCHECKED);
InfoForm->BestPath->Caption="";
}
}
//---------------------------------------------------------------------------
/*
主菜單項(xiàng)“拖動(dòng)”事件處理函數(shù)
選中該子菜單項(xiàng)時(shí),在該項(xiàng)前打勾號(hào)。
當(dāng)移動(dòng)棋子時(shí),采用動(dòng)畫方式。
*/
void __fastcall TMainForm::MouseDragClick(TObject *Sender)
{
POINT Point;
if (!Dragging && GotStartSquare)
{
DrawNormalBitmap(MoveStartSquare);
GetCursorPos(&Point);
DragStart(MoveStartSquare, Point);
}
Dragging = true;
CheckMenuItem(TChessMenu->Handle, MouseDrag->Command, MF_CHECKED);
CheckMenuItem(TChessMenu->Handle, MouseSelect->Command, MF_UNCHECKED);
}
//---------------------------------------------------------------------------
/*
主菜單項(xiàng)“選擇”事件處理函數(shù)
選中該子菜單項(xiàng)時(shí),在該項(xiàng)前打勾號(hào)。
移動(dòng)棋子時(shí),采用棋子反相方式。
*/
void __fastcall TMainForm::MouseSelectClick(TObject *Sender)
{
DragEnd(true);
Dragging = false;
CheckMenuItem(TChessMenu->Handle, MouseDrag->Command, MF_UNCHECKED);
CheckMenuItem(TChessMenu->Handle, MouseSelect->Command, MF_CHECKED);
if (GotStartSquare)
DrawInvertedBitmap(MoveStartSquare);
}
//---------------------------------------------------------------------------
/*
主窗體鼠標(biāo)移動(dòng)事件處理函數(shù)
選中棋子時(shí),在當(dāng)前鼠標(biāo)位置顯示棋子(動(dòng)畫)。
*/
void __fastcall TMainForm::FormMouseMove(TObject *Sender,
TShiftState Shift, int X, int Y)
{
if (!GotStartSquare || !Dragging)
return;
Drag(Point(X,Y));
}
//---------------------------------------------------------------------------
/*
主菜單項(xiàng)“聲音”事件處理函數(shù)
翻轉(zhuǎn)子菜單項(xiàng)”聲音“時(shí),改變?cè)擁?xiàng)前的勾號(hào)。
只有系統(tǒng)中配有多媒體喇叭時(shí),才有可能發(fā)出聲音。
*/
void __fastcall TMainForm::SoundClick(TObject *Sender)
{
SoundOn = !SoundOn;
if (SoundOn)
Sound->Checked = true;
else
Sound->Checked = false;
}
//---------------------------------------------------------------------------
/*
主窗體定時(shí)器事件處理函數(shù)
每當(dāng)程序發(fā)出WM_TIMER消息時(shí),在信息框體中顯示下棋時(shí)間。
*/
void __fastcall TMainForm::Timer1Timer(TObject *Sender)
{
DisplayTime();
}
//---------------------------------------------------------------------------
/*
主窗體窗體關(guān)閉事件處理函數(shù)
釋放系統(tǒng)資源。
對(duì)于非可視化指針,用new創(chuàng)建,還需用delete撤消。
*/
void __fastcall TMainForm::FormDestroy(TObject *Sender)
{
delete FileName;
delete InfoForm;
delete EditForm;
DeleteObject(hColorBrush);
for (int i = 0; i < 7; i++)
{
DeleteObject(MaskArray[i]);
for (int j = 0; j < 2; j++)
DeleteObject(PieceBmpArray[i][j]);
}
Timer1->Enabled=false;
}
//---------------------------------------------------------------------------
/*
主菜單項(xiàng)“每次移動(dòng)時(shí)間”事件處理函數(shù)
選中該子菜單項(xiàng)時(shí),在該項(xiàng)前打勾號(hào)。
模態(tài)彈出“設(shè)置每次移動(dòng)秒數(shù)”對(duì)話框,設(shè)置每次移動(dòng)程序“想問(wèn)題”的時(shí)間。如果
輸入沒(méi)有兩位小數(shù)的浮點(diǎn)數(shù)值,彈出錯(cuò)誤對(duì)話框。
*/
void __fastcall TMainForm::MoveTimeClick(TObject *Sender)
{
char *SecsPerMove = new char[40];
COLORTYPE color;
int temp;
sprintf(SecsPerMove, "%.2f", AverageTime);
if (InputSecPerMoveDlg->ShowModal()==mrOk)
{
SecsPerMove = InputSecPerMoveDlg->Edit1->Text.c_str();
double NewMoveTime = atof(SecsPerMove);
if (NewMoveTime > 0.)
{
if (Level != normal)
{
UnCheckLevelMenu(Level);
CheckMenuItem(TChessMenu->Handle, MoveTime->Command, MF_CHECKED);
}
Level = normal;
AverageTime = NewMoveTime;
PrintCurLevel();
/*
在C++ Builder中枚舉常量加一個(gè)常數(shù),不能成為后一個(gè)常量。
只得采用整數(shù)間接來(lái)達(dá)到目的。
*/
for (temp=0;temp<=1;temp++)
{
{ if (temp==0)
color = red;
if (temp==1)
color= black;
ChessTime[color].totaltime = (MoveNumber / 2) * AverageTime;
}
}
MaxLevel = MAXPLY;
}
else
Error("時(shí)間不正確。請(qǐng)重輸。");
}
delete SecsPerMove;
}
//---------------------------------------------------------------------------
/*
主菜單項(xiàng)“總時(shí)間”事件處理函數(shù)
選中該子菜單項(xiàng)時(shí),在該項(xiàng)前打勾號(hào)。
模態(tài)彈出“設(shè)置每次游戲分鐘數(shù)”對(duì)話框,設(shè)置每次游戲程序的時(shí)間。如果
輸入沒(méi)有兩位小數(shù)的浮點(diǎn)數(shù)值,彈出錯(cuò)誤對(duì)話框。
*/
void __fastcall TMainForm::TotalTimeClick(TObject *Sender)
{
char *TotalMoveTime = new char[40];
int temp;
COLORTYPE color;
sprintf(TotalMoveTime, "%.2f", AverageTime);
if (InputMinPerGameDlg->ShowModal()==mrOk)
{
TotalMoveTime = InputMinPerGameDlg->Edit1->Text.c_str();
double NewMoveTime = atof(TotalMoveTime);
if (NewMoveTime > 0.)
{
if (Level != fullgametime)
{
UnCheckLevelMenu(Level);
CheckMenuItem(TChessMenu->Handle, TotalTime->Command, MF_CHECKED);
}
Level = fullgametime;
AverageTime = NewMoveTime;
PrintCurLevel();
/*
在C++ Builder中枚舉常量加一個(gè)常數(shù),不能成為后一個(gè)常量。
只得采用整數(shù)間接來(lái)達(dá)到目的。
*/
for (temp=0;temp<=1;temp++)
{ { if (temp==0)
color = red;
if (temp==1)
color= black;
ChessTime[color].totaltime = (MoveNumber / 2) * AverageTime;
}
}
MaxLevel = MAXPLY;
}
else
Error("時(shí)間不正確。請(qǐng)重輸。");
}
delete TotalMoveTime;
}
//---------------------------------------------------------------------------
/*
主菜單項(xiàng)“匹配”事件處理函數(shù)
選中該子菜單項(xiàng)時(shí),在該項(xiàng)前打勾號(hào)。同時(shí)設(shè)置信息窗體中的級(jí)別文本。
*/
void __fastcall TMainForm::MatchingClick(TObject *Sender)
{
if (Level != matching)
{
UnCheckLevelMenu(Level);
CheckMenuItem(TChessMenu->Handle, Matching->Command, MF_CHECKED);
}
Level = matching;
PrintCurLevel();
}
//---------------------------------------------------------------------------
/*
主菜單項(xiàng)“退出”事件處理函數(shù)
*/
void __fastcall TMainForm::Exit1Click(TObject *Sender)
{
MainForm->Close();
}
/*
自定義消息處理函數(shù)
響應(yīng)IDM_EXIT消息請(qǐng)求關(guān)閉窗口。
*/
void TMainForm::Exit(TMessage &Msg )
{
MainForm->Exit1Click(Exit1);
}
//---------------------------------------------------------------------------
/*
自定義結(jié)束游戲函數(shù)
動(dòng)態(tài)創(chuàng)建對(duì)話框。注意,別忘記撤消創(chuàng)建的資源。
*/
void TMainForm::EndGame()
{
TEndChessGameDlg *EndChessGameDlg = new TEndChessGameDlg(Application,(AnsiString) EndGameMessage);
EndChessGameDlg->ShowModal();
delete EndChessGameDlg;
return;
}
//---------------------------------------------------------------------------
/*
關(guān)閉應(yīng)用程序時(shí),為避免數(shù)據(jù)丟失,應(yīng)對(duì)一些重要的數(shù)據(jù)進(jìn)行保存。
FormCloseQuery是解決問(wèn)題的關(guān)鍵。當(dāng)窗口關(guān)閉時(shí),
調(diào)用VCL函數(shù)TForm::Close。
Close并不是自己關(guān)閉窗體。首先它申請(qǐng)F(tuán)ormCloseQuery句柄允許。
程序退出前,必須經(jīng)過(guò)FormCloseQuery句柄函數(shù)允許。
當(dāng)選擇“游戲->退出”或撳擊程序標(biāo)題欄的關(guān)閉圖標(biāo)時(shí),F(xiàn)ormCloseQuery
將執(zhí)行。如果FormCloseQuery設(shè)置CanClose為false,窗體將不關(guān)閉。
設(shè)置CanClose為true則允許程序關(guān)閉。在FormCloseQuery返回前,調(diào)用
QuitProgram()保存chess.ini文件。
*/
void __fastcall TMainForm::FormCloseQuery(TObject *Sender, bool &CanClose)
{
if (ComputerThinking || AutoPlay)
{
::PostMessage(MainForm->Handle,WM_COMMAND,Stop->Command,0);
::PostMessage(MainForm->Handle,WM_COMMAND,IDM_EXIT,0);
CanClose=false;
}
QuitProgram();
CanClose = true;
}
//---------------------------------------------------------------------------
?? 快捷鍵說(shuō)明
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -