ChessDlg.cpp
上传用户:onward9999
上传日期:2022-06-27
资源大小:989k
文件大小:27k
源码类别:

其他游戏

开发平台:

Visual C++

  1. // ChessDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "Chess.h"
  5. #include "ChessDlg.h"
  6. #include "CoolButton.h"
  7. #include "BaseDef.h"
  8. #include "OptionDlg.h"
  9. #include "ThinkOptionDlg.h"
  10. #include "BaiDlg.h"
  11. #include "OpenDlg.h"
  12. #define CHESS_C_UNDO 0
  13. #define CHESS_C_REDO 1
  14. #define CHESS_C_HELP 2
  15. #define CHESS_C_CUT  3
  16. #ifdef _DEBUG
  17. #define new DEBUG_NEW
  18. #undef THIS_FILE
  19. static char THIS_FILE[] = __FILE__;
  20. #endif
  21. /////////////////////////////////////////////////////////////////////////////
  22. // CAboutDlg dialog used for App About
  23. class CAboutDlg : public CDialog
  24. {
  25. public:
  26. CAboutDlg();
  27. // Dialog Data
  28. //{{AFX_DATA(CAboutDlg)
  29. enum { IDD = IDD_ABOUTBOX };
  30. CCoolButton m_btWeb;
  31. CCoolButton m_btEmail;
  32. CCoolButton m_btOK;
  33. //}}AFX_DATA
  34. // ClassWizard generated virtual function overrides
  35. //{{AFX_VIRTUAL(CAboutDlg)
  36. protected:
  37. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  38. //}}AFX_VIRTUAL
  39. // Implementation
  40. protected:
  41. //{{AFX_MSG(CAboutDlg)
  42. afx_msg void OnButtonweb();
  43. afx_msg void OnButtonemail();
  44. //}}AFX_MSG
  45. DECLARE_MESSAGE_MAP()
  46. };
  47. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  48. {
  49. //{{AFX_DATA_INIT(CAboutDlg)
  50. //}}AFX_DATA_INIT
  51. }
  52. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  53. {
  54. CDialog::DoDataExchange(pDX);
  55. //{{AFX_DATA_MAP(CAboutDlg)
  56. DDX_Control(pDX, IDC_BUTTONWEB, m_btWeb);
  57. DDX_Control(pDX, IDC_BUTTONEMAIL, m_btEmail);
  58. DDX_Control(pDX, IDOK, m_btOK);
  59. //}}AFX_DATA_MAP
  60. }
  61. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  62. //{{AFX_MSG_MAP(CAboutDlg)
  63. ON_BN_CLICKED(IDC_BUTTONWEB, OnButtonweb)
  64. ON_BN_CLICKED(IDC_BUTTONEMAIL, OnButtonemail)
  65. //}}AFX_MSG_MAP
  66. END_MESSAGE_MAP()
  67. /////////////////////////////////////////////////////////////////////////////
  68. // CChessDlg dialog
  69. CChessDlg::CChessDlg(CWnd* pParent /*=NULL*/)
  70. : CDialog(CChessDlg::IDD, pParent)
  71. {
  72. //{{AFX_DATA_INIT(CChessDlg)
  73. //}}AFX_DATA_INIT
  74. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  75. CWinApp* pApp=AfxGetApp();
  76. m_hIcon = pApp->LoadIcon(IDR_MAINFRAME);
  77. m_hIconUndo = pApp->LoadIcon(IDI_UNDO);
  78. m_hIconRedo = pApp->LoadIcon(IDI_REDO);
  79. m_hIconHelp = pApp->LoadIcon(IDI_HELP);
  80. m_hIconCut = pApp->LoadIcon(IDI_CUT );
  81. //棋子图标
  82. m_hIconMan[RED_K]=pApp->LoadIcon(IDI_R_K);
  83. m_hIconMan[RED_S]=pApp->LoadIcon(IDI_R_S);
  84. m_hIconMan[RED_X]=pApp->LoadIcon(IDI_R_X);
  85. m_hIconMan[RED_M]=pApp->LoadIcon(IDI_R_M);
  86. m_hIconMan[RED_J]=pApp->LoadIcon(IDI_R_J);
  87. m_hIconMan[RED_P]=pApp->LoadIcon(IDI_R_P);
  88. m_hIconMan[RED_B]=pApp->LoadIcon(IDI_R_B);
  89. m_hIconMan[BLACK_K]=pApp->LoadIcon(IDI_B_K);
  90. m_hIconMan[BLACK_S]=pApp->LoadIcon(IDI_B_S);
  91. m_hIconMan[BLACK_X]=pApp->LoadIcon(IDI_B_X);
  92. m_hIconMan[BLACK_M]=pApp->LoadIcon(IDI_B_M);
  93. m_hIconMan[BLACK_J]=pApp->LoadIcon(IDI_B_J);
  94. m_hIconMan[BLACK_P]=pApp->LoadIcon(IDI_B_P);
  95. m_hIconMan[BLACK_B]=pApp->LoadIcon(IDI_B_B);
  96. m_pdcBack=new CDC;
  97. m_pdcBoard=new CDC;
  98. m_bThinking=FALSE;
  99. m_bCheck[0]=FALSE;
  100. m_bCheck[1]=FALSE;
  101. FixManMap();
  102. }
  103. void CChessDlg::DoDataExchange(CDataExchange* pDX)
  104. {
  105. CDialog::DoDataExchange(pDX);
  106. //{{AFX_DATA_MAP(CChessDlg)
  107. DDX_Control(pDX, IDC_CHESS_CUT, m_btCut);
  108. DDX_Control(pDX, IDC_PROGRESS1, m_progressStep);
  109. DDX_Control(pDX, IDC_STEPLIST, m_listStep);
  110. DDX_Control(pDX, IDC_CHESS_UNDO, m_btUndo);
  111. DDX_Control(pDX, IDC_CHESS_REDO, m_btRedo);
  112. DDX_Control(pDX, IDC_CHESS_HELP, m_btHelp);
  113. //}}AFX_DATA_MAP
  114. }
  115. BEGIN_MESSAGE_MAP(CChessDlg, CDialog)
  116. //{{AFX_MSG_MAP(CChessDlg)
  117. ON_COMMAND(ID_ABOUTBOX, OnAboutbox)
  118. ON_WM_PAINT()
  119. ON_WM_QUERYDRAGICON()
  120. ON_WM_MOUSEMOVE()
  121. ON_WM_LBUTTONDOWN()
  122. ON_WM_LBUTTONUP()
  123. ON_WM_DESTROY()
  124. ON_COMMAND(ID_FILE_BAI, OnFileBai)
  125. ON_COMMAND(ID_FILE_NEW, OnFileNew)
  126. ON_COMMAND(ID_FILE_SAVE, OnFileSave)
  127. ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
  128. ON_COMMAND(ID_FILE_OPTION, OnFileOption)
  129. ON_BN_CLICKED(IDC_CHESS_UNDO, OnEditUndo)
  130. ON_BN_CLICKED(IDC_CHESS_REDO, OnEditRedo)
  131. ON_BN_CLICKED(IDC_CHESS_HELP, OnEditHelp)
  132. ON_COMMAND(ID_CHESS_CUT, OnEditCut)
  133. ON_WM_TIMER()
  134. ON_LBN_DBLCLK(IDC_STEPLIST, OnDblclkSteplist)
  135. ON_COMMAND(ID_HELP_EMAIL, OnHelpEmail)
  136. ON_COMMAND(ID_HELP_WEB, OnHelpWeb)
  137. ON_COMMAND(ID_FILE_THINKOPTION, OnFileThinkOption)
  138. ON_COMMAND(ID_CHESS_HELP, OnEditHelp)
  139. ON_COMMAND(ID_CHESS_REDO, OnEditRedo)
  140. ON_COMMAND(ID_CHESS_UNDO, OnEditUndo)
  141. ON_BN_CLICKED(IDC_CHESS_CUT, OnEditCut)
  142. ON_COMMAND(ID_HELP_USE, OnHelpUse)
  143. //}}AFX_MSG_MAP
  144. END_MESSAGE_MAP()
  145. /////////////////////////////////////////////////////////////////////////////
  146. // CChessDlg message handlers
  147. BOOL CChessDlg::OnInitDialog()
  148. {
  149. CDialog::OnInitDialog();
  150. // Set the icon for this dialog.  The framework does this automatically
  151. //  when the application's main window is not a dialog
  152. SetIcon(m_hIcon, FALSE);
  153. SetIcon(m_hIcon, TRUE); // Set small icon
  154. m_btUndo.SetIcon(m_hIconUndo,32,32);
  155. m_btRedo.SetIcon(m_hIconRedo,32,32);
  156. m_btHelp.SetIcon(m_hIconHelp,32,32);
  157. m_btCut .SetIcon(m_hIconCut ,32,32);
  158. CClientDC dc(this);
  159. //Create Bitmap for DC
  160. m_Bitmap1.CreateCompatibleBitmap(&dc,XBW,YBW);
  161. m_Bitmap2.CreateCompatibleBitmap(&dc,XBW,YBW);
  162. m_pdcBack->CreateCompatibleDC(&dc);
  163. m_pdcBoard->CreateCompatibleDC(&dc);
  164. m_pdcBoard->SelectObject(&m_Bitmap1);
  165. m_pdcBack->SelectObject(&m_Bitmap2);
  166. MakeBoard(); //draw Chessboard in Board DC(3D)
  167. m_Setting.Load();
  168. m_Thinker.SetLevel(m_Setting.m_nLevel);
  169. m_Thinker.LoadThinkSetting();
  170. m_progressStep.CProgressCtrl::SetRange(0,100);
  171. m_nTimer=SetTimer(1,80,NULL);
  172. New();
  173. return TRUE;  // return TRUE  unless you set the focus to a control
  174. }
  175. // If you add a minimize button to your dialog, you will need the code below
  176. //  to draw the icon.  For MFC applications using the document/view model,
  177. //  this is automatically done for you by the framework.
  178. void CChessDlg::OnPaint() 
  179. {
  180. if (IsIconic())
  181. {
  182. CPaintDC dc(this); // device context for painting
  183. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  184. // Center icon in client rectangle
  185. int cxIcon = GetSystemMetrics(SM_CXICON);
  186. int cyIcon = GetSystemMetrics(SM_CYICON);
  187. CRect rect;
  188. GetClientRect(&rect);
  189. int x = (rect.Width() - cxIcon + 1) / 2;
  190. int y = (rect.Height() - cyIcon + 1) / 2;
  191. // Draw the icon
  192. dc.DrawIcon(x, y, m_hIcon);
  193. }
  194. else
  195. {
  196. CClientDC dc(this);
  197. CRect rc;
  198. GetUpdateRect(&rc,TRUE);
  199. rc.OffsetRect(-XOFFSET,-YOFFSET);
  200. UpdateFace(rc);
  201. CDialog::OnPaint();
  202. }
  203. }
  204. // The system calls this to obtain the cursor to display while the user drags
  205. //  the minimized window.
  206. HCURSOR CChessDlg::OnQueryDragIcon()
  207. {
  208. return (HCURSOR) m_hIcon;
  209. }
  210. void CChessDlg::OnMouseMove(UINT nFlags, CPoint point) 
  211. {
  212. if(m_nManPickUp!=32)
  213. {
  214. CPoint last=m_pointPickUp;
  215. point.Offset(-XOFFSET-MW/2,-YOFFSET-MW/2);
  216. if(point.x<SW)point.x=SW;
  217. if(point.x>BWA*8+SW)point.x=BWA*8+SW;
  218. if(point.y<SW)point.y=SW;
  219. if(point.y>BWA*9+SW)point.y=BWA*9+SW;
  220. m_pointPickUp=point;
  221. CRect rc=MakeUpdateRect(last,m_pointPickUp);
  222. UpdateFace(rc);
  223. }
  224. CDialog::OnMouseMove(nFlags, point);
  225. }
  226. void CChessDlg::OnAboutbox() 
  227. {
  228. CAboutDlg dlgAbout;
  229.   dlgAbout.DoModal();
  230. }
  231. CChessDlg::~CChessDlg()
  232. {
  233.   if(m_pdcBack)delete m_pdcBack;
  234.   if (m_pdcBoard)delete m_pdcBoard;
  235. }
  236. void CChessDlg::OnFileOption() 
  237. {
  238. COptionDlg optiondlg;
  239. optiondlg.m_CORM0=m_Setting.m_nCOrM[0];
  240. optiondlg.m_CORM1=m_Setting.m_nCOrM[1];
  241. optiondlg.m_nMode =m_Setting.m_nMode;
  242. optiondlg.m_nLevel=m_Setting.m_nLevel;
  243. m_bThinking=FALSE;
  244. EnableCommand(CHESS_C_CUT,FALSE);
  245. int respond=optiondlg.DoModal();
  246. if(respond==IDOK)
  247. {
  248. m_Setting.m_nCOrM[0]=optiondlg.m_CORM0;
  249. m_Setting.m_nCOrM[1]=optiondlg.m_CORM1;
  250. m_Setting.m_nMode=optiondlg.m_nMode;
  251. m_Setting.m_nLevel=optiondlg.m_nLevel;
  252. m_Thinker.SetLevel(m_Setting.m_nLevel);
  253. m_Setting.Save();
  254. ShowStatus();
  255. if(m_Setting.m_nCOrM[m_Setting.m_nPlayer[m_Face.side]]==COM)
  256. Think();
  257. }
  258. }
  259. void CChessDlg::MakeBoard()
  260. {
  261. //填充
  262. CBrush br(GetSysColor(COLOR_3DFACE));
  263. CRect rect(0,0,XBW,YBW);
  264. m_pdcBoard->FillRect(rect,&br);
  265. //画棋盘
  266. CPen pen[2];
  267. pen[0].CreatePen(PS_SOLID,0,GetSysColor(COLOR_3DHILIGHT));
  268. pen[1].CreatePen(PS_SOLID,0,GetSysColor(COLOR_3DSHADOW));
  269. for(int i=0;i<=1;i++)
  270. {
  271. m_pdcBoard->SelectObject(pen[i]);
  272. //画边框(双线)
  273. m_pdcBoard->MoveTo(XC[i]-3,YC[i]-3);
  274. m_pdcBoard->LineTo(XC[i]-3,YC[i]+3+BWA*9);
  275. m_pdcBoard->LineTo(XC[i]+3+BWA*8,YC[i]+3+BWA*9);
  276. m_pdcBoard->LineTo(XC[i]+3+BWA*8,YC[i]-3);
  277. m_pdcBoard->LineTo(XC[i]-3,YC[i]-3);
  278. m_pdcBoard->MoveTo(XC[i],YC[i]);
  279. m_pdcBoard->LineTo(XC[i],YC[i]+BWA*9);
  280. m_pdcBoard->LineTo(XC[i]+BWA*8,YC[i]+BWA*9);
  281. m_pdcBoard->LineTo(XC[i]+BWA*8,YC[i]);
  282. m_pdcBoard->LineTo(XC[i],YC[i]);
  283. //画竖线
  284. int n;
  285. for( n=1;n<=8;n++)
  286. {
  287. m_pdcBoard->MoveTo(XC[i],YC[i]+BWA*n);
  288. m_pdcBoard->LineTo(XC[i]+BWA*8,YC[i]+BWA*n);
  289. }
  290. for( n=1;n<=7;n++)
  291. {
  292. m_pdcBoard->MoveTo(XC[i]+BWA*n,YC[i]);
  293. m_pdcBoard->LineTo(XC[i]+BWA*n,YC[i]+BWA*4);
  294. m_pdcBoard->MoveTo(XC[i]+BWA*n,YC[i]+BWA*5);
  295. m_pdcBoard->LineTo(XC[i]+BWA*n,YC[i]+BWA*9);
  296. }
  297. //画宫
  298. m_pdcBoard->MoveTo(XC[i]+BWA*3,YC[i]+BWA*0);
  299. m_pdcBoard->LineTo(XC[i]+BWA*5,YC[i]+BWA*2); //上捺
  300. m_pdcBoard->MoveTo(XC[i]+BWA*5,YC[i]+BWA*0);
  301. m_pdcBoard->LineTo(XC[i]+BWA*3,YC[i]+BWA*2); //上撇
  302. m_pdcBoard->MoveTo(XC[i]+BWA*3,YC[i]+BWA*7);
  303. m_pdcBoard->LineTo(XC[i]+BWA*5,YC[i]+BWA*9); //下捺
  304. m_pdcBoard->MoveTo(XC[i]+BWA*5,YC[i]+BWA*7);
  305. m_pdcBoard->LineTo(XC[i]+BWA*3,YC[i]+BWA*9); //下撇
  306. //画星
  307. MB_DrawStar(7,2,i);
  308. MB_DrawStar(1,2,i);
  309. MB_DrawStar(0,3,i);
  310. MB_DrawStar(2,3,i);
  311. MB_DrawStar(4,3,i);
  312. MB_DrawStar(6,3,i);
  313. MB_DrawStar(8,3,i);
  314. MB_DrawStar(7,7,i);
  315. MB_DrawStar(1,7,i);
  316. MB_DrawStar(0,6,i);
  317. MB_DrawStar(2,6,i);
  318. MB_DrawStar(4,6,i);
  319. MB_DrawStar(6,6,i);
  320. MB_DrawStar(8,6,i);
  321. }
  322. }
  323. void CChessDlg::MB_DrawStar(int x,int y,int i)
  324. {
  325. if(x!=0)
  326. {
  327. m_pdcBoard->MoveTo(XC[i]+BWA*x-3,YC[i]+BWA*y-3);
  328. m_pdcBoard->LineTo(XC[i]+BWA*x-6,YC[i]+BWA*y-3);
  329. m_pdcBoard->MoveTo(XC[i]+BWA*x-3,YC[i]+BWA*y-3);
  330. m_pdcBoard->LineTo(XC[i]+BWA*x-3,YC[i]+BWA*y-6);
  331. m_pdcBoard->MoveTo(XC[i]+BWA*x-3,YC[i]+BWA*y+3);
  332. m_pdcBoard->LineTo(XC[i]+BWA*x-6,YC[i]+BWA*y+3);
  333. m_pdcBoard->MoveTo(XC[i]+BWA*x-3,YC[i]+BWA*y+3);
  334. m_pdcBoard->LineTo(XC[i]+BWA*x-3,YC[i]+BWA*y+6);
  335. }
  336. if(x!=8)
  337. {
  338. m_pdcBoard->MoveTo(XC[i]+BWA*x+3,YC[i]+BWA*y-3);
  339. m_pdcBoard->LineTo(XC[i]+BWA*x+6,YC[i]+BWA*y-3);
  340. m_pdcBoard->MoveTo(XC[i]+BWA*x+3,YC[i]+BWA*y-3);
  341. m_pdcBoard->LineTo(XC[i]+BWA*x+3,YC[i]+BWA*y-6);
  342. m_pdcBoard->MoveTo(XC[i]+BWA*x+3,YC[i]+BWA*y+3);
  343. m_pdcBoard->LineTo(XC[i]+BWA*x+6,YC[i]+BWA*y+3);
  344. m_pdcBoard->MoveTo(XC[i]+BWA*x+3,YC[i]+BWA*y+3);
  345. m_pdcBoard->LineTo(XC[i]+BWA*x+3,YC[i]+BWA*y+6);
  346. }
  347. }
  348. BOOL CChessDlg::CanGo()
  349. {
  350. for(int i=0;i<2;i++)
  351. if (m_Face.man[i*16].x==0 && m_nManPickUp!=i*16)
  352. {
  353. MessageBox( "已经结束.","提醒",MB_OK|MB_ICONINFORMATION);
  354. return FALSE;
  355. }
  356. return TRUE;
  357. }
  358. BOOL CChessDlg::CanGo(int man, int x, int y)
  359. {
  360. int xfrom,yfrom;
  361. if(!CanGo())return FALSE;
  362. if(man==32)
  363. {
  364. if(m_pointBeforePickUp.x==x&&m_pointBeforePickUp.y==y)//是原来的点
  365. return FALSE;
  366. man=m_nManPickUp;
  367. xfrom=m_pointBeforePickUp.x;
  368. yfrom=m_pointBeforePickUp.y;
  369. }
  370. else
  371. {
  372. if(m_Face.man[man].x==0)return FALSE;
  373. if(m_Face.man[man].x==x&&m_Face.man[man].y==y)return FALSE;
  374. xfrom=m_Face.man[man].x;
  375. yfrom=m_Face.man[man].y;
  376. }
  377. if(!::CanGo(m_nManMap,man,xfrom,yfrom,x,y))return FALSE;
  378. return TRUE;
  379. }
  380. BOOL CChessDlg::Go(int man, int x, int y)
  381. {
  382. if(!CanGo( man,x, y))return FALSE;
  383. m_listStep.AddString(GetStepName(man,x,y));
  384. m_listStep.SetCurSel(m_listStep.GetCount()-1);
  385. CStep* pStep=new CStep;
  386. if(man==32)
  387. {
  388. pStep->man=m_nManPickUp;
  389. pStep->from.x=int(m_pointBeforePickUp.x);
  390. pStep->from.y=int(m_pointBeforePickUp.y);
  391. }
  392. else 
  393. {
  394. pStep->man=man;
  395. pStep->from.x=int(m_Face.man[man].x);
  396. pStep->from.y=int(m_Face.man[man].y);
  397. }
  398. pStep->to.x=x;
  399. pStep->to.y=y;
  400. if(m_nManMap[x][y]!=32)
  401. {
  402. pStep->eaten=m_nManMap[x][y];//吃掉的子
  403. m_Face.man[m_nManMap[x][y]].x=0;
  404. }
  405. m_StepList.Go(pStep);
  406. Move(man,x,y);
  407. m_Face.side=!m_Face.side;
  408. EnableCommand(CHESS_C_UNDO,TRUE);
  409. EnableCommand(CHESS_C_REDO,FALSE);
  410. EnableCommand(CHESS_C_CUT,FALSE);
  411. m_nStepCount++;
  412. m_bThinking=FALSE;
  413. ShowStatus();
  414. if(m_Setting.m_nCOrM[m_Setting.m_nPlayer[m_Face.side]]==COM) //轮到电脑
  415. Think();
  416. return TRUE;
  417. }
  418. void CChessDlg::Move(int man, int x, int y)
  419. {
  420. if(man!=32)
  421. {
  422. PickUp(man);
  423. }
  424. CPoint base(m_pointPickUp);
  425. int count=max(abs((x-1)*BWA+SW-m_pointPickUp.x),abs((y-1)*BWA+SW-m_pointPickUp.y));
  426. float xstep=float((x-1)*BWA+SW-m_pointPickUp.x)/count;
  427. float ystep=float((y-1)*BWA+SW-m_pointPickUp.y)/count;
  428. int index=ManToIcon[m_nManPickUp];
  429. UINT last=GetTickCount();
  430. count/=3;
  431. for(int i=0;i<count;i++)
  432. {
  433. while(GetTickCount()>last && GetTickCount()-last<25);//改变这个数来改变移动速度
  434. last=GetTickCount();
  435. CPoint point=m_pointPickUp;
  436. m_pointPickUp.x=long(base.x+xstep*i*3);
  437. m_pointPickUp.y=long(base.y+ystep*i*3);
  438. CRect rc;
  439. rc=MakeUpdateRect(m_pointPickUp,point);
  440. UpdateFace(rc);
  441. }
  442. PutDown(x,y);
  443. }
  444. void CChessDlg::UpdateFace(CRect &rc)
  445. {
  446. CRect rc1(0,0,BWA*9,BWA*10);
  447. //取得真正要更新的区域:
  448. rc1.IntersectRect(&rc1,&rc);
  449. m_pdcBack->BitBlt(rc.left,rc.top,rc.Width(),rc.Height(),m_pdcBoard,rc.left,rc.top,SRCCOPY);
  450. int left=(rc1.left)/BWA,
  451. top=(rc1.top)/BWA,
  452. right=(rc1.right)/BWA,
  453. bottom=(rc1.bottom)/BWA;
  454. for(int i=left;i<=right;i++)
  455. for(int j=top;j<=bottom;j++)
  456. if(m_nManMap[i+1][j+1]!=32)
  457. m_pdcBack->DrawIcon(i*BWA+SW,j*BWA+SW,m_hIconMan[ManToIcon[m_nManMap[i+1][j+1]]]);
  458. if(m_nManPickUp!=32) m_pdcBack->DrawIcon(m_pointPickUp,m_hIconMan[ManToIcon[m_nManPickUp]]);
  459. CClientDC dc(this);
  460. dc.BitBlt(rc.left+XOFFSET,rc.top+YOFFSET,rc.Width(),rc.Height(),m_pdcBack,rc.left,rc.top,SRCCOPY);
  461. }
  462. void CChessDlg::FixManMap()
  463. {
  464. for(int i=0;i<11;i++)
  465. for(int j=0;j<12;j++)m_nManMap[i][j]=32;
  466. for(i=0;i<32;i++)
  467. {
  468. if(m_Face.man[i].x)
  469. m_nManMap[m_Face.man[i].x][m_Face.man[i].y]=i;
  470. }
  471. }
  472. void CChessDlg::OnLButtonDown(UINT nFlags, CPoint point) 
  473. {
  474. CPoint p=point;
  475. if(m_Setting.m_nCOrM[m_Setting.m_nPlayer[m_Face.side]]==MAN&&!m_bThinking) //是人走
  476. if(FaceToPoint(p)) //点的是棋位
  477. if(m_nManMap[p.x][p.y]!=32 //有棋子
  478. && m_nManMap[p.x][p.y]/16==m_Face.side) //是现在走的一方
  479. {
  480. SetCapture();
  481. m_pointBeforePickUp=p;
  482. PickUp(m_nManMap[p.x][p.y]);
  483. }
  484. CDialog::OnLButtonDown(nFlags, point);
  485. }
  486. void CChessDlg::OnLButtonUp(UINT nFlags, CPoint point) 
  487. {
  488. if(m_nManPickUp!=32) //移动中
  489. {
  490. CPoint p=point;
  491. if(FaceToPoint(p)) //是棋位
  492. {
  493. if(!Go(32,p.x,p.y)) //不能走
  494. Move(32,m_pointBeforePickUp.x,m_pointBeforePickUp.y);
  495. }
  496. else //不是棋位
  497. {
  498. Move(32,m_pointBeforePickUp.x,m_pointBeforePickUp.y);
  499. }
  500. ReleaseCapture();
  501. }
  502. CDialog::OnLButtonUp(nFlags, point);
  503. }
  504. void CChessDlg::PickUp(int man)
  505. {
  506. m_pointPickUp.x=(m_Face.man[man].x-1)*BWA+SW;
  507. m_pointPickUp.y=(m_Face.man[man].y-1)*BWA+SW;
  508. m_nManPickUp=man;
  509. m_Face.man[man].x=0;
  510. FixManMap();
  511. }
  512. void CChessDlg::PutDown(int x,int y)
  513. {
  514. m_Face.man[m_nManPickUp].x=x;
  515. m_Face.man[m_nManPickUp].y=y;
  516. m_nManPickUp=32;
  517. FixManMap();
  518. CRect rc;
  519. CPoint point((x-1)*BWA,(y-1)*BWA);
  520. rc=MakeUpdateRect(m_pointPickUp,point);
  521. UpdateFace(rc);
  522. }
  523. CRect CChessDlg::MakeUpdateRect(CPoint p1, CPoint p2)
  524. {
  525. CRect rc;
  526. rc.SetRect(p1,p2);
  527. rc.NormalizeRect();
  528. rc.bottom+=BWA;
  529. rc.right+=BWA;
  530. return rc;
  531. }
  532. BOOL CChessDlg::FaceToPoint(CPoint &point)
  533. {
  534. if((point.x-XOFFSET)%BWA<SW||(point.x-XOFFSET)%BWA>BWA-SW)return FALSE;
  535. if((point.y-YOFFSET)%BWA<SW||(point.y-YOFFSET)%BWA>BWA-SW)return FALSE;
  536. CPoint p;
  537. p.x=(point.x-XOFFSET)/BWA+1;
  538. p.y=(point.y-YOFFSET)/BWA+1;
  539. if(p.x<1||p.x>9||p.y<1||p.y>10)return FALSE;
  540. point=p;
  541. return TRUE;
  542. }
  543. void CChessDlg::OnDestroy() 
  544. {
  545. CDialog::OnDestroy();
  546. m_Thinker.Exit();
  547. }
  548. void CChessDlg::OnFileBai() 
  549. {
  550. CBaiDlg baidlg(this);
  551. baidlg.DoModal();
  552. }
  553. void CChessDlg::New(CFace face)
  554. {
  555. m_Thinker.Cut();
  556. m_StepList.RemoveAll();
  557. while(m_listStep.GetCount()!=0)m_listStep.DeleteString(0);
  558. m_nStepCount=1;
  559. switch(m_Setting.m_nMode)
  560. {
  561. case 0:
  562. m_Setting.m_nPlayer[0]=!m_Setting.m_nPlayer[0];
  563. m_Setting.m_nPlayer[1]=!m_Setting.m_nPlayer[1];
  564. break;
  565. case 1:
  566. m_Setting.m_nPlayer[0]=0;
  567. m_Setting.m_nPlayer[1]=1;
  568. break;
  569. case 2:
  570. m_Setting.m_nPlayer[0]=1;
  571. m_Setting.m_nPlayer[1]=0;
  572. break;
  573. }
  574. EnableCommand(CHESS_C_UNDO,FALSE);
  575. EnableCommand(CHESS_C_REDO,FALSE);
  576. EnableCommand(CHESS_C_CUT ,FALSE);
  577. m_bThinking=FALSE;
  578. m_Face=face;
  579. FixManMap();
  580. m_nManPickUp=32;
  581. CRect rc(0,0,BWA*9,BWA*10);
  582. UpdateFace(rc);
  583. ShowStatus();
  584. if(m_Setting.m_nCOrM[m_Setting.m_nPlayer[m_Face.side]]==COM)
  585. Think();
  586. }
  587. void CChessDlg::New()
  588. {
  589. CFace face;
  590. New(face);
  591. }
  592. void CChessDlg::OnFileNew() 
  593. {
  594. New();
  595. }
  596. void CChessDlg::OnFileSave() 
  597. {
  598. CFileDialog filedlg(FALSE,"ccr","未命名",OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"棋谱|*.ccr||",this);
  599. int respond=filedlg.DoModal();
  600. if(respond==IDOK)m_Face.Save(filedlg.GetFileName());
  601. }
  602. void CChessDlg::OnFileOpen() 
  603. {
  604. CFileDialog filedlg(TRUE,"ccr","",NULL,"棋谱|*.ccr||",this);
  605. int respond=filedlg.DoModal();
  606. if(respond==IDOK)
  607. {
  608. CFace face;
  609. if(face.Open(filedlg.GetFileName()))
  610. {
  611. if(face.IsNormal())New(face);
  612. else 
  613. {
  614. CString str;
  615. str.Format(""%s"n不是正确的棋谱",filedlg.GetFileName());
  616. MessageBox(str,"错误-象棋",MB_OK|MB_ICONERROR);
  617. }
  618. }
  619. else 
  620. {
  621. CString str;
  622. str.Format("不能打开"%s"",filedlg.GetFileName());
  623. MessageBox(str,"错误-象棋",MB_OK|MB_ICONERROR);
  624. }
  625. }
  626. }
  627. void CChessDlg::OnEditUndo() 
  628. {
  629. m_nStepCount--;
  630. CStep* pStep=m_StepList.Undo();
  631. if(pStep==NULL)
  632. {
  633. return;
  634. }
  635. m_progressStep.SetPos(0);
  636. PickUp(pStep->man);
  637. if(pStep->eaten!=32)
  638. {
  639. m_Face.man[pStep->eaten]=pStep->to;
  640. FixManMap();
  641. }
  642. EnableCommand(CHESS_C_REDO,TRUE);
  643. if(m_StepList.IsHead()) EnableCommand(CHESS_C_UNDO,FALSE);
  644. Move(32,pStep->from.x,pStep->from.y);
  645. m_Face.side=!m_Face.side;
  646. m_listStep.DeleteString(m_listStep.GetCount()-1);
  647. m_listStep.SetCurSel(m_listStep.GetCount()-1);
  648. m_bThinking=FALSE;
  649. ShowStatus();
  650. EnableCommand(CHESS_C_CUT,FALSE);
  651. }
  652. void CChessDlg::EnableCommand(int n, BOOL benable)
  653. {
  654. CMenu* menu=GetMenu();
  655. switch(n)
  656. {
  657. case CHESS_C_UNDO:
  658. m_btUndo.EnableWindow(benable);
  659. menu->EnableMenuItem(ID_CHESS_UNDO,!benable);
  660. break;
  661. case CHESS_C_REDO:
  662. m_btRedo.EnableWindow(benable);
  663. menu->EnableMenuItem(ID_CHESS_REDO,!benable);
  664. break;
  665. case CHESS_C_HELP:
  666. m_btHelp.EnableWindow(benable);
  667. menu->EnableMenuItem(ID_CHESS_HELP,!benable);
  668. break;
  669. case CHESS_C_CUT:
  670. m_btCut.EnableWindow(benable);
  671. menu->EnableMenuItem(ID_CHESS_CUT,!benable);
  672. break;
  673. }
  674. }
  675. void CChessDlg::OnEditRedo() 
  676. {
  677. CStep* pStep=m_StepList.Redo();
  678. if(pStep==NULL)
  679. {
  680. return;
  681. }
  682. EnableCommand(CHESS_C_UNDO,TRUE);
  683. if(m_StepList.IsEnd()) EnableCommand(CHESS_C_REDO,FALSE);
  684. m_listStep.AddString(GetStepName(pStep->man,pStep->to.x,pStep->to.y));
  685. m_listStep.SetCurSel(m_listStep.GetCount()-1);
  686. if(pStep->eaten!=32)m_Face.man[pStep->eaten].x=0;
  687. m_Face.side=!m_Face.side;
  688. Move(pStep->man,pStep->to.x,pStep->to.y);
  689. m_nStepCount++;
  690. ShowStatus();
  691. }
  692. void CChessDlg::OnTimer(UINT nIDEvent) 
  693. {
  694. if(m_bThinking)
  695. {
  696. m_progressStep.SetPos(m_Thinker.GetPercent());
  697. if(m_Thinker.IsThinkOver())
  698. {
  699. CMove move;
  700. if(m_bThinking&&m_Thinker.GetMove(move,m_Face))
  701. {
  702. m_progressStep.SetPos(0);
  703. if(!Go(move))
  704. {
  705. TRACE("Result is error!n");
  706. Think();
  707. }
  708. else TRACE("Result is OK!n");
  709. }
  710. else TRACE("Can't get Move!n");
  711. }
  712. }
  713. CDialog::OnTimer(nIDEvent);
  714. }
  715. CString CChessDlg::GetStepName(int man, int x, int y)
  716. {
  717. int xfrom,yfrom;
  718. if (man==32)
  719. {
  720.  man=m_nManPickUp;
  721.  xfrom=m_pointBeforePickUp.x;
  722.  yfrom=m_pointBeforePickUp.y;
  723. }
  724. else
  725. {
  726. xfrom=m_Face.man[man].x;
  727. yfrom=m_Face.man[man].y;
  728. }
  729. const char strman[14][3]=
  730. {"帅","仕","相","马","车","炮","兵","将","士","象","马","车","炮","卒"};
  731. const char strnumc[10][3]=
  732. {"零","一","二","三","四","五","六","七","八","九"};
  733. const char strnuma[10][3]=
  734. {"0","1","2","3","4","5","6","7","8","9"};
  735. const char strjpt[3][3]={"进","退","平"};
  736. const char strqh[2][3]={"前","后"};
  737. int j,nformat,nresult,njpt,ndis,nA=0,nB=0;
  738. CString strresult;
  739. if(SideOfMan[man]==RED)
  740. {
  741. for(j=yfrom-1 ;j>0;j--)
  742. if(ManToType[man]==ManToType[m_nManMap[xfrom][j]] )nA++;
  743. for(j=yfrom+1 ;j<11;j++)
  744. if(ManToType[man]==ManToType[m_nManMap[xfrom][j]] )nB++;
  745. if (nA+nB> 1) {nformat=2;nresult=nA+1;}
  746. else if (nA+nB==1) {nformat=1;nresult=nA==0?0:1;}
  747. else {nformat=0;nresult=10-xfrom;}
  748. if (y> yfrom) njpt=1;
  749. else if (y==yfrom) njpt=2;
  750. else njpt=0;
  751. if( ManToType[man]==RED_M||
  752. ManToType[man]==RED_S||
  753. ManToType[man]==RED_X)ndis=10-x;
  754. else
  755. {
  756. switch(njpt)
  757. {
  758. case 0: ndis = yfrom-y; break;
  759. case 1: ndis = y-yfrom; break;
  760. case 2: ndis =10-x; break;
  761. default: break;
  762. }
  763. }
  764. switch(nformat)
  765. {
  766. case 0: strresult.Format("%s%s%s%s",
  767. strman[ManToType[man]],
  768. strnumc[nresult],
  769. strjpt[njpt],
  770. strnumc[ndis]);
  771. break;
  772. case 2: strresult.Format("%s%s%s%s",
  773. strnuma[nresult],
  774. strman[ManToType[man]],
  775. strjpt[njpt],
  776. strnumc[ndis]);
  777. break;
  778. case 1: strresult.Format("%s%s%s%s",
  779. strqh[nresult],
  780. strman[ManToType[man]],
  781. strjpt[njpt],
  782. strnumc[ndis]);
  783. break;
  784. default:break;
  785. }
  786. }
  787. else //Black side
  788. {
  789. for(j=yfrom+1 ;j<11;j++)
  790. if(ManToType[man]==ManToType[m_nManMap[xfrom][j]] )nA++;
  791. for(j=yfrom-1 ;j>0 ;j--)
  792. if(ManToType[man]==ManToType[m_nManMap[xfrom][j]] )nB++;
  793. if (nA+nB> 1) {nformat=2;nresult=nA+1;}
  794. else if (nA+nB==1) {nformat=1;nresult=nA==0?0:1;}
  795. else {nformat=0;nresult=xfrom;}
  796. if (y< yfrom) njpt=1;
  797. else if (y==yfrom) njpt=2;
  798. else njpt=0;
  799. if( ManToType[man]==BLACK_M||
  800. ManToType[man]==BLACK_S||
  801. ManToType[man]==BLACK_X)ndis=x;
  802. else
  803. {
  804. switch(njpt)
  805. {
  806. case 0: ndis = y-yfrom; break;
  807. case 1: ndis = yfrom-y; break;
  808. case 2: ndis = x; break;
  809. default: break;
  810. }
  811. }
  812. switch(nformat)
  813. {
  814. case 0: strresult.Format("%s%s%s%s",
  815. strman[ManToType[man]],
  816. strnuma[nresult],
  817. strjpt[njpt],
  818. strnuma[ndis]);
  819. break;
  820. case 2: strresult.Format("%s%s%s%s",
  821. strnuma[nresult],
  822. strman[ManToType[man]],
  823. strjpt[njpt],
  824. strnuma[ndis]);
  825. break;
  826. case 1: strresult.Format("%s%s%s%s",
  827. strqh[nresult],
  828. strman[ManToType[man]],
  829. strjpt[njpt],
  830. strnuma[ndis]);
  831. break;
  832. default:break;
  833. }
  834. }
  835. CString str1;
  836. str1.Format("%2d ",m_nStepCount);
  837.   return str1+strresult;
  838. }
  839. void CChessDlg::ShowStatus()
  840. {
  841. CString str,str1,str2;
  842. str1=m_Face.side==BLACK?"黑":"红";
  843. TestCheck();
  844. if(m_Setting.m_nCOrM[m_Setting.m_nPlayer[m_Face.side]]==COM)
  845. str2.Format("电脑%d级",m_Setting.m_nLevel);
  846. else
  847. str2="人";
  848. str.Format("第%d步-%s方-棋手%d(%s)",
  849. m_nStepCount,str1,m_Setting.m_nPlayer[m_Face.side]+1,str2);
  850. if(m_bThinking)str+="  [Thinking ...]";
  851. else
  852. {
  853. if (m_bCheck[0]||m_bCheck[1])
  854. {
  855. str+="  将军!";
  856. }
  857. if (m_bCheck[0])str+=" 红方危险!";
  858. if (m_bCheck[1])str+=" 黑方危险!";
  859. }
  860. SetWindowText(str);
  861. }
  862. void CChessDlg::Think()
  863. {
  864. if(CanGo())
  865. {
  866. m_Thinker.Think(m_Face);
  867. m_bThinking=TRUE;
  868. EnableCommand(CHESS_C_CUT);
  869. ShowStatus();
  870. }
  871. }
  872. void CChessDlg::OnEditHelp() 
  873. {
  874. if(!m_bThinking)Think();
  875. }
  876. BOOL CChessDlg::Go(CMove move)
  877. {
  878. return Go(move.man,move.x,move.y);
  879. }
  880. BOOL CChessDlg::CanGo( const CMove &move)
  881. {
  882. return CanGo(move.man,move.x,move.y);
  883. }
  884. void CChessDlg::OnDblclkSteplist() 
  885. {
  886. int count=m_listStep.GetCurSel();
  887. count++;
  888. while(m_nStepCount!=count)OnEditUndo();
  889. }
  890. BOOL CChessDlg::OpenURL(const char *LinkName)
  891. {
  892. HINSTANCE hRun = ShellExecute(GetParent()->GetSafeHwnd(), _T("open"), LinkName, NULL, NULL, SW_SHOW);
  893. if ((int) hRun <= 32)
  894. {
  895. TRACE("Failed to invoke URL using ShellExecuten");
  896. return FALSE;
  897. }
  898. return TRUE;
  899. }
  900. void CChessDlg::OnHelpEmail() 
  901. {
  902. OpenEmail();
  903. }
  904. void CChessDlg::OnHelpWeb() 
  905. {
  906. // TODO: Add your command handler code here
  907. OpenWeb();
  908. }
  909. BOOL CChessDlg::OpenEmail()
  910. {
  911. return OpenURL("mailto:thecct@netease.com");
  912. }
  913. BOOL CChessDlg::OpenWeb()
  914. {
  915. return OpenURL("http://thecct.go.163.com");
  916. }
  917. void CAboutDlg::OnButtonweb() 
  918. {
  919. ((CChessDlg*)GetParent())->OpenWeb();
  920. }
  921. void CAboutDlg::OnButtonemail() 
  922. {
  923. ((CChessDlg*)GetParent())->OpenEmail();
  924. }
  925. void CChessDlg::OnEditCut() 
  926. {
  927. if(m_bThinking)
  928. {
  929. m_Thinker.Cut();
  930. m_bThinking=FALSE;
  931. EnableCommand(CHESS_C_CUT,FALSE);
  932. ShowStatus();
  933. m_progressStep.SetPos(0);
  934. }
  935. }
  936. void CChessDlg::OnFileThinkOption() 
  937. {
  938. CThinkOptionDlg dlg;
  939. m_Thinker.Cut();
  940. m_bThinking=FALSE;
  941. ShowStatus();
  942. dlg.m_edits1=m_Thinker.BV1[1];
  943. dlg.m_editx1=m_Thinker.BV1[2];
  944. dlg.m_editm1=m_Thinker.BV1[3];
  945. dlg.m_editj1=m_Thinker.BV1[4];
  946. dlg.m_editp1=m_Thinker.BV1[5];
  947. dlg.m_editb1=m_Thinker.BV1[6];
  948. dlg.m_edits2=m_Thinker.BV2[1];
  949. dlg.m_editx2=m_Thinker.BV2[2];
  950. dlg.m_editm2=m_Thinker.BV2[3];
  951. dlg.m_editj2=m_Thinker.BV2[4];
  952. dlg.m_editp2=m_Thinker.BV2[5];
  953. dlg.m_editb2=m_Thinker.BV2[6];
  954. dlg.m_edit30=m_Thinker.BV3[0];
  955. dlg.m_edit31=m_Thinker.BV3[1];
  956. dlg.m_edit32=m_Thinker.BV3[2];
  957. dlg.m_edit33=m_Thinker.BV3[3];
  958. dlg.m_edit34=m_Thinker.BV3[4];
  959. int respond=dlg.DoModal();
  960. if(respond==IDOK)
  961. {
  962. m_Thinker.BV1[1]=dlg.m_edits1;
  963. m_Thinker.BV1[2]=dlg.m_editx1;
  964. m_Thinker.BV1[3]=dlg.m_editm1;
  965. m_Thinker.BV1[4]=dlg.m_editj1;
  966. m_Thinker.BV1[5]=dlg.m_editp1;
  967. m_Thinker.BV1[6]=dlg.m_editb1;
  968. m_Thinker.BV2[1]=dlg.m_edits2;
  969. m_Thinker.BV2[2]=dlg.m_editx2;
  970. m_Thinker.BV2[3]=dlg.m_editm2;
  971. m_Thinker.BV2[4]=dlg.m_editj2;
  972. m_Thinker.BV2[5]=dlg.m_editp2;
  973. m_Thinker.BV2[6]=dlg.m_editb2;
  974. m_Thinker.BV3[0]=dlg.m_edit30;
  975. m_Thinker.BV3[1]=dlg.m_edit31;
  976. m_Thinker.BV3[2]=dlg.m_edit32;
  977. m_Thinker.BV3[3]=dlg.m_edit33;
  978. m_Thinker.BV3[4]=dlg.m_edit34;
  979. m_Thinker.SaveThinkSetting();
  980. }
  981. }
  982. void CChessDlg::TestCheck()
  983. {
  984. int i;
  985. m_bCheck[0]=FALSE;
  986. m_bCheck[1]=FALSE;
  987. FixManMap();
  988. if(m_Face.man[16].x )
  989. for (i=0;i<=15;i++)
  990. {
  991. if(!m_Face.man[i].x)continue;
  992. if(::CanGo(m_nManMap
  993. ,i
  994. ,m_Face.man[i].x,m_Face.man[i].y
  995. ,m_Face.man[16].x,m_Face.man[16].y))
  996. {
  997. m_bCheck[1]=TRUE;
  998. break;
  999. }
  1000. }
  1001. if(m_Face.man[0].x )
  1002. for (i=16;i<=31;i++)
  1003. {
  1004. if(!m_Face.man[i].x)continue;
  1005. if(::CanGo(m_nManMap,i
  1006. ,m_Face.man[i].x,m_Face.man[i].y
  1007. ,m_Face.man[0].x,m_Face.man[0].y))
  1008. {
  1009. m_bCheck[0]=TRUE;
  1010. break;
  1011. }
  1012. }
  1013. }
  1014. void CChessDlg::OnHelpUse() 
  1015. {
  1016. HINSTANCE hRun = ShellExecute(GetParent()->GetSafeHwnd(), _T("open"), "chesshelp.html", NULL, NULL, SW_SHOW);
  1017.  if ((int) hRun <= 32)
  1018.  {
  1019. MessageBox("找不到帮助文件"chesshelp.html"","错误",MB_ICONERROR);
  1020.  }
  1021. }