#define OO 1
#define XX 2
#include <afxwin.h>
#include<Windows.h>
#include <iostream>
#include <mmsystem.h>
#include "XXOO.h"
#include "resource.h"
xxooApp theApp;
BOOL xxooApp::InitInstance()
{
m_pMainWnd = new xxooWnd;
m_pMainWnd->ShowWindow(m_nCmdShow);
m_pMainWnd->UpdateWindow();
return TRUE;
}
BEGIN_MESSAGE_MAP(xxooWnd , CFrameWnd)
ON_WM_PAINT()
ON_COMMAND(ID_xExit, &xxooWnd::Onxexit)
ON_COMMAND(ID_xAbout, &xxooWnd::Onxabout)
ON_COMMAND(ID_xNetGame, &xxooWnd::Onxnetgame)
ON_WM_CREATE()
ON_WM_DESTROY()
ON_WM_CLOSE()
ON_WM_LBUTTONDOWN()
ON_COMMAND(ID_xNewGame, &xxooWnd::Onxnewgame)
END_MESSAGE_MAP()
const CRect xxooWnd::m_squre[9] = {
CRect(0 , 0 , 100 , 100) , CRect(110 , 0 , 210, 100), CRect(220 , 0 , 320 , 100),
CRect(0 , 110 , 100 ,210) , CRect(110 , 110 , 210 ,210), CRect(220 , 110 , 320 , 210),
CRect(0 , 220 , 100, 320) , CRect(110 , 220 , 210 ,320), CRect(220 , 220 , 320 , 320),
};
xxooWnd::xxooWnd()
{
CRect rect (0, 0, 330, 365);
Create(0 , _T("XXOO") , WS_CAPTION|WS_SYSMENU, rect , 0 , _T("MENU1") , 0 , 0);
}
void xxooWnd::OnPaint()
{
CPaintDC dc (this);
DrawBoard(&dc);
}
void xxooWnd::DrawBoard(CDC* pDC)
{
CPen pen (PS_SOLID, 10, RGB (0, 0, 0));
CPen* pOldPen = pDC->SelectObject (&pen);
pDC->MoveTo (105, 5);
pDC->LineTo (105, 310);
pDC->MoveTo (215, 5);
pDC->LineTo (215, 310);
pDC->MoveTo (5, 105);
pDC->LineTo (315, 105);
pDC->MoveTo (5, 215);
pDC->LineTo (315, 215);
for (int i=0; i<9; i++)
{
if (m_grid[i] == OO)
DrawO (pDC, i);
else if (m_grid[i] == XX)
DrawX (pDC, i);
}
pDC->SelectObject (pOldPen);
}
void xxooWnd::Onxexit()
{
xxooWnd::OnClose();
// TODO: 在此添加命令处理程序代码
}
void xxooWnd::Onxabout()
{
CDialog dig(IDD_xABOUT,this);
dig.DoModal();
// TODO: 在此添加命令处理程序代码
}
void xxooWnd::Onxnetgame()
{
CDialog dig_net(IDD_xNET,this);
dig_net.DoModal();
// TODO: 在此添加命令处理程序代码
}
int xxooWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
ReGame();
PlaySound(_T("Start.wav"),0,0);
// TODO: 在此添加您专用的创建代码
return 0;
}
void xxooWnd::OnClose()
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
CFrameWnd::OnClose();
PlaySound(_T("End.wav") , 0 , 0);
}
void xxooWnd::DrawO(CDC* pDC, int SqNpos)
{
CPen pen(PS_SOLID , 16, RGB(0 , 0, 255));
CPen* OldPen = pDC->SelectObject(&pen);
pDC->SelectStockObject(NULL_BRUSH);
CRect rect = m_squre[SqNpos];
rect.DeflateRect(16 , 16);
pDC->Ellipse(rect);
pDC->SelectObject(OldPen);
}
void xxooWnd::DrawX(CDC* pDC, int SqNpos)
{
CPen pen(PS_SOLID , 16, RGB(0 , 0, 255));
CPen* OldPen = pDC->SelectObject(&pen);
pDC->SelectStockObject(NULL_BRUSH);
CRect rect = m_squre[SqNpos];
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(OldPen);
}
int xxooWnd::GetRectID(CPoint point)
{
for (int i=0; i<9; i++)
{
if (m_squre[i].PtInRect (point))
{
return i;
}
}
return -1;
}
void xxooWnd::OnLButtonDown(UINT nFlags, CPoint point)
{
CClientDC dc (this);
if (m_nextone == XX)
{
int npos = GetRectID(point);
if (npos == -1 || (m_grid[npos] != 0))
{
return;
}
DrawO(&dc , npos);
m_grid[npos] = OO;
m_nextone = OO;
GameOver();
return;
}
if (m_nextone == OO)
{
int npos = GetRectID(point);
if (npos == -1 || (m_grid[npos] != 0))
{
return;
}
DrawX(&dc , npos);
m_grid[npos] = XX;
m_nextone = XX;
GameOver();
return;
}
}
int xxooWnd::WhoWin(void)
{
static int nPattern[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
};
for (int i=0; i<8; i++) {
if ((m_grid[nPattern[i][0]] == OO) && // OO win
(m_grid[nPattern[i][1]] == OO) &&
(m_grid[nPattern[i][2]] == OO))
return OO;
if ((m_grid[nPattern[i][0]] == XX) && //XX win
(m_grid[nPattern[i][1]] == XX) &&
(m_grid[nPattern[i][2]] == XX))
return XX;
}
return 0;
}
void xxooWnd::GameOver(void)
{
int nWinner;
if (nWinner =WhoWin())
{
CString str = (nWinner==OO ) ? _T("OO WIN O(∩_∩)O") : _T("XX WIN (*^__^*) ");
AfxMessageBox(str , MB_OK);
ReGame();
}
else if (IsDraw())
{
AfxMessageBox(_T("平了~") , MB_OK);
ReGame();
}
}
void xxooWnd::ReGame(void)
{
m_nextone = XX;
::ZeroMemory (m_grid, 9 * sizeof (int));
Invalidate ();
}
void xxooWnd::Onxnewgame()
{
ReGame();
// TODO: 在此添加命令处理程序代码
}
bool xxooWnd::IsDraw(void)
{
for (int i=0; i<9; i++) {
if (m_grid[i] == 0)
return FALSE;
}
return TRUE;
}
没有合适的资源?快使用搜索试试~ 我知道了~
一个MFC写的XXOO的游戏-
共17个文件
wav:3个
h:2个
ico:2个
需积分: 12 31 下载量 148 浏览量
2008-11-16
12:54:15
上传
评论
收藏 3.81MB RAR 举报
温馨提示
MFC写的,还有些功能未实现,可以XXOO了~
资源推荐
资源详情
资源评论
收起资源包目录
XXOO.rar (17个子文件)
XXOO
XXOO.sln 877B
XXOO.suo 45KB
XXOO.ncb 15.53MB
XXOO
Start.wav 178KB
resource.h 2KB
XXOO.vcproj 5KB
XXOO1.rc 4KB
XO.wav 117KB
ICON1.ico 158KB
XXOO1.APS 870KB
ICON.ico 31KB
XXOO.vcproj.GOOGLE.Dai.user 1KB
desktop.ini 133B
XXOO.gif 97KB
End.wav 381KB
XXOO.cpp 5KB
res
XXOO.h 822B
共 17 条
- 1
资源评论
lehomedai
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功