ChessBoard.h

  1. #ifndef __CHESS_BOARD_H__
  2. #define __CHESS_BOARD_H__
  3.  
  4. #include "DataStruct.h"
  5.  
  6. #define COL_WIDTH 45
  7. #define ROW_WIDTH 45
  8.  
  9. class CChessBoard : public CWnd
  10. {
  11. private:
  12. CBitmap m_bitBlackChess, m_bitWhiteChess;
  13. CBitmap m_bitChessBoard;
  14. CBitmap m_motive[];
  15. int m_iMotiveNumber;
  16. bool m_bPlayMotive;
  17. int m_iMotivex, m_iMotivey;
  18. // Construction
  19. public:
  20. board_type m_oChessBoard;
  21. CChessBoard();
  22.  
  23. public:
  24. void NewGame();
  25. void MoveBack();
  26. void PlayMotive(int row, int col, UINT8 obcolor);
  27.  
  28. public:
  29. virtual BOOL Create(RECT &rect, CWnd * pParentWnd, UINT nID);
  30.  
  31. public:
  32. virtual ~CChessBoard();
  33.  
  34. protected:
  35. //{{AFX_MSG(CChessBoard)
  36. afx_msg void OnPaint();
  37. afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
  38. afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
  39. //}}AFX_MSG
  40. afx_msg void OnComRun(WPARAM wParam, LPARAM lParam);
  41. afx_msg void OnTranChess(WPARAM wParam, LPARAM lParam);
  42. DECLARE_MESSAGE_MAP()
  43. };
  44.  
  45. #endif

DataStruct.h

  1. #ifndef __DATA_STRUCT_H__
  2. #define __DATA_STRUCT_H__
  3.  
  4. typedef unsigned char UINT8;
  5. typedef unsigned short UINT16;
  6. typedef short INT16;
  7. typedef char INT8;
  8.  
  9. #define BOARD_COLS 8
  10. #define BOARD_ROWS 8
  11. typedef struct
  12. {
  13. UINT8 board[BOARD_ROWS+][BOARD_COLS+];
  14. }board_type;
  15.  
  16. typedef UINT16 move_key_type;
  17.  
  18. typedef struct
  19. {
  20. board_type board;
  21. UINT16 movepos;
  22. INT16 value;
  23. }tree_node_type;
  24.  
  25. #define CHESS_NONE 0x00
  26. #define CHESS_BLACK 0x01
  27. #define CHESS_WHITE 0x02
  28. #define CHESS_BORDER 0xFF
  29.  
  30. #define BD_PROTECTED 0x80
  31. #define FD_PROTECTED 0x40
  32. #define H_PROTECTED 0x20
  33. #define V_PROTECTED 0x10
  34.  
  35. #define THITHER_COLOR(color) ((~color)&0x03)
  36.  
  37. #define INITIAL_VALUE (32767)
  38.  
  39. #define STEP_MONMENT1 10
  40. #define STEP_MONMENT2 48
  41.  
  42. #define LEVEL_LOW 0
  43. #define LEVEL_NOR 1
  44. #define LEVEL_HIGH 2
  45. //游戏难度等级
  46. extern UINT8 g_iGameLevel; //游戏难度等级
  47. extern UINT8 g_bStart; //游戏开始标志
  48.  
  49. #define USER_PASS 1
  50. #define COMPUTER_PASS 2
  51. #define GAME_OVER 4
  52.  
  53. #define WM_TRANCHESS (WM_USER+10)
  54. /*可一次吃掉的最多的子的个数*/
  55. #define MAX_AFFECTED_PIECES 19
  56.  
  57. extern UINT8 computer_side;
  58. extern UINT8 cur_step;
  59. extern UINT16 step_array[];
  60.  
  61. extern INT16 calc_board_status(board_type *board_ptr, UINT8 obcolor);
  62. extern void init_board(board_type *board_ptr);
  63. extern void computer_play(board_type *board_ptr, HWND hwnd);
  64. extern UINT8 do_move_chess(board_type *board_ptr, UINT16 movepos, UINT8 obcolor, HWND hwnd);
  65. extern void get_chess_score(board_type *board_ptr, UINT16 &iWscore, UINT16 &iBscore);
  66.  
  67. #endif

HelpDlg.h

  1. #if !defined(AFX_HELPDLG_H__A6CEBADE_794E_4F8C_85FB_311FC78558A3__INCLUDED_)
  2. #define AFX_HELPDLG_H__A6CEBADE_794E_4F8C_85FB_311FC78558A3__INCLUDED_
  3.  
  4. #if _MSC_VER > 1000
  5. #pragma once
  6. #endif // _MSC_VER > 1000
  7. // HelpDlg.h : header file
  8. //
  9.  
  10. /////////////////////////////////////////////////////////////////////////////
  11. // CHelpDlg dialog
  12.  
  13. class CHelpDlg : public CDialog
  14. {
  15. // Construction
  16. public:
  17. CHelpDlg(CWnd* pParent = NULL); // standard constructor
  18.  
  19. // Dialog Data
  20. //{{AFX_DATA(CHelpDlg)
  21. enum { IDD = IDD_HELP };
  22. // NOTE: the ClassWizard will add data members here
  23. //}}AFX_DATA
  24.  
  25. // Overrides
  26. // ClassWizard generated virtual function overrides
  27. //{{AFX_VIRTUAL(CHelpDlg)
  28. protected:
  29. virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
  30. //}}AFX_VIRTUAL
  31.  
  32. // Implementation
  33. protected:
  34.  
  35. // Generated message map functions
  36. //{{AFX_MSG(CHelpDlg)
  37. virtual void OnOK();
  38. //}}AFX_MSG
  39. DECLARE_MESSAGE_MAP()
  40. };
  41.  
  42. //{{AFX_INSERT_LOCATION}}
  43. // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
  44.  
  45. #endif // !defined(AFX_HELPDLG_H__A6CEBADE_794E_4F8C_85FB_311FC78558A3__INCLUDED_)

Othello.h

  1. // Othello.h : main header file for the OTHELLO application
  2. //
  3.  
  4. #if !defined(AFX_OTHELLO_H__09373553_CD5E_4C18_9F3D_0A728D2E52CA__INCLUDED_)
  5. #define AFX_OTHELLO_H__09373553_CD5E_4C18_9F3D_0A728D2E52CA__INCLUDED_
  6.  
  7. #if _MSC_VER > 1000
  8. #pragma once
  9. #endif // _MSC_VER > 1000
  10.  
  11. #ifndef __AFXWIN_H__
  12. #error include 'stdafx.h' before including this file for PCH
  13. #endif
  14.  
  15. #include "resource.h" // main symbols
  16.  
  17. /////////////////////////////////////////////////////////////////////////////
  18. // COthelloApp:
  19. // See Othello.cpp for the implementation of this class
  20. //
  21.  
  22. class COthelloApp : public CWinApp
  23. {
  24. public:
  25. COthelloApp();
  26.  
  27. // Overrides
  28. // ClassWizard generated virtual function overrides
  29. //{{AFX_VIRTUAL(COthelloApp)
  30. public:
  31. virtual BOOL InitInstance();
  32. //}}AFX_VIRTUAL
  33.  
  34. // Implementation
  35.  
  36. //{{AFX_MSG(COthelloApp)
  37. // NOTE - the ClassWizard will add and remove member functions here.
  38. // DO NOT EDIT what you see in these blocks of generated code !
  39. //}}AFX_MSG
  40. DECLARE_MESSAGE_MAP()
  41. };
  42.  
  43. /////////////////////////////////////////////////////////////////////////////
  44.  
  45. //{{AFX_INSERT_LOCATION}}
  46. // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
  47.  
  48. #endif // !defined(AFX_OTHELLO_H__09373553_CD5E_4C18_9F3D_0A728D2E52CA__INCLUDED_)

