📄 firstdlg.cpp
字号:
// FirstDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Guess.h"
#include "FirstDlg.h"
#include<stdlib.h>
#include<string.h>
#include<time.h>
#define SIZE 4
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CFirstDlg dialog
CFirstDlg::CFirstDlg(CWnd* pParent /*=NULL*/)
: CDialog(CFirstDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CFirstDlg)
m_strEdit1 = _T("");
m_strEdit2 = _T("");
//}}AFX_DATA_INIT
}
void CFirstDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CFirstDlg)
DDX_Control(pDX, IDC_LIST1, m_ListBox2);
DDX_Control(pDX, IDC_BUTTON3, m_RelBtn3);
DDX_Control(pDX, IDC_BUTTON2, m_RelBtn2);
DDX_Control(pDX, IDC_BUTTON1, m_RelBtn1);
DDX_Text(pDX, IDC_EDIT1, m_strEdit1);//将IDC_EDIT1与m_strEdit1进行数据交换
DDV_MaxChars(pDX, m_strEdit1, 4); // 校验m_strEdit1的最大字符个数不超过4
DDX_Text(pDX, IDC_EDIT2, m_strEdit2);//将IDC_EDIT1与m_strEdit1进行数据交换
DDV_MaxChars(pDX, m_strEdit2, 4); // 校验m_strEdit2的最大字符个数不超过4
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CFirstDlg, CDialog)
//{{AFX_MSG_MAP(CFirstDlg)
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
ON_LBN_SELCHANGE(IDC_LIST1, OnSelchangeList1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CFirstDlg message handlers
char b1[SIZE]={'\0'},c1[5]={'0','A','0','B'},d1[1]={'\0'};//b对应随机数,a对应玩家,c对应于判断结果
int counter1=1,countera1=0,counterb1=0;
int againsign1=-2,ansersign1=-1;//全局变量
void CFirstDlg::OnButton1()
{
// TODO: Add your control notification handler code here
if(againsign1==-2)
{
MessageBox("请单击新游戏开始游戏!");
return;
}
if(againsign1==1)//判断是否重新开始,若是则清空编辑框1
{
m_strEdit1.Format("%s",d1);
UpdateData(false);
return;
}
if(ansersign1==1)//判断答案是否已经给出
{
MessageBox("答案给出,继续游戏请单击新游戏!");
return;
}
int i,j,counter0;
char a1[SIZE+1]={'\0'};
countera1=0,counterb1=0;
UpdateData(); // 默认参数值是TRUE
strcpy(a1,m_strEdit1);
for(i=0;i<SIZE;i++)//判断玩家输入数字是否符合要求
{
if(a1[i]>'9'||a1[i]<'0')
{
MessageBox("你的输入有误!\n请重新输入!");
return;
}
for(j=i+1;j<SIZE;j++)
if(a1[j]==a1[i])
{
MessageBox("你的输入有误!\n请重新输入!");
return;
}
}
for(i=0;i<=SIZE-1;i++)//求大小和位置都正确的数字个数
if(a1[i]==b1[i])
countera1++;
counter0=0;
for(i=0;i<=SIZE-1;i++)
{
for(j=0;j<=SIZE-1;j++)
if(a1[i]==b1[j])
counter0++;
}
counterb1=(counter0-countera1);//求只有大小正确的数字个数
c1[0]=countera1+48;
c1[2]=counterb1+48;
counter1++;//判断次数加1
if(countera1==4)
{
if(counter1<10)
{
MessageBox("恭喜你取得胜利!");
CFirstDlg::OnSelchangeList1() ;//使列表框显示判断结果
m_ListBox2.InsertString( -1, a1);
counter1=10;
}
else
MessageBox(" 此局胜负已分\n若要继续,请单击重新开始!");
}
else if(counter1==9)
{
MessageBox("很抱歉,你输了!");
counter1++;
}
else if(counter1>9)
MessageBox("超过了允许判断的次数\n要重新开始请按重新开始!");
else
{
MessageBox("请再接再厉!");
m_ListBox2.InsertString( -1, a1);
CFirstDlg::OnSelchangeList1() ;//使列表框显示判断结果
}
}
void CFirstDlg::OnButton2()
{
// TODO: Add your control notification handler code here
if(againsign1==1)
{
m_strEdit2.Format("%s",d1);
UpdateData(false);
againsign1=-1;
return;
}
if(againsign1==-2||ansersign1==1)
{
MessageBox("请单击新游戏开始游戏!");
return;
}
m_strEdit2.Format("%s",b1);
UpdateData(false);
againsign1=-1;
ansersign1=1;
}
void CFirstDlg::OnButton3()
{
// TODO: Add your control notification handler code here
counter1=1;
ansersign1=-1;
againsign1=1;//改变标记
CFirstDlg::OnButton1();//清空编辑框1
CFirstDlg::OnButton2();//清空编辑框2
m_ListBox2.ResetContent();//清空列表框
int i,j,stop=1,temp[SIZE],extra;
srand(time(NULL));
while(stop==1)//获得SIZE个不相同的随机数,保存在b中
{
extra=1;
for(i=0;i<=SIZE-1;i++)
temp[i]=rand()%10;//获得随机数
for(i=0;i<=SIZE-1;i++)
{
for(j=i+1;j<=SIZE-1;j++)
{
if(temp[i]==temp[j]) extra=2;
}
}
if(temp[0]!=0&&extra==1)
{
stop=1;
for(i=0;i<=SIZE-1;i++)
b1[i]=temp[i]+48;
stop=2;
}
}
}
void CFirstDlg::OnSelchangeList1()
{
// TODO: Add your control notification handler code here
m_ListBox2.InsertString( -1, c1);
UpdateData(false);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -