📄 guessthenumdlg.cpp
字号:
// GuessTheNumDlg.cpp : implementation file
//
#include "stdafx.h"
#include "GuessTheNum.h"
#include "GuessTheNumDlg.h"
int RandNum;//随机数
int Num[4];//保存生成的数字的数组
int count=10;//猜数的次数
int GameState;
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CGuessTheNumDlg dialog
CGuessTheNumDlg::CGuessTheNumDlg(CWnd* pParent /*=NULL*/)
: CDialog(CGuessTheNumDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CGuessTheNumDlg)
m_thousand = 0;
m_hundred = 0;
m_tenth = 0;
m_single = 0;
m_bs = 0;
m_as = 0;
m_Num = 0;
m_score = 0;
m_times = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CGuessTheNumDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CGuessTheNumDlg)
DDX_Text(pDX, IDC_EDIT_THOUSAND, m_thousand);
DDV_MinMaxInt(pDX, m_thousand, 0, 9);
DDX_Text(pDX, IDC_EDIT_HUNDRED, m_hundred);
DDV_MinMaxInt(pDX, m_hundred, 0, 9);
DDX_Text(pDX, IDC_EDIT_TEN_DIGIT, m_tenth);
DDV_MinMaxInt(pDX, m_tenth, 0, 9);
DDX_Text(pDX, IDC_EDIT_SINGLE_DIGIT, m_single);
DDV_MinMaxInt(pDX, m_single, 0, 9);
DDX_Text(pDX, IDC_BS, m_bs);
DDV_MinMaxInt(pDX, m_bs, 0, 4);
DDX_Text(pDX, IDC_AS, m_as);
DDV_MinMaxInt(pDX, m_as, 0, 4);
DDX_Text(pDX, IDC_EDIT1, m_Num);
DDV_MinMaxInt(pDX, m_Num, 0, 9999);
DDX_Text(pDX, IDC_EDIT_SCORE, m_score);
DDX_Text(pDX, IDC_EDIT_TIMES, m_times);
DDV_MinMaxInt(pDX, m_times, 0, 10);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CGuessTheNumDlg, CDialog)
//{{AFX_MSG_MAP(CGuessTheNumDlg)
ON_BN_CLICKED(IDC_NEWGAME, OnNewgame)
ON_BN_CLICKED(IDC_COMPARE, OnCompare)
ON_EN_CHANGE(IDC_EDIT_HUNDRED, OnChange)
ON_BN_CLICKED(IDC_OVER, OnOver)
ON_EN_CHANGE(IDC_EDIT_SINGLE_DIGIT, OnChange)
ON_EN_CHANGE(IDC_EDIT_TEN_DIGIT, OnChange)
ON_EN_CHANGE(IDC_EDIT_THOUSAND, OnChange)
ON_BN_CLICKED(IDC_DISPLAYNUM, OnDisplaynum)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CGuessTheNumDlg message handlers
BOOL CGuessTheNumDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 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
CenterWindow(GetDesktopWindow()); // center to the hpc screen
// TODO: Add extra initialization here
GetDlgItem(IDC_COMPARE)->EnableWindow(GameState);
GetDlgItem(IDC_DISPLAYNUM)->EnableWindow(GameState);
return TRUE; // return TRUE unless you set the focus to a control
}
int random()
{
int t;
//产生0000~9999之间四位不重复的随机数,并且判断是否有重复数字
while(1)
{
RandNum=Random()%9000+1000;
t=RandNum;
for(int i=0;i<=3;i++)
{
Num[i]=t%10;
t/=10;
}
for(i=0;i<=3;i++)
{
for(int j=0;j<=3;j++)
{
if(i!=j)
{
if(Num[i]==Num[j]) t=1; //碰到重复数字
}
}
}
if(t==1)continue; //碰到重复数字,则重新生成随机数
else break;
}
return RandNum;
}
void CGuessTheNumDlg::OnNewgame()
{
// TODO: Add your control notification handler code here
random();
GameState=1;
m_thousand = 0;
m_hundred = 0;
m_tenth = 0;
m_single = 0;
m_Num=0;
m_score=0;
m_bs = 0;
m_as = 0;
count=10;
m_times=count;
UpdateData(FALSE);
GetDlgItem(IDC_COMPARE)->EnableWindow(GameState);
GetDlgItem(IDC_DISPLAYNUM)->EnableWindow(GameState);
}
void CGuessTheNumDlg::OnCompare()
{
// TODO: Add your control notification handler code here
m_bs = 0;
m_as = 0;
UpdateData(FALSE);
int i,j,t;
int a[4];
UpdateData(TRUE);
//判断是否输入相同的数字
while(1)
{
t=0;
int temp[4]={m_single,m_tenth,m_hundred,m_thousand};
for(i=0;i<=3;i++)
a[i]=temp[i];
for(i=0;i<=3;i++)
{
for(j=0;j<=3;j++)
{
if(i!=j)
{
if(a[i]==a[j]) t=1; //碰到相同的数字
}
}
}
if(t==1)
{
AfxMessageBox(_T("repeated number")); //重复则跳出,等待下一次输入
break;
}
else //不重复,开始比较
{
count--;
m_times=count;
UpdateData(FALSE);
for(int i=0;i<=3;i++) //比较
{
for(int j=0;j<=3;j++)
{
if(a[i]==Num[j]&&i==j)
{
m_as++;
m_score+=10;
}
else if(a[i]==Num[j]&&i!=j)
{
m_bs++;
m_score+=5;
}
}
}
if(m_as==4) //全部猜对
{
m_Num=RandNum;
UpdateData(FALSE);
AfxMessageBox(_T("right"));
OnNewgame();
break;
}
if(count==0) //次数用完
{
AfxMessageBox(_T("game over!you have used up all your chances"));
OnNewgame();//重新开始
break;
}
break;
}
}
UpdateData(FALSE);
}
void CGuessTheNumDlg::OnChange()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
UpdateData(TRUE);
}
void CGuessTheNumDlg::OnOver()
{
// TODO: Add your control notification handler code here
OnOK();
}
void CGuessTheNumDlg::OnDisplaynum()
{
// TODO: Add your control notification handler code here
if(count<=3)
{
m_score=0;
m_Num=RandNum;
UpdateData(FALSE);
}
else
AfxMessageBox(_T("You still have over three times,try again"));
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -