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

📄 wumpusdlg.cpp

📁 一款有关于人工智能的游戏的源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// WumpusDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Wumpus.h"
#include "WumpusDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CWumpusDlg dialog

CWumpusDlg::CWumpusDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CWumpusDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CWumpusDlg)
	m_nHeight = 0;
	m_nWidth = 0;
	m_nScorses = 0;
	m_ManualSetCheck = FALSE;
	m_setstyle = -1;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CWumpusDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CWumpusDlg)
	DDX_Control(pDX, IDC_WORLD_STATIC, m_WumpusWorld);
	DDX_Control(pDX, IDC_AUTO_SET_BUTTON, m_AutoSetButton);
	DDX_Control(pDX, IDC_INITIALIZE_BUTTON, m_InitButton);
	DDX_Control(pDX, IDC_START_BUTTON, m_StartButton);
	DDX_Control(pDX, IDC_RESTART_BUTTON, m_RestartButton);
	DDX_Control(pDX, IDC_STATE_EDIT, m_StateEdit);
	DDX_Text(pDX, IDC_HEIGHT_EDIT, m_nHeight);
	DDX_Text(pDX, IDC_WIDTH_EDIT, m_nWidth);
	DDX_Text(pDX, IDC_SCORES_EDIT, m_nScorses);
	DDX_Check(pDX, IDC_MANUAL_SET_CHECK, m_ManualSetCheck);
	DDX_Radio(pDX, IDC_SET_PIT_RADIO, m_setstyle);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CWumpusDlg, CDialog)
	//{{AFX_MSG_MAP(CWumpusDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_INITIALIZE_BUTTON, OnInitializeButton)
	ON_BN_CLICKED(IDC_EXIT_BUTTON, OnExitButton)
	ON_BN_CLICKED(IDC_ABOUT_BUTTON, OnAboutButton)
	ON_BN_CLICKED(IDC_MANUAL_SET_CHECK, OnManualSetCheck)
	ON_BN_CLICKED(IDC_AUTO_SET_BUTTON, OnAutoSetButton)
	ON_BN_CLICKED(IDC_START_BUTTON, OnStartButton)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_SET_PIT_RADIO, OnSetPitRadio)
	ON_BN_CLICKED(IDC_SET_WUMPUS_RADIO, OnSetWumpusRadio)
	ON_BN_CLICKED(IDC_SET_GOLD_RADIO, OnSetGoldRadio)
	//}}AFX_MSG_MAP
	ON_MESSAGE(UM_MOUSEMOVE,OnStaticMouseMove)
	
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWumpusDlg message handlers

BOOL CWumpusDlg::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(1,250,NULL);
	SetTimer(2,100,NULL);
	m_ManualSetCheck=FALSE;
	m_IsInitialized=FALSE;
	memset(m_World,0,8*8*sizeof(int));
	m_nWidth=m_nHeight=5;
	UpdateData(FALSE);
	at_wumpus.cx=0;
	at_wumpus.cy=0;
	arrow_wumpus=0;
	arrow_draw=false;
	step_wumpus=0;
	end_up=FALSE;
	step_count=0;
	found_gold=FALSE;
	on_time=0;
	pass[0].cx=0;
	pass[0].cy=0;
	m=-1;
	n=-1;
	pit=0;
	wumpus=0;
	gold=0;
////////////////////////
	scores=0;
	arrow=TRUE;
	direction=0;
	from_direction=0;
	m_feasible=0;
	step=0;
	step_end=0;
	on_start=0;
	x=0;y=0;
	for(i=0;i<5;i++)
		for(j=0;j<5;j++)
		{
			Hero[i][j].count =0;
			Hero[i][j].feasible=0;
			Hero[i][j].right =-1;
			Hero[i][j].down =-1;
			Hero[i][j].left =-1;
			Hero[i][j].up=-1;
			Hero[i][j].pit =FALSE;
			Hero[i][j].gold =FALSE;
			Hero[i][j].wumpus =FALSE;
			Hero[i][j].Breeze =FALSE;
			Hero[i][j].Stench =FALSE;
			Hero[i][j].IsPit =0;
			Hero[i][j].IsWumpus =0;
		}
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CWumpusDlg::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 CWumpusDlg::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
	{
		CDialog::OnPaint();
	}


	CWnd * pWnd = GetDlgItem(IDC_WORLD_STATIC);
	CDC *pPalaceDC = pWnd->GetWindowDC() ;
	CBrush brush(RGB(0,0,0)) ;
	CRect rect ;
	pWnd->Invalidate(FALSE) ;
	pWnd->UpdateWindow() ;
	CRect crect;
