⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 box8dlg.h

📁 自制 拼图游戏 mfc程序 9格子拼图 可打开外部图片进行游戏 支持bmp,jpg,gif格式 记录最近打开的问件 上方有菜单
💻 H
字号:
// box8Dlg.h : header file
//

#if !defined(AFX_BOX8DLG_H__B4D1ECE9_9F4D_4B40_B53F_0BE0BD7A101A__INCLUDED_)
#define AFX_BOX8DLG_H__B4D1ECE9_9F4D_4B40_B53F_0BE0BD7A101A__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include<list>
using namespace std;
#include"Picture.h"
inline int rand(int x,int y)
{
  	UINT range=y-x+1;
	return rand()%(range)+x;
}
inline void swap(int& x,int& y)
{
	int tmp=x;x=y;y=tmp;
}

class BoxOder // 储存每个box当前为那个图像块
{
	int oder[9];
	int oderBack[9];BOOL backAble;
	int blankBoxIndex;
	inline void GetBoxXY(int& objX,int& objY,const int objIndex)
	{
		objX=objIndex%3;
		objY=objIndex/3;
	}	
	void StoreBack()
	{
		backAble=true;
		memcpy(oderBack,oder,9*sizeof(int));
	}
public:
	BOOL IsBackAble()
	{
		return backAble;
	}
	BOOL IsWin()
	{
		for(int i=0;i<8;i++)
		{
			if(oder[i]!=i+1)
				return false;
		}
		return true;
	}
	BOOL Back()
	{
		if(backAble==false)
			return false;
		backAble=false;
		memcpy(oder,oderBack,9*sizeof(int));
		for(int i=0;i<9;i++)
		{
			if(oder[i]==0)
			{
				blankBoxIndex=i;
				break;
			}
		}
		return true;
	}
	BOOL MoveByDir(UINT dir)
	{
		int index=-1;
		switch(dir)
		{
		case VK_UP:
			index=blankBoxIndex+3;
			break;
		case VK_DOWN:
			index=blankBoxIndex-3;
			break;
		case VK_LEFT:
			index=blankBoxIndex+1;
			break;
		case VK_RIGHT:
			index=blankBoxIndex-1;
			break;
		}
		if(index<0||index>8) return false;
		return Move(index);
	}
	BOOL Move(int objIndex)
	{
		
		if(objIndex==blankBoxIndex||IsWin())  return false;
		//0 mean donot moved
			
		int objX=0,objY=0;
		int blankX=0,blankY=0;
		GetBoxXY(objX,objY,objIndex);
		GetBoxXY(blankX,blankY,blankBoxIndex);
		if(objX!=blankX && objY!=blankY)
			return 0;

		StoreBack();  //want move stroe back
		if(abs(objX-blankX)==1
			||abs(objY-blankY)==1)
		{
			swap(oder[objIndex],oder[blankBoxIndex]);
		}
		else if(abs(objX-blankX)==2
			||abs(objY-blankY)==2)
		{
			int midBoxIndex=(objIndex+blankBoxIndex)/2;	
			swap(oder[midBoxIndex],oder[blankBoxIndex]);
			swap(oder[objIndex],oder[midBoxIndex]);
		}
		// is moved
		blankBoxIndex=objIndex;
		return true;
	}
	BoxOder()
	{
		srand(time(0));
		//ReSet();
	}
	void JianCha()
	{
		int hiNum=0;
		for(int i=0;i<8;i++){
			if(oder[i]==0)
				continue;
			for(int j=i+1;j<9;j++){
				if(oder[j]==0)
					continue;
				if(oder[i]>oder[j])
				{
					//cout<<oder[i],">",oder[j],"\n");
					hiNum++;
				}
			}
		}
		//putNums(oder);
		//puts(hiNum);
		if(hiNum%2==1)
		{
			int i=1;
			if(oder[0]==0 || oder[1]==0)
				i=2;
			swap(oder[0],oder[i]);
		}
		for( i=0;i<9;i++)
		{
			if(oder[i]==0)
			{
				this->blankBoxIndex=i;
				break;
			}
		}
	}
	void ReSet()
	{
		backAble=false;
		for(int i=0;i<9;i++)
		{
			BOOL newRandNum=false;
			do
			{
				newRandNum=false;
				oder[i]=rand(0,8); //zero mean blank box
				for(int j=0;j<i;j++)
					if(oder[i]==oder[j])
						break;
				if(i==j)
					newRandNum=true;
			}while(!newRandNum);
// 			if(oder[i]==0)
// 				this->blankBoxIndex=i;
		}
		JianCha();

	}
	int operator[](int index)
	{
		return oder[index];
	}
};
/////////////////////////////////////////////////////////////////////////////
// CBox8Dlg dialog
class CBox8Dlg : public CDialog
{
// Construction
public:
	CBox8Dlg(CWnd* pParent = NULL);	// standard constructor

// Dialog Data
	//{{AFX_DATA(CBox8Dlg)
	enum { IDD = IDD_BOX8_DIALOG };
		// NOTE: the ClassWizard will add data members here
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CBox8Dlg)
	public:
	virtual void Serialize(CArchive& ar);
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);	// DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	HICON m_hIcon;

	// Generated message map functions
	//{{AFX_MSG(CBox8Dlg)
	virtual BOOL OnInitDialog();
	afx_msg void OnPaint();
	afx_msg HCURSOR OnQueryDragIcon();
	afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
	afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
	afx_msg void OnMenuitemNew();
	afx_msg void OnMenuitemQuit();
	afx_msg void OnMenuitemBack();
	afx_msg void OnMenuitemOpen();
	afx_msg void OnMenuitemClearPath();
	afx_msg void OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags);
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
private:
	BOOL bOnSize;
	void UpdateMenuPLItem();
	void UpdateMenuBackItem();
	void OnDraw(CDC *pDC);
	void OnMenuitemIL(UINT id);

	CPicture pic;
	list<CString> imgPathList;
	BOOL showBoxNum;
	int clickBoxIndex;
	CRect rectABox[9];
	BoxOder boxOder;
	UINT bmpWidth,bmpHeigth;
	CBitmap unhandBitmap;
	CDC mDC;
	inline int GetClickIndex(CPoint p)
	{
		for(int i=0;i<9;i++)		
			if(rectABox[i].PtInRect(p)) // change boxoder and invalidate for show			
				return i;
		return 0xffff;
	}
	BOOL Load();
	void Store();
	void LoadImage(CString path, BOOL bResize=false);
};

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_BOX8DLG_H__B4D1ECE9_9F4D_4B40_B53F_0BE0BD7A101A__INCLUDED_)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -