//powerball.cpp
#include <iostream>
#include <string>
#include "Functions.h"
using namespace std;
int main()
{
cout <<"Powerball Demo Program"<<endl
<<"This program simulates the powerball game. User can choose a powerball "
<<"(range from 1 to 42) and five white balls (range from 1 to 55, no repeat)" <<endl;
int userBall[6];
int systemBall[6];
bool checkmate;
string ans;
do
{
bool valid=AskForPowerball(userBall);
if (valid)
{
SystemPowerball(systemBall); //到这句就不动了,就像进入了死循环
checkmate=CheckMatch(userBall, systemBall);
WriteCheck(checkmate, userBall, systemBall);
}
cout <<"\nDo you want to play again? (yes/no) ";
getline(cin, ans);
}while (ans=="yes");
return 0;
}
//Functions.h
bool AskForPowerball(int userBall[], int max=6);
void SystemPowerball(int systemBall[], int max=6);
bool CheckMatch(int userBall[], int systemBall[], int max=6);
void WriteCheck(bool check, int userBall[], int systemBall[], int max=6);
//Functions.cpp
#include <iostream>
#include <ctime>
#include <iomanip>
using namespace std;
bool AskForPowerball(int userBall[], int max=6)
{
cout <<"\nPlease enter your 6 Powerballs in order (red first, seperated with space)\n";
for (int i=0; i<max; i++)
cin >>userBall[i];
if (cin.fail())
{
cin.clear();
cin.ignore(1024, '\n');
cout <<"Invalid Input!"<<endl;
return false;
}
cin.ignore(1024, '\n');
//检查红球和白球是否超出范围,白球是否重复
if (userBall[0]<1||userBall[0]>42)
{
cout <<"\nRed powerball out of range!"<<endl;
return false;
}
bool checkWhite[56]; //用check array 检查重复,也可以排列后比较相邻检查重复
for (int i=0; i<56; i++)
checkWhite[i]=false;
for (int i=1; i<max; i++)
{
if (userBall[i]<1||userBall[i]>55)
{
cout <<"\nWhite powerball out of range!"<<endl;
return false;
}
if (!checkWhite[userBall[i]])
checkWhite[userBall[i]]=true;
else
{
cout <<"\nRepeated white ball "<<userBall[i]<<endl;
return false;
}
}
return true;
}
void SystemPowerball(int systemBall[], int max=6)
{
srand((unsigned)time(NULL));
//red ball
systemBall[0]=rand()%42+1;
//five white ball
bool checkWhite[56];
for (int i=0; i<56; i++)
checkWhite[i]=false;
for (int i=1; i<max; i++)
{
bool again=true;
do
{
systemBall[i]=rand()%55+1;
if (!checkWhite[systemBall[i]])
{
checkWhite[systemBall[i]]=true;
again=false;
}
}while(again=true);
}
}
bool CheckMatch(int userBall[], int systemBall[], int max=6)
{
//check powerball match first
if (userBall[0]!=systemBall[0])
return false;
//white ball rank
for (int i=1; i<max-1; i++)
for (int j=2; j<max; j++)
{
int temp1;
if (userBall[j-1]>userBall[j])
{
temp1=userBall[j-1];
userBall[j-1]=userBall[j];
userBall[j]=temp1;
}
if (systemBall[j-1]>systemBall[j])
{
temp1=systemBall[j-1];
systemBall[j-1]=systemBall[j];
systemBall[j]=temp1;
}
};
//check white ball match
for (int i=1; i<max; i++)
if (userBall[i] != systemBall[i])
return false;
return true;
}
void WriteCheck(bool checkmate, int userBall[], int systemBall[], int max=6)
{
cout <<"\nUser selected balls:"
<<"\n powerball: "<<userBall[0]
<<"\n white ball: ";
for (int i=1; i<max; i++)
cout <<setw(5)<<userBall[i];
cout <<endl;
cout <<"\nSystem selected balls:"
<<"\n powerball: "<<systemBall[0]
<<"\n white ball: ";
for (int i=1; i<max; i++)
cout <<setw(5)<<systemBall[i];
cout <<endl;
if (checkmate)
cout <<"\nCongratulations! your powerball and white balls match the system!"<<endl;
else
cout <<"\nSorry, your balls don't match the system."<<endl;
}