程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> VC >> 關於VC++ >> VC++游戲開發:五子棋

VC++游戲開發:五子棋

編輯:關於VC++

一、程序運行時情況

二、本程序裡的計算機具有自動判斷的功能,能根據當前棋子的布局情況采用相關的 走子方式。為了避免在走子時候出現屏幕閃爍,采用雙緩沖進行繪圖輸出,以下是軟件實 現的部分代碼:

#include "stdafx.h"
在CFiveChessView類中定義了如下的數據結構:
  enum  {MAX_X = 18, MAX_Y=18, MIN_X =1, MIN_Y =1};

  // NONE :空位置:BLACK :黑 WHITE 白
  enum  {NONE = 0, BLACK = 1, WHITE = 2};
  // ATTACK :進攻:DEFEND :防守
    enum  {ATTACK = 0, DEFEND = 1};
  // MAN_FIRST :黑先:COMPUTER_FIRST :白先
    enum  {MAN_FIRST = 0, COMPUTER_FIRST = 1};
  // BOTH_PEASE :和:BLACK_WIN :黑勝 WHITE_WIN: 白勝
    enum  {BOTH_PEASE=0, BLACK_WIN = 1, WHITE_WIN = 2};
  // L_TO_R:左到右 T_TO_B :上到下: LB_TO_RT :左下到右上,LT_TO_RB左上到右 下
    enum  {L_TO_R=0, T_TO_B = 1, LB_TO_RT = 2 , LT_TO_RB = 3};
    CChess fiveChess;
  int m_nLastBlackPos_x;
  int m_nLastBlackPos_y;
  int m_nCurrentWhitePos_x;
  int m_nCurrentWhitePos_y;
在CMainFrame類中PreCreateWindow(CREATESTRUCT& cs)增加如下代碼設置固定窗口 大小:
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
  if( !CFrameWnd::PreCreateWindow(cs) )
    return FALSE;
  cs.style = WS_OVERLAPPED | WS_SYSMENU | WS_MINIMIZEBOX;// | WS_THICKFRAME;
  cs.style &= ~WS_BORDER;
  cs.dwExStyle &= ~WS_EX_CLIENTEDGE;
  int iWinBorderX = GetSystemMetrics(SM_CXBORDER);
  int iWinBorderY = GetSystemMetrics(SM_CYBORDER);
  int iCaptionY = GetSystemMetrics(SM_CYCAPTION);
  int iMenuY = GetSystemMetrics(SM_CYMENU);
  int iStausY = GetSystemMetrics(SM_CYMENU);
  cs.cx = 510 + iWinBorderX ;
  cs.cy = 530 + iWinBorderY + iCaptionY + iStausY + iMenuY;
  return TRUE;
}

本軟件還定義了CChess類,實現了走棋的數據結構和相關算法:

class CChess : public CObject 
{
public:
  enum  {MAX_X = 18, MAX_Y=18, MIN_X =1, MIN_Y =1};
  // NONE :空位置:BLACK :黑 WHITE 白
  enum  {NONE = 0, BLACK = 1, WHITE = 2};
  // ATTACK :進攻:DEFEND :防守
    enum  {ATTACK = 0, DEFEND = 1};
  // MAN_FIRST :人先:COMPUTER_FIRST:計算機先
    enum  {MAN_FIRST = 0, COMPUTER_FIRST = 1};

  // BOTH_PEASE :和:BLACK_WIN :黑勝 WHITE_WIN: 白勝
    enum  {BOTH_PEASE=0, BLACK_WIN = 1, WHITE_WIN = 2};
     // L_TO_R:左到右 T_TO_B :上到下: LB_TO_RT :左下到右上,LT_TO_RB左上 到右下
 //   enum  {L_TO_R=0, T_TO_B = 1, LB_TO_RT = 2 , LT_TO_RB = 3};
  //const static int
public:
  static int Start_X;
  static int Start_Y;
  static int Width_X;
  static int Height_Y;
    CObList pList;
  CList <CNode,CNode&> List; // 熱解二數據鏈表
public:
  void Draw(CDC *pDC,int x, int y, int nState);
  void Init();
  inline void SetChessState (int x,
          int y,
          int nState)
  {
    if (x>=0 && x <MAX_X && y>=0 && y <MAX_Y )
        m_chessman[x][y] = nState;
  }
  inline int GetChessState (int x, int y ){ return m_chessman[x][y];}
  inline void SetWhoFirst(int nWhoFirst) {m_nWhoFirst = nWhoFirst;}
  inline int GetWhoFirst() { return m_nWhoFirst;}
  inline void SetGoStyle(int nGoStyle) {m_nGoStyle = nGoStyle;}
  inline int GetGoStyle() { return m_nGoStyle;}
  inline void SetResult(int nResult) {m_nResult = nResult;}
  inline int GetResult() { return m_nResult;}
public:
  BOOL GetSixComputerPos(int &col, int &row);
  BOOL ScanTwo(int side);
  int WhoWin(int state);
  BOOL ScanLTtoRbFive(int side);
  BOOL ScanLbtoRtFive(int side);
  BOOL ScanTtoBFive(int side);
  BOOL ScanLtoRFive(int side);
  void ScanLTtoRbSix(int side);
  void ScanLbtoRtSix(int side);
  void ScanTtoBSix(int side);
  void ScanLtoRSix(int side);
  int m_nWhoFirst ;  //誰先出
  int m_nGoStyle ;  //走棋方式;
  int m_nResult  ;  //勝負結果
  int m_chessman[MAX_X][MAX_Y]; //保存棋子狀態

  int m_nFNonePos_x;  //空位置 x坐標
  int m_nFNonePos_y;  //空位置 y坐標

  int m_nSNonePos_x;  //空位置 x坐標
  int m_nSNonePos_y;  //空位置 y坐標
  int m_nTNonePos_x;  //空位置 x坐標
  int m_nTNonePos_y;  //空位置 y坐標
  CChess();
  virtual ~CChess();
};

三、本程序在windows 2000,visual c++ 6.0下編譯通過。

下載源代碼:http://www.vckbase.com/code/downcode.asp?id=2607

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved