mfc 井字游戏程序分析,描述整个程序处理过程。

本文详细分析了一个MFC井字游戏的程序,包括9个矩形的状态管理,X和O的交替绘制,以及游戏结束条件的检查。通过OnLButtonDown和OnRButtonDown响应鼠标点击事件来画棋,OnLButtonDblClk用于重新开始游戏。自定义函数如GetRectID确定点击位置,DrawX和DrawO绘制棋子,ResetGame重置游戏,CheckForGameOver和IsWinner判断游戏结束状态。
摘要由CSDN通过智能技术生成

此文纯属个人分析!高手绕道!

源码如下:(代码这么长,晕倒!?   跳过代码看分析!)


头文件:(TicTac.h)
//#include<afxwin.h>
#define EX 1
#define OH 2
class CMyApp:public CWinApp
{
public:
	virtual BOOL InitInstance();
};

class CMainWindow:public CWnd
{
protected:
	static const CRect m_rcSquares[9];
	int m_nGameGrid[9];
	int m_nNextChar;
	int GetRectID(CPoint point);
	void DrawBoard(CDC* pDC);
	void DrawX(CDC* pDC,int nPos);
	void DrawO(CDC* pDC,int nPos);
	void ResetGame();
	void CheckForGameOver();
	int IsWinner();
	BOOL IsDraw();
public:
	CMainWindow();
protected:
	virtual void PostNcDestroy();

	afx_msg void OnPaint();
	afx_msg void OnLButtonDown(UINT nFlags,CPoint point);
	afx_msg void OnRButtonDown(UINT nFlags,CPoint point);
	afx_msg void OnLButtonDblClk(UINT nFlags,CPoint point);

	DECLARE_MESSAGE_MAP()
};
源文件:(TicTac.cpp)
#include<afxwin.h>
#include"TicTac.h"
CMyApp myApp;
/CMyAPP member functions//
BOOL CMyApp::InitInstance()
{
	m_pMainWnd=new CMainWindow;
	m_pMainWnd->ShowWindow(m_nCmdShow);
	m_pMainWnd->UpdateWindow();
	return TRUE;
}
/CMainWindow message map and member functions/
BEGIN_MESSAGE_MAP(CMainWindow,CWnd)
	ON_WM_PAINT()
	ON_WM_LBUTTONDOWN()
	ON_WM_RBUTTONDOWN()
	ON_WM_LBUTTONDBLCLK()
END_MESSAGE_MAP()

const CRect CMainWindow::m_rcSquares[9]={
	CRect(16,16,112,112),
	CRect(128,16,224,112),
	CRect(240,16,336,112),
	CRect(16,128,112,224),
	CRect(128,128,224,224),
	CRect(240,128,336,224),
	CRect(16,240,112,336),
	CRect(128,240,224,336),
	CRect(240,240,336,336)
};
CMainWindow::CMainWindow(){
	m_nNextChar=EX;
	::ZeroMemory(m_nGameGrid,9*sizeof(int));
	//Register a WNDCLASS.
	CString strWndClass=AfxRegisterWndClass(
		CS_DBLCLKS,
		AfxGetApp()->LoadStandardCursor(IDC_ARROW),
		(HBRUSH)(COLOR_3DFACE+1),
		AfxGetApp()->LoadStandardIcon(IDI_WINLOGO)
		);
	//create a window
	CreateEx(0,strWndClass,_T("Tic-Tac
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值