WEB开发网
开发学院软件开发VC VC++游戏开发:五子棋 阅读

VC++游戏开发:五子棋

 2006-07-23 11:33:33 来源:WEB开发网   
核心提示:本文示例源代码或素材下载 一、程序运行时情况二、本程序里的计算机具有自动判断的功能,能根据当前棋子的布局情况采用相关的走子方式,VC++游戏开发:五子棋,为了避免在走子时候出现屏幕闪烁,采用双缓冲进行绘图输出,以下是软件实现的部分代码:#include "stdafx.h"在CFiveChe

本文示例源代码或素材下载

一、程序运行时情况

二、本程序里的计算机具有自动判断的功能,能根据当前棋子的布局情况采用相关的走子方式。为了避免在走子时候出现屏幕闪烁,采用双缓冲进行绘图输出,以下是软件实现的部分代码:

#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下编译通过。

Tags:VC 游戏 开发

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接