OthelloDlg.h

  1. // OthelloDlg.h : header file
  2. //
  3.  
  4. #if !defined(AFX_OTHELLODLG_H__DAC0C8C2_DDB6_4DA7_A56E_440CDF9A626B__INCLUDED_)
  5. #define AFX_OTHELLODLG_H__DAC0C8C2_DDB6_4DA7_A56E_440CDF9A626B__INCLUDED_
  6.  
  7. #if _MSC_VER > 1000
  8. #pragma once
  9. #endif // _MSC_VER > 1000
  10.  
  11. /////////////////////////////////////////////////////////////////////////////
  12. // COthelloDlg dialog
  13.  
  14. #include "ChessBoard.h"
  15.  
  16. class COthelloDlg : public CDialog
  17. {
  18. // Construction
  19. public:
  20. void GameStart();
  21. void PlayBackMusic(BOOL bCheck);
  22. void InitMenu();
  23. COthelloDlg(CWnd* pParent = NULL); // standard constructor
  24.  
  25. int m_nBlackCount; //黑子个数
  26. int m_nWhiteCount; //白子个数
  27. CChessBoard m_chess;//棋盘对象
  28.  
  29. // Dialog Data
  30. //{{AFX_DATA(COthelloDlg)
  31. enum { IDD = IDD_OTHELLO_DIALOG };
  32. // NOTE: the ClassWizard will add data members here
  33. //}}AFX_DATA
  34.  
  35. // ClassWizard generated virtual function overrides
  36. //{{AFX_VIRTUAL(COthelloDlg)
  37. protected:
  38. virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
  39. //}}AFX_VIRTUAL
  40.  
  41. // Implementation
  42. protected:
  43. HICON m_hIcon;
  44.  
  45. // Generated message map functions
  46. //{{AFX_MSG(COthelloDlg)
  47. virtual BOOL OnInitDialog();
  48. afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
  49. afx_msg void OnPaint();
  50. afx_msg HCURSOR OnQueryDragIcon();
  51. afx_msg void OnAbout();
  52. afx_msg void OnExitGame();
  53. afx_msg void OnGameStart();
  54. afx_msg void OnHelp();
  55. afx_msg void OnLevelHigh();
  56. afx_msg void OnLevelLow();
  57. afx_msg void OnLevelNor();
  58. afx_msg void OnPlayMusic();
  59. afx_msg void OnBackBtn();
  60. //}}AFX_MSG
  61. afx_msg void OnRecalc(WPARAM wParam, LPARAM lParam);
  62. DECLARE_MESSAGE_MAP()
  63. };
  64.  
  65. //{{AFX_INSERT_LOCATION}}
  66. // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
  67.  
  68. #endif // !defined(AFX_OTHELLODLG_H__DAC0C8C2_DDB6_4DA7_A56E_440CDF9A626B__INCLUDED_)

Resource.h

  1. //{{NO_DEPENDENCIES}}
  2. // Microsoft Developer Studio generated include file.
  3. // Used by Othello.rc
  4. //
  5. #define IDM_ABOUTBOX 0x0010
  6. #define IDD_ABOUTBOX 100
  7. #define IDS_ABOUTBOX 101
  8. #define ID_CHESSBOARD 101
  9. #define IDD_OTHELLO_DIALOG 102
  10. #define IDR_MAINFRAME 128
  11. #define IDD_HELP 130
  12. #define IDR_MAIN_MENU 132
  13. #define IDB_TURN1 134
  14. #define IDB_TURN2 135
  15. #define IDB_TURN3 136
  16. #define IDB_TURN4 137
  17. #define IDB_TURN5 138
  18. #define IDB_TURN6 139
  19. #define IDB_BLACKCHESS 140
  20. #define IDB_CHESSBOARD 141
  21. #define IDB_WHITECHESS 142
  22. #define IDC_STATUS 1000
  23. #define IDC_BLACK_COUNT 1001
  24. #define IDC_WHITE_COUNT 1002
  25. #define IDC_BACK_BTN 1003
  26. #define IDR_GAME_START 32771
  27. #define IDR_EXIT_GAME 32772
  28. #define IDR_PLAY_MUSIC 32773
  29. #define IDR_LEVEL_HIGH 32774
  30. #define IDR_LEVEL_NOR 32775
  31. #define IDR_LEVEL_LOW 32776
  32. #define IDR_HELP 32777
  33. #define IDR_ABOUT 32778
  34.  
  35. // Next default values for new objects
  36. //
  37. #ifdef APSTUDIO_INVOKED
  38. #ifndef APSTUDIO_READONLY_SYMBOLS
  39. #define _APS_NEXT_RESOURCE_VALUE 143
  40. #define _APS_NEXT_COMMAND_VALUE 32779
  41. #define _APS_NEXT_CONTROL_VALUE 1004
  42. #define _APS_NEXT_SYMED_VALUE 101
  43. #endif
  44. #endif

StdAfx.h

  1. // stdafx.h : include file for standard system include files,
  2. // or project specific include files that are used frequently, but
  3. // are changed infrequently
  4. //
  5.  
  6. #if !defined(AFX_STDAFX_H__0F6DA0D4_2DD7_4BB6_82C5_0E64F3D63B2D__INCLUDED_)
  7. #define AFX_STDAFX_H__0F6DA0D4_2DD7_4BB6_82C5_0E64F3D63B2D__INCLUDED_
  8.  
  9. #if _MSC_VER > 1000
  10. #pragma once
  11. #endif // _MSC_VER > 1000
  12.  
  13. #define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers
  14.  
  15. #include <afxwin.h> // MFC core and standard components
  16. #include <afxext.h> // MFC extensions
  17. #include <afxdisp.h> // MFC Automation classes
  18. #include <afxdtctl.h> // MFC support for Internet Explorer 4 Common Controls
  19. #ifndef _AFX_NO_AFXCMN_SUPPORT
  20. #include <afxcmn.h> // MFC support for Windows Common Controls
  21. #endif // _AFX_NO_AFXCMN_SUPPORT
  22.  
  23. #define UM_RECALC (WM_USER+100)
  24. #define UM_COMRUN (UM_RECALC +1)
  25.  
  26. //{{AFX_INSERT_LOCATION}}
  27. // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
  28.  
  29. #endif // !defined(AFX_STDAFX_H__0F6DA0D4_2DD7_4BB6_82C5_0E64F3D63B2D__INCLUDED_)

