/*第5题 福彩游戏--源代码及关键源代码注解如下:*/
//brought to you by the Red Deer College Code Junkies.
//included libraries necessary at runtime
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <cmath>
#include <iomanip.h>
#include <iostream.h>
//calling the various functions used in the program
int isMatch(int test, int cp_num[] );
int getgamenbr();
int menu();
int j();
int main()
{
//defining the variables used in the program
char name[20], Reply2;
int seed, PowerBall, total, play=1;
int truematch[6];
int menu();
cout<<"-------------------------\n";
cout<<"* Welcome to PowerBall! *\n";
cout<<"-------------------------\n";
cout<<" -by Chris Grose\n\n";
//getting user input for name. Will store in an array, and will be called throughout the //program
cout<<"Please enter your name [1 to 20 characters]: ";
cin.getline (name,20);
//welcoming the user with a personal touch!
cout<<"\n Welcome "<<name<<"! Please enter your lucky number for seed: ";
cin>>seed;
//setting the random variable when generating random numbers [depending on what seed the //user enters]
srand(seed);
do
{
//users inputted whiteball numbers
int num [5];
//computers inputted whiteball numbers
int cp_num [6];
cout <<"\n"<<name<<", please enter your white ball numbers.\n"; //user input, declared
cout <<"\n";
//getting five inputs for the users whiteball numbers. stored in the array called 'num'
int j;
for (j=0; j<5; j++)
{
cout<<"Enter Ball # "<<j+1<<": ";
while (!(cin >> num[j]))
{
//error messages if the user tries to enter something invalid when prompted
cin.clear();
while (!(cin.get() !='\n'))
continue;
cout <<"Sorry, that is an invalid entry. Please enter a number between 1 and 48. Thank you!\n";
}
if ((num[j]<1))
{
cout << "Sorry, you have entered a number less than 0. Please enter a number between 1 and 48.\n ";
j--;
}
if ((num[j]>48))
{
cout <<"Sorry, you have entered a number greater than 48. Please enter a number between 1 and 48.\n";
j--;
}
}
cout <<"\n";
//getting the users choice for the powerball
cout <<name<<", please enter your PowerBall number: ";
cin >>PowerBall;
//getting random whiteball numbers for the computers choices [calling from the 'getgamenbr' //function]
cp_num[0]= getgamenbr();
cp_num[1]= getgamenbr();
cp_num[2]= getgamenbr();
cp_num[3]= getgamenbr();
cp_num[4]= getgamenbr();
cp_num[5]= getgamenbr();
//calculating if the computers whiteball choices match the users choices and displays 0 if there
//is no match, and 1 if there is a match
truematch[0]= isMatch(num[0],cp_num);
truematch[1]= isMatch(num[1],cp_num);
truematch[2]= isMatch(num[2],cp_num);
truematch[3]= isMatch(num[3],cp_num);
truematch[4]= isMatch(num[4],cp_num);
truematch[5]= isMatch(num[5],cp_num);
//displaying the results of the game in a visually appealing manner
cout<<"\n******************************************************";
cout <<"\nPlay #"<<play;
cout<<"\n****************************************************************\n";
cout <<"\n";
//the computers whiteball numbers
cout <<"The computers picks were: "<<cp_num[0]<<" "<<cp_num[1]<<" "<<cp_num[2]<<" "<<cp_num[3]<<" "<<cp_num[4]<<" "<<"PB"<<" "<<cp_num[5]<<"\n";
cout <<"---------------------------------------------------------------------------\n";
//the users whiteball numbers
cout <<name<<", your picks were: "<<num[0]<<" "<<num[1]<<" "<<num[2]<<" "<<num[3]<<" "<<num[4]<<" "<<"PB"<<" "<<PowerBall<<"\n";
cout <<"---------------------------------------------------------------------------\n";
//showing if any of the numbers [computers vs users] match
cout <<name<<", your matches were: "<<truematch[0]<<" "<<truematch[1]<<" "<<truematch[2]<<" "<<truematch[3]<<" "<<truematch[4]<<" "<<"PB"<<" "<<truematch[5]<<"\n";
cout <<"---------------------------------------------------------------------------\n";
//adds up total matches
total=(truematch[0] + truematch[1] + truematch[3] + truematch[4] + truematch[5]);
//displays how many numbers matched
cout <<"\nTotal Matches: "<<total<<"\n\n";
//showing the appropriate message depending on how many numbers were matched [if all 6
//match, you win. otherwise, you lose!]
if (total==6)
{
cout <<"Congratulations "<<name<<"! You won the jackpot!!";
}
else
cout <<"Sorry "<<name<<", you did not win the jackpot!";
cout<<"\n********************************************************************\n";
//asking the user if he/she would like to play the game again
cout<<name<<", would you like to play the game again? [y or n]"<<endl;
cin>> Reply2;
play=play + 1;
//exiting the program if user response is 'n' to play again
}while (Reply2=='y'||Reply2=='y');
cout<<"\nThank you for playing PowerBall "<<name<<"! :-)"<<endl;
exit (1);
return 0;
}
int getgamenbr()
{
//function to return a random number between 1 and 48
return (rand() % 48) + 1;
}
int isMatch(int test, int cp_num[] )
{
//calculating if there are any matches
int match = 0;
//loop to test all numbers
for (int i=0; i<5; i++)
if (test == cp_num[i]) match = 1;
cout <<"\n";
return match;
}
int j()
{
int n = 0;
if (j == j)
n=1;
cout<<"\n";
cout<<"Only enter a number once.";
return j();
}