利用rand()函数,随机生成下号码模拟一种娱乐游戏
首先了解下游戏规则:
游戏采用两个号码区组成,从红球号码区的【1~ 33】个号码中选择6个号码,从蓝球号码区的【1~16】个号码里选择1个号码组成1组号码。游戏方式有自选一组和随机选一组到几组,也可以选择三种复式组合方式:
- 红球复式
从红球号码中选择7-20个号码,从蓝色球号码里选择一个号码,组合成多组号码 - 蓝球复式
从红球号码中选择6个号码,从蓝色球号码里选择2-16号码,组合成多组号码 - 全复式
从红球号码中选择7-20个号码,从蓝色球号码里选择2-16号码,组合成多组号码
识其规则,接下来着手搞一下。
1)、7个号码,设计7个文本框用于显示号码。一个按钮用于调用生成随机数指令。六个号码随机数产生于1~ 33,一个号码随机数产自1~16;
int red1 = rand() % (33 - 1 + 1) + 1;
int red2 = rand() % (33 - 1 + 1) + 1;
int red3 = rand() % (33 - 1 + 1) + 1;
int red4 = rand() % (33 - 1 + 1) + 1;
int red5 = rand() % (33 - 1 + 1) + 1;
int red6 = rand() % (33 - 1 + 1) + 1;
int blue1 = rand() % (16 - 1 + 1) + 1;
2)、三种组合方式设计
首先根据规则设置要取的号码数量,如红球复式方式,需要手动去选择【7~20】个号码,确认完以后,就开始随机组合,并显示。然后可根据显示的组合进行选取有眼缘的号码进行比大小。
模式选择界面设计如下
设计显示区域,这里使用了List Box控件进行显示。主界面如下
程序设计
主界面显示
// DoubleBallrandNumber.cpp: 实现文件
BOOL DoubleBallrandNumber::OnInitDialog()
{
CDialogEx::OnInitDialog();
// TODO: 在此添加额外的初始化
mybrush.CreateSolidBrush(RGB(87, 192,255));
//Edit_red1.SetBkColor(RGB(255, 0, 0));
myFont.CreatePointFont(150, _T("宋体"));
Redball1.SetFont(&myFont);
Redball2.SetFont(&myFont);
Redball3.SetFont(&myFont);
Redball4.SetFont(&myFont);
Redball5.SetFont(&myFont);
Redball6.SetFont(&myFont);
Redball7.SetFont(&myFont);
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}
摇一摇按钮响应事件
void DoubleBallrandNumber::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
int red1 = rand() % (33 - 1 + 1) + 1;
SetDlgItemInt(IDC_STATIC_1, red1);
int red2 = rand() % (33 - 1 + 1) + 1;
CString strred2; strred2.Format(_T("%d"),red2);
SetDlgItemTextW(IDC_STATIC_2, strred2);
int red3 = rand() % (33 - 1 + 1) + 1;
SetDlgItemInt(IDC_STATIC_3, red3);
int red4 = rand() % (33 - 1 + 1) + 1;
SetDlgItemInt(IDC_STATIC_4, red4);
int red5 = rand() % (33 - 1 + 1) + 1;
SetDlgItemInt(IDC_STATIC_5, red5);
int red6 = rand() % (33 - 1 + 1) + 1;
SetDlgItemInt(IDC_STATIC_6, red6);
int blue1 = rand() % (16 - 1 + 1) + 1;
SetDlgItemInt(IDC_STATIC_7, blue1);
}
红球复式按钮响应事件,这里使用了for循环进行随机数的筛选组合
蓝球复式按钮响应事件
//蓝球复式
//从红球号码中选择6个号码,从蓝色球号码里选择2-16号码,组合成多注投注号码
void DoubleBallrandNumber::OnBnClickedButtonBlue()
{
// TODO: 在此添加控件通知处理程序代码
DatarulesDlg dlg;
dlg.flag = 2;
dlg.DoModal();
if (1 > dlg.blueballnum)
{
MessageBox(L"蓝球数量选择异常,请重新选择");
return;
}
CString strredball, strblueball, strshow;
for (int i = 0; i < 6; i++)
{
int _red = rand() % (33 - 1 + 1) + 1;
CString str; str.Format(_T("%d "), _red);
strredball += str;
}
vector<CString>vecblueball;
for (int i = 0; i < dlg.blueballnum; i++)
{
int _blue = rand() % (16 - 1 + 1) + 1;
strblueball.Format(_T("%d "), _blue);
vecblueball.push_back(strblueball);
}
NumberList2.ResetContent();
for (int i = 0; i < vecblueball.size(); i++)
{
strshow = strredball + vecblueball[i];
NumberList2.AddString(strshow);
}
}
全复式按钮响应事件
演示效果
摇一摇,随机双色球号码
红球复式
蓝球复式
全复式