ChessBoard.cpp

  1. // ChessBoard1.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "ChessBoard.h"
  6. #include "resource.h"
  7.  
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13.  
  14. UINT8 g_bStart = ;
  15.  
  16. /////////////////////////////////////////////////////////////////////////////
  17. // CChessBoard
  18. CChessBoard::CChessBoard()
  19. {
  20. m_iMotiveNumber=;
  21. m_iMotivex = m_iMotivey=;
  22. m_bPlayMotive = FALSE;
  23. init_board(&m_oChessBoard);
  24. }
  25.  
  26. CChessBoard::~CChessBoard()
  27. {
  28. }
  29.  
  30. BEGIN_MESSAGE_MAP(CChessBoard, CWnd)
  31. //{{AFX_MSG_MAP(CChessBoard)
  32. ON_WM_PAINT()
  33. ON_WM_LBUTTONDOWN()
  34. ON_WM_CREATE()
  35. //}}AFX_MSG_MAP
  36. ON_MESSAGE(UM_COMRUN, OnComRun)
  37. ON_MESSAGE(WM_TRANCHESS, OnTranChess)
  38. END_MESSAGE_MAP()
  39. //////////////////////////////////////////////////////////////////////////
  40. //延时函数
  41. //////////////////////////////////////////////////////////////////////////
  42. void delay(INT32 millisecond)
  43. {
  44. clock_t start = clock();
  45. do
  46. {
  47. MSG msg;
  48. if (::PeekMessage( &msg, NULL, , , PM_NOREMOVE ) )
  49. {
  50. if ( !AfxGetApp()->PumpMessage())
  51. {
  52. ::PostQuitMessage();
  53. return;
  54. }
  55. }
  56. }while(clock()<start+millisecond);
  57. }
  58. //////////////////////////////////////////////////////////////////////////
  59. //悔棋函数
  60. //////////////////////////////////////////////////////////////////////////
  61. void CChessBoard::MoveBack()
  62. {
  63. if(cur_step<)
  64. {//如果当前步骤下于2,说明没有开始游戏
  65. return;
  66. }
  67. UINT8 comside = computer_side;
  68. UINT8 step = cur_step;
  69. INT16 movearray[];
  70. //把下棋步骤数组中的数据复制到移动数组中
  71. memcpy(movearray, step_array, *sizeof(INT16));
  72. init_board(&m_oChessBoard);
  73. computer_side = comside;
  74. UINT8 col= CHESS_BLACK;
  75. for(int i=; i<step-; i++, col = ~col & )
  76. {
  77. do_move_chess(&m_oChessBoard, movearray[i], col, );
  78. }
  79. OnPaint();
  80. Invalidate();
  81. }
  82. //////////////////////////////////////////////////////////////////////////
  83. //改变棋子接口函数
  84. //////////////////////////////////////////////////////////////////////////
  85. void CChessBoard::OnTranChess(WPARAM wParam, LPARAM lParam)
  86. {
  87. int row = wParam/-;
  88. int col = wParam%-;
  89. CRect r(col*COL_WIDTH+, row*ROW_WIDTH+,
  90. col*COL_WIDTH+COL_WIDTH+, row*ROW_WIDTH+ROW_WIDTH+);
  91.  
  92. m_bPlayMotive = FALSE;
  93. OnPaint();
  94. InvalidateRect(&r);
  95.  
  96. if((lParam>>) !=)
  97. PlayMotive(row, col, UINT8(lParam));
  98. }
  99. //////////////////////////////////////////////////////////////////////////
  100. //由电脑人工智能下棋
  101. //////////////////////////////////////////////////////////////////////////
  102. void CChessBoard::OnComRun(WPARAM wParam, LPARAM lParam)
  103. {
  104. computer_play(&m_oChessBoard, m_hWnd);
  105. UINT16 wscore, bscore;
  106. get_chess_score(&m_oChessBoard, wscore, bscore);
  107. GetParent()->SendMessage(UM_RECALC, WPARAM(wscore|0x80000000), LPARAM(bscore));
  108. }
  109. //////////////////////////////////////////////////////////////////////////
  110. //新游戏
  111. //////////////////////////////////////////////////////////////////////////
  112. void CChessBoard::NewGame()
  113. {
  114. if(cur_step >)
  115. {
  116. if(MessageBox("开始新游戏吗?", "黑白棋",
  117. MB_YESNO|MB_ICONQUESTION) == IDYES)
  118. {
  119. g_bStart = ;
  120. init_board(&m_oChessBoard);
  121. Invalidate();
  122. }
  123. }
  124. }
  125. //////////////////////////////////////////////////////////////////////////
  126. //窗口建立函数
  127. //////////////////////////////////////////////////////////////////////////
  128. BOOL CChessBoard::Create(RECT &rect, CWnd *pParentWnd, UINT nID)
  129. {
  130. CString szClassName = AfxRegisterWndClass(CS_CLASSDC|CS_SAVEBITS|
  131. CS_HREDRAW|CS_VREDRAW,
  132. , (HBRUSH)CBrush(RGB(,,)), );
  133. rect.right = rect.left + +;
  134. rect.bottom = rect.top ++;
  135. if(!CWnd::CreateEx(WS_EX_CLIENTEDGE, szClassName, _T(""),
  136. WS_CHILD|WS_VISIBLE|WS_TABSTOP, rect,
  137. pParentWnd, nID, NULL)) //WS_EX_CLIENTEDGE
  138.  
  139. return FALSE;
  140. UpdateWindow();
  141. m_bitBlackChess.LoadBitmap(IDB_BLACKCHESS);
  142. m_bitChessBoard.LoadBitmap(IDB_CHESSBOARD);
  143. m_bitWhiteChess.LoadBitmap(IDB_WHITECHESS);
  144.  
  145. m_motive[].LoadBitmap(IDB_WHITECHESS);
  146. m_motive[].LoadBitmap(IDB_TURN1);
  147. m_motive[].LoadBitmap(IDB_TURN2);
  148. m_motive[].LoadBitmap(IDB_TURN3);
  149. m_motive[].LoadBitmap(IDB_TURN4);
  150. m_motive[].LoadBitmap(IDB_TURN5);
  151. m_motive[].LoadBitmap(IDB_TURN6);
  152. m_motive[].LoadBitmap(IDB_BLACKCHESS);
  153.  
  154. return TRUE;
  155. }
  156. //////////////////////////////////////////////////////////////////////////
  157. //播放棋子翻动动画
  158. //////////////////////////////////////////////////////////////////////////
  159. void CChessBoard::PlayMotive(int row, int col, UINT8 obcolor)
  160. {
  161. m_iMotivex = col*COL_WIDTH+;
  162. m_iMotivey = row*COL_WIDTH+;
  163. CRect r(m_iMotivex, m_iMotivey,
  164. m_iMotivex+COL_WIDTH,
  165. m_iMotivey +ROW_WIDTH);
  166. m_bPlayMotive = TRUE;
  167. if(obcolor == CHESS_BLACK)
  168. {//把棋子从白面向黑面翻转
  169. for(m_iMotiveNumber =; m_iMotiveNumber<; m_iMotiveNumber++)
  170. {
  171. OnPaint();
  172. InvalidateRect(&r);
  173. delay();
  174. }
  175. }
  176. else
  177. {//把棋子从黑面向白面翻转
  178. for(m_iMotiveNumber =; m_iMotiveNumber>=; m_iMotiveNumber--)
  179. {
  180. OnPaint();
  181. InvalidateRect(&r);
  182. delay();
  183. }
  184. }
  185. m_bPlayMotive = FALSE;
  186. }
  187. //////////////////////////////////////////////////////////////////////////
  188. //窗口绘图函数
  189. //////////////////////////////////////////////////////////////////////////
  190. void CChessBoard::OnPaint()
  191. {
  192.  
  193. CPaintDC dc(this);
  194. CDC imgdc;
  195. imgdc.CreateCompatibleDC(&dc);
  196. imgdc.SelectObject(&m_bitChessBoard);
  197. dc.BitBlt(, , , , &imgdc,,,SRCCOPY);
  198. if(m_bPlayMotive)
  199. {
  200. imgdc.SelectObject(&m_motive[m_iMotiveNumber]);
  201. dc.BitBlt(m_iMotivex, m_iMotivey, , , &imgdc, , , SRCCOPY);
  202. return;
  203. }
  204.  
  205. for(int i=; i<BOARD_ROWS; i++)
  206. {
  207. for(int j=; j<BOARD_COLS; j++)
  208. {
  209. if(m_oChessBoard.board[i+][j+] == CHESS_BLACK)
  210. {
  211. imgdc.SelectObject(&m_bitBlackChess);
  212. dc.BitBlt(j*COL_WIDTH+, i*ROW_WIDTH+, , , &imgdc,,,SRCCOPY);
  213. }
  214. else if(m_oChessBoard.board[i+][j+] == CHESS_WHITE)
  215. {
  216. imgdc.SelectObject(&m_bitWhiteChess);
  217. dc.BitBlt(j*COL_WIDTH+, i*ROW_WIDTH+, , , &imgdc,,,SRCCOPY);
  218. }
  219. }
  220. }
  221. }
  222. //////////////////////////////////////////////////////////////////////////
  223. //鼠标左键响应函数
  224. //////////////////////////////////////////////////////////////////////////
  225. void CChessBoard::OnLButtonDown(UINT nFlags, CPoint point)
  226. {
  227.  
  228. BYTE row = (point.y-)/ROW_WIDTH+;
  229. BYTE col = (point.x-)/COL_WIDTH+;
  230.  
  231. if(do_move_chess(&m_oChessBoard, row*+col, ~computer_side&, m_hWnd))
  232. {
  233. UINT16 wscore, bscore;
  234. get_chess_score(&m_oChessBoard, wscore, bscore);
  235. GetParent()->SendMessage(UM_RECALC, WPARAM(wscore), LPARAM(bscore));
  236. PostMessage(UM_COMRUN);
  237. }
  238. else
  239. {
  240. MessageBeep(MB_OK);
  241. }
  242.  
  243. CWnd::OnLButtonDown(nFlags, point);
  244. }
  245.  
  246. int CChessBoard::OnCreate(LPCREATESTRUCT lpCreateStruct)
  247. {
  248. if (CWnd::OnCreate(lpCreateStruct) == -)
  249. return -;
  250.  
  251. EndWaitCursor();
  252. return ;
  253. }

