📄 aidlg.cpp
字号:
// AIDlg.cpp : implementation file
//
#include "stdafx.h"
#include "AI.h"
#include "AIDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
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
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CAIDlg dialog
CAIDlg::CAIDlg(CWnd* pParent /*=NULL*/)
: CDialog(CAIDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CAIDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
head=NULL;
autohead=NULL;
BITMAP BM;
Bmpic.LoadBitmap(IDB_BITMAP);
Bmpic.GetBitmap(&BM);
Bmpic.GetObject(sizeof(BM),&BM);
Width=BM.bmWidth;
Height=BM.bmHeight;
MouseFlag=TRUE;
TimeCount=0;
MoveCount=0;
Initial();
}
void CAIDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAIDlg)
DDX_Control(pDX, IDC_STATIC_TIME, m_Time);
DDX_Control(pDX, IDC_STATIC_COUNT, m_Count);
DDX_Control(pDX, IDC_EDIT9, m_Edit9);
DDX_Control(pDX, IDC_EDIT8, m_Edit8);
DDX_Control(pDX, IDC_EDIT7, m_Edit7);
DDX_Control(pDX, IDC_EDIT6, m_Edit6);
DDX_Control(pDX, IDC_EDIT5, m_Edit5);
DDX_Control(pDX, IDC_EDIT4, m_Edit4);
DDX_Control(pDX, IDC_EDIT3, m_Edit3);
DDX_Control(pDX, IDC_EDIT2, m_Edit2);
DDX_Control(pDX, IDC_EDIT1, m_Edit1);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAIDlg, CDialog)
//{{AFX_MSG_MAP(CAIDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_LBUTTONDOWN()
ON_BN_CLICKED(IDC_BTN_SET, OnBtnSet)
ON_BN_CLICKED(IDC_BTN_READ, OnBtnRead)
ON_COMMAND(ID_AUTOCOMPLETE, OnAutocomplete)
ON_WM_TIMER()
ON_WM_CLOSE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CAIDlg message handlers
BOOL CAIDlg::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
SetTimer(2,1000,NULL);
return TRUE; // return TRUE unless you set the focus to a control
}
void CAIDlg::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 CAIDlg::OnPaint()
{
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
{
CPaintDC dc(this);
// CDialog::OnPaint();
// CDC *pDC=GetDC();
CDC memdc;
memdc.CreateCompatibleDC(NULL);
memdc.SelectObject(&Bmpic);
int i,j,picwidth,picheight;
picwidth=120;
picheight=160;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{dc.BitBlt( Pic[i][j].PicRect.left,
Pic[i][j].PicRect.top,
picwidth,
picheight,
&memdc,
j*picwidth,//Pic[i][j].PicRect.left,
i*picheight,//Pic[i][j].PicRect.top,
SRCCOPY);
}
dc.StretchBlt ( 390,290,120,160,&memdc, 0, 0, 450, 600, SRCCOPY);
}
//CDialog::OnPaint();
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CAIDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CAIDlg::Initial()
{ int i,j;
InitialStatus[0]=3;//mean the Pic[0][0] is set to [1][0]
InitialStatus[1]=0;//mean the Pic[0][1] is set to [0][0]
InitialStatus[2]=2;//mean the Pic[0][2] is set to [0][2]
InitialStatus[3]=1;//mean the Pic[1][0] is set to [0][1]
InitialStatus[4]=4;//mean the Pic[1][1] is set to [1][1]
InitialStatus[5]=5;//mean the Pic[1][2] is set to [1][2]
InitialStatus[6]=6;//mean the Pic[2][0] is set to [2][0]
InitialStatus[7]=7;//mean the Pic[2][1] is set to [2][1]
InitialStatus[8]=8;//mean the Pic[2][2] is set to [2][2]
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
Pic[i][j].PicRect.left=150*j+10;
Pic[i][j].PicRect.top=200*i+10;
Pic[i][j].PicRect.right=Pic[i][j].PicRect.left+150;
Pic[i][j].PicRect.bottom=Pic[i][j].PicRect.top+200;
ObjectStatus[3*i+j]=i*3+j;
Pic[i][j].position=InitialStatus[i*3+j];
//ObjectStatus[3*i+j]=Pic[i][j].position;
}
SetPicPosition();
}
void CAIDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
int i,j;//mouseposition record the mouse is on which
//small picture
if(MouseFlag)
for(i=0;i<3;i++)
for(j=0;j<3;j++)
if(Pic[i][j].PicRect.PtInRect(point))
{
OnMove(Pic[i][j].position,i,j);
}
if(Finished())
MessageBox("恭喜你,你已经成功了!");
//CDialog::OnLButtonDown(nFlags, point);
}
void CAIDlg::OnBtnSet()
{
// TODO: Add your control notification handler code here
char cbuffer[10];
CString strCount;
int num,i;
int sum=0;
for (num=0;num<9;num++)
{
check[0][num]=0;
}
m_Edit1.GetWindowText(LPTSTR(cbuffer),10);
check[1][0]=atoi(cbuffer);
m_Edit2.GetWindowText(LPTSTR(cbuffer),10);
check[1][1]=atoi(cbuffer);
m_Edit3.GetWindowText(LPTSTR(cbuffer),10);
check[1][2]=atoi(cbuffer);
m_Edit4.GetWindowText(LPTSTR(cbuffer),10);
check[1][3]=atoi(cbuffer);
m_Edit5.GetWindowText(LPTSTR(cbuffer),10);
check[1][4]=atoi(cbuffer);
m_Edit6.GetWindowText(LPTSTR(cbuffer),10);
check[1][5]=atoi(cbuffer);
m_Edit7.GetWindowText(LPTSTR(cbuffer),10);
check[1][6]=atoi(cbuffer);
m_Edit8.GetWindowText(LPTSTR(cbuffer),10);
check[1][7]=atoi(cbuffer);
m_Edit9.GetWindowText(LPTSTR(cbuffer),10);
check[1][8]=atoi(cbuffer);
//the code add to here to make sure that the user set the correct modle.
for(num=0;num<9;num++)
{
if(check[1][num]<9&&check[1][num]>0)
{
i=check[1][num];
if(check[0][i]!=0)
{
MessageBox("对不起,您的输入有误,请重试!");
return;
}
check[0][i]=i;
}
else
check[1][num]=0;
}
for(num=1;num<9;num++)
{
if(check[0][num]!=num)
{
MessageBox("对不起,您的输入有误,请重试!");
return;
}
}
for(i=0;i<9;i++)
{
if(check[1][i]==0)
continue;
else
{
for(num=i+1;num<9;num++)
{
if(check[1][num]==0)
continue;
else if(check[1][i]>check[1][num])
sum++;
}
}
}
if(sum%2==0)
MessageBox("此状态无解!");
else
{
OnBtnRefresh();
OnBtnSave();
MoveCount=0;
strCount.Format("次数:%d",MoveCount);
m_Count.SetWindowText(strCount);
}
}
void CAIDlg::OnBtnSave()
{
// TODO: Add your control notification handler code here
FILE *fp;
if((fp=fopen("save.dll","wb"))==NULL)
{
MessageBox("Sorry,there is an error in saving");
return;
}
fwrite(check,sizeof(check),1,fp);
fclose(fp);
}
void CAIDlg::OnBtnRead()
{
// TODO: Add your control notification handler code here
CString strCount;
MoveCount=0;
FILE *fp;
if((fp=fopen("save.dll","rb"))==NULL)
{
MessageBox("Sorry,there is an error in loading");
return;
}
fread(check,sizeof(check),1,fp);
fclose(fp);
OnBtnRefresh();
strCount.Format("次数:%d",MoveCount);
m_Count.SetWindowText(strCount);
}
//the function below set the small picture to the right position
//according to the Pic[i][j].positon;
void CAIDlg::SetPicPosition()
{
int i,j,Xposition,Yposition;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
Xposition=Pic[i][j].position%3;
Yposition=int(Pic[i][j].position/3);
Pic[i][j].PicRect.left=Xposition*120+10;
Pic[i][j].PicRect.top=Yposition*160+10;
Pic[i][j].PicRect.right=Pic[i][j].PicRect.left+120;
Pic[i][j].PicRect.bottom=Pic[i][j].PicRect.top+160;
}
// Invalidate();
}
void CAIDlg::Convert()
{
int ConverTable[9]={4,0,1,2,5,8,7,6,3};
int i,j,Xposition,Yposition;
for(i=0;i<9;i++)
{
j=ConverTable[check[1][i]];
Xposition=j%3;
Yposition=int(j/3);
Pic[Yposition][Xposition].position=i;
}
}
void CAIDlg::OnBtnRefresh()
{
// TODO: Add your control notification handler code here
Convert();
SetPicPosition();
Invalidate();
}
void CAIDlg::OnMove(int &Position,int i,int j)//need to compeleted
{
int temp;
if(CanMove(Position))
{
//InvalidateRect(&Pic[i][j].PicRect,TRUE);
//InvalidateRect(&Pic[1][1].PicRect,TRUE);
temp=Position;
CString strCount;
Pic[i][j].position=Pic[1][1].position;
Pic[1][1].position=temp;
SetPicPosition();
InvalidateRect(&Pic[i][j].PicRect,FALSE);
InvalidateRect(&Pic[1][1].PicRect,FALSE);
MoveCount++;
strCount.Format("次数:%d",MoveCount);
m_Count.SetWindowText(strCount);
}
}
BOOL CAIDlg::CanMove(int position)
{
int modle;
modle=position%3;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -