program71dlg.cpp
来自「单机人对机器的游戏,共有23块宝石,两个人轮流取,看谁得到最后一块最大的为胜利者」· C++ 代码 · 共 355 行
CPP
355 行
// program71Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "program71.h"
#include "program71Dlg.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 };
//}}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()
};
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CProgram71Dlg dialog
CProgram71Dlg::CProgram71Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CProgram71Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CProgram71Dlg)
m_Caption1 = _T("");
m_Caption2 = _T("");
m_Caption3 = _T("");
m_TXT = _T("");
m_Radio = -1;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CProgram71Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CProgram71Dlg)
DDX_Control(pDX, IDC_BUTSTART, m_IconStart);
DDX_Control(pDX, IDC_BUTOK, m_IconOk);
DDX_Control(pDX, IDC_BUTHELP, m_IconHelp);
DDX_Control(pDX, IDC_BUTEXIT, m_IconExit);
DDX_Text(pDX, IDC_EDIT1, m_Caption1);
DDX_Text(pDX, IDC_EDIT2, m_Caption2);
DDX_Text(pDX, IDC_EDIT3, m_Caption3);
DDX_Text(pDX, IDC_EDITTXT, m_TXT);
DDX_Radio(pDX, IDC_RADIO1, m_Radio);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CProgram71Dlg, CDialog)
//{{AFX_MSG_MAP(CProgram71Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTEXIT, OnButexit)
ON_BN_CLICKED(IDC_BUTOK, OnButok)
ON_BN_CLICKED(IDC_BUTHELP, OnButhelp)
ON_BN_CLICKED(IDC_BUTSTART, OnButstart)
ON_BN_CLICKED(IDC_RADIO3, OnRadio3)
ON_BN_CLICKED(IDC_RADIO2, OnRadio2)
ON_BN_CLICKED(IDC_RADIO1, OnRadio1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CProgram71Dlg message handlers
BOOL CProgram71Dlg::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
OnButstart() ;
return TRUE; // return TRUE unless you set the focus to a control
}
void CProgram71Dlg::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 CProgram71Dlg::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();
}
//为按钮设置图标
HICON hIcon1=AfxGetApp()->LoadIcon(IDI_BUT1);
m_IconStart.SetIcon(hIcon1);
HICON hIcon2=AfxGetApp()->LoadIcon(IDI_BUT2);
m_IconOk.SetIcon(hIcon2);
HICON hIcon3=AfxGetApp()->LoadIcon(IDI_BUT3);
m_IconExit.SetIcon(hIcon3);
HICON hIcon4=AfxGetApp()->LoadIcon(IDI_BUT4);
m_IconHelp.SetIcon(hIcon4);
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CProgram71Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CProgram71Dlg::OnRadio2()
{
m_Radio=1;
}
void CProgram71Dlg::OnRadio1()
{
m_Radio=0;
}
void CProgram71Dlg::OnRadio3()
{
m_Radio=2;
}
void CProgram71Dlg::OnButexit()
{
CDialog::OnCancel();
}
void CProgram71Dlg::OnButok()
{
CString csTmp=m_TXT;
int n;
switch(m_nStart)
{
case 1:
switch(m_Radio)
{
case 0:
m_TXT="你想先取?那就开始吧!\r\n";
m_bOrder=true;
break;
case 1:
m_TXT="让Betty先取?好吧,开始!\r\n";
m_bOrder=false;
break;
case 2:
srand((unsigned)time( NULL));
if(rand()%2>0)
{
m_TXT="扔硬币?\r\n正面朝上,你先取。\r\n";
m_bOrder=true;
}
else
{
m_TXT="扔硬币?\r\n背面朝上,Betty先取。\r\n";
m_bOrder=false;
}
default:
break;
// return;
}
m_Caption1="取一块";
m_Caption2="取两块";
m_Caption3="取三块";
m_nStart=2;
m_Radio=0;
m_nTotal=23;
break;
case 2:
if(m_bOrder)
{
n=GetYou();
if(m_nTotal>0)
m_TXT.Format("%s你取了%d块宝石,还剩%d块\r\n",csTmp,n,m_nTotal);
else
{
m_TXT.Format("%s你取了%d块宝石.\r\n你拿到了最大的一块! 哈.....",csTmp,n);
m_nStart=0;
}
csTmp=m_TXT;
m_bOrder=FALSE;
}
if(m_nTotal>0)
{
n=GetBetty();
if(m_nTotal>0)
m_TXT.Format("%sBetty取了%d块宝石,还剩%d块\r\n",csTmp,n,m_nTotal);
else
{
m_TXT.Format("%sBetty取了%d块宝石.\r\n她拿到了最大的一块,\r\n你输了,Wu..Wu.....",csTmp,n);
m_nStart=0;
}
m_bOrder=TRUE;
}
break;
default:
break;
}
UpdateData(FALSE);
}
void CProgram71Dlg::OnButhelp()
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
void CProgram71Dlg::OnButstart()
{
m_Caption1="你先取";
m_Caption2="Betty先取";
m_Caption3="扔硬币吧";
m_nStart=1;
m_Radio=0;
m_TXT=" 你和朋友Betty出去探险,一起在山中找到一堆宝石,总共有23块,
其中最大的一块价值远远超出了其他的的宝石。怎么分呢?\r\n
你们决定由双方的智慧来分取最大的一块。按约定,每人每次可以取1~3块,不能不取,
也不能多取,最大的一块必须是最后一次取。\r\n 好了,开始发挥你的智慧吧。";
UpdateData(FALSE);
}
int CProgram71Dlg::GetYou()
{
int n;
switch(m_Radio)
{
case 0:
n=1;
break;
case 1:
n=2;
break;
default:
n=3;
}
if(n>m_nTotal)
n=m_nTotal;
m_nTotal=m_nTotal-n;
return n;
}
int CProgram71Dlg::GetBetty()
{
int n;
if(m_nTotal%4==0)
{
srand((unsigned)time( NULL));
n=rand()%3+1;
}
else
n=m_nTotal%4;
m_nTotal=m_nTotal-n;
return n;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?