📄 mainwnd.cpp
字号:
// MainWnd.cpp : CMainWnd 类的实现
//
#include "stdafx.h"
#include "5ziqi.h"
#include "Mmsystem.h"
#pragma comment(lib, "winmm.lib")
#include "MainWnd.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CMainWnd
IMPLEMENT_DYNAMIC(CMainWnd, CWnd)
BEGIN_MESSAGE_MAP(CMainWnd, CWnd)
ON_WM_CREATE()
ON_WM_PAINT()
ON_WM_LBUTTONDOWN()
ON_WM_RBUTTONDOWN()
ON_BN_CLICKED(IDC_BUTTON_RESTART,OnButtonClicked)
ON_BN_CLICKED(IDC_BUTTON_FIRST,OnFirstClicked)
ON_BN_CLICKED(IDC_BUTTON_SOUND,OnSoundClicked)
END_MESSAGE_MAP()
//初始化矩形区数组
// CMainWnd 构造/析构
CMainWnd::CMainWnd()
: m_cmpturn(FALSE)
, pbtRestart(NULL)
, pbtFirst(NULL)
, m_nVictory(0)
, m_nLoss(0)
, m_fScale(0)
, pbtSound(NULL)
{
// TODO: 在此添加成员初始化代码
CString strWndClass = AfxRegisterWndClass(CS_DBLCLKS,
AfxGetApp()->LoadStandardCursor(IDC_CROSS),(HBRUSH)::GetStockObject(LTGRAY_BRUSH));
CreateEx(0,strWndClass,_T("傻瓜五子棋v1.0(版权归 Z_k 所有,欢迎盗版^^)"),WS_OVERLAPPED|WS_SYSMENU|WS_CAPTION|WS_MINIMIZEBOX,
CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL);
}
CMainWnd::~CMainWnd()
{
}
int CMainWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CWnd::OnCreate(lpCreateStruct) == -1)
return -1;
CRect rect(0,0,500,400);
CalcWindowRect(&rect);
SetWindowPos(NULL,0,0,rect.Width(),rect.Height(),
SWP_NOZORDER|SWP_NOMOVE|SWP_NOREDRAW);
//初始化矩形区数组
RECT r={-16,-16,20,20};
for (int ix=0;ix<10;++ix)
{
r.top += 36;
r.bottom += 36;
for (int iy=0;iy<10;++iy)
{
r.left += 36;
r.right += 36;
CRect rect(&r);
m_rcSquares[ix][iy] = rect;
}
r.left = -16;
r.right = 20;
}
//初始化网格标志数组
for (int ix=0;ix<10;++ix)
for (int iy=0;iy<10;++iy)
m_nGrid[ix][iy] = 0;
//构造按钮
CRect rcButton(400,300,480,330);
pbtRestart = new CButton;
pbtRestart->Create(_T("重 来"),WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,rcButton,this,IDC_BUTTON_RESTART);
CRect rcFirst(400,260,480,290);
CButton* pbtFirst = new CButton;
pbtFirst->Create(_T("电脑先下"),WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,rcFirst,this,IDC_BUTTON_FIRST);
CRect rcSound(400,220,480,250);
CButton* pbtSound = new CButton;
pbtSound->Create(_T("背景音乐"),WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,rcSound,this,IDC_BUTTON_SOUND);
//
// CRect rcSound(400,220,480,250);
// CButton* pbtSound = new CButton;
// pbtSound->Create(_T("背景音乐"),WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,rcFirst,this,IDC_BUTTON_SOUND);
//打开MIDI
/*用MCI 打开MIDI
MCI_OPEN_PARMS OpenParms;
OpenParms.lpstrDeviceType = TEXT("sequencer");//(LPCSTR) "MCI_DEVTYPE_SEQUENCER";//MIDI类型
OpenParms.lpstrElementName = TEXT("25524huan01.mid");
OpenParms.wDeviceID = 0;
mciSendCommand (0, MCI_OPEN,
MCI_WAIT | MCI_OPEN_TYPE | MCI_PLAY |
MCI_OPEN_ELEMENT,(DWORD) (LPMCI_OPEN_PARMS)&OpenParms);
MCI_PLAY_PARMS PlayParms;//开始播放
PlayParms.dwFrom = 0;// 指定从什么地方(时间)播放
mciSendCommand (OpenParms.wDeviceID, MCI_PLAY,MCI_FROM,(DWORD) (LPMCI_PLAY_PARMS)&PlayParms);
*/
// PlaySound(_T("FIGHT1.WAV"),NULL,SND_LOOP);
// sndPlaySound(_T("d:\\FIGHT1.WAV"),SND_ASYNC);
return 0;
}
BOOL CMainWnd::PreCreateWindow(CREATESTRUCT& cs)
{
if( !CWnd::PreCreateWindow(cs) )
return FALSE;
// TODO: 在此处通过修改
// CREATESTRUCT cs 来修改窗口类或样式
cs.style = WS_OVERLAPPED | WS_CAPTION | FWS_ADDTOTITLE
| WS_MINIMIZEBOX | WS_SYSMENU ;
cs.dwExStyle &= ~WS_EX_CLIENTEDGE;
//cs.lpszClass = AfxRegisterWndClass(0);
return TRUE;
}
// CMainWnd 诊断
#ifdef _DEBUG
void CMainWnd::AssertValid() const
{
CWnd::AssertValid();
}
void CMainWnd::Dump(CDumpContext& dc) const
{
CWnd::Dump(dc);
}
#endif //_DEBUG
// CMainWnd 消息处理程序
BOOL CMainWnd::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)
{
// 让视图第一次尝试该命令
// 否则,执行默认处理
return CWnd::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
}
void CMainWnd::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: 在此处添加消息处理程序代码
// 不为绘图消息调用 CWnd::OnPaint()
CBrush brush(RGB(0,200,150));
dc.SelectObject(&brush);
CFont font;
font.CreatePointFont(90, _T("Arial"));
dc.SelectObject(&font);
dc.Rectangle(20,20,380,380);
dc.SetBkMode(TRANSPARENT);
dc.TextOut(400,340,_T("Version 1.0"));
dc.TextOut(420,360,_T("By Z_k"));
//计算胜率
if ((m_nVictory + m_nLoss) != 0)
m_fScale = (float)m_nVictory/(float)(m_nVictory+m_nLoss)*(float)100;
CString result;
result.Format(_T("胜 : %d\n负 : %d\n胜率 : %.2f\n"),m_nVictory,m_nLoss,m_fScale);
//result.Format(_T("胜 : %d"),m_nVictory);
dc.DrawText(result,CRect(405,20,500,100),DT_LEFT);
for (int i=20;i<=380;i+=36)
{
dc.MoveTo(20,i);
dc.LineTo(380,i);
}
for (int i=20;i<=380;i+=36)
{
dc.MoveTo(i,20);
dc.LineTo(i,380);
}
for (int c=0;c<10;++c)
for (int d=0;d<10;++d)
{
if (m_nGrid[c][d] == 1) DrawWhite(m_rcSquares[c][d]);
if (m_nGrid[c][d] == 2) DrawBlack(m_rcSquares[c][d]);
}
}
void CMainWnd::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
for (int ix=0;ix<10;++ix)
for (int iy=0;iy<10;++iy)
{
if (m_rcSquares[ix][iy].PtInRect(point) && m_nGrid[ix][iy] == 0)
{
m_nGrid[ix][iy] = 1; //必须放再DrawWhite前才能清除数组无效点
m_cmpturn = TRUE;
DrawWhite(m_rcSquares[ix][iy]);
if (Check(ix,iy))
{
MessageBox(_T("你真厉害!"));
m_nVictory += 1;
OnButtonClicked();
}
break;
}
}
//轮到电脑下棋
if (m_cmpturn)
{
CPoint pt = GetPosition();
ComputerDown(pt);
if (Check(pt.x,pt.y))
{
MessageBox(_T("你太菜了!"));
m_nLoss += 1;
OnButtonClicked();
}
m_cmpturn = FALSE;
}
CWnd::OnLButtonDown(nFlags, point);
}
void CMainWnd::DrawWhite(CRect rect)
{
CClientDC dc(this);
CPen pen(PS_NULL,0,RGB(255,255,255));
dc.SelectObject(&pen);
rect.DeflateRect(3,3);
dc.Ellipse(&rect);
// PlaySound(MAKEINTRESOURCE(IDR_WAVE1),AfxGetResourceHandle(),SND_ASYNC|SND_RESOURCE|SND_NODEFAULT);
//清除无效点
for (int i=0;i<AIarray.GetSize();++i)
if (m_nGrid[AIarray[i].pt.x][AIarray[i].pt.y] != 0)
AIarray.RemoveAt(i);
}
void CMainWnd::DrawBlack(CRect rect)
{
CClientDC dc(this);
CBrush brush(RGB(0,0,0));
dc.SelectObject(&brush);
rect.DeflateRect(3,3);
dc.Ellipse(&rect);
PlaySound(MAKEINTRESOURCE(IDR_WAVE1),AfxGetResourceHandle(),SND_ASYNC|SND_RESOURCE|SND_NODEFAULT);
//清除无效点
for (int i=0;i<AIarray.GetSize();++i)
if (m_nGrid[AIarray[i].pt.x][AIarray[i].pt.y] != 0)
AIarray.RemoveAt(i);
}
void CMainWnd::OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
for (int ix=0;ix<10;++ix)
for (int iy=0;iy<10;++iy)
{
if (m_rcSquares[ix][iy].PtInRect(point) && m_nGrid[ix][iy] == 0)
{
// CString string;
// string.Format(_T("point:%d,%d"),ix,iy);
// MessageBox(string);
m_nGrid[ix][iy] = 2;
DrawBlack(m_rcSquares[ix][iy]);
if (Check(ix,iy))
MessageBox(_T("Good!"));
break;
}
}
CWnd::OnRButtonDown(nFlags, point);
}
BOOL CMainWnd::Check(int x,int y)
{
int count = 1;
int chkcount = 0;
CPoint a(0,0);
CPoint b(0,0);
classAI tempAi;
//向上查
if (x !=0 && m_nGrid[x][y] == m_nGrid[x-1][y])
{
++count; a.x=x-2; a.y=y;
if (x !=1 && m_nGrid[x][y] == m_nGrid[x-2][y])
{
++count; a.x=x-3; a.y=y;
if (x !=2 && m_nGrid[x][y] == m_nGrid[x-3][y])
{
++count; a.x=x-4; a.y=y;
if (x !=3 && m_nGrid[x][y] == m_nGrid[x-4][y])
{
++count; a.x=x-5; a.y=y;
}
}
}
}
else {a.x=x-1; a.y=y;}
//向下查
if (x !=9 && m_nGrid[x][y] == m_nGrid[x+1][y])
{
++count; b.x=x+2; b.y=y;
if (x !=8 && m_nGrid[x][y] == m_nGrid[x+2][y])
{
++count; b.x=x+3; b.y=y;
if (x !=7 && m_nGrid[x][y] == m_nGrid[x+3][y])
{
++count; b.x=x+4; b.y=y;
if (x !=6 && m_nGrid[x][y] == m_nGrid[x+4][y])
{
++count; b.x=x+5; b.y=y;
}
}
}
}
else {b.x=x+1; b.y=y;}
tempAi.count = count;
tempAi.pt = a;
SaveAI(tempAi);
tempAi.pt = b;
SaveAI(tempAi);
if (count > chkcount) chkcount = count;
//向左查
count = 1; //计数复位
if (y !=0 && m_nGrid[x][y] == m_nGrid[x][y-1])
{
++count; a.x=x; a.y=y-2;
if (y !=1 && m_nGrid[x][y] == m_nGrid[x][y-2])
{
++count; a.x=x; a.y=y-3;
if (y !=2 && m_nGrid[x][y] == m_nGrid[x][y-3])
{
++count; a.x=x; a.y=y-4;
if (y !=3 && m_nGrid[x][y] == m_nGrid[x][y-4])
{
++count; a.x=x; a.y=y-5;
}
}
}
}
else { a.x=x; a.y=y-1;}
//向右查
if (y !=9 && m_nGrid[x][y] == m_nGrid[x][y+1])
{
++count; b.x=x; b.y=y+2;
if (y !=8 && m_nGrid[x][y] == m_nGrid[x][y+2])
{
++count; b.x=x; b.y=y+3;
if (y !=7 && m_nGrid[x][y] == m_nGrid[x][y+3])
{
++count; b.x=x; b.y=y+4;
if (y !=6 && m_nGrid[x][y] == m_nGrid[x][y+4])
{
++count; b.x=x; b.y=y+5;
}
}
}
}
else {b.x=x; b.y=y+1;}
tempAi.count = count;
tempAi.pt = a;
SaveAI(tempAi);
tempAi.pt = b;
SaveAI(tempAi);
if (count > chkcount) chkcount = count;
//向右上查
count = 1; //计数复位
if (x !=0 && y !=9 && m_nGrid[x][y] == m_nGrid[x-1][y+1])
{
++count; a.x=x-2; a.y=y+2;
if (x !=1 && y !=8 && m_nGrid[x][y] == m_nGrid[x-2][y+2])
{
++count; a.x=x-3; a.y=y+3;
if (x !=2 && y !=7 && m_nGrid[x][y] == m_nGrid[x-3][y+3])
{
++count; a.x=x-4; a.y=y+4;
if (x !=3 && y !=6 && m_nGrid[x][y] == m_nGrid[x-4][y+4])
{
++count; a.x=x-5; a.y=y+5;
}
}
}
}
else {a.x=x-1; a.y=y+1;}
//向左下查
if (x !=9 && y !=0 && m_nGrid[x][y] == m_nGrid[x+1][y-1])
{
++count; b.x=x+2; b.y=y-2;
if (x !=8 && y !=1 && m_nGrid[x][y] == m_nGrid[x+2][y-2])
{
++count; b.x=x+3; b.y=y-3;
if (x !=7 && y !=2 && m_nGrid[x][y] == m_nGrid[x+3][y-3])
{
++count; b.x=x+4; b.y=y-4;
if (x !=6 && y !=3 && m_nGrid[x][y] == m_nGrid[x+4][y-4])
{
++count; b.x=x+5; b.y=y-5;
}
}
}
}
else {b.x=x+1; b.y=y-1;}
tempAi.count = count;
tempAi.pt = a;
SaveAI(tempAi);
tempAi.pt = b;
SaveAI(tempAi);
if (count > chkcount) chkcount = count;
//向左上查
count = 1; //计数复位
if (x !=0 && y !=0 && m_nGrid[x][y] == m_nGrid[x-1][y-1])
{
++count; a.x=x-2; a.y=y-2;
if (x !=1 && y !=1 && m_nGrid[x][y] == m_nGrid[x-2][y-2])
{
++count; a.x=x-3; a.y=y-3;
if (x !=2 && y !=2 && m_nGrid[x][y] == m_nGrid[x-3][y-3])
{
++count; a.x=x-4; a.y=y-4;
if (x !=3 && y !=3 && m_nGrid[x][y] == m_nGrid[x-4][y-4])
{
++count; a.x=x-5; a.y=y-5;
}
}
}
}
else {a.x=x-1; a.y=y-1;}
//向右下查
if (x !=9 && y !=9 && m_nGrid[x][y] == m_nGrid[x+1][y+1])
{
++count; b.x=x+2; b.y=y+2;
if (x !=8 && y !=8 && m_nGrid[x][y] == m_nGrid[x+2][y+2])
{
++count; b.x=x+3; b.y=y+3;
if (x !=7 && y !=7 && m_nGrid[x][y] == m_nGrid[x+3][y+3])
{
++count; b.x=x+4; b.y=y+4;
if (x !=6 && y !=6 && m_nGrid[x][y] == m_nGrid[x+4][y+4])
{
++count; b.x=x+5; b.y=y+5;
}
}
}
}
else {b.x=x+1; b.y=y+1;}
tempAi.count = count;
tempAi.pt = a;
SaveAI(tempAi);
tempAi.pt = b;
SaveAI(tempAi);
if (count > chkcount) chkcount = count;
return (chkcount>=5) ? TRUE : FALSE; //返回TRUE表示5颗子相连获胜
}
void CMainWnd::ComputerDown(CPoint &pt)
{
m_nGrid[pt.x][pt.y] = 2;//必须放再DrawBlack前才能清除数组无效点
DrawBlack(m_rcSquares[pt.x][pt.y]);
}
CPoint CMainWnd::GetPosition()
{
int max = 0;
int index = 0;
for (int i=0;i<AIarray.GetSize();++i)
if (AIarray[i].count > max)
{
max = AIarray[i].count;
index = i;
}
return AIarray[index].pt;
}
void CMainWnd::SaveAI(classAI &ai)
{
if (ai.pt.x >= 0 && ai.pt.x <= 9 && ai.pt.y >= 0 &&ai.pt.y <=9
&& m_nGrid[ai.pt.x][ai.pt.y] == 0)
AIarray.Add(ai);
}
afx_msg void CMainWnd::OnButtonClicked()
{
//初始化网格标志数组
for (int ix=0;ix<10;++ix)
for (int iy=0;iy<10;++iy)
m_nGrid[ix][iy] = 0;
//初始化m_cmpturn
m_cmpturn = FALSE;
//AI数组复位
AIarray.RemoveAll();
Invalidate();
}
afx_msg void CMainWnd::OnFirstClicked()
{
//pbtFirst->SetWindowText(_T("人先下"));
classAI ai;
ai.count = 1;
ai.pt.x = 4;
ai.pt.y = 5;
SaveAI(ai);
ComputerDown(ai.pt);
}
afx_msg void CMainWnd::OnSoundClicked()
{
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -