⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tic_mfcdlg.cpp

📁 由MFC实现的一字棋
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// Tic_MFCDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Tic_MFC.h"
#include "Tic_MFCDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define GRID 100
#define EMPTY 0
#define PLAYER 1
#define COMPUTER 2
/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CTic_MFCDlg dialog

CTic_MFCDlg::CTic_MFCDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTic_MFCDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTic_MFCDlg)
	m_dept = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTic_MFCDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTic_MFCDlg)
	DDX_Text(pDX, IDC_EDIT1, m_dept);
	DDV_MinMaxInt(pDX, m_dept, 1, 8);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTic_MFCDlg, CDialog)
	//{{AFX_MSG_MAP(CTic_MFCDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_LBUTTONDOWN()
	ON_BN_CLICKED(IDC_BUTTON1, OnStartCom)
	ON_BN_CLICKED(IDC_BUTTON2, OnStartPly)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTic_MFCDlg message handlers

BOOL CTic_MFCDlg::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);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here

	turn = -1;
	m_dept = 2;
	UpdateData(false);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CTic_MFCDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}



//初始化数组
const CRect CTic_MFCDlg::m_chessSquares[9] = 
{
    CRect ( 150,  25, 150+GRID, 25+GRID),
    CRect ( 250,  25, 250+GRID, 25+GRID),
    CRect ( 350,  25, 350+GRID, 25+GRID),
    CRect ( 150, 125, 150+GRID, 125+GRID),
    CRect ( 250, 125, 250+GRID, 125+GRID),
    CRect ( 350, 125, 350+GRID, 125+GRID),
    CRect ( 150, 225, 150+GRID, 225+GRID),
    CRect ( 250, 225, 250+GRID, 225+GRID),
    CRect ( 350, 225, 350+GRID, 225+GRID)
};

 int CTic_MFCDlg::winPattern[8][3]= 
 {
    0, 1, 2,
    3, 4, 5,
    6, 7, 8,
    0, 3, 6,
    1, 4, 7,
    2, 5, 8,
    0, 4, 8,
    2, 4, 6
};

// 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 CTic_MFCDlg::OnPaint() 
{
	CPaintDC dc(this);
	DrawBoard( &dc );
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}


}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CTic_MFCDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

//********************************************************************************************************
//完成功能:鼠标左键相应,在点击的那格放置玩家棋子,之后再相应计算机走下一步
//*********************************************************************************************************
void CTic_MFCDlg::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	
	CDialog::OnLButtonDown(nFlags, point);

	if (turn == -1)
	{
		MessageBox (_T ("请先在左方控制台开始游戏!"), _T ("游戏未开始"),MB_ICONEXCLAMATION | MB_OK);
		return;
	}
	CClientDC dc (this);
	int i;

	int Pos = 0;
	for (i=0; i<9; i++) 
	{
        if (m_chessSquares[i].PtInRect (point))
		{
			if (Chess[i] == EMPTY)
			{
				Pos = i;
				Chess[i] = turn;
				break;
			}
		}
    }

	//i!=9说明玩家可以摆放棋子
	if ( i != 9)
	{
		PutAChess(&dc,Pos);

		//玩家放完后,机器下棋,首先判断棋盘有没有满
		for (i=0; i<9; i++) 
		{
			if (Chess[i] == EMPTY)
				break;
		}
		if (i!=9)
		{
			int result;
			AlphaBeta(Chess,SearchDepth,COMPUTER,-100,100,&result);
			Chess[result] = turn;
			PutAChess(&dc,result);
		}
	}

	
	
}

//********************************************************************************************************
//完成功能:在棋盘上画一个X,玩家
//*********************************************************************************************************
void CTic_MFCDlg::DrawX(CDC *pDC, int Pos)
{
	CPen pen (PS_SOLID, 2, RGB (255, 0, 0));
    CPen* pOldPen = pDC->SelectObject (&pen);

    CRect rect = m_chessSquares[Pos];
    rect.DeflateRect (16, 16);
    pDC->MoveTo (rect.left, rect.top);
    pDC->LineTo (rect.right, rect.bottom);
    pDC->MoveTo (rect.left, rect.bottom);
    pDC->LineTo (rect.right, rect.top);

    pDC->SelectObject (pOldPen);

}

//********************************************************************************************************
//完成功能:在棋盘上画一个O,电脑
//*********************************************************************************************************
void CTic_MFCDlg::DrawO(CDC *pDC, int Pos)
{
	CPen pen (PS_SOLID, 2, RGB (0, 0, 255));
    CPen* pOldPen = pDC->SelectObject (&pen);
    pDC->SelectStockObject (NULL_BRUSH);

    CRect rect = m_chessSquares[Pos];
    rect.DeflateRect (16, 16);
    pDC->Ellipse (rect);

    pDC->SelectObject (pOldPen);

}

//********************************************************************************************************
//完成功能:根据Chess棋盘数组 画出棋盘
//*********************************************************************************************************
void CTic_MFCDlg::DrawBoard(CDC *pDC)
{
	CPen pen (PS_SOLID, 1, RGB (0, 0, 0));
    CPen* pOldPen = pDC->SelectObject (&pen);

    pDC->MoveTo (250, 25);
    pDC->LineTo (250, 325);

    pDC->MoveTo (350, 25);
    pDC->LineTo (350, 325);

    pDC->MoveTo (150, 125);
    pDC->LineTo (450, 125);

    pDC->MoveTo (150, 225);
    pDC->LineTo (450, 225);

	pDC->MoveTo (150, 25);
    pDC->LineTo (450, 25);

	pDC->MoveTo (450, 25);
    pDC->LineTo (450, 325);

	pDC->MoveTo (450, 325);
    pDC->LineTo (150, 325);

	pDC->MoveTo (150, 325);
    pDC->LineTo (150, 25);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -