mergenewView.cpp
上传用户:sycq158
上传日期:2008-10-22
资源大小:15361k
文件大小:9k
- // mergenewView.cpp : implementation of the CMergenewView class
- //
- #include "stdafx.h"
- #include "mergenew.h"
- #include "mergenewDoc.h"
- #include "mergenewView.h"
- #include "path.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- Stack _stack;
- Stack _stack2;
- /////////////////////////////////////////////////////////////////////////////
- // CMergenewView
- IMPLEMENT_DYNCREATE(CMergenewView, CView)
- BEGIN_MESSAGE_MAP(CMergenewView, CView)
- //{{AFX_MSG_MAP(CMergenewView)
- ON_WM_KEYDOWN()
- ON_COMMAND(ID_NEWGAME, OnNewgame)
- ON_COMMAND(ID_RESULT, OnResult)
- ON_COMMAND(ID_DOUBLERESULT, OnDoubleresult)
- //}}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()
- /////////////////////////////////////////////////////////////////////////////
- // CMergenewView construction/destruction
- CMergenewView::CMergenewView()
- {
- // TODO: add construction code here
- width=20;
- point.x=1;
- point.y=1;
- showtag=false;
- resulttag=false;
- turn=false;
- doubleresult=false;
- }
- CMergenewView::~CMergenewView()
- {
- }
- BOOL CMergenewView::PreCreateWindow(CREATESTRUCT& cs)
- {
- // TODO: Modify the Window class or styles here by modifying
- // the CREATESTRUCT cs
- return CView::PreCreateWindow(cs);
- }
- /////////////////////////////////////////////////////////////////////////////
- // CMergenewView drawing
- void CMergenewView::OnDraw(CDC* pDC)
- {
- CMergenewDoc* pDoc = GetDocument();
- ASSERT_VALID(pDoc);
- if(showtag)
- {
- DrawMerge(pDC);
- }
- if(resulttag&&turn)
- {
- DrawMerge(pDC);
- DrawResult(pDC);
- }
- if(doubleresult)
- {
- DrawMerge(pDC);
- DrawDoubleResult(pDC);
- }
- // TODO: add draw code for native data here
- }
- /////////////////////////////////////////////////////////////////////////////
- // CMergenewView printing
- BOOL CMergenewView::OnPreparePrinting(CPrintInfo* pInfo)
- {
- // default preparation
- return DoPreparePrinting(pInfo);
- }
- void CMergenewView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add extra initialization before printing
- }
- void CMergenewView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add cleanup after printing
- }
- /////////////////////////////////////////////////////////////////////////////
- // CMergenewView diagnostics
- #ifdef _DEBUG
- void CMergenewView::AssertValid() const
- {
- CView::AssertValid();
- }
- void CMergenewView::Dump(CDumpContext& dc) const
- {
- CView::Dump(dc);
- }
- CMergenewDoc* CMergenewView::GetDocument() // non-debug version is inline
- {
- ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMergenewDoc)));
- return (CMergenewDoc*)m_pDocument;
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CMergenewView message handlers
- void CMergenewView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
- {
- // TODO: Add your message handler code here and/or call default
- CClientDC dc(this);
- switch(nChar)
- {
- case VK_DOWN:
- {
- if(point.x+1<SIZEX_MAZE-1)
- if(!maze[point.x+1][point.y])
- {
- DrawColorBlocks(&dc,point);
- point.x++;
- DrawColorBlocks(&dc,point,125,125,125);
- }
- break;
- }
- case VK_LEFT:
- {
- if(point.y-1>0)
- if(!maze[point.x][point.y-1])
- {
- DrawColorBlocks(&dc,point);
- point.y--;
- DrawColorBlocks(&dc,point,125,125,125);
- }
- break;
- }
- case VK_RIGHT:
- {
- if(point.y+1<SIZEY_MAZE-1)
- if(!maze[point.x][point.y+1])
- {
- DrawColorBlocks(&dc,point);
- point.y++;
- DrawColorBlocks(&dc,point,125,125,125);
- }
- break;
- }
- case VK_UP:
- {
- if(point.x-1>0)
- if(!maze[point.x-1][point.y])
- {
- DrawColorBlocks(&dc,point);
- point.x--;
- DrawColorBlocks(&dc,point,125,125,125);
- }
- break;
- }
- //case VK_DOWNLEFT:
- case VK_DELETE:
- {
- if(point.x+1<SIZEX_MAZE-1&&point.y-1>0)
- if(!maze[point.x+1][point.y-1])
- {
- DrawColorBlocks(&dc,point);
- point.x++;
- point.y--;
- DrawColorBlocks(&dc,point,125,125,125);
- }
- break;
- }
- //case VK_DOWNRIGHT:
- case VK_END:
- {
- if(point.x+1<SIZEX_MAZE-1&&point.y+1<SIZEY_MAZE-1)
- if(!maze[point.x+1][point.y+1])
- {
- DrawColorBlocks(&dc,point);
- point.x++;
- point.y++;
- DrawColorBlocks(&dc,point,125,125,125);
- }
- break;
- }
- //case VK_UPLEFT:
- case VK_INSERT:
- {
- if(point.x-1>0&&point.y-1>0)
- if(!maze[point.x-1][point.y-1])
- {
- DrawColorBlocks(&dc,point);
- point.x--;
- point.y--;
- DrawColorBlocks(&dc,point,125,125,125);
- }
- break;
- }
- //case VK_UPRIGHT:
- case VK_HOME:
- {
- if(point.x-1>0&&point.y+1<SIZEY_MAZE-1)
- if(!maze[point.x-1][point.y+1])
- {
- DrawColorBlocks(&dc,point);
- point.x--;
- point.y++;
- DrawColorBlocks(&dc,point,125,125,125);
- }
- break;
- }
- default:
- break;
- }
-
- if(point.x==EXIT_ROW&&point.y==EXIT_COL)
- {
- showtag=false;
- resulttag=false;
- turn=false;
- point.x=1;
- point.y=1;
- MessageBox("you are wonderful");
- }
- CView::OnKeyDown(nChar, nRepCnt, nFlags);
- }
- void CMergenewView::DrawColorBlocks(CDC * pDC,CPoint point)
- {
- CBrush backbrush,*oldbrush;
- CRect rect;
- rect.top=width*point.x;
- rect.bottom=width*(point.x+1);
- rect.left=width*point.y;
- rect.right=width*(point.y+1);
- backbrush.CreateSolidBrush(RGB(255,133,0));
- oldbrush=pDC->SelectObject(&backbrush);
- pDC->Rectangle(rect);
- pDC->SelectObject(oldbrush);
- }
- void CMergenewView::DrawColorBlocks(CDC * pDC,CPoint point,int r,int g,int b)
- {
- CBrush backbrush,*oldbrush;
- CRect rect;
- rect.top=width*point.x;
- rect.bottom=width*(point.x+1);
- rect.left=width*point.y;
- rect.right=width*(point.y+1);
- backbrush.CreateSolidBrush(RGB(r,g,b));
- oldbrush=pDC->SelectObject(&backbrush);
- pDC->Rectangle(rect);
- pDC->SelectObject(oldbrush);
- }
- void CMergenewView::DrawWhiteBlocks(CDC * pDC,CPoint point)
- {
- CBrush backbrush,*oldbrush;
- CRect rect;
- rect.top=width*point.x;
- rect.bottom=width*(point.x+1);
- rect.left=width*point.y;
- rect.right=width*(point.y+1);
- backbrush.CreateSolidBrush(RGB(0,255,255));
- oldbrush=pDC->SelectObject(&backbrush);
- pDC->Rectangle(rect);
- pDC->SelectObject(oldbrush);
- }
- void CMergenewView::DrawWhiteBlocks(CDC * pDC,CRect& rect)
- {
- CBrush backbrush,*oldbrush;
- backbrush.CreateSolidBrush(RGB(0,255,255));
- oldbrush=pDC->SelectObject(&backbrush);
- pDC->Rectangle(rect);
- pDC->SelectObject(oldbrush);
- }
- void CMergenewView::DrawColorBlocks(CDC * pDC,CRect &rect)
- {
- CBrush brush,*oldbrush;
- brush.CreateSolidBrush(RGB(255,123,0));
- oldbrush=pDC->SelectObject(&brush);
- pDC->Rectangle(rect);
- pDC->SelectObject(oldbrush);
- }
- void CMergenewView::DrawColorBlocks(CDC * pDC,CRect &rect,int r,int g,int b)
- {
- CBrush brush,*oldbrush;
- brush.CreateSolidBrush(RGB(r,g,b));
- oldbrush=pDC->SelectObject(&brush);
- pDC->Rectangle(rect);
- pDC->SelectObject(oldbrush);
- }
- void CMergenewView::DrawMerge(CDC * pDC)
- {
- CBrush brush;
- CRect rect;
- int i=0,j=0;
- for(i=0;i<SIZEX_MAZE;i++)
- {
- rect.top=width*i;
- rect.bottom=width*(i+1);
- for(j=0;j<SIZEX_MAZE;j++)
- {
- rect.left=width*j;
- rect.right=width*(j+1);
- if(!maze[i][j])
- {
- DrawColorBlocks(pDC,rect);
- }
- else
- {
- DrawWhiteBlocks(pDC,rect);
- }
- }
- }
- }
- void CMergenewView::DrawResult(CDC * pDC)
- {
- count=SearchPath(_stack);
- CBrush brush;
- CRect rect;
- int i=0,j=0;
- for(i=0;i<=_stack.GetLength();i++)
- {
- rect.top=width*_stack.stack[i].row;
- rect.bottom=width*(_stack.stack[i].row+1);
- rect.left=width*_stack.stack[i].col;
- rect.right=width*(_stack.stack[i].col+1);
- DrawColorBlocks(pDC,rect,10,10,200);
- }
- }
- void CMergenewView::DrawDoubleResult(CDC * pDC)
- {
- _stack.StackEmpty();
- _stack2.StackEmpty();
- count=SearchPath(_stack,_stack2);
- CBrush brush;
- CRect rect;
- int i=0;
-
- for(i=0;i<=_stack.GetLength();i++)
- {
- rect.top=width*_stack.stack[i].row;
- rect.bottom=width*(_stack.stack[i].row+1);
- rect.left=width*_stack.stack[i].col;
- rect.right=width*(_stack.stack[i].col+1);
- DrawColorBlocks(pDC,rect,1,1,200);
- }
- for(i=0;i<=_stack2.GetLength();i++)
- {
- rect.top=width*_stack2.stack[i].row;
- rect.bottom=width*(_stack2.stack[i].row+1);
- rect.left=width*_stack2.stack[i].col;
- rect.right=width*(_stack2.stack[i].col+1);
- DrawColorBlocks(pDC,rect,255,0,255);
- }
- }
- void CMergenewView::OnNewgame()
- {
- // TODO: Add your command handler code here
- CClientDC dc(this);
- InitAll();
- DrawMerge(&dc);
- _stack.StackEmpty();
- showtag=true;
- turn=true;
- resulttag=false;
- doubleresult=false;
- }
- void CMergenewView::OnResult()
- {
- // TODO: Add your command handler code here
- CClientDC dc(this);
- CRect rect(550,30,600,80);
- char a[10];
- a[0]=NULL;
- if(turn)
- {
- DrawResult(&dc);
- sprintf(a,"%ld",count);
- dc.DrawText(a,strlen(a),&rect,DT_CENTER);
- resulttag=true;
- }
- }
- void CMergenewView::OnDoubleresult()
- {
- CClientDC dc(this);
- CRect rect(550,100,600,180);
- char a[10];
- a[0]=NULL;
- if(turn)
- {
- DrawDoubleResult(&dc);
- sprintf(a,"%ld",count);
- dc.DrawText(a,strlen(a),&rect,DT_CENTER);
- doubleresult=true;
- }
- }