dispose.cpp

  1. #include "stdafx.h"
  2. #include "dataStruct.h"
  3.  
  4. UINT8 computer_side = CHESS_BLACK;
  5. UINT8 max_depth = ;
  6.  
  7. UINT8 cur_depth = ;
  8.  
  9. UINT8 cur_step =;
  10. UINT16 step_array[];
  11.  
  12. UINT8 g_iGameLevel = LEVEL_LOW; //游戏难度等级
  13. const UINT8 depth1[]={, , };
  14. const UINT8 depth2[]={, , };
  15.  
  16. /*找出所有在水平方向受保护的obcolor方的棋子,并累计分数*/
  17. INT16 scan_horiz_aixes(board_type *board_ptr, UINT8 obcolor)
  18. {
  19. /*扫描8个水平方向*/
  20. INT16 score=;
  21. UINT8 *cur_ptr, *stop_ptr;
  22. UINT8 piece[][];
  23. UINT8 count=, tmpscore;
  24. UINT8 bFull;
  25. for(UINT8 row=; row<; row++)
  26. {
  27. tmpscore = (row == || row == ) ? :;
  28. cur_ptr = &board_ptr->board[row][];
  29. stop_ptr= &board_ptr->board[row][];
  30. bFull = TRUE;
  31. count=;
  32. while(cur_ptr < stop_ptr)
  33. {
  34. if(*cur_ptr == obcolor)
  35. {
  36. piece[count][] = cur_ptr - &board_ptr->board[row][];
  37. while(*cur_ptr == obcolor)
  38. cur_ptr ++;
  39. piece[count++][] = cur_ptr - &board_ptr->board[row][];
  40. }
  41. if(!*cur_ptr)
  42. bFull = FALSE;
  43. cur_ptr++;
  44. }
  45. while(count--)
  46. {
  47. UINT8 nums = (piece[count][]-piece[count][]);
  48. if(bFull || piece[count][]== || piece[count][] == )
  49. score += nums;
  50. if(piece[count][]== || piece[count][] == )
  51. score += tmpscore;
  52. else if(!bFull && (piece[count][] == || piece[count][] == ) && (row == || row == ))
  53. score -= tmpscore;
  54. }
  55. }
  56.  
  57. return score;
  58. }
  59.  
  60. /*找出所有在垂直方向受保护的obcolor方的棋子,并累计分数*/
  61. INT16 scan_vertical_aixes(board_type *board_ptr, UINT8 obcolor)
  62. {
  63. INT16 score=;
  64. UINT8 *cur_ptr, *stop_ptr;
  65. UINT8 piece[][];
  66. UINT8 count=, tmpscore;
  67. UINT8 bFull;
  68. for(UINT8 col=; col<; col++)
  69. {
  70. tmpscore = (col == || col == ) ? :;
  71. cur_ptr = &board_ptr->board[][col];
  72. stop_ptr= &board_ptr->board[][col];
  73. bFull = TRUE;
  74. count=;
  75. while(cur_ptr < stop_ptr)
  76. {
  77. if(*cur_ptr == obcolor)
  78. {
  79. piece[count][] = (cur_ptr - &board_ptr->board[][col])/;
  80. while(*cur_ptr == obcolor)
  81. cur_ptr += ;
  82. piece[count++][] = (cur_ptr - &board_ptr->board[][col])/;
  83. }
  84. if(!*cur_ptr)
  85. bFull = FALSE;
  86. cur_ptr += ;
  87. }
  88. while(count--)
  89. {
  90. UINT8 nums = (piece[count][]-piece[count][]);
  91. if(bFull || piece[count][]== || piece[count][] == )
  92. score += nums;
  93. if(piece[count][]== || piece[count][] == )
  94. score += tmpscore;
  95. else if(!bFull && (piece[count][] == || piece[count][] == ) && (col == || col == ))
  96. score -= (tmpscore<<);
  97. }
  98. }
  99. return score;
  100. }
  101.  
  102. /*找出所有在右上到左下方向受保护的obcolor方的棋子,并累计分数*/
  103. INT16 scan_fd_aixes(board_type *board_ptr, UINT8 obcolor)
  104. {
  105. INT16 score =;
  106. UINT8 *cur_ptr, *stop_ptr, *base_ptr;
  107. UINT8 piece[][];
  108. UINT8 count=, tmpscore;
  109. UINT8 bFull;
  110. for(INT8 aixes = -; aixes <= ; aixes++)
  111. {
  112. tmpscore = (aixes == ) ? :;
  113. if(aixes <=)
  114. {
  115. base_ptr = cur_ptr = &board_ptr->board[][+aixes];
  116. stop_ptr = &board_ptr->board[+aixes][];
  117. }
  118. else
  119. {
  120. base_ptr = cur_ptr = &board_ptr->board[aixes+][];
  121. stop_ptr= &board_ptr->board[][aixes];
  122. }
  123. bFull = TRUE;
  124. count=;
  125. while(cur_ptr < stop_ptr)
  126. {
  127. if(*cur_ptr == obcolor)
  128. {
  129. piece[count][] = cur_ptr - board_ptr->board[];
  130. while(*cur_ptr == obcolor)
  131. cur_ptr += ;
  132. piece[count++][] = cur_ptr- board_ptr->board[];
  133. }
  134. if(!*cur_ptr)
  135. bFull = FALSE;
  136. cur_ptr += ;
  137. }
  138. while(count--)
  139. {
  140. UINT8 nums = (piece[count][]-piece[count][])/;
  141. BOOL toborder = (piece[count][] == base_ptr - board_ptr->board[] ||
  142. piece[count][] == stop_ptr - board_ptr->board[]);
  143. if(bFull || toborder)
  144. score += nums;
  145.  
  146. if((aixes == || aixes == -) && toborder)
  147. score -= tmpscore;
  148. /*如果是这块棋到达边界*/
  149. else if(toborder)
  150. score += tmpscore;
  151. /*如果有棋在角边上,则扣分*/
  152. else if(!bFull && (piece[count][] == ||
  153. piece[count][] == ))
  154. score -= (tmpscore<<);
  155. }
  156. }
  157.  
  158. /*如果角边有棋子,扣分*/
  159. if(board_ptr->board[][] == obcolor)
  160. score += ;
  161. else
  162. {
  163. if(board_ptr->board[][] == obcolor)
  164. score -=;
  165. if(board_ptr->board[][] == obcolor)
  166. score -=;
  167. if(board_ptr->board[][]== obcolor)
  168. score -=;
  169. }
  170.  
  171. if(board_ptr->board[][] == obcolor)
  172. score +=;
  173. else
  174. {
  175. if(board_ptr->board[][] == obcolor)
  176. score -=;
  177. if(board_ptr->board[][]== obcolor)
  178. score -=;
  179. if(board_ptr->board[][]== obcolor)
  180. score -= ;
  181. }
  182. return score;
  183. }
  184. /*找出所有在左上到右下方向受保护的obcolor方的棋子,并累计分数*/
  185. INT16 scan_bd_aixes(board_type *board_ptr, UINT8 obcolor)
  186. {
  187.  
  188. INT16 score =;
  189. UINT8 *cur_ptr, *stop_ptr, *base_ptr;
  190. UINT8 piece[][];
  191. UINT8 count=, tmpscore;
  192. UINT8 bFull;
  193. for(INT8 aixes = -; aixes <= ; aixes++)
  194. {
  195. tmpscore = (aixes == ) ? :;
  196. if(aixes <=)
  197. {
  198. base_ptr = cur_ptr = &board_ptr->board[-aixes][];
  199. stop_ptr = &board_ptr->board[][+aixes];
  200. }
  201. else
  202. {
  203. base_ptr = cur_ptr = &board_ptr->board[][aixes+];
  204. stop_ptr= &board_ptr->board[-aixes][];
  205. }
  206. bFull = TRUE;
  207. count=;
  208. while(cur_ptr < stop_ptr)
  209. {
  210. if(*cur_ptr == obcolor)
  211. {
  212. piece[count][] = cur_ptr - board_ptr->board[];
  213. while(*cur_ptr == obcolor)
  214. cur_ptr += ;
  215. piece[count++][] = cur_ptr- board_ptr->board[];
  216. }
  217. if(!*cur_ptr)
  218. bFull = FALSE;
  219. cur_ptr += ;
  220. }
  221. while(count--)
  222. {
  223. UINT8 nums = (piece[count][]-piece[count][])/;
  224. BOOL toborder = (piece[count][] == base_ptr - board_ptr->board[] ||
  225. piece[count][] == stop_ptr - board_ptr->board[]);
  226. if(bFull || toborder)
  227. score += nums;
  228. /*如果角边有棋子,扣分*/
  229. if((aixes == || aixes == -) && toborder)
  230. score -= tmpscore;
  231. /*如果是这块棋到达边界*/
  232. else if(toborder)
  233. score += tmpscore;
  234. /*如果有棋在角边上,则扣分, 主对角线方向*/
  235. else if(!bFull && (piece[count][] == ||
  236. piece[count][] == ))
  237. score -= (tmpscore<<);
  238. }
  239. }
  240.  
  241. /*如果角边有棋子,扣分*/
  242. if(board_ptr->board[][] == obcolor)
  243. score += ;
  244. else
  245. {
  246. if(board_ptr->board[][] == obcolor)
  247. score -=;
  248. if(board_ptr->board[][] == obcolor)
  249. score -=;
  250. if(board_ptr->board[][]== obcolor)
  251. score -=;
  252. }
  253.  
  254. if(board_ptr->board[][] == obcolor)
  255. score +=;
  256. else
  257. {
  258. if(board_ptr->board[][] == obcolor)
  259. score -=;
  260. if(board_ptr->board[][]== obcolor)
  261. score -=;
  262. if(board_ptr->board[][]== obcolor)
  263. score -= ;
  264. }
  265. return score;
  266. }
  267.  
  268. INT16 sample_calc_board_status(board_type *board_ptr, UINT8 obcolor)
  269. {
  270. INT16 score=;
  271. UINT8 *ptr = &board_ptr->board[][];
  272. UINT8 *stop = &board_ptr->board[][];
  273. UINT8 tmpcol = ~obcolor &0x03;
  274. while(ptr<stop)
  275. {
  276. if(*ptr == obcolor)
  277. score++;
  278. else if(*ptr == tmpcol)
  279. score--;
  280. ptr++;
  281. }
  282. return score;
  283. }
  284.  
  285. /*计算棋局board_ptr的状态分*/
  286. INT16 calc_board_status(board_type *board_ptr, UINT8 obcolor)
  287. {
  288. INT16 score=;
  289. score += scan_horiz_aixes(board_ptr, obcolor);
  290. score += scan_vertical_aixes(board_ptr, obcolor);
  291. score += scan_bd_aixes(board_ptr, obcolor);
  292. score += scan_fd_aixes(board_ptr, obcolor);
  293. UINT8 tmpcol = ~obcolor & 0x03 ;
  294. if(board_ptr->board[][] == tmpcol)
  295. score -= ;
  296. if(board_ptr->board[][] == tmpcol)
  297. score -= ;
  298. if(board_ptr->board[][] == tmpcol)
  299. score -= ;
  300. if(board_ptr->board[][] == tmpcol)
  301. score -= ;
  302. return score;
  303. }
  304.  
  305. /*从start_pos出发找到一个可下子的点,返回受影响的子的个数,
  306. affected_list存放受影响的棋格的指针,第一个指针为落子的点*/
  307. const INT16 delta_array[] = {-, , -, , -, , -, };
  308. INT16 find_move(board_type *board_ptr, INT16 start_pos,
  309. UINT8 obcolor, INT16 *affected_list)
  310. {
  311. UINT8 *cel_ptr = board_ptr->board[] + start_pos;
  312. UINT8 *stop_ptr = &board_ptr->board[][], *p;
  313. INT16 *aff_ptr = affected_list+, *hold_aff;
  314. UINT8 aixes;
  315. UINT8 thithercolor = THITHER_COLOR(obcolor);
  316. while()
  317. {
  318. /*找到一个空格子*/
  319. while(*cel_ptr)
  320. if(++cel_ptr>=stop_ptr)
  321. return ;
  322. /*检查在8个方向上是否能吃掉对方的棋子,并记录被吃掉棋子的下标*/
  323. for(aixes =;aixes<; aixes++)
  324. {
  325. hold_aff = aff_ptr;
  326. p = cel_ptr + delta_array[aixes];
  327. while(*p == thithercolor)
  328. {
  329. *aff_ptr++ = p - board_ptr->board[];
  330. p+= delta_array[aixes];
  331. }
  332. if(*p != obcolor)
  333. aff_ptr = hold_aff;
  334. }
  335. /*如果cel_ptr对应的点可以吃掉对方的子*/
  336. if(aff_ptr - affected_list > )
  337. {
  338. *affected_list = cel_ptr - board_ptr->board[];
  339. return (aff_ptr - affected_list);
  340. }
  341. cel_ptr++;
  342. }
  343. }
  344.  
  345. void init_board(board_type *board_ptr)
  346. {
  347. memset(board_ptr, , sizeof(board_type));
  348. /*init boarder*/
  349. memset(board_ptr->board[], 0xff, );
  350. memset(board_ptr->board[], 0xff, );
  351. for(int i=; i<; i++)
  352. {
  353. board_ptr->board[i][] = board_ptr->board[i][] =0xff;
  354. }
  355.  
  356. /*init chess*/
  357. board_ptr->board[][] = board_ptr->board[][] = CHESS_WHITE;
  358. board_ptr->board[][] = board_ptr->board[][] = CHESS_BLACK;
  359. cur_step = ;
  360. computer_side = CHESS_WHITE;
  361. }
  362.  
  363. /*从棋盘的一个状态出发,扩展此结点,并返回此结点的部分回溯值*/
  364. void extend_node_one(tree_node_type *node_ptr, tree_node_type *parent_ptr,UINT8 obcolor)
  365. {
  366. tree_node_type childnode;
  367. INT16 affected_list[MAX_AFFECTED_PIECES];
  368. INT16 start_pos = , num;
  369. num = find_move(&node_ptr->board, start_pos, obcolor, affected_list);
  370. /*如果是终局状态,则返回状态估值函数的值*/
  371. if(++cur_depth == max_depth || num== )
  372. {
  373. /*如果已方PASS但没到棋局结束,要扣分*/
  374. node_ptr->value = calc_board_status(&node_ptr->board, computer_side);
  375. if(!num)
  376. {
  377. /*如果双方都没棋下*/
  378. if(!find_move(&node_ptr->board, , ~obcolor&0x03, affected_list))
  379. return;
  380.  
  381. if(obcolor == computer_side)
  382. {
  383. node_ptr->value -= ;
  384. return ;
  385. }
  386. node_ptr->value += ;
  387. }
  388. return;
  389. }
  390. /*初始化回溯值*/
  391. node_ptr->value = (obcolor == computer_side)? -INITIAL_VALUE : INITIAL_VALUE;
  392. memcpy(&childnode.board, &node_ptr->board, sizeof(board_type));
  393. while(num)
  394. {
  395. while(num--)
  396. childnode.board.board[][affected_list[num]] = obcolor;
  397. /*递归计算部分回溯值*/
  398. UINT8 depth = cur_depth;
  399. extend_node_one(&childnode, node_ptr, (~obcolor)&0x03);
  400. cur_depth = depth;
  401. /*如果此结点是棋手一方,则部分回溯值是子结点中最大的一个*/
  402. if(obcolor == computer_side)
  403. {
  404. if(childnode.value > node_ptr->value)
  405. {
  406. node_ptr->value = childnode.value;
  407. node_ptr->movepos = affected_list[];
  408. }
  409. }
  410. /*如果是对手一方,部分回溯值是子结点中最小的一个*/
  411. else
  412. {
  413. if(childnode.value < node_ptr->value)
  414. {
  415. node_ptr->value = childnode.value;
  416. node_ptr->movepos = affected_list[];
  417. }
  418. }
  419. /* α-β裁减的判断 在考虑轮到棋手下棋的一个亲节点及轮到对手下棋的一个子节点时,
  420. 如果该子节点的数值已经小于或等于其亲节点的回溯值,
  421. 那么就不需要对该节点或者其后续节点做更多的处理了。
  422. 计算的过程可以直接返回到亲节点上。
  423. */
  424. /*在考虑轮到对手下棋的一个亲节点及轮到棋手下棋的一个子节点时,
  425. 如果该子节点的部分回溯值已经大于或等于其亲节点的部分回溯值,
  426. 那么就不需要对该子节点或者其后裔节点做更多的处理了。
  427. 计算过程可以直接返回到亲节点上。*/
  428. if(parent_ptr)
  429. {
  430. if(obcolor != computer_side)
  431. {
  432. /*α裁减*/
  433. if(node_ptr->value <= parent_ptr->value)
  434. return;
  435. }
  436. else
  437. {
  438. /*β裁减*/
  439. if(node_ptr->value >= parent_ptr->value)
  440. return;
  441. }
  442. }
  443. /*找到下一个可落子的点*/
  444. start_pos = affected_list[]+;
  445. memcpy(&childnode.board, &node_ptr->board, sizeof(board_type));
  446. num = find_move(&childnode.board, start_pos, obcolor, affected_list);
  447. }
  448. return;
  449. }
  450.  
  451. void extend_node_two(tree_node_type *node_ptr, tree_node_type *parent_ptr,UINT8 obcolor)
  452. {
  453. tree_node_type childnode;
  454. INT16 affected_list[MAX_AFFECTED_PIECES];
  455. INT16 start_pos = , num;
  456. num = find_move(&node_ptr->board, start_pos, obcolor, affected_list);
  457. /*如果是终局状态,则返回状态估值函数的值*/
  458. if(!num)
  459. {
  460. /*如果已方PASS但没到棋局结束,要扣分*/
  461. node_ptr->value = sample_calc_board_status(&node_ptr->board, computer_side);
  462. /*如果双方都没棋下*/
  463. if(!find_move(&node_ptr->board, , ~obcolor&0x03, affected_list))
  464. return;
  465.  
  466. if(obcolor == computer_side)
  467. {
  468. node_ptr->value -= ;
  469. return;
  470. }
  471. node_ptr->value += ;
  472. return;
  473. }
  474. /*初始化回溯值*/
  475. node_ptr->value = (obcolor == computer_side)? -INITIAL_VALUE : INITIAL_VALUE;
  476. memcpy(&childnode.board, &node_ptr->board, sizeof(board_type));
  477. while(num)
  478. {
  479. while(num--)
  480. childnode.board.board[][affected_list[num]] = obcolor;
  481. /*递归计算部分回溯值*/
  482. UINT8 depth = cur_depth;
  483. extend_node_two(&childnode, node_ptr, (~obcolor)&0x03);
  484. cur_depth = depth;
  485. /*如果此结点是棋手一方,则部分回溯值是子结点中最大的一个*/
  486. if(obcolor == computer_side)
  487. {
  488. if(childnode.value > node_ptr->value)
  489. {
  490. node_ptr->value = childnode.value;
  491. node_ptr->movepos = affected_list[];
  492. }
  493. }
  494. /*如果是对手一方,部分回溯值是子结点中最小的一个*/
  495. else
  496. {
  497. if(childnode.value < node_ptr->value)
  498. {
  499. node_ptr->value = childnode.value;
  500. node_ptr->movepos = affected_list[];
  501. }
  502. }
  503. /* α-β裁减的判断 在考虑轮到棋手下棋的一个亲节点及轮到对手下棋的一个子节点时,
  504. 如果该子节点的数值已经小于或等于其亲节点的回溯值,
  505. 那么就不需要对该节点或者其后续节点做更多的处理了。
  506. 计算的过程可以直接返回到亲节点上。
  507. */
  508. /*在考虑轮到对手下棋的一个亲节点及轮到棋手下棋的一个子节点时,
  509. 如果该子节点的部分回溯值已经大于或等于其亲节点的部分回溯值,
  510. 那么就不需要对该子节点或者其后裔节点做更多的处理了。
  511. 计算过程可以直接返回到亲节点上。*/
  512. if(parent_ptr)
  513. {
  514. if(obcolor != computer_side)
  515. {
  516. /*α裁减*/
  517. if(node_ptr->value <= parent_ptr->value)
  518. return;
  519. }
  520. else
  521. {
  522. /*β裁减*/
  523. if(node_ptr->value >= parent_ptr->value)
  524. return ;
  525. }
  526. }
  527. /*找到下一个可落子的点*/
  528. start_pos = affected_list[]+;
  529. memcpy(&childnode.board, &node_ptr->board, sizeof(board_type));
  530. num = find_move(&childnode.board, start_pos, obcolor, affected_list);
  531. }
  532. return;
  533. }
  534.  
  535. void get_chess_score(board_type *board_ptr, UINT16 &iWscore, UINT16 &iBscore)
  536. {
  537. iWscore =; iBscore =;
  538. for(INT16 i=; i<=BOARD_ROWS; i++)
  539. for(INT16 j=; j<=BOARD_COLS; j++)
  540. {
  541. if(board_ptr->board[i][j] == CHESS_BLACK)
  542. iBscore++;
  543. else if(board_ptr->board[i][j] == CHESS_WHITE)
  544. iWscore++;
  545. }
  546. }
  547.  
  548. void game_over(board_type *board_ptr, HWND hwnd)
  549. {
  550. UINT16 wscore, bscore;
  551. char strcomwin[]="虽然你很历害,但我还是赢了你!";
  552. char struserwin[]="让你一次,下次你可没这么走运了!";
  553. char strdogfall[]="我没好好下,你才有机会平局!";
  554. char *text;
  555. get_chess_score(board_ptr, wscore, bscore);
  556.  
  557. g_bStart = ;
  558.  
  559. if(computer_side == CHESS_WHITE)
  560. {
  561. if(wscore > bscore)
  562. {
  563. text = strcomwin;
  564. }
  565. else if(wscore <bscore)
  566. {
  567. text = struserwin;
  568. }
  569. else
  570. {
  571. text = strdogfall;
  572. }
  573. }
  574. else
  575. {
  576. if(wscore > bscore)
  577. text = struserwin;
  578. else if(wscore <bscore)
  579. text = strcomwin;
  580. else text = strdogfall;
  581. }
  582. MessageBox(hwnd, text, "黑白棋", MB_OK|MB_ICONINFORMATION);
  583. }
  584.  
  585. void computer_play(board_type *board_ptr, HWND hwnd)
  586. {
  587. cur_depth =;
  588. tree_node_type node;
  589. INT16 affected_list[MAX_AFFECTED_PIECES];
  590. start:
  591. memcpy(&node.board, board_ptr, sizeof(board_type));
  592. node.movepos =;
  593. if(cur_step>= STEP_MONMENT2)
  594. {
  595. extend_node_two(&node, NULL, computer_side);
  596. }
  597. else if(cur_step > STEP_MONMENT1)
  598. {
  599. max_depth = depth2[g_iGameLevel];
  600. extend_node_one(&node, NULL, computer_side);
  601. }
  602. else
  603. {
  604. max_depth = depth1[g_iGameLevel];
  605. extend_node_one(&node, NULL, computer_side);
  606. }
  607.  
  608. if(!do_move_chess(board_ptr, node.movepos, computer_side, hwnd))
  609. {
  610. if(!find_move(board_ptr, , (~computer_side)&0x03, affected_list))
  611. {
  612. game_over(board_ptr, hwnd);
  613. return;
  614. }
  615. else
  616. {
  617. MessageBox(hwnd,"我没棋下了,你再走一步!", "黑白棋", MB_OK|MB_ICONINFORMATION);
  618. return;
  619. }
  620. }
  621. else
  622. {
  623. if(!find_move(board_ptr, , (~computer_side)&0x03, affected_list))
  624. {
  625. if(!find_move(board_ptr, , computer_side, affected_list))
  626. {
  627. game_over(board_ptr, hwnd);
  628. return;
  629. }
  630. else
  631. {
  632.  
  633. MessageBox(hwnd ,"你没棋下了,我再走一步!", "黑白棋", MB_OK|MB_ICONINFORMATION);
  634.  
  635. goto start;
  636. }
  637.  
  638. }
  639. }
  640. }
  641.  
  642. UINT8 do_move_chess(board_type *board_ptr, UINT16 movepos, UINT8 obcolor, HWND hwnd)
  643. {
  644. INT16 affected_list[MAX_AFFECTED_PIECES];
  645. INT16 num = find_move(board_ptr, movepos, obcolor, affected_list);
  646. if(!num || affected_list[] != movepos)
  647. return ;
  648. for(int i=; i<num; i++)
  649. {
  650. board_ptr->board[][affected_list[i]] = obcolor;
  651. if(hwnd)
  652. ::SendMessage(hwnd, WM_TRANCHESS, WPARAM(affected_list[i]),LPARAM(i<<|obcolor));
  653. }
  654. step_array[cur_step++] = movepos;
  655.  
  656. return ;
  657. }