//	m_WorldStatic.GetClientRect(crect);
	m_CellWidth=(int)crect.Width()/m_nWidth;
	m_CellHeight=(int)crect.Height()/m_nHeight;

	CBitmap bitmap;
	BITMAP bmp;
	CDC dcCompitable;
	if( m_IsInitialized == TRUE )
	{
		int i,j ;
		CPoint topLeft,bottomRight ;
		for(i=0;i<m_nWidth;i++)
		{
			for(j=0;j<m_nHeight;j++)
			{
				topLeft = CPoint(i*m_CellWidth,j*m_CellHeight) ;
				bottomRight = CPoint((i+1)*m_CellWidth,(j+1)*m_CellHeight) ;
				rect.SetRect(topLeft,bottomRight) ;
				switch(m_World[i][j])
				{
				case 0:
					pPalaceDC->FillSolidRect(&rect,RGB(255,255,255)) ;
					break;
				case 1:	
					bitmap.LoadBitmap(IDB_PIT);
					break;
				case 2:
					bitmap.LoadBitmap(IDB_GOLD);
					break;
				case 3:
					bitmap.LoadBitmap(IDB_WUMPUS);
					break;
				case 4:
					bitmap.LoadBitmap (IDB_HERO);
					break;
				}
			if(m_World[i][j]!=0)
			{
				bitmap.GetBitmap(&bmp);

				dcCompitable.CreateCompatibleDC (pPalaceDC);
				dcCompitable.SelectObject (&bitmap);

				pPalaceDC->StretchBlt (i*m_CellWidth,j*m_CellHeight,m_CellWidth,m_CellHeight,&dcCompitable,0,0,bmp.bmWidth,bmp.bmHeight,SRCCOPY);					
				
				dcCompitable.DeleteDC();
				bitmap.DeleteObject();	
			}
				pPalaceDC->FrameRect(&rect,&brush) ;//绘制矩形边框
			}
		}		
	}
	CPen pen(PS_DASHDOT,1,RGB(255,0,0));
	pPalaceDC->SelectObject(pen);
	int a,b;
	if(arrow_draw)
	{
//		PAINTSTRUCT ps;
//		CPen pen(PS_DASHDOT,1,RGB(255,0,0));
	
//		pPalaceDC->SelectObject(pen);

		a=pass[step_wumpus-1].cx;
		b=pass[step_wumpus-1].cy;
		i=(arrow_wumpus-3)/40;
		if(pass[step_wumpus].cx-pass[step_wumpus-1].cx==1)			//向右射箭
		{
			a=(int)(a+1)*m_CellWidth;
			b=(int)((b+0.5)*m_CellHeight);
			pPalaceDC->MoveTo(a+i*m_CellWidth,b);
			pPalaceDC->LineTo(a+i*m_CellWidth+m_CellWidth/2,b);
		}
		if(pass[step_wumpus].cx-pass[step_wumpus-1].cx==-1)			//向左射箭
		{
			a=(int)(a*m_CellWidth);
			b=(int)((b+0.5)*m_CellHeight);
			pPalaceDC->MoveTo(a-i*m_CellWidth,b);
			pPalaceDC->LineTo(a-i*m_CellWidth-m_CellWidth/2,b);
		}
		if(pass[step_wumpus].cy-pass[step_wumpus-1].cy==1)			//向下射箭
		{
			a=(int)((a+0.5)*m_CellWidth);
			b=(int)((b+1)*m_CellHeight);
			pPalaceDC->MoveTo(a,b+i*m_CellHeight);
			pPalaceDC->LineTo(a,b+i*m_CellHeight+m_CellHeight/2);
		}
		if(pass[step_wumpus].cy-pass[step_wumpus-1].cy==-1)			//向上射箭
		{
			a=(int)((a+0.5)*m_CellWidth);
			b=(int)(b*m_CellHeight);
			pPalaceDC->MoveTo(a,b-i*m_CellHeight);
			pPalaceDC->LineTo(a,b-i*m_CellHeight-m_CellHeight/2);
		}

	}

}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CWumpusDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CWumpusDlg::OnInitializeButton() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	if( m_nWidth<4 ||m_nWidth>8 ||m_nHeight<4||m_nHeight>8 )
	{
		MessageBox("Please reset the width and height!","Waring",MB_OK|MB_ICONERROR) ;
		return ;
	}
	memset(m_World,0,8*8*sizeof(int));
	m_nGold=0;
	m_nPit=0;
	m_nWumpus=0;
	m_IsInitialized=TRUE;
//	m_InitButton.EnableWindow(FALSE);
	m_AutoSetButton.EnableWindow(TRUE);
	m_StartButton.EnableWindow(TRUE);
	Invalidate(FALSE);
}

void CWumpusDlg::OnExitButton() 
{
	// TODO: Add your control notification handler code here
	OnOK();
}

void CWumpusDlg::OnAboutButton() 
{
	// TODO: Add your control notification handler code here
	CAboutDlg dlg;
	dlg.DoModal();
}

void CWumpusDlg::OnManualSetCheck() 
{
	// TODO: Add your control notification handler code here
	if(m_ManualSetCheck==TRUE)
	{
		m_ManualSetCheck=FALSE;
		m_AutoSetButton.EnableWindow(TRUE);
	}
	else
	{
		m_ManualSetCheck=TRUE;
		m_AutoSetButton.EnableWindow(FALSE);
	}
}

void CWumpusDlg::OnAutoSetButton() 
{
	// TODO: Add your control notification handler code here
	arrow=TRUE;
	at_wumpus.cx=0;
	at_wumpus.cy=0;
	arrow_draw=false;
	arrow_wumpus=0;
	step_wumpus=0;
	direction=0;
	end_up=FALSE;
	found_gold=FALSE;
	from_direction=0;
	m=-1;
	n=-1;
	pit=0;
	wumpus=0;	
	gold=0;
	for(i=0;i<5;i++)
		for(j=0;j<5;j++)
		{
			Hero[i][j].count =0;
			Hero[i][j].feasible=0;
			Hero[i][j].pit =FALSE;
			Hero[i][j].gold =FALSE;
			Hero[i][j].wumpus =FALSE;
			Hero[i][j].Breeze =FALSE;
			Hero[i][j].Stench =FALSE;
			Hero[i][j].IsPit =0;
			Hero[i][j].IsWumpus =0;
		}
	m_nDrawType=0;
	for(i=0;i<5;i++)
		for(j=0;j<5;j++)
			m_World[i][j]=0;
	pass[0].cx=0;
	pass[0].cy=0;
	scores=0;
	m_feasible=0;
	step=0;
	step_count=0;
	step_end=0;
	on_start=0;
	x=0;y=0;

///////////////////////////////////////////////////////////////////////////////////



⌨️ 快捷键说明

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