📄 chessdlg.cpp
字号:
// chessDlg.cpp : implementation file
//
#include "stdafx.h"
#include "chess.h"
#include "chessDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
CHESSBOARD First=
{
{1,2,3,4,5,4,3,2,1},
{0,0,0,0,0,0,0,0,0},
{0,6,0,0,0,0,0,6,0},
{7,0,7,0,7,0,7,0,7},
{0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0},
{14,0,14,0,14,0,14,0,14},
{0,13,0,0,0,0,0,13,0},
{0,0,0,0,0,0,0,0,0},
{8,9,10,11,12,11,10,9,8}
};
/*
{
{0,0,0,0,0,0,0,6,0},
{0,0,0,0,0,5,0,0,0},
{0,0,0,0,0,4,0,0,0},
{0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0},
{0,0,9,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0},
{0,8,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,1},
{0,0,0,12,0,11,0,0,0}
};
*/
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
void CChessDlg::CreateBit(BYTE C[][9],__int32 b[][3],BYTE bitboard[][10][9])
{
memset(b,0,60);
memset(bitboard,0,15*10*9*1);
for (int x=0;x<10;x++)
for (int y=0;y<9;y++)
if (C[x][y])
{
int i=C[x][y];
if (b[i][0]||b[i][1]||b[i][2]) continue;
int j,k;
for (j=0;j<3;j++)
for (k=0;k<10;k++)
if (C[k][j]==i) {b[i][0]|=(1<<(30-j*10-k));bitboard[i][k][j]=1;}
for (j=3;j<6;j++)
for (k=0;k<10;k++)
if (C[k][j]==i) {b[i][1]|=(1<<(30-(j-3)*10-k));bitboard[i][k][j]=1;}
for (j=6;j<9;j++)
for (k=0;k<10;k++)
if (C[k][j]==i) {b[i][2]|=(1<<(30-(j-6)*10-k));bitboard[i][k][j]=1;}
}
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CChessDlg dialog
CChessDlg::CChessDlg(CWnd* pParent /*=NULL*/)
: CDialog(CChessDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CChessDlg)
m_depth = -1;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_ppm=new ProductMove(this);
m_pv=new Value(this);
m_ps=new Search(this);
IsThinking=FALSE;
}
void CChessDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CChessDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CChessDlg, CDialog)
//{{AFX_MSG_MAP(CChessDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_LBUTTONDOWN()
ON_WM_CREATE()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONUP()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CChessDlg message handlers
BOOL CChessDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CChessDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CChessDlg::OnPaint()
{
Draw();
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CChessDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CChessDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
if (IsThinking) return;
BYTE x,y;
x=(point.x-LEFT+24)/48;
y=(point.y-TOP+24)/48;
if (x>=0&&x<=8&&y>=0&&y<=9&&Current[y][x]!=0)
{
Moving=Current[y][x];
From=CPoint(y,x);
Current[y][x]=0;
Draw();
Current[y][x]=Moving;
m_list.BeginDrag (Moving-1,CPoint(15,-10));
m_list.DragEnter (this,point);
}
CDialog::OnLButtonDown(nFlags, point);
}
int CChessDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CDialog::OnCreate(lpCreateStruct) == -1)
return -1;
m_list.Create(40,40,ILC_COLOR32|ILC_MASK,0,14);
m_qizi.LoadBitmap (IDB_BITMAP2);
m_list.Add (&m_qizi,RGB(255,255,255));
memcpy(Current,First,10*9);
king[0]=FALSE;
king[1]=FALSE;
for (int x=0;x<=9;x++)
for (int y=0;y<=8;y++)
if (Current[x][y]==5) king[0]=TRUE;else if (Current[x][y]==12) king[1]=TRUE;
Moving=0;
top=0;
m_depth=1;
m_pt=new CProduct(this);
CreateBit(Current,bit,BitBoard);
for (x=0;x<10;x++)
{
memset(board,0,90);
for (int z=0;z<9;z++) board[x][z]=1;
for (z=0;z<9;z++) {BitCreate(board,chex[x][z]);BitCreate(board,paox[x][z]);}
}
for (x=0;x<9;x++)
{
memset(board,0,90);
for (int z=0;z<10;z++) board[z][x]=1;
for (z=0;z<10;z++) {BitCreate(board,chey[z][x]);BitCreate(board,paoy[z][x]);}
}
for (x=0;x<10;x++)
for (int y=0;y<9;y++)
{
memset(board,0,90);
if (x-1>=0) board[x-1][y]=1;
if (x+1<=9) board[x+1][y]=1;
if (y-1>=0) board[x][y-1]=1;
if (y+1<=8) board[x][y+1]=1;
BitCreate(board,ma[x][y]);
BitCreate(board,bin[x][y]);
BitCreate(board,jiang[x][y]);
memset(board,0,90);
if (x-1>=0&&y-1>=0) board[x-1][y-1]=1;
if (x-1>=0&&y+1<=8) board[x-1][y+1]=1;
if (x+1<=9&&y-1>=0) board[x+1][y-1]=1;
if (x+1<=9&&y+1<=8) board[x+1][y+1]=1;
BitCreate(board,xiang[x][y]);
BitCreate(board,shi[x][y]);
memset(board,0,90);
if (x-1>=0&&y-2>=0) board[x-1][y-2]=1;
if (x-1>=0&&y+2<=8) board[x-1][y+2]=1;
if (x+1<=9&&y-2>=0) board[x+1][y-2]=1;
if (x+1<=9&&y+2<=8) board[x+1][y+2]=1;
if (x-2>=0&&y-1>=0) board[x-2][y-1]=1;
if (x-2>=0&&y+1<=8) board[x-2][y+1]=1;
if (x+2<=9&&y-1>=0) board[x+2][y-1]=1;
if (x+2<=9&&y+1<=8) board[x+2][y+1]=1;
BitCreate(board,zouma[x][y]);
memset(board,0,90);
if (x-2>=0&&y-2>=0) board[x-2][y-2]=1;
if (x-2>=0&&y+2<=8) board[x-2][y+2]=1;
if (x+2<=9&&y-2>=0) board[x+2][y-2]=1;
if (x+2<=9&&y+2<=8) board[x+2][y+2]=1;
BitCreate(board,zouxiang[x][y]);
memset(board,0,90);
board[x][y]=1;
BitCreate(board,bitData[x][y]);
}
memset(board,0,90);
for (x=0;x<5;x++)
for (int y=0;y<9;y++)
board[x][y]=1;
BitCreate(board,zu[0]);
memset(board,0,90);
for (x=5;x<10;x++)
for (int y=0;y<9;y++)
board[x][y]=1;
BitCreate(board,zu[1]);
memset(board,0,90);
for (x=0;x<3;x++)
for (int y=3;y<6;y++)
board[x][y]=1;
BitCreate(board,shuai[0]);
memset(board,0,90);
for (x=7;x<10;x++)
for (int y=3;y<6;y++)
board[x][y]=1;
BitCreate(board,shuai[1]);
for (x=0;x<10;x++)
for (int y=0;y<9;y++)
{
m_ppm->Extend (CPoint(x,y),Move[x][y],Top[x][y],Current);
}
for (x=0;x<3;x++) m_pt->Print (ma[3][4][x]);
return 0;
}
void CChessDlg::BitCreate(BYTE C[][9],__int32 b[])
{
int j,k;
memset(b,0,3*4);
for (j=0;j<3;j++)
for (k=0;k<10;k++)
if (C[k][j]==1) b[0]|=(1<<(30-j*10-k));
for (j=3;j<6;j++)
for (k=0;k<10;k++)
if (C[k][j]==1) b[1]|=(1<<(30-(j-3)*10-k));
for (j=6;j<9;j++)
for (k=0;k<10;k++)
if (C[k][j]==1) b[2]|=(1<<(30-(j-6)*10-k));
}
void CChessDlg::OnMouseMove(UINT nFlags, CPoint point)
{
if (point.x>48*8+LEFT+24) point.x=48*8+LEFT+24;
if (point.x<LEFT-24) point.x=LEFT-24;
if (point.y>48*9+TOP+24) point.y=48*9+TOP+24;
if (point.y<TOP-24) point.y=TOP-24;
m_list.DragMove (point);
// MessageBox(s);
CDialog::OnMouseMove(nFlags, point);
}
void CChessDlg::Thinking()
{
IsThinking=TRUE;
IsThinking=FALSE;
}
void CChessDlg::OnLButtonUp(UINT nFlags, CPoint point)
{
if (Moving==0)
{
return;
}
m_list.DragLeave (this);
m_list.EndDrag ();
if (point.x>48*8+LEFT+24) point.x=48*8+LEFT+24;
if (point.x<LEFT-24) point.x=LEFT-24;
if (point.y>48*9+TOP+24) point.y=48*9+TOP+24;
if (point.y<TOP-24) point.y=TOP-24;
BYTE x,y;
x=(point.x-LEFT+24)/48;
y=(point.y-TOP+24)/48;
if (m_ppm->CanMove(From,CPoint(y,x),Current)) {
int j=Current[y][x];
yuanlai[top][0]=Current[y][x];
Current[y][x]=Moving;
Current[From.x][From.y]=0;
m_pt->Pt(Current,bit,BitBoard,From,CPoint(y,x),Move,Top);
int i=Moving;
bit[i][0]^=bitData[From.x][From.y][0];
bit[i][1]^=bitData[From.x][From.y][1];
bit[i][2]^=bitData[From.x][From.y][2];
if (j){
bit[j][0]^=bitData[y][x][0];
bit[j][1]^=bitData[y][x][1];
bit[j][2]^=bitData[y][x][2];
}
bit[i][0]^=bitData[y][x][0];
bit[i][1]^=bitData[y][x][1];
bit[i][2]^=bitData[y][x][2];
Draw();
movelist[top++][0].from =From;
movelist[top-1][0].to =CPoint(y,x);
Thinking();
}
else Current[From.x][From.y]=Moving;
Moving=0;
Draw();
CDialog::OnLButtonUp(nFlags, point);
}
void CChessDlg::Draw()
{
CBitmap bit;
bit.LoadBitmap (IDB_BITMAP1);
CClientDC dc(this);
CDC dc1;
dc1.CreateCompatibleDC (NULL);
dc1.SelectObject (&bit);
int x,y;
for (x=0;x<=8;x++)
for (y=0;y<=9;y++)
if (Current[y][x]!=0)
{
m_list.Draw(&dc1,Current[y][x]-1,CPoint(x*48+LEFT-24,y*48+TOP-24),ILD_TRANSPARENT);
}
dc1.TextOut(30,48*10,message);
dc1.TextOut(200,48*10,message1);
dc.BitBlt (LEFT-33,TOP-31,48*10,48*11,&dc1,0,0,SRCCOPY);
m_pt->Draw ();
}
void CChessDlg::OnButton2()
{
if (MessageBox("真的要重新开始游戏吗?","Caution!",MB_YESNO|MB_ICONWARNING)==IDNO) return;
memcpy(Current,First,90);
top=0;
Draw();
}
void CChessDlg::OnButton1()
{
if (top==0) return;
for (int x=1;x>=0;x--)
{
Current[movelist[top-1][x].from.x][movelist[top-1][x].from.y]=Current[movelist[top-1][x].to.x][movelist[top-1][x].to.y];
Current[movelist[top-1][x].to.x][movelist[top-1][x].to.y]=yuanlai[top-1][x];
}
top--;
Draw();
}
void CChessDlg::OnRadio1()
{
MaxDepth=4;
}
void CChessDlg::OnRadio2()
{
MaxDepth=5;
}
void CChessDlg::OnRadio3()
{
MaxDepth=6;
}
void CChessDlg::OnRadio4()
{
MaxDepth=7;
}
void CChessDlg::OnRadio5()
{
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -