//MFC_xoDLG.h
// MFC_xoDlg.h : 头文件
//
#pragma once
// CMFC_xoDlg 对话框
class CMFC_xoDlg : public CDialog
{
// 构造
public:
CMFC_xoDlg(CWnd* pParent = NULL); // 标准构造函数
// 对话框数据
enum { IDD = IDD_MFC_XO_DIALOG };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 实现
protected:
HICON m_hIcon;
// 生成的消息映射函数
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnBnClickedButton1();
afx_msg void OnBnClickedButton2();
afx_msg void OnBnClickedButton3();
afx_msg void OnBnClickedButton4();
afx_msg void OnBnClickedButton5();
afx_msg void OnBnClickedButton6();
afx_msg void OnBnClickedButton7();
afx_msg void OnBnClickedButton8();
afx_msg void OnBnClickedButton9();
// 用来记录当前显示的是X还是O
// X的值用0表示,O的值用1表示
// player1 是X,player2 是O
// used 初始值为0,即先显示X
int used;
//用来记录对应的棋子的点击情况
char chess[3][3];//={'1','2','3', '4','5','6', '7','8','9'};
//用来记录下棋的个数
static int k;
int r1,c1;
int r2,c2;
// 用来判断谁赢了游戏
void CJudgeWinner(void);
};
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//MFC_xoDLG.cpp
// MFC_xoDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "MFC_xo.h"
#include "MFC_xoDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// 用于应用程序“关于”菜单项的 CAboutDlg 对话框
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// 对话框数据
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 实现
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()
// CMFC_xoDlg 对话框
CMFC_xoDlg::CMFC_xoDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMFC_xoDlg::IDD, pParent)
, used(0),r1(0),c1(0),r2(0),c2(0)
{
//chess[3][3]={{'1','2','3'}, {'4','5','6'}, {'7','8','9'}};
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
/////////////////////////////////////
//初始化k
int CMFC_xoDlg::k=0;
/////////////////////////////////////
void CMFC_xoDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CMFC_xoDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDC_BUTTON1, &CMFC_xoDlg::OnBnClickedButton1)
ON_BN_CLICKED(IDC_BUTTON2, &CMFC_xoDlg::OnBnClickedButton2)
ON_BN_CLICKED(IDC_BUTTON3, &CMFC_xoDlg::OnBnClickedButton3)
ON_BN_CLICKED(IDC_BUTTON4, &CMFC_xoDlg::OnBnClickedButton4)
ON_BN_CLICKED(IDC_BUTTON5, &CMFC_xoDlg::OnBnClickedButton5)
ON_BN_CLICKED(IDC_BUTTON6, &CMFC_xoDlg::OnBnClickedButton6)
ON_BN_CLICKED(IDC_BUTTON7, &CMFC_xoDlg::OnBnClickedButton7)
ON_BN_CLICKED(IDC_BUTTON8, &CMFC_xoDlg::OnBnClickedButton8)
ON_BN_CLICKED(IDC_BUTTON9, &CMFC_xoDlg::OnBnClickedButton9)
END_MESSAGE_MAP()
// CMFC_xoDlg 消息处理程序
BOOL CMFC_xoDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 将“关于...”菜单项添加到系统菜单中。
// IDM_ABOUTBOX 必须在系统命令范围内。
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);
}
}
// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
// TODO: 在此添加额外的初始化代码
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
void CMFC_xoDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。
void CMFC_xoDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// 使图标在工作区矩形中居中
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;
// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CMFC_xoDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
//////////////////////////////////////////
//初始化 used 为0;
//used=0; 低级错误啊,应该在构造函数中初始化嘛!!!!!!!
//////////////////////////////////////////
void CMFC_xoDlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
CWnd* pWnd = GetDlgItem(IDC_BUTTON1);
if(used==0)
{
pWnd->SetWindowText(_T("X"));
used=1;
CMFC_xoDlg::chess[0][0]='X';
k++;
}
else if(used==1)
{
pWnd->SetWindowText(_T("O"));
used=0;
CMFC_xoDlg::chess[0][0]='O';
k++;
}
CJudgeWinner();
}
void CMFC_xoDlg::OnBnClickedButton2()
{
// TODO: 在此添加控件通知处理程序代码
CWnd* pWnd = GetDlgItem(IDC_BUTTON2);
if(used==0)
{
pWnd->SetWindowText(_T("X"));
used=1;
CMFC_xoDlg::chess[0][1]='X';
k++;
}
else if(used==1)
{
pWnd->SetWindowText(_T("O"));
used=0;
CMFC_xoDlg::chess[0][1]='O';
k++;
}
CJudgeWinner();
}
void CMFC_xoDlg::OnBnClickedButton3()
{
// TODO: 在此添加控件通知处理程序代码
CWnd* pWnd = GetDlgItem(IDC_BUTTON3);
if(used==0)
{
pWnd->SetWindowText(_T("X"));
used=1;
CMFC_xoDlg::chess[0][2]='X';
k++;
}
else if(used==1)
{
pWnd->SetWindowText(_T("O"));
used=0;
CMFC_xoDlg::chess[0][2]='O';
k++;
}
CJudgeWinner();
}
void CMFC_xoDlg::OnBnClickedButton4()
{
// TODO: 在此添加控件通知处理程序代码
CWnd* pWnd = GetDlgItem(IDC_BUTTON4);
if(used==0)
{
pWnd->SetWindowText(_T("X"));
used=1;
CMFC_xoDlg::chess[1][0]='X';
k++;
}
else if(used==1)
{
pWnd->SetWindowText(_T("O"));
used=0;
CMFC_xoDlg::chess[1][0]='O';
k++;
}
CJudgeWinner();
}
void CMFC_xoDlg::OnBnClickedButton5()
{
// TODO: 在此添加控件通知处理程序代码
CWnd* pWnd = GetDlgItem(IDC_BUTTON5);
if(used==0)
{
pWnd->SetWindowText(_T("X"));
used=1;
CMFC_xoDlg::chess[1][1]='X';
k++;
}
else if(used==1)
{
pWnd->SetWindowText(_T("O"));
used=0;
CMFC_xoDlg::chess[1][1]='O';
k++;
}
CJudgeWinner();
}
void CMFC_xoDlg::OnBnClickedButton6()
{
// TODO: 在此添加控件通知处理程序代码
CWnd* pWnd = GetDlgItem(IDC_BUTTON6);
if(used==0)
{
pWnd->SetWindowText(_T("X"));
used=1;
CMFC_xoDlg::chess[1][2]='X';
k++;
}
else if(used==1)
{
pWnd->SetWindowText(_T("O"));
used=0;
CMFC_xoDlg::chess[1][2]='O';
k++;
}
CJudgeWinner();
}
void CMFC_xoDlg::OnBnClickedButton7()
{
// TODO: 在此添加控件通知处理程序代码
CWnd* pWnd = GetDlgItem(IDC_BUTTON7);
if(used==0)
{
pWnd->SetWindowText(_T("X"));
used=1;
CMFC_xoDlg::chess[2][0]='X';
k++;
}
else if(used==1)
{
pWnd->SetWindowText(_T("O"));
used=0;
CMFC_xoDlg::chess[2][0]='O';
k++;
}
CJudgeWinner();
}
void CMFC_xoDlg::OnBnClickedButton8()
{
// TODO: 在此添加控件通知处理程序代码
CWnd* pWnd = GetDlgItem(IDC_BUTTON8);
if(used==0)
{
pWnd->SetWindowText(_T("X"));
used=1;
CMFC_xoDlg::chess[2][1]='X';
k++;
}
else if(used==1)
{
pWnd->SetWindowText(_T("O"));
used=0;
CMFC_xoDlg::chess[2][1]='O';
k++;
}
CJudgeWinner();
}
void CMFC_xoDlg::OnBnClickedButton9()
{
// TODO: 在此添加控件通知处理程序代码
CWnd* pWnd = GetDlgItem(IDC_BUTTON9);
if(used==0)
{
pWnd->SetWindowText(_T("X"));
used=1;
CMFC_xoDlg::chess[2][2]='X';
k++;
}
else if(used==1)
{
pWnd->SetWindowText(_T("O"));
used=0;
CMFC_xoDlg::chess[2][2]='O';
k++;
}
CJudgeWinner();
}
// 用来判断谁赢了游戏
void CMFC_xoDlg::CJudgeWinner(void)
{
///////////////////////////////////////////////////////////////////////////
//judge who wins:
if(k>=2)//下了至少3个棋子才开始判断
{
//////////////////////////////////////////////////////////////////
//judge if player2 wins
while(r1<3){
if(chess[0][r1]=='O'&&chess[1][r1]=='O'&&chess[2][r1]=='O'){
MessageBox(_T("player2 wins!"));
//break;
}
else
r1++;
}
while(c1<3){
if(chess[c1][0]=='O'&&chess[c1][1]=='O'&&chess[c1][2]=='O'){
MessageBox(_T("player2 wins!"));
// break;
}
else
c1++;
}//while(c1<3)
if(chess[0][0]=='O'&&chess[1][1]=='O'&&chess[2][2]=='O'){
MessageBox(_T("player2 wins!"));
//break;
}
if(chess[0][2]=='O'&&chess[1][1]=='O'&&chess[2][0]=='O'){
MessageBox(_T("player2 wins!"));
//break;
}
r1=0;
c1=0;
////////////////////////////////////
//judge if player1 wins
while(r2<3){
if(chess[0][r2]=='X'&&chess[1][r2]=='X'&&chess[2][r2]=='X'){
MessageBox(_T("player 1 wins!"));
//break;
}
else
r2++;
}
while(c2<3){
if(chess[c2][0]=='X'&&chess[c2][1]=='X'&&chess[c2][2]=='X'){
MessageBox(_T("player 1 wins!"));
//break;
}
else
c2++;
}//while(c2<3)
if(chess[0][0]=='X'&&chess[1][1]=='X'&&chess[2][2]=='X'){
MessageBox(_T("player 1 wins!"));
//break;
}
if(chess[0][2]=='X'&&chess[1][1]=='X'&&chess[2][0]=='X'){
MessageBox(_T("player 1 wins!"));
//break;
}
r2=0;
c2=0;
} //if(k>=2)
}
评论