📄 maseview.cpp
字号:
// MaseView.cpp : implementation of the CMaseView class
//
#include "stdafx.h"
#include "Mase.h"
#include "MaseDoc.h"
#include "MaseView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMaseView
/////////////////////////////////////////////////////
Status InitStack(SqStack &S)
{
S.base = (SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType)) ;
if (!S.base) exit(OVERFLOW) ;
S.top = S.base ;
S.stacksize = STACK_INIT_SIZE ;
return OK ;
}
Status Push(SqStack &S, SElemType e)
{
if (S.top- S.base >= S.stacksize) {
S.base = (SElemType*)realloc(S.base, (S.stacksize + STACKINCREMENT)*sizeof(SElemType)) ;
if (!S.base) exit(OVERFLOW) ;
S.top = S.base + S.stacksize ;
S.stacksize += STACKINCREMENT ;
}
*S.top++ = e ;
return OK ;
}
Status Pop(SqStack &S, SElemType &e)
{
if (S.top == S.base ) return ERROR ;
e = *--S.top ;
return OK ;
}
Status StackEmpty(SqStack S)
{
if (S.top == S.base)
return TRUE ;
return FALSE ;
}
Status CompareStack(SqStack S, SElemType &e)
{
SElemType top ;
while ((S.top != S.base) && !StackEmpty(S)) {
top = *--S.top ;
if ((e.seat.x == top.seat.x)&&(e.seat.y == top.seat.y) ) return TRUE ;
}
//
return FALSE ;
}
/////////////////////////////////////////////////////
IMPLEMENT_DYNCREATE(CMaseView, CView)
BEGIN_MESSAGE_MAP(CMaseView, CView)
//{{AFX_MSG_MAP(CMaseView)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMaseView construction/destruction
CMaseView::CMaseView()
{
// TODO: add construction code here
for (int i =0 ; i<10 ; i++)
{ for(int j= 0 ; j<10 ; j++)
{
if (i>0 && (i!=9) && j>0) j += 8 ;
mase[i][j] = "■" ;
}
}
mase[1][3] = "■" ;
mase[2][3] = "■" ;
mase[1][7] = "■" ;
mase[2][7] = "■" ;
mase[3][5] = "■" ;
mase[3][6] = "■" ;
mase[4][2] = "■" ;
mase[4][3] = "■" ;
mase[4][4] = "■" ;
mase[5][4] = "■" ;
mase[6][2] = "■" ;
mase[6][6] = "■" ;
mase[7][2] = "■" ;
mase[7][3] = "■" ;
mase[7][4] = "■" ;
mase[7][6] = "■" ;
mase[7][7] = "■" ;
mase[8][1] = "■" ;
for ( i =0 ; i<10 ; i++)
{ for(int j= 0 ; j<10 ; j++)
{
if(mase[i][j] != "■")
mase[i][j] = "□" ;
}
}
mase[8][8] = "○" ;
}
CMaseView::~CMaseView()
{
}
BOOL CMaseView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMaseView drawing
void CMaseView::OnDraw(CDC* pDC)
{
CMaseDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CRect rect ;
GetClientRect(&rect) ;
int x = 100;
int y = 100 ;
for ( int i =0 ; i<10 ; i++)
{ for(int j= 0 ; j<10 ; j++)
{
pDC->TextOut(j*14, i*14,mase[i][j]) ;
/*
if (pDoc->mase[i][j] == "□") {
pDC->SetPixel(x+j*(14) + 7, y+i*(14) + 7, RGB(255,0,0)) ;*/
// }
}
}
// pDC->TextOut(x + 8*14, y+ 8*14,"○") ;
PosType Start ;
PosType End ;
Start.x = 1 ;
Start.y = 1 ;
End.x = 8 ;
End.y = 8 ;
MazePath(Start, End) ;
/*
if (i==1) pDC->TextOut(0, 0, "TRUE") ;
else
pDC->TextOut(0,0,"FALSE") ;*/
CString str;
SElemType e ;
int j ;
j = 0 ;
POINT point, LastPoint;
// point.x = 21 ;
// point.y = 21 ;
LastPoint.x = 8 * 14 + 7 ;
LastPoint.y = 8 * 14 + 7 ;
pDC->MoveTo(LastPoint) ;
// CPen pen()
while (S.top != S.base) {
Pop(S,e) ;
x = e.seat.y*(14) + 7 ;
y = e.seat.x*(14) + 7 ;
pDC->SetPixel(x, y , RGB(255,0,0)) ;
point.x = x ;
point.y = y ;
pDC->LineTo(point) ;
LastPoint = point ;
pDC->MoveTo(LastPoint) ;
}
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CMaseView printing
BOOL CMaseView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CMaseView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CMaseView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CMaseView diagnostics
#ifdef _DEBUG
void CMaseView::AssertValid() const
{
CView::AssertValid();
}
void CMaseView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CMaseDoc* CMaseView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMaseDoc)));
return (CMaseDoc*)m_pDocument;
}
Status CMaseView::Pass(PosType CurrentPos)
{
int x,y ;
x = CurrentPos.x ;
y = CurrentPos.y ;
SElemType e ;
e.seat = CurrentPos ;
int flag ;
flag = CompareStack(S,e) ;
if (flag) return FALSE ;
if (mase[x][y] == "■") return FALSE ;
return TRUE ;
}
PosType CMaseView::NextPos(PosType curpos, int di)
{
PosType Position = curpos ;
switch(di) {
case 1:
Position.y += 1 ;
break;
case 2:
Position.x += 1 ;
break;
case 3:
Position.y -= 1 ;
break;
case 4:
Position.x -= 1 ;
break;
default:
break;
}
return Position ;
}
Status CMaseView::MazePath( PosType start, PosType End)
{
int curstep ;
curstep = 1 ;
SElemType e ;
InitStack(S) ;
PosType curpos ;
curpos = start ;
do {
if(Pass(curpos))
{
e.ord = curstep ;
e.seat = curpos ;
e.di = 1 ;
Push(S,e) ;
if ((curpos.x == End.x)&&(curpos.y==End.y)) return TRUE ;
curpos = NextPos(curpos, 1) ;
curstep++ ;
}
else
{
if (!StackEmpty(S)) {
Pop(S,e) ;
while (e.di == 4 && !StackEmpty(S)) {
Pop(S,e) ;
}//while
if (e.di<4) {
e.di++ ; Push(S,e) ;
curpos = NextPos(e.seat,e.di) ;
}
}
}
} while(!StackEmpty(S));
return FALSE ;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMaseView message handlers
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -