mergenewView.cpp
上传用户:sycq158
上传日期:2008-10-22
资源大小:15361k
文件大小:9k
源码类别:

游戏

开发平台:

Visual C++

  1. // mergenewView.cpp : implementation of the CMergenewView class
  2. //
  3. #include "stdafx.h"
  4. #include "mergenew.h"
  5. #include "mergenewDoc.h"
  6. #include "mergenewView.h"
  7. #include "path.h"
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13. Stack _stack;
  14. Stack _stack2;
  15. /////////////////////////////////////////////////////////////////////////////
  16. // CMergenewView
  17. IMPLEMENT_DYNCREATE(CMergenewView, CView)
  18. BEGIN_MESSAGE_MAP(CMergenewView, CView)
  19. //{{AFX_MSG_MAP(CMergenewView)
  20. ON_WM_KEYDOWN()
  21. ON_COMMAND(ID_NEWGAME, OnNewgame)
  22. ON_COMMAND(ID_RESULT, OnResult)
  23. ON_COMMAND(ID_DOUBLERESULT, OnDoubleresult)
  24. //}}AFX_MSG_MAP
  25. // Standard printing commands
  26. ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
  27. ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
  28. ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
  29. END_MESSAGE_MAP()
  30. /////////////////////////////////////////////////////////////////////////////
  31. // CMergenewView construction/destruction
  32. CMergenewView::CMergenewView()
  33. {
  34. // TODO: add construction code here
  35. width=20;
  36. point.x=1;
  37. point.y=1;
  38. showtag=false;
  39. resulttag=false;
  40. turn=false;
  41. doubleresult=false;
  42. }
  43. CMergenewView::~CMergenewView()
  44. {
  45. }
  46. BOOL CMergenewView::PreCreateWindow(CREATESTRUCT& cs)
  47. {
  48. // TODO: Modify the Window class or styles here by modifying
  49. //  the CREATESTRUCT cs
  50. return CView::PreCreateWindow(cs);
  51. }
  52. /////////////////////////////////////////////////////////////////////////////
  53. // CMergenewView drawing
  54. void CMergenewView::OnDraw(CDC* pDC)
  55. {
  56. CMergenewDoc* pDoc = GetDocument();
  57. ASSERT_VALID(pDoc);
  58. if(showtag)
  59. {
  60. DrawMerge(pDC);
  61. }
  62. if(resulttag&&turn)
  63. {
  64. DrawMerge(pDC);
  65. DrawResult(pDC);
  66. }
  67. if(doubleresult)
  68. {
  69. DrawMerge(pDC);
  70. DrawDoubleResult(pDC);
  71. }
  72. // TODO: add draw code for native data here
  73. }
  74. /////////////////////////////////////////////////////////////////////////////
  75. // CMergenewView printing
  76. BOOL CMergenewView::OnPreparePrinting(CPrintInfo* pInfo)
  77. {
  78. // default preparation
  79. return DoPreparePrinting(pInfo);
  80. }
  81. void CMergenewView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  82. {
  83. // TODO: add extra initialization before printing
  84. }
  85. void CMergenewView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  86. {
  87. // TODO: add cleanup after printing
  88. }
  89. /////////////////////////////////////////////////////////////////////////////
  90. // CMergenewView diagnostics
  91. #ifdef _DEBUG
  92. void CMergenewView::AssertValid() const
  93. {
  94. CView::AssertValid();
  95. }
  96. void CMergenewView::Dump(CDumpContext& dc) const
  97. {
  98. CView::Dump(dc);
  99. }
  100. CMergenewDoc* CMergenewView::GetDocument() // non-debug version is inline
  101. {
  102. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMergenewDoc)));
  103. return (CMergenewDoc*)m_pDocument;
  104. }
  105. #endif //_DEBUG
  106. /////////////////////////////////////////////////////////////////////////////
  107. // CMergenewView message handlers
  108. void CMergenewView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
  109. {
  110. // TODO: Add your message handler code here and/or call default
  111. CClientDC dc(this);
  112. switch(nChar)
  113. {
  114. case VK_DOWN:
  115. {
  116. if(point.x+1<SIZEX_MAZE-1)
  117. if(!maze[point.x+1][point.y])
  118. {
  119. DrawColorBlocks(&dc,point);
  120. point.x++;
  121. DrawColorBlocks(&dc,point,125,125,125);
  122. }
  123. break;
  124. }
  125. case VK_LEFT:
  126. {
  127. if(point.y-1>0)
  128. if(!maze[point.x][point.y-1])
  129. {
  130. DrawColorBlocks(&dc,point);
  131. point.y--;
  132. DrawColorBlocks(&dc,point,125,125,125);
  133. }
  134. break;
  135. }
  136. case VK_RIGHT:
  137. {
  138. if(point.y+1<SIZEY_MAZE-1)
  139. if(!maze[point.x][point.y+1])
  140. {
  141. DrawColorBlocks(&dc,point);
  142. point.y++;
  143. DrawColorBlocks(&dc,point,125,125,125);
  144. }
  145. break;
  146. }
  147. case VK_UP:
  148. {
  149. if(point.x-1>0)
  150. if(!maze[point.x-1][point.y])
  151. {
  152. DrawColorBlocks(&dc,point);
  153. point.x--;
  154. DrawColorBlocks(&dc,point,125,125,125);
  155. }
  156. break;
  157. }
  158. //case VK_DOWNLEFT:
  159. case VK_DELETE:
  160. {
  161. if(point.x+1<SIZEX_MAZE-1&&point.y-1>0)
  162. if(!maze[point.x+1][point.y-1])
  163. {
  164. DrawColorBlocks(&dc,point);
  165. point.x++;
  166. point.y--;
  167. DrawColorBlocks(&dc,point,125,125,125);
  168. }
  169. break;
  170. }
  171. //case VK_DOWNRIGHT:
  172. case VK_END:
  173. {
  174. if(point.x+1<SIZEX_MAZE-1&&point.y+1<SIZEY_MAZE-1)
  175. if(!maze[point.x+1][point.y+1])
  176. {
  177. DrawColorBlocks(&dc,point);
  178. point.x++;
  179. point.y++;
  180. DrawColorBlocks(&dc,point,125,125,125);
  181. }
  182. break;
  183. }
  184. //case VK_UPLEFT:
  185. case VK_INSERT:
  186. {
  187. if(point.x-1>0&&point.y-1>0)
  188. if(!maze[point.x-1][point.y-1])
  189. {
  190. DrawColorBlocks(&dc,point);
  191. point.x--;
  192. point.y--;
  193. DrawColorBlocks(&dc,point,125,125,125);
  194. }
  195. break;
  196. }
  197. //case VK_UPRIGHT:
  198. case VK_HOME:
  199. {
  200. if(point.x-1>0&&point.y+1<SIZEY_MAZE-1)
  201. if(!maze[point.x-1][point.y+1])
  202. {
  203. DrawColorBlocks(&dc,point);
  204. point.x--;
  205. point.y++;
  206. DrawColorBlocks(&dc,point,125,125,125);
  207. }
  208. break;
  209. }
  210. default:
  211. break;
  212. }
  213. if(point.x==EXIT_ROW&&point.y==EXIT_COL)
  214. {
  215. showtag=false;
  216. resulttag=false;
  217. turn=false;
  218. point.x=1;
  219. point.y=1;
  220. MessageBox("you are wonderful");
  221. }
  222. CView::OnKeyDown(nChar, nRepCnt, nFlags);
  223. }
  224. void CMergenewView::DrawColorBlocks(CDC * pDC,CPoint point)
  225. {
  226. CBrush backbrush,*oldbrush;
  227. CRect rect;
  228. rect.top=width*point.x;
  229. rect.bottom=width*(point.x+1);
  230. rect.left=width*point.y;
  231. rect.right=width*(point.y+1);
  232. backbrush.CreateSolidBrush(RGB(255,133,0));
  233. oldbrush=pDC->SelectObject(&backbrush);
  234. pDC->Rectangle(rect);
  235. pDC->SelectObject(oldbrush);
  236. }
  237. void CMergenewView::DrawColorBlocks(CDC * pDC,CPoint point,int r,int g,int b)
  238. {
  239. CBrush backbrush,*oldbrush;
  240. CRect rect;
  241. rect.top=width*point.x;
  242. rect.bottom=width*(point.x+1);
  243. rect.left=width*point.y;
  244. rect.right=width*(point.y+1);
  245. backbrush.CreateSolidBrush(RGB(r,g,b));
  246. oldbrush=pDC->SelectObject(&backbrush);
  247. pDC->Rectangle(rect);
  248. pDC->SelectObject(oldbrush);
  249. }
  250. void CMergenewView::DrawWhiteBlocks(CDC * pDC,CPoint point)
  251. {
  252. CBrush backbrush,*oldbrush;
  253. CRect rect;
  254. rect.top=width*point.x;
  255. rect.bottom=width*(point.x+1);
  256. rect.left=width*point.y;
  257. rect.right=width*(point.y+1);
  258. backbrush.CreateSolidBrush(RGB(0,255,255));
  259. oldbrush=pDC->SelectObject(&backbrush);
  260. pDC->Rectangle(rect);
  261. pDC->SelectObject(oldbrush);
  262. }
  263. void CMergenewView::DrawWhiteBlocks(CDC * pDC,CRect& rect)
  264. {
  265. CBrush backbrush,*oldbrush;
  266. backbrush.CreateSolidBrush(RGB(0,255,255));
  267. oldbrush=pDC->SelectObject(&backbrush);
  268. pDC->Rectangle(rect);
  269. pDC->SelectObject(oldbrush);
  270. }
  271. void CMergenewView::DrawColorBlocks(CDC * pDC,CRect &rect)
  272. {
  273. CBrush brush,*oldbrush;
  274. brush.CreateSolidBrush(RGB(255,123,0));
  275. oldbrush=pDC->SelectObject(&brush);
  276. pDC->Rectangle(rect);
  277. pDC->SelectObject(oldbrush);
  278. }
  279. void CMergenewView::DrawColorBlocks(CDC * pDC,CRect &rect,int r,int g,int b)
  280. {
  281. CBrush brush,*oldbrush;
  282. brush.CreateSolidBrush(RGB(r,g,b));
  283. oldbrush=pDC->SelectObject(&brush);
  284. pDC->Rectangle(rect);
  285. pDC->SelectObject(oldbrush);
  286. }
  287. void CMergenewView::DrawMerge(CDC * pDC)
  288. {
  289. CBrush brush;
  290. CRect rect;
  291. int i=0,j=0;
  292. for(i=0;i<SIZEX_MAZE;i++)
  293. {
  294. rect.top=width*i;
  295. rect.bottom=width*(i+1);
  296. for(j=0;j<SIZEX_MAZE;j++)
  297. {
  298. rect.left=width*j;
  299. rect.right=width*(j+1);
  300. if(!maze[i][j])
  301. {
  302. DrawColorBlocks(pDC,rect);
  303. }
  304. else
  305. {
  306. DrawWhiteBlocks(pDC,rect);
  307. }
  308. }
  309. }
  310. }
  311. void CMergenewView::DrawResult(CDC * pDC)
  312. {
  313. count=SearchPath(_stack);
  314. CBrush brush;
  315. CRect rect;
  316. int i=0,j=0;
  317. for(i=0;i<=_stack.GetLength();i++)
  318. {
  319. rect.top=width*_stack.stack[i].row;
  320. rect.bottom=width*(_stack.stack[i].row+1);
  321. rect.left=width*_stack.stack[i].col;
  322. rect.right=width*(_stack.stack[i].col+1);
  323. DrawColorBlocks(pDC,rect,10,10,200);
  324. }
  325. }
  326. void CMergenewView::DrawDoubleResult(CDC * pDC)
  327. {
  328. _stack.StackEmpty();
  329. _stack2.StackEmpty();
  330. count=SearchPath(_stack,_stack2);
  331. CBrush brush;
  332. CRect rect;
  333. int i=0;
  334. for(i=0;i<=_stack.GetLength();i++)
  335. {
  336. rect.top=width*_stack.stack[i].row;
  337. rect.bottom=width*(_stack.stack[i].row+1);
  338. rect.left=width*_stack.stack[i].col;
  339. rect.right=width*(_stack.stack[i].col+1);
  340. DrawColorBlocks(pDC,rect,1,1,200);
  341. }
  342. for(i=0;i<=_stack2.GetLength();i++)
  343. {
  344. rect.top=width*_stack2.stack[i].row;
  345. rect.bottom=width*(_stack2.stack[i].row+1);
  346. rect.left=width*_stack2.stack[i].col;
  347. rect.right=width*(_stack2.stack[i].col+1);
  348. DrawColorBlocks(pDC,rect,255,0,255);
  349. }
  350. }
  351. void CMergenewView::OnNewgame() 
  352. {
  353. // TODO: Add your command handler code here
  354. CClientDC dc(this);
  355. InitAll();
  356. DrawMerge(&dc);
  357. _stack.StackEmpty();
  358. showtag=true;
  359. turn=true;
  360. resulttag=false;
  361. doubleresult=false;
  362. }
  363. void CMergenewView::OnResult() 
  364. {
  365. // TODO: Add your command handler code here
  366. CClientDC dc(this);
  367. CRect rect(550,30,600,80);
  368. char a[10];
  369. a[0]=NULL;
  370. if(turn)
  371. {
  372. DrawResult(&dc);
  373. sprintf(a,"%ld",count);
  374. dc.DrawText(a,strlen(a),&rect,DT_CENTER);
  375. resulttag=true;
  376. }
  377. }
  378. void CMergenewView::OnDoubleresult() 
  379. {
  380. CClientDC dc(this);
  381. CRect rect(550,100,600,180);
  382. char a[10];
  383. a[0]=NULL;
  384. if(turn)
  385. {
  386. DrawDoubleResult(&dc);
  387. sprintf(a,"%ld",count);
  388. dc.DrawText(a,strlen(a),&rect,DT_CENTER);
  389. doubleresult=true;
  390. }
  391. }