HelpDlg.cpp

 // HelpDlg.cpp : implementation file
// #include "stdafx.h"
#include "Othello.h"
#include "HelpDlg.h" #ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif /////////////////////////////////////////////////////////////////////////////
// CHelpDlg dialog CHelpDlg::CHelpDlg(CWnd* pParent /*=NULL*/)
: CDialog(CHelpDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CHelpDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
} void CHelpDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CHelpDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
} BEGIN_MESSAGE_MAP(CHelpDlg, CDialog)
//{{AFX_MSG_MAP(CHelpDlg)
//}}AFX_MSG_MAP
END_MESSAGE_MAP() /////////////////////////////////////////////////////////////////////////////
// CHelpDlg message handlers void CHelpDlg::OnOK()
{
// TODO: Add extra validation here CDialog::OnOK();
}

Othello.cpp

 // Othello.cpp : Defines the class behaviors for the application.
// #include "stdafx.h"
#include "Othello.h"
#include "OthelloDlg.h" #ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif /////////////////////////////////////////////////////////////////////////////
// COthelloApp BEGIN_MESSAGE_MAP(COthelloApp, CWinApp)
//{{AFX_MSG_MAP(COthelloApp)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG
ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP() /////////////////////////////////////////////////////////////////////////////
// COthelloApp construction COthelloApp::COthelloApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
} /////////////////////////////////////////////////////////////////////////////
// The one and only COthelloApp object COthelloApp theApp; /////////////////////////////////////////////////////////////////////////////
// COthelloApp initialization BOOL COthelloApp::InitInstance()
{
AfxEnableControlContainer(); // Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need. #ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif COthelloDlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
} // Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}

