一、程序運行時情況 二、本程序里的計算機具有自動判斷的功能,能根據當前棋子的布局情況采用相關的走子方式。為了避免在走子時候出現屏幕閃爍,采用雙緩沖進行繪圖輸出,以下是軟件實現的部分代碼:
01.#include "stdafx.h"
02.在CFiveChessView類中定義了如下的數據結構:
03.
04. enum {MAX_X = 18, MAX_Y=18, MIN_X =1, MIN_Y =1};
05.
06. // NONE :空位置:BLACK :黑 WHITE 白
07. enum {NONE = 0, BLACK = 1, WHITE = 2};
08.
09. // ATTACK :進攻:DEFEND :防守
10. enum {ATTACK = 0, DEFEND = 1};
11.
12. // MAN_FIRST :黑先:COMPUTER_FIRST :白先
13. enum {MAN_FIRST = 0, COMPUTER_FIRST = 1};
14.
15. // BOTH_PEASE :和:BLACK_WIN :黑勝 WHITE_WIN: 白勝
16. enum {BOTH_PEASE=0, BLACK_WIN = 1, WHITE_WIN = 2};
17.
18. // L_TO_R:左到右 T_TO_B :上到下: LB_TO_RT :左下到右上,LT_TO_RB左上到右下
19. enum {L_TO_R=0, T_TO_B = 1, LB_TO_RT = 2 , LT_TO_RB = 3};
20.
21.
22. CChess fiveChess;
23. int m_nLastBlackPos_x;
24. int m_nLastBlackPos_y;
25. int m_nCurrentWhitePos_x;
26. int m_nCurrentWhitePos_y;
27.
28.在CMainFrame類中PreCreateWindow(CREATESTRUCT& cs)增加如下代碼設置固定窗口大小:
01.BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
02.{
03. if( !CFrameWnd::PreCreateWindow(cs) )
04. return FALSE;
05. cs.style = WS_OVERLAPPED | WS_SYSMENU | WS_MINIMIZEBOX;// | WS_THICKFRAME;
06. cs.style &= ~WS_BORDER;
07. cs.dwExStyle &= ~WS_EX_CLIENTEDGE;
08. int iWinBorderX = GetSystemMetrics(SM_CXBORDER);
09. int iWinBorderY = GetSystemMetrics(SM_CYBORDER);
10. int iCaptionY = GetSystemMetrics(SM_CYCAPTION);
11. int iMenuY = GetSystemMetrics(SM_CYMENU);
12. int iStausY = GetSystemMetrics(SM_CYMENU);
13. cs.cx = 510 + iWinBorderX ;
14. cs.cy = 530 + iWinBorderY + iCaptionY + iStausY + iMenuY;
15. return TRUE;
16.}
本軟件還定義了CChess類,實現了走棋的數據結構和相關算法:
01.class CChess : public CObject
02.{
03.public:
04. enum {MAX_X = 18, MAX_Y=18, MIN_X =1, MIN_Y =1};
05.
06. // NONE :空位置:BLACK :黑 WHITE 白
07. enum {NONE = 0, BLACK = 1, WHITE = 2};
08.
09. // ATTACK :進攻:DEFEND :防守
10. enum {ATTACK = 0, DEFEND = 1};
11.
12. // MAN_FIRST :人先:COMPUTER_FIRST:計算機先
13. enum {MAN_FIRST = 0, COMPUTER_FIRST = 1};
14.
15. // BOTH_PEASE :和:BLACK_WIN :黑勝 WHITE_WIN: 白勝
16. enum {BOTH_PEASE=0, BLACK_WIN = 1, WHITE_WIN = 2};
17.
18. // L_TO_R:左到右 T_TO_B :上到下: LB_TO_RT :左下到右上,LT_TO_RB左上到右下
19. // enum {L_TO_R=0, T_TO_B = 1, LB_TO_RT = 2 , LT_TO_RB = 3};
20.
21. //const static int
22.public:
23. static int Start_X;
24. static int Start_Y;
25. static int Width_X;
26. static int Height_Y;
27. CObList pList;
28. CList <CNode,CNode&> List; // 熱解二數據鏈表
29.
30.
31.public:
32.
33. void Draw(CDC *pDC,int x, int y, int nState);
34. void Init();
35. inline void SetChessState (int x,
36. int y,
37. int nState)
38. {
39. if (x>=0 && x <MAX_X && y>=0 && y <MAX_Y )
40. m_chessman[x][y] = nState;
41. }
42. inline int GetChessState (int x, int y ){ return m_chessman[x][y];}
43.
44. inline void SetWhoFirst(int nWhoFirst) {m_nWhoFirst = nWhoFirst;}
45. inline int GetWhoFirst() { return m_nWhoFirst;}
46.
47. inline void SetGoStyle(int nGoStyle) {m_nGoStyle = nGoStyle;}
48. inline int GetGoStyle() { return m_nGoStyle;}
49.
50. inline void SetResult(int nResult) {m_nResult = nResult;}
51. inline int GetResult() { return m_nResult;}
52.
53.
54.public:
55. BOOL GetSixComputerPos(int &col, int &row);
56. BOOL ScanTwo(int side);
57. int WhoWin(int state);
58. BOOL ScanLTtoRbFive(int side);
59. BOOL ScanLbtoRtFive(int side);
60. BOOL ScanTtoBFive(int side);
61. BOOL ScanLtoRFive(int side);
62.
63. VC++游戲開發:五子棋
64. void ScanLTtoRbSix(int side);
65. void ScanLbtoRtSix(int side);
66. void ScanTtoBSix(int side);
67. void ScanLtoRSix(int side);
68.
69. int m_nWhoFirst ; //誰先出
70. int m_nGoStyle ; //走棋方式;
71. int m_nResult ; //勝負結果
72. int m_chessman[MAX_X][MAX_Y]; //保存棋子狀態
73.
74.
75. int m_nFNonePos_x; //空位置 x坐標
76. int m_nFNonePos_y; //空位置 y坐標
77.
78.
79. int m_nSNonePos_x; //空位置 x坐標
80. int m_nSNonePos_y; //空位置 y坐標
81.
82. int m_nTNonePos_x; //空位置 x坐標
83. int m_nTNonePos_y; //空位置 y坐標
84.
85. CChess();
86. virtual ~CChess();
87.
88.};
三、本程序在windows 2000,visual c++ 6.0下編譯通過。
四、由于時間匆忙,程序中的部分算法設計不夠合理,歡迎各位大蝦們多多指教,希望能與你們多多交流,聯系方式:
ldh5118@sohu.com,
qq:36201365
|