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

📄 wzqdlg.cpp

📁 一个基于对话框的五子棋游戏,支持人机对战.
💻 CPP
字号:
// WzqDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Wzq.h"
#include "WzqDlg.h"
#include <mmsystem.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	CLinkButton	m_ok1;
	//}}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)
	DDX_Control(pDX, IDOK, m_ok1);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWzqDlg dialog

CWzqDlg::CWzqDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CWzqDlg::IDD, pParent)
{
	CBitmap b,c,d,e,f;
	CSize x;
	b.LoadBitmap(IDB_QP);
	x.cx =446 ;
	x.cy =446; 
	//Creates a memory device context that is compatible with the device specified by pDC
	qp.CreateCompatibleDC(NULL);
	//Selects an object into the device context
	qp.SelectObject(&b);
	qp.SetSize(x);
	b.DeleteObject(); 

	c.LoadBitmap(IDB_QZH);
	x.cx = 27 ;
	x.cy =27 ;
	qzh.CreateCompatibleDC(NULL);
	qzh.SelectObject(&c);
	qzh.SetSize(x);
	c.DeleteObject(); 

	d.LoadBitmap(IDB_QZB);
	x.cx = 27 ;
	x.cy =27; 
	qzb.CreateCompatibleDC(NULL);
	qzb.SelectObject(&d);
	qzb.SetSize(x);
	d.DeleteObject(); 

	e.LoadBitmap(IDB_MASK);
	x.cx = 27 ;
	x.cy =27; 
	mask.CreateCompatibleDC(NULL);
	mask.SelectObject(&e);
	mask.SetSize(x);
	d.DeleteObject();

	f.LoadBitmap(IDB_DEAD);
	x.cx=27;
	x.cy=27;
	dead.CreateCompatibleDC(NULL);
	dead.SelectObject(&f);
	dead.SetSize(x);
	f.DeleteObject();
	goflag=TRUE;
	computergo=FALSE;
	//{{AFX_DATA_INIT(CWzqDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CWzqDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CWzqDlg)
	DDX_Control(pDX, IDCANCEL6, m_huiqi);
	DDX_Control(pDX, IDCANCEL5, m_help);
	DDX_Control(pDX, IDCANCEL4, m_zuozhe);
	DDX_Control(pDX, IDCANCEL3, m_choice);
	DDX_Control(pDX, IDCANCEL2, m_newgame);
	DDX_Control(pDX, IDCANCEL, m_cancel);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CWzqDlg, CDialog)
	//{{AFX_MSG_MAP(CWzqDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_LBUTTONDOWN()
	ON_BN_CLICKED(IDCANCEL2, OnPlayAgain)
	ON_BN_CLICKED(IDCANCEL3, OnChoice)
	ON_BN_CLICKED(IDCANCEL4, Onzuozhe)
	ON_BN_CLICKED(IDCANCEL5, Onhelp)
	ON_BN_CLICKED(IDCANCEL6, Onhuiqi)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWzqDlg message handlers

BOOL CWzqDlg::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
	Mycomputer.SetDC(&qp,&qzh,&qzb,&mask,&dead,this->GetDC());
	Mycomputer.NewGame();
	Music("IDR_WAVE1","WAVE");
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

// 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 CWzqDlg::OnPaint() 
{
	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();
		Mycomputer.DrawAgain();
	}
}

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

void CWzqDlg::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	int x1,y1;
	x1=(int)(point.x-7)/29;
	y1=(int)(point.y-7)/29;