OthelloDlg.cpp

 // OthelloDlg.cpp : implementation file
// #include "stdafx.h"
#include "Othello.h"
#include "OthelloDlg.h" #include "HelpDlg.h" #include <mmsystem.h> #include "DataStruct.h" #ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif /////////////////////////////////////////////////////////////////////////////
// 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() /////////////////////////////////////////////////////////////////////////////
// COthelloDlg dialog COthelloDlg::COthelloDlg(CWnd* pParent /*=NULL*/)
: CDialog(COthelloDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_nBlackCount = ;
m_nWhiteCount = ;
} void COthelloDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
} BEGIN_MESSAGE_MAP(COthelloDlg, CDialog)
//{{AFX_MSG_MAP(COthelloDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_COMMAND(IDR_ABOUT, OnAbout)
ON_COMMAND(IDR_EXIT_GAME, OnExitGame)
ON_COMMAND(IDR_GAME_START, OnGameStart)
ON_COMMAND(IDR_HELP, OnHelp)
ON_COMMAND(IDR_LEVEL_HIGH, OnLevelHigh)
ON_COMMAND(IDR_LEVEL_LOW, OnLevelLow)
ON_COMMAND(IDR_LEVEL_NOR, OnLevelNor)
ON_COMMAND(IDR_PLAY_MUSIC, OnPlayMusic)
ON_BN_CLICKED(IDC_BACK_BTN, OnBackBtn)
//}}AFX_MSG_MAP
ON_MESSAGE(UM_RECALC, OnRecalc)
END_MESSAGE_MAP() /////////////////////////////////////////////////////////////////////////////
// COthelloDlg message handlers void COthelloDlg::OnRecalc(WPARAM wParam, LPARAM lParam)
{
CString strStatus;
CString strCount;
if(wParam & 0x80000000)
{
strStatus.Format(" 我找到一步好棋,现该你了!"); }
else
{
strStatus.Format("我正在想,你别急!");
}
strCount.Format(" 黑子:%02d ", UINT(lParam));
SetDlgItemText(IDC_STATUS, strStatus);
SetDlgItemText(IDC_BLACK_COUNT, strCount);
strCount.Format(" 白子:%02d ", (wParam&0xFFFF));
SetDlgItemText(IDC_WHITE_COUNT, strCount);
} BOOL COthelloDlg::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);
}
} SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon InitMenu(); g_iGameLevel = LEVEL_LOW; SetDlgItemText(IDC_STATUS, "欢迎来玩黑白棋!"); m_chess.Create(CRect(,, ,), this, ID_CHESSBOARD); return TRUE; // return TRUE unless you set the focus to a control
} void COthelloDlg::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 COthelloDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), ); // Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + ) / ;
int y = (rect.Height() - cyIcon + ) / ; // Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
} CString strCount;
strCount.Format(" 黑子:%02d ", m_nBlackCount);
SetDlgItemText(IDC_BLACK_COUNT, strCount);
strCount.Format(" 白子:%02d ", m_nWhiteCount);
SetDlgItemText(IDC_WHITE_COUNT, strCount);
} // The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR COthelloDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
} void COthelloDlg::OnAbout()
{
CAboutDlg dlg; //创建关于对话框类对象
dlg.DoModal(); //弹出关于对话框
} void COthelloDlg::OnExitGame()
{
CDialog::OnCancel(); //调用基类退出函数
} void COthelloDlg::OnGameStart()
{
GameStart(); //调用游戏开始接口函数
} void COthelloDlg::OnHelp()
{
CHelpDlg dlg; //创建帮助对话框类对象
dlg.DoModal(); //弹出帮助对话框
} void COthelloDlg::OnLevelHigh()
{
CWnd* pMain = AfxGetMainWnd();
CMenu* pMenu = pMain->GetMenu();
//判断播放音乐菜单当前状态
BOOL bCheck = (BOOL)pMenu->GetMenuState(IDR_LEVEL_HIGH, MF_CHECKED); if( !bCheck )
{
pMenu->CheckMenuItem(IDR_LEVEL_HIGH,
MF_BYCOMMAND | MF_CHECKED);
pMenu->CheckMenuItem(IDR_LEVEL_LOW,
MF_BYCOMMAND | MF_UNCHECKED);
pMenu->CheckMenuItem(IDR_LEVEL_NOR,
MF_BYCOMMAND | MF_UNCHECKED);
g_iGameLevel = LEVEL_HIGH;
}
} void COthelloDlg::OnLevelLow()
{
CWnd* pMain = AfxGetMainWnd();
CMenu* pMenu = pMain->GetMenu();
//判断播放音乐菜单当前状态
BOOL bCheck = (BOOL)pMenu->GetMenuState(IDR_LEVEL_LOW, MF_CHECKED); if( !bCheck )
{
pMenu->CheckMenuItem(IDR_LEVEL_HIGH,
MF_BYCOMMAND | MF_UNCHECKED);
pMenu->CheckMenuItem(IDR_LEVEL_LOW,
MF_BYCOMMAND | MF_CHECKED);
pMenu->CheckMenuItem(IDR_LEVEL_NOR,
MF_BYCOMMAND | MF_UNCHECKED);
g_iGameLevel = LEVEL_LOW;
}
} void COthelloDlg::OnLevelNor()
{
CWnd* pMain = AfxGetMainWnd();
CMenu* pMenu = pMain->GetMenu();
//判断播放音乐菜单当前状态
BOOL bCheck = (BOOL)pMenu->GetMenuState(IDR_LEVEL_NOR, MF_CHECKED); if( !bCheck )
{
pMenu->CheckMenuItem(IDR_LEVEL_HIGH,
MF_BYCOMMAND | MF_UNCHECKED);
pMenu->CheckMenuItem(IDR_LEVEL_LOW,
MF_BYCOMMAND | MF_UNCHECKED);
pMenu->CheckMenuItem(IDR_LEVEL_NOR,
MF_BYCOMMAND | MF_CHECKED);
g_iGameLevel = LEVEL_NOR;
}
} void COthelloDlg::OnPlayMusic()
{
CWnd* pMain = AfxGetMainWnd();
CMenu* pMenu = pMain->GetMenu();
//判断播放音乐菜单当前状态
BOOL bCheck = (BOOL)pMenu->GetMenuState(IDR_PLAY_MUSIC, MF_CHECKED); if(g_bStart)
{
if(bCheck)
{
pMenu->CheckMenuItem(IDR_PLAY_MUSIC, MF_BYCOMMAND | MF_UNCHECKED);
}
else
{
pMenu->CheckMenuItem(IDR_PLAY_MUSIC, MF_BYCOMMAND | MF_CHECKED);
} PlayBackMusic(!bCheck); //调用播放背景音乐功能函数
} } void COthelloDlg::InitMenu()
{
//初始化菜单
CWnd* pMain = AfxGetMainWnd();
CMenu* pMenu = pMain->GetMenu();
pMenu->CheckMenuItem(IDR_LEVEL_LOW, MF_BYCOMMAND | MF_CHECKED);
pMenu->CheckMenuItem(IDR_LEVEL_HIGH, MF_BYCOMMAND | MF_UNCHECKED);
pMenu->CheckMenuItem(IDR_LEVEL_NOR, MF_BYCOMMAND | MF_UNCHECKED);
pMenu->CheckMenuItem(IDR_PLAY_MUSIC,MF_BYCOMMAND| MF_UNCHECKED);
} void COthelloDlg::PlayBackMusic(BOOL bCheck)
{
if(bCheck)
{ //播放指定音乐文件
sndPlaySound("music.wav",SND_ASYNC);
}
else
{ //停止播放
sndPlaySound(NULL,SND_PURGE);
}
} void COthelloDlg::GameStart()
{
m_nBlackCount = ;
m_nWhiteCount = ;
m_chess.NewGame();
} void COthelloDlg::OnBackBtn()
{
m_chess.MoveBack();
}

