📄 mfcpodlg.cpp
字号:
for (int i=0;i<8;i++){
for(int j=0;j<8;j++){
if (ai.board[i][j]){
DrawBW(i ,j, ai.board[i][j]); //1=black
}
}
}
}
//undo move
void CMfcPoDlg::OnUndoMove()
{
Undo();
}
void CMfcPoDlg::OnFileExit()
{
OnClose();
}
void CMfcPoDlg::OnFileNewgame()
{
OnNewgame();
}
//suggest move
void CMfcPoDlg::OnHelpSuggestmove()
{
int score, x, y, time, nodes;
if (ai.move(ai.to_play,score,x,y,time,nodes)){
CString str;
str.Format("%s : Suggest Move (%c%d)",(ai.to_play==BLACK)?"BLACK":"WHITE",x+'A', y+1);
MessageBox(str, "Suggest Move",MB_OK|MB_ICONINFORMATION);
}
}
void CMfcPoDlg::OnHelpUndomove()
{
Undo();
}
//evaluate current board
void CMfcPoDlg::OnEvaluateBoardevaluation()
{
CString str;
int wc,bc;
ai.total_disks = ai.disks_count();
if (ai.to_play==1){ //black
bc = ai.stat_eval();
ai.to_play=-1;
wc = ai.stat_eval();
ai.to_play=1;
}
else{ //white
wc = ai.stat_eval();
ai.to_play=1;
bc = ai.stat_eval();
ai.to_play=-1;
}
str.Format("WHITE : %d\nBLACK : %d",wc, bc);
MessageBox(str, "Board Evaluation", MB_OK | MB_ICONINFORMATION);
}
//save current board
void CMfcPoDlg::OnFILESAVEGame()
{
char path[50];
CFileDialog savegame(FALSE, NULL, "savegame.dat", OFN_CREATEPROMPT | OFN_OVERWRITEPROMPT | OFN_HIDEREADONLY, NULL, this);
::GetCurrentDirectory(50,path);
savegame.m_ofn.lpstrInitialDir = path;
savegame.m_ofn.lpstrFilter="Dat files [*.dat]\x00*.dat\x00";
if (savegame.DoModal() == IDOK){
CFile save_file;
CString SaveFileName;
SaveFileName = savegame.GetPathName();
if (save_file.Open(SaveFileName, CFile::modeCreate | CFile::modeNoTruncate | CFile::modeWrite)){
CString savestr;
for (int i=0;i<8;i++)
for (int j=0;j<8;j++)
savestr += ai.board[j][i]+0x31; //to be readable, 0=white, 1=empty, 2 black
save_file.Write(savestr, savestr.GetLength()); //save board
savestr.Format("\r\n%c\r\n",ai.to_play + 0x31); //save turn
save_file.Write(savestr, savestr.GetLength());
savestr.Format("%d\r\n",oneplayer); //save player
save_file.Write(savestr, savestr.GetLength());
}
}
}
//load current board
void CMfcPoDlg::OnFileLoadgame()
{
char path[50];
CFileDialog savegame(TRUE, NULL, "savegame.dat", OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY, NULL, this);
::GetCurrentDirectory(50,path);
savegame.m_ofn.lpstrInitialDir = path;
savegame.m_ofn.lpstrFilter="Dat files [*.dat]\x00*.dat\x00";
if (savegame.DoModal() == IDOK){
CFile save_file;
CString SaveFileName;
SaveFileName = savegame.GetPathName();
if (save_file.Open(SaveFileName, CFile::modeRead|CFile::shareDenyNone)){
save_file.SeekToBegin();
CString loadstr;
char tempa[66];
char temp;
save_file.Read(tempa,66);
int count=0;
if ((tempa[0]!=0x30)&&(tempa[0]!=0x31)&&(tempa[0]!=0x32))
{
MessageBox("Save file format error!","Error",MB_OK|MB_ICONEXCLAMATION);
save_file.Close();
return;
}
for (int i=0;i<8;i++)
for (int j=0;j<8;j++)
ai.board[j][i]=tempa[count++]-0x31;
loadstr.Empty();
while (save_file.Read(&temp,1)){
if ((temp!=0x0a)&&(temp!=0x0d))
loadstr+=temp;
if (temp=='\n')
break;
}
ai.to_play = loadstr[0]-0x31; //turn
loadstr.Empty();
while (save_file.Read(&temp,1)){
if ((temp!=0x0a)&&(temp!=0x0d))
loadstr+=temp;
if (temp=='\n')
break;
}
oneplayer = atoi(loadstr); //one/two player game
m_hislist.ResetContent();
ClearUndo();
Invalidate();
}
}
}
void CMfcPoDlg::OnHelpAbout()
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
//create board texture
void CMfcPoDlg::CreateBoard()
{
CClientDC clientdc(this);
CBitmap m_bgbitmap;
m_bgdc.CreateCompatibleDC(&clientdc);
m_bgbitmap.CreateCompatibleBitmap(&clientdc,XA,XB);
m_bgdc.SelectObject(&m_bgbitmap);
CBitmap BGbitmap;
BGbitmap.LoadBitmap(IDB_BG);
CDC BGdc;
BGdc.CreateCompatibleDC(&clientdc);
BGdc.SelectObject(&BGbitmap);
for(int i=0;i<=XA;i+=64)
for(int j=0;j<=XB;j+=64)
m_bgdc.BitBlt(i,j,64,64,&BGdc,0,0,SRCCOPY);
//draw grid
CPoint pStart, pEnd;
CPen *lOldPen;
CRect lRect;
COLORREF cr1,cr2;
int r=170;
int g=160;
int b=160;
cr1=RGB(min(255,r+40),min(255,g+40),min(255,b+40));
cr2=RGB(max(0,r-40),max(0,g-40),max(0,b-40));
CPen pen[2];
pen[0].CreatePen(PS_SOLID,0,cr1);
pen[1].CreatePen(PS_SOLID,0,cr2);
GetClientRect(lRect);
lRect.NormalizeRect();
lOldPen = m_bgdc.SelectObject(&pen[0]);
for (int line=0;line<=1;line++){
m_bgdc.SelectObject(&pen[line]);
for (int i=16;i<325;i+=38){
//horizontal line
pStart.y = i-line;
pStart.x = 16;
pEnd.y = pStart.y;
pEnd.x = 320;
m_bgdc.MoveTo(pStart);
m_bgdc.LineTo(pEnd);
//vertical line
pStart.y = 16;
pStart.x = i-line;
pEnd.y = 320;
pEnd.x = pStart.x;
m_bgdc.MoveTo(pStart);
m_bgdc.LineTo(pEnd);
}
}
m_bgdc.SetBkMode(TRANSPARENT);
CString temp;
for (int x=0; x<8; x++){
temp.Format("%c",'A'+x);
m_bgdc.TextOut(x*38+30,0,temp); //draw x-axis label
}
for (int y=0;y<8;y++){
temp.Format("%d",y+1);
m_bgdc.TextOut(3,y*38+30,temp); //draw y-axis label
}
m_bgdc.SetBkMode(OPAQUE);
m_bgdc.SelectObject(lOldPen);
}
int CMfcPoDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CDialog::OnCreate(lpCreateStruct) == -1)
return -1;
CreateBoard();
return 0;
}
void CMfcPoDlg::OnViewMirrorxy()
{
int tempBox[8][8];
int i,j;
for (i=0; i<8; i++){
for (j=0; j<8; j++){
tempBox[i][j] = ai.board[i][j];
}
}
for (i=0; i<8; i++){
for (j=0; j<8; j++){
ai.board[i][j] = tempBox[j][i];
}
}
CString temp; //change the listbox item's name
for (i=0;i<m_hislist.GetCount();i++){
m_hislist.GetText(i,temp);
m_hislist.DeleteString(i);
char tempc = temp[7];
temp.SetAt(7,temp[6]-0x10);
temp.SetAt(6,tempc+0x10);
m_hislist.InsertString(i,temp);
}
Invalidate();
}
void CMfcPoDlg::OnViewMirrorx()
{
int tempBox[8][8];
int i,j;
for (i=0; i<8; i++){
for (j=0; j<8; j++){
tempBox[i][j] = ai.board[i][j];
}
}
for (i=0; i<8; i++){
for (j=0; j<8; j++){
ai.board[i][j] = tempBox[7-i][j];
}
}
CString temp; //change the listbox item's name
for (i=0;i<m_hislist.GetCount();i++){
m_hislist.GetText(i,temp);
m_hislist.DeleteString(i);
temp.SetAt(6,0x89-temp[6]);
m_hislist.InsertString(i,temp);
}
Invalidate();
}
void CMfcPoDlg::OnViewMirrory()
{
int tempBox[8][8],i,j;
for (i=0;i<8;i++){
for (j=0;j<8;j++){
tempBox[i][j] = ai.board[i][j];
}
}
for (i=0;i<8;i++){
for (j=0;j<8;j++){
ai.board[i][j] = tempBox[i][7-j];
}
}
CString temp; //change the listbox item's name
for (i=0;i<m_hislist.GetCount();i++){
m_hislist.GetText(i,temp);
m_hislist.DeleteString(i);
temp.SetAt(7,0x69-temp[7]);
m_hislist.InsertString(i,temp);
}
Invalidate();
}
void CMfcPoDlg::OnViewRotateclockwise()
{
OnViewMirrorxy();
OnViewMirrorx();
}
void CMfcPoDlg::OnViewRotateanticlockwise()
{
OnViewMirrorx();
OnViewMirrorxy();
}
CString CMfcPoDlg::MoveDisplay(int color, int x, int y)
{
CString temp;
temp.Format("%c%d",x+'A', y+1);
if (color==BLACK)
temp = "BLACK "+temp;
else
temp = "WHITE "+temp;
return temp;
}
void CMfcPoDlg::ShowScore()
{
int bcount,wcount;
ai.disks_count(bcount, wcount);
CString str;
if (bcount>wcount){
str.Format("BLACK wins %d - %d", bcount, wcount);
MessageBox(str, "Game Over!", MB_OK);
}
else if (bcount<wcount){
str.Format("WHITE wins %d - %d", wcount, bcount);
MessageBox(str, "Game Over!", MB_OK);
}
else
MessageBox("Draw Game!","Game Over",MB_OK);
}
void CMfcPoDlg::OnOptionsAisetting()
{
CSetting set;
set.m_ordermoves = ai.ordermove;
set.depth = ai.depth;
set.eg_empty = ai.eg_empty;
set.searchmethod = ai.searchmethod;
if (set.DoModal()==IDOK){
ai.ordermove = set.m_ordermoves;
ai.depth = set.depth;
ai.eg_empty = set.eg_empty;
ai.searchmethod = set.searchmethod;
}
}
void CMfcPoDlg::ShowResult(int x, int y, int score, int time, int nodes)
{
m_info.Empty();
double speed=0;
if (time)
speed = (double)nodes/time;
m_info.Format("Best Move: %c%d\nScore: %d\nTime Elapsed: %.2lfs\nNodes: %d\nSpeed: %.2lf Kn/s\nSearch Depth: %d",x+'A', y+1, score,(double)(time)/1000, nodes, speed, ai.depth);
UpdateData(FALSE);
}
void CMfcPoDlg::AddUndo()
{
bs *undo;
undo = new bs;
for (int i=0; i<8; i++)
for (int j=0; j<8; j++)
undo->board[i][j] = ai.board[i][j];
undo->turn = ai.to_play;
undo->hislistno = m_hislist.GetCount();
undos.undo[undos.no_indos] = undo;
undos.no_indos++;
}
void CMfcPoDlg::Undo()
{
if (undos.no_indos==0)
return;
bs *undo;
undo = undos.undo[undos.no_indos-1];
for (int i=0; i<8; i++)
for (int j=0; j<8; j++)
ai.board[i][j] = undo->board[i][j];
do{
m_hislist.DeleteString(m_hislist.GetCount()-1);
}while (m_hislist.GetCount()>undo->hislistno);
ai.to_play = undo->turn;
delete undo;
undos.no_indos--;
Invalidate();
}
void CMfcPoDlg::ClearUndo()
{
if (undos.no_indos == 0)
return;
bs *undo;
for (int i =0; i<undos.no_indos; i++){
undo = undos.undo[i];
delete undo;
}
undos.no_indos = 0;
}
void CMfcPoDlg::OnDestroy()
{
ClearUndo();
CDialog::OnDestroy();
}
void CMfcPoDlg::OnClose()
{
ClearUndo();
CDialog::OnClose();
OnOK();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -