MainFrm.cpp
资源名称:8-Games.rar [点击查看]
上传用户:tjwfgggs
上传日期:2022-06-07
资源大小:4331k
文件大小:6k
源码类别:
其他游戏
开发平台:
Visual C++
- // MainFrm.cpp : implementation of the CMainFrame class
- //
- #include "stdafx.h"
- #include "Eight.h"
- #include "EightDoc.h"
- #include "MainFrm.h"
- #include "NineBox.h"
- #include "ProcMtd.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CMainFrame
- IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
- BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
- //{{AFX_MSG_MAP(CMainFrame)
- ON_WM_CREATE()
- ON_COMMAND(ID_MOVE_DOWN, OnMoveDown)
- ON_COMMAND(ID_MOVE_LEFT, OnMoveLeft)
- ON_COMMAND(ID_MOVE_RIGHT, OnMoveRight)
- ON_COMMAND(ID_MOVE_UP, OnMoveUp)
- ON_COMMAND(ID_NEW, OnNew)
- ON_COMMAND(ID_RESET, OnReset)
- ON_COMMAND(ID_AI_P, OnAiP)
- ON_WM_CHAR()
- ON_WM_TIMER()
- ON_WM_ACTIVATE()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- static UINT indicators[] =
- {
- ID_SEPARATOR, // status line indicator
- ID_INDICATOR_CAPS,
- ID_INDICATOR_NUM,
- ID_INDICATOR_SCRL,
- };
- /////////////////////////////////////////////////////////////////////////////
- // CMainFrame construction/destruction
- CMainFrame::CMainFrame()
- {
- // TODO: add member initialization code here
- }
- CMainFrame::~CMainFrame()
- {
- }
- int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
- return -1;
- if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
- | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
- !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
- {
- TRACE0("Failed to create toolbarn");
- return -1; // fail to create
- }
- if (!m_wndStatusBar.Create(this) ||
- !m_wndStatusBar.SetIndicators(indicators,
- sizeof(indicators)/sizeof(UINT)))
- {
- TRACE0("Failed to create status barn");
- return -1; // fail to create
- }
- // TODO: Delete these three lines if you don't want the toolbar to
- // be dockable
- m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
- EnableDocking(CBRS_ALIGN_ANY);
- DockControlBar(&m_wndToolBar);
- return 0;
- }
- BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
- {
- if( !CFrameWnd::PreCreateWindow(cs) )
- return FALSE;
- // TODO: Modify the Window class or styles here by modifying
- // the CREATESTRUCT cs
- cs.x=250;
- cs.y=150;
- cs.cx=500;
- cs.cy=500;
- cs.lpszName="八数码游戏";
- cs.style = WS_OVERLAPPED | WS_CAPTION
- | WS_SYSMENU | WS_MINIMIZEBOX;
- return TRUE;
- }
- /////////////////////////////////////////////////////////////////////////////
- // CMainFrame diagnostics
- #ifdef _DEBUG
- void CMainFrame::AssertValid() const
- {
- CFrameWnd::AssertValid();
- }
- void CMainFrame::Dump(CDumpContext& dc) const
- {
- CFrameWnd::Dump(dc);
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CMainFrame message handlers
- void CMainFrame::OnMoveDown()
- {
- // TODO: Add your command handler code here
- CEightDoc* doc=(CEightDoc*)this->GetActiveDocument();
- NineBox* obj=doc->GetBoxObj();
- obj->MoveDown();
- doc->UpdateAllViews(0);
- }
- void CMainFrame::OnMoveLeft()
- {
- // TODO: Add your command handler code here
- CEightDoc* doc=(CEightDoc*)this->GetActiveDocument();
- NineBox* obj=doc->GetBoxObj();
- obj->MoveLeft();
- doc->UpdateAllViews(0);
- }
- void CMainFrame::OnMoveRight()
- {
- // TODO: Add your command handler code here
- CEightDoc* doc=(CEightDoc*)this->GetActiveDocument();
- NineBox* obj=doc->GetBoxObj();
- obj->MoveRight();
- doc->UpdateAllViews(0);
- }
- void CMainFrame::OnMoveUp()
- {
- // TODO: Add your command handler code here
- CEightDoc* doc=(CEightDoc*)this->GetActiveDocument();
- NineBox* obj=doc->GetBoxObj();
- obj->MoveUp();
- doc->UpdateAllViews(0);
- }
- /*
- *建立一个新的可解的8数码问题
- */
- void CMainFrame::OnNew()
- {
- // TODO: Add your command handler code here
- srand(time(0));
- CEightDoc* doc=(CEightDoc*)this->GetActiveDocument();
- NineBox* obj=doc->GetBoxObj();
- obj->ResetValue();
- for (int i=0;i<1000;i++)
- {
- int j=rand()%4;
- switch(j) {
- case 1:
- obj->MoveUp();
- break;
- case 2:
- obj->MoveDown();
- break;
- case 3:
- obj->MoveLeft();
- break;
- case 0:
- obj->MoveRight();
- break;
- default:;
- }
- }
- doc->UpdateAllViews(0);
- obj->SetLastMoveType(-1);
- }
- /*
- *初试化8数码布局
- */
- void CMainFrame::OnReset()
- {
- // TODO: Add your command handler code here
- CEightDoc* doc=(CEightDoc*)this->GetActiveDocument();
- NineBox* obj=doc->GetBoxObj();
- obj->ResetValue();
- doc->UpdateAllViews(0);
- }
- /*
- *用过程状态法来求解8数码问题
- */
- void CMainFrame::OnAiP()
- {
- // TODO: Add your command handler code here
- CEightDoc* doc=(CEightDoc*)this->GetActiveDocument();
- NineBox* box=doc->GetBoxObj();
- obj=new ProcMtd(box);
- this->SetTimer(1,500,0);
- }
- void CMainFrame::do_aip()
- {
- CEightDoc* doc=(CEightDoc*)this->GetActiveDocument();
- NineBox* box=doc->GetBoxObj();
- static B_init=false;
- int ir=0;
- if(!B_init)
- {
- ir=obj->Init();
- }
- if(ir==0)
- {
- B_init=true;
- ir=obj->solve();
- }
- if(ir==0)
- {
- this->KillTimer(1);
- delete obj;obj=0;
- }
- //--------------------------
- switch(ir)
- {
- case 1:
- box->MoveUp();
- break;
- case 2:
- box->MoveDown();
- break;
- case 3:
- box->MoveLeft();
- break;
- case 4:
- box->MoveRight();
- break;
- default:;
- }
- doc->UpdateAllViews(0);
- //---------------------------------
- }
- void CMainFrame::OnTimer(UINT nIDEvent)
- {
- switch(nIDEvent)
- {
- case 1:
- do_aip();
- break;
- default:;
- }
- CFrameWnd::OnTimer(nIDEvent);
- }