//	char a[20];
//	_gcvt(x1,3,a);
//	AfxMessageBox(a);
//	_gcvt(y1,3,a);
//	AfxMessageBox(a);
	if(x1<0 || x1>14 || y1<0 || y1>14)//判断鼠标左击是否过界
	{
		AfxMessageBox("过界了!");
		return;
	}
	else if(Mycomputer.QiPan[y1][x1].Qizhi!=0)//鼠标左击的是否是已经走了的棋
		return;
	if(Mycomputer.stophand)//有赢家后就不能再走棋了	
	{
		AfxMessageBox("请按[新游戏]按钮重新开局!");
		return;
	}
	else if(Mycomputer.Model==2)//如果是两人对弈模式
	{
		if(goflag)//判断是谁下棋
		{
			Mycomputer.BuildHuiQi(y1,x1);
			Mycomputer.DownBaizhi(x1,y1);//先走的下白棋
			if(Mycomputer.WhoWin(y1,x1))
			{
				Mycomputer.DrawAgain();
				Mycomputer.stophand=TRUE;
				AfxMessageBox("白棋赢!");
			}
			goflag=FALSE;
		}
		else 
		{
			Mycomputer.BuildHuiQi(y1,x1);
			Mycomputer.Downheizhi(x1,y1);
			if(Mycomputer.WhoWin(y1,x1))
			{
				Mycomputer.DrawAgain();
				Mycomputer.stophand=TRUE;
				AfxMessageBox("黑棋赢!");
			}
			goflag=TRUE;
		}
	}
    else//其他模式
	{
		Mycomputer.BuildHuiQi(y1,x1);
		Mycomputer.DownBaizhi(x1,y1);
		if(Mycomputer.WhoWin(y1,x1))
		{
			Mycomputer.DrawAgain();
			Mycomputer.stophand=TRUE;
			AfxMessageBox("白棋赢!");
		}
		else
			Mycomputer.PcThink();
	}
	CDialog::OnLButtonDown(nFlags, point);
}

void CWzqDlg::OnPlayAgain() 
{
	// TODO: Add your control notification handler code here
	Mycomputer.NewGame();
	goflag=TRUE;
}

void CWzqDlg::OnChoice() 
{
	// TODO: Add your control notification handler code here
	CThoice dlg;
	dlg.GetModel(Mycomputer.Model);
	dlg.DoModal();
	Mycomputer.Model=dlg.SetModel();
	Mycomputer.NewGame();
	goflag=TRUE;
}

void CWzqDlg::Onzuozhe() 
{
	// TODO: Add your control notification handler code here
	CAboutDlg dlg;
	dlg.DoModal();	
}

void CWzqDlg::Onhelp() 
{
	// TODO: Add your control notification handler code here
	if(Mycomputer.Model==0 || Mycomputer.Model==1)//人先机后或机先人后
	{
		computergo=TRUE;
		Mycomputer.GetComputerGo(computergo);
		Mycomputer.PcThink();
		Mycomputer.PcThink();
		computergo=FALSE;
	}
	else if(Mycomputer.Model==2)//两人对弈
	{
		if(goflag)
		{
			Mycomputer.GetComputerGo(goflag);
			Mycomputer.PcThink();
			goflag=FALSE;
		}
		else
		{
			Mycomputer.GetComputerGo(goflag);
			Mycomputer.PcThink();
			goflag=TRUE;
		}

	}
	else
		AfxMessageBox("帮助出现了问题!");
	
}

void CWzqDlg::Onhuiqi() 
{
	// TODO: Add your control notification handler code here
	if(Mycomputer.Model==2)
	{
		goflag=!goflag;
		Mycomputer.HuiQiGo();
	}
	else
	{
		Mycomputer.HuiQiGo();
		Mycomputer.HuiQiGo();
	}
	Mycomputer.DrawAgain();
}

void CWzqDlg::Music(CString x, CString y)
{
	HINSTANCE h=AfxGetInstanceHandle();
	HRSRC hr=FindResource(h,x,y);
	HGLOBAL hg=LoadResource(h,hr);
	LPSTR lp=(LPSTR)LockResource(hg);
	sndPlaySound(lp,SND_LOOP|SND_MEMORY|SND_ASYNC);
	FreeResource(hg);
}

⌨️ 快捷键说明

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