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

📄 drawdlg.cpp

📁 实现Agent,绕墙走的功能,实现了图形化界面.点中按钮<AddWall>或<AddBlock>,使其处于按下状态,然后就可以在左边的矩形区域内通过点击鼠标左键设定墙或障碍物.
💻 CPP
字号:
// DrawDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Draw.h"
#include "DrawDlg.h"
#include "BoardDraw.h"
#include "Board.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

Board myboard;
/*	value		stand for
	-1			wall_cell
	0			space_cell
	1			block_cell
	2			agent_cell
*/

/////////////////blank data
int eg_0[MAX_BOARD_ROW+2][MAX_BOARD_COL+2] = {//15,23
	{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},	//0
	{-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1},	//1
	{-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1},	//2
	{-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1},	//3
	{-1,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,-1},	//4
	{-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1},		//5
	{-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1},		//6
	{-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1},		//7
	{-1,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,-1},	//8
	{-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1},	//9
	{-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,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-1,0,0,0,0,0,0,0,0,0,0,-1,-1},
	{-1,-1,-1,0,0,0,0,-1,-1,-1,-1,0,0,0,0,0,0,0,0,0,0,-1,-1},
	{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
	{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}
};
/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CDrawDlg dialog

CDrawDlg::CDrawDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDrawDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDrawDlg)
	m_radio = -1;
	m_autorun = FALSE;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CDrawDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDrawDlg)
	DDX_Control(pDX, IDC_COMBO_SAMPLE, m_combo);
	DDX_Radio(pDX, IDC_RADIO_SPACE, m_radio);
	DDX_Check(pDX, IDC_CHECK_AUTORUN, m_autorun);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDrawDlg, CDialog)
	//{{AFX_MSG_MAP(CDrawDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_RBUTTONUP()
	ON_CBN_SELCHANGE(IDC_COMBO_SAMPLE, OnSelchangeComboSample)
	ON_BN_CLICKED(IDC_CHECK_AUTORUN, OnCheckAutorun)
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDrawDlg message handlers

BOOL CDrawDlg::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
	myboard.m_pDC = this->GetDC();
	this->SetWindowText("Agent");
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CDrawDlg::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 CDrawDlg::OnPaint() 
{
	myboard.m_pDC = this->GetDC();
	myboard.DrawBoard();
	
	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 CDrawDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CDrawDlg::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	//caculating the row and col
	pDown = point;

	CDialog::OnLButtonDown(nFlags, point);
}

void CDrawDlg::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	pUp = point;
	UpdateData(TRUE);
	DrawBoard(pDown,pUp,m_radio);

	CDialog::OnLButtonUp(nFlags, point);
}

void CDrawDlg::DrawBoard(CPoint startp, CPoint endp, int flag)
{
	if ( flag == -1 ) return;
	int i_start,j_start;
	int i_end,j_end;
	i_start = (startp.y - oy)/length;
	j_start = (startp.x - ox)/length;
	i_end = (endp.y - oy)/length;
	j_end = (endp.x - ox)/length;
	if ( i_end > MAX_BOARD_ROW ) i_end = MAX_BOARD_ROW;
	if ( j_end > MAX_BOARD_COL ) j_end = MAX_BOARD_COL;
	for ( int i=i_start; i<=i_end; i++ ) {
		for ( int j=j_start; j<=j_end; j++ ) {
			Position pos(i,j);
			Agent	agent(i,j);
			switch(flag) {
			case	0: //draw space
				if ( myboard.GetCell(pos) == agent_cell ) {
					myboard.Remove(agent);
				}
				myboard.SetCell(space_cell,pos);
				break;
			case	1: //draw wall
				if ( myboard.GetCell(pos) == agent_cell ) {
					myboard.Remove(agent);
				}
				myboard.SetCell(wall_cell,pos);
				break;
			case	2: //draw block
				if ( myboard.GetCell(pos) == agent_cell ) {
					myboard.Remove(agent);
				}
				myboard.SetCell(block_cell,pos);
				break;
			case	3: //draw agent
				if ( myboard.GetCell(pos) == agent_cell ) {
					return;
				}
				myboard.SetCell(agent_cell,pos);
				myboard.Add(agent);
				break;
			}//switch
		}//for j
	}// for i
	myboard.m_pDC = this->GetDC();
	myboard.DrawBoard();
}

void CDrawDlg::OnRButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	myboard.NextStatus();
	myboard.DrawBoard();

	CDialog::OnRButtonUp(nFlags, point);
}

void CDrawDlg::OnSelchangeComboSample() 
{
	// TODO: Add your control notification handler code here
	
	int index = m_combo.GetCurSel();
	switch ( index ) {
	case 0:	//Blank
		myboard.LoadBoard(&eg_0[0]);
		myboard.DrawBoard();
		break;
	case 1:	//sample 1
		myboard.LoadBoard(&eg_1[0]);
		myboard.DrawBoard();
		break;
	case 2: //sample 2;
		myboard.LoadBoard(&eg_2[0]);
		myboard.DrawBoard();
		break;
	case 3: //AI Sample
		myboard.LoadBoard(&eg_3[0]);
		myboard.DrawBoard();
		break;
	}
}

void CDrawDlg::OnCheckAutorun() 
{
	// TODO: Add your control notification handler code here
	SetTimer(1,100,NULL);
}

void CDrawDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	if ( nIDEvent == 1 ) {
		UpdateData(TRUE);
		if ( m_autorun == FALSE )
			KillTimer(nIDEvent);
		myboard.NextStatus();
		myboard.DrawBoard();
	}
	CDialog::OnTimer(nIDEvent);
}

⌨️ 快捷键说明

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