StdAfx.cpp

 // stdafx.cpp : source file that includes just the standard includes
// Othello.pch will be the pre-compiled header
// stdafx.obj will contain the pre-compiled type information #include "stdafx.h"

C++的简单“五子棋”游戏,只是核心代码,资源代码未添加的更多相关文章

  1. Leo-io 的C语言实现简单五子棋游戏观后感

    源代码: /************************************************************** ** 文 件 名:wuziqi.cpp ** 功    能:扫 ...

  2. 用Java写的简单五子棋游戏(原创五子连珠算法)

    源码jar包(已安装jdk环境可直接运行) 下载地址:http://download.csdn.net/detail/eguid_1/9532912 五子连珠算法为自创算法,对于五子棋该算法性能足以. ...

  3. 原生JS+Canvas实现五子棋游戏

    一.功能模块 先看下现在做完的效果: 线上体验:https://wj704.github.io/five_game.html 主要功能模块为: 1.人机对战功能 2.悔棋功能 3.撤销悔棋功能 二.代 ...

  4. 基于Blazor写一个简单的五子棋游戏

    写这个五子棋游戏,其实主要目的是想尝试一下微软新作Blazor.Blazor对于那些搞.NET的程序员,又想做一些前端工作,真的挺友好,不用一句JS就可搞定前端交互,美哉.现在已经有很流行的前端框架, ...

  5. java 五子棋游戏

    五子棋游戏 一,1.五子棋的基本常识 与任何一种竞技棋一样,五子棋的每一局棋也分为三个阶段:开局,中局和残局. 五子棋的开始阶段称为开局,或称布局.其开局阶段是十分短暂的,大约在七着与十几着之间.在这 ...

  6. 信息安全系统设计基础课程实践:简单TUI游戏设计

    简单TUI游戏设计                目       录               一                      Curses库简介与基本开发方法             ...

  7. 自定义View实现五子棋游戏

    成功的路上一点也不拥挤,因为坚持的人太少了. ---简书上看到的一句话 未来请假三天顺带加上十一回家结婚,不得不说真是太坑了,去年婚假还有10天,今年一下子缩水到了3天,只能赶着十一办事了. 最近还在 ...

  8. Android实训案例(八)——单机五子棋游戏,自定义棋盘,线条,棋子,游戏逻辑,游戏状态存储,再来一局

    Android实训案例(八)--单机五子棋游戏,自定义棋盘,线条,棋子,游戏逻辑,游戏状态存储,再来一局 阿法狗让围棋突然就被热议了,鸿洋大神也顺势出了篇五子棋单机游戏的视频,我看到了就像膜拜膜拜,就 ...

  9. cocos2d-x游戏引擎核心之六——绘图原理和绘图技巧

    一.OpenGL基础 游戏引擎是对底层绘图接口的包装,Cocos2d-x 也一样,它是对不同平台下 OpenGL 的包装.OpenGL 全称为 Open Graphics Library,是一个开放的 ...

随机推荐

  1. MySQL数据类型 int(M) 表示什么意思?详解mysql int类型的长度值问题

    MySQL 数据类型中的 integer types 有点奇怪.你可能会见到诸如:int(3).int(4).int(8) 之类的 int 数据类型.刚接触 MySQL 的时候,我还以为 int(3) ...

  2. ASP.NET Web API学习 (一)

    开发环境:win10,使用VS2015社区版和SQLSERVER2012开发 1.打开VS2015应用程序,点击左上角按钮:文件--新建--项目,弹出窗口中选择ASP.NET Web应用程序, 2.点 ...

  3. JVM相关参数的采集

    1.以-jar方式启动jar包: java -Dcom.sun.management.jmxremote=true -Dcom.sun.management.jmxremote.port=40100 ...

  4. Shell 编程 : 数值,字符,字符串

    数值运算命令     expr 命令     expr expression     expression 是由字符串 以及 运算符所组成的,每一个字符串或说运算符之间必须用空格隔开,   运算符的优 ...

  5. ABAP 上传文件到内表

    http://www.cnblogs.com/jiangzhengjun/p/4265642.html http://www.xuebuyuan.com/1233478.html

  6. .NET LINQ 生成操作

    生成操作      生成是指创建新的值序列. 方法 方法名 说明 C# 查询表达式语法 Visual Basic 查询表达式语法 更多信息 DefaultIfEmpty 将空集合替换为具有默认值的单一 ...

  7. 解决dom4j java.lang.NoClassDefFoundError: org/jaxen/JaxenException

    即使用这个方法需要以下两个包: dom4j-1.6.1.jar-306 KB jaxen-1.1-beta-6.jar-238 KB 或者 <dependency> <groupId ...

  8. python:列表与元组

    1.python包含六种内建的序列,列表和元组是其中的两种,列表可以修改,元组则不能 2.通用序列操作 2.1 索引:和C#的区别是索引可以为负数,最后一个元素索引为-1,索引超出范围会报错 例:&g ...

  9. js学习笔记---事件代理

    事件机制可以分为捕获型和冒泡型.捕获型是事件由父级元素(DOM)传递到子元素.冒泡型正好相反.事件机制默认为冒泡型.事件机制可以通过参数指定. 事件委托可以将我们绑定在document上的事件自动绑定 ...

  10. [译]:Orchard入门——部件管理

    原文链接:Managing Widgets 在Orchard中,部件是可以加入到当前当前主题任何位置或区域(如侧栏sidebar或底部区域footer)的UI块(如:HTML)或代码部分(如:内容部分 ...