?? mainform1.cpp
字號:
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 + "的位置錯誤";
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();
}
//---------------------------------------------------------------------------
/*
主菜單項“提示”事件處理函數
把程序計算得出的最好走法在信息框中顯示出來。
*/
void __fastcall TMainForm::Hint1Click(TObject *Sender)
{
SearchHintMove();
DisplayHint();
}
//---------------------------------------------------------------------------
/*
主菜單項“對弈”事件處理函數
每點擊一次,程序走一步,每走完一次,程序輪流改變對手。
*/
void __fastcall TMainForm::PassClick(TObject *Sender)
{
if (Level == easygame)
HideAttacks();
CurPlayer = Opponent;
ComputerColor = Player;
ComputersTurn();
}
//---------------------------------------------------------------------------
/*
主菜單項“輪流”子菜單項”紅方“事件處理函數
選中子菜單項”紅方“時,在該項前打勾。同時設置為當前玩家。
*/
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;
}
}
//---------------------------------------------------------------------------
/*
主菜單項“輪流”子菜單項”黑方“事件處理函數
選中子菜單項”黑方“時,在該項前打勾。同時設置為當前玩家。
*/
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;
}
}
//---------------------------------------------------------------------------
/*
主菜單項“顏色”事件處理函數
彈出顏色對話框,設置棋盤背景顏色。
Invalidate函數請求重畫
*/
void __fastcall TMainForm::ColorsClick(TObject *Sender)
{
ColorDlg->ShowModal();
hColorBrush = CreateSolidBrush(
RGB(ColorSquareColors[0], ColorSquareColors[1], ColorSquareColors[2]));
Invalidate();
}
//---------------------------------------------------------------------------
/*
主菜單項“棋子值”事件處理函數
模態彈出設置棋子值對話框,設置每個棋子(士、象、馬、車、炮、兵)的估價值。
*/
void __fastcall TMainForm::PieceValuesClick(TObject *Sender)
{
PieceValueDlg->ShowModal();
}
//---------------------------------------------------------------------------
/*
主菜單項“最好路線”事件處理函數
翻轉子菜單項”最好路線“時,改變該項前的勾號。
勾住 ”最好路線“時,顯示對弈時的最好路線。
去掉勾時,則不顯示對弈時的最好路線 。
*/
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="";
}
}
//---------------------------------------------------------------------------
/*
主菜單項“拖動”事件處理函數
選中該子菜單項時,在該項前打勾號。
當移動棋子時,采用動畫方式。
*/
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);
}
//---------------------------------------------------------------------------
/*
主菜單項“選擇”事件處理函數
選中該子菜單項時,在該項前打勾號。
移動棋子時,采用棋子反相方式。
*/
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);
}
//---------------------------------------------------------------------------
/*
主窗體鼠標移動事件處理函數
選中棋子時,在當前鼠標位置顯示棋子(動畫)。
*/
void __fastcall TMainForm::FormMouseMove(TObject *Sender,
TShiftState Shift, int X, int Y)
{
if (!GotStartSquare || !Dragging)
return;
Drag(Point(X,Y));
}
//---------------------------------------------------------------------------
/*
主菜單項“聲音”事件處理函數
翻轉子菜單項”聲音“時,改變該項前的勾號。
只有系統中配有多媒體喇叭時,才有可能發出聲音。
*/
void __fastcall TMainForm::SoundClick(TObject *Sender)
{
SoundOn = !SoundOn;
if (SoundOn)
Sound->Checked = true;
else
Sound->Checked = false;
}
//---------------------------------------------------------------------------
/*
主窗體定時器事件處理函數
每當程序發出WM_TIMER消息時,在信息框體中顯示下棋時間。
*/
void __fastcall TMainForm::Timer1Timer(TObject *Sender)
{
DisplayTime();
}
//---------------------------------------------------------------------------
/*
主窗體窗體關閉事件處理函數
釋放系統資源。
對于非可視化指針,用new創建,還需用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;
}
//---------------------------------------------------------------------------
/*
主菜單項“每次移動時間”事件處理函數
選中該子菜單項時,在該項前打勾號。
模態彈出“設置每次移動秒數”對話框,設置每次移動程序“想問題”的時間。如果
輸入沒有兩位小數的浮點數值,彈出錯誤對話框。
*/
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中枚舉常量加一個常數,不能成為后一個常量。
只得采用整數間接來達到目的。
*/
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("時間不正確。請重輸。");
}
delete SecsPerMove;
}
//---------------------------------------------------------------------------
/*
主菜單項“總時間”事件處理函數
選中該子菜單項時,在該項前打勾號。
模態彈出“設置每次游戲分鐘數”對話框,設置每次游戲程序的時間。如果
輸入沒有兩位小數的浮點數值,彈出錯誤對話框。
*/
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中枚舉常量加一個常數,不能成為后一個常量。
只得采用整數間接來達到目的。
*/
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("時間不正確。請重輸。");
}
delete TotalMoveTime;
}
//---------------------------------------------------------------------------
/*
主菜單項“匹配”事件處理函數
選中該子菜單項時,在該項前打勾號。同時設置信息窗體中的級別文本。
*/
void __fastcall TMainForm::MatchingClick(TObject *Sender)
{
if (Level != matching)
{
UnCheckLevelMenu(Level);
CheckMenuItem(TChessMenu->Handle, Matching->Command, MF_CHECKED);
}
Level = matching;
PrintCurLevel();
}
//---------------------------------------------------------------------------
/*
主菜單項“退出”事件處理函數
*/
void __fastcall TMainForm::Exit1Click(TObject *Sender)
{
MainForm->Close();
}
/*
自定義消息處理函數
響應IDM_EXIT消息請求關閉窗口。
*/
void TMainForm::Exit(TMessage &Msg )
{
MainForm->Exit1Click(Exit1);
}
//---------------------------------------------------------------------------
/*
自定義結束游戲函數
動態創建對話框。注意,別忘記撤消創建的資源。
*/
void TMainForm::EndGame()
{
TEndChessGameDlg *EndChessGameDlg = new TEndChessGameDlg(Application,(AnsiString) EndGameMessage);
EndChessGameDlg->ShowModal();
delete EndChessGameDlg;
return;
}
//---------------------------------------------------------------------------
/*
關閉應用程序時,為避免數據丟失,應對一些重要的數據進行保存。
FormCloseQuery是解決問題的關鍵。當窗口關閉時,
調用VCL函數TForm::Close。
Close并不是自己關閉窗體。首先它申請FormCloseQuery句柄允許。
程序退出前,必須經過FormCloseQuery句柄函數允許。
當選擇“游戲->退出”或撳擊程序標題欄的關閉圖標時,FormCloseQuery
將執行。如果FormCloseQuery設置CanClose為false,窗體將不關閉。
設置CanClose為true則允許程序關閉。在FormCloseQuery返回前,調用
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;
}
//---------------------------------------------------------------------------
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -