You are on page 1of 4

CS 501 Fall 2021

Assignment 1 University of Bridgeport

General Instructions
In this assignment, you will be implementing the ‘Lotto’ game in an object-oriented
manner. This program would involve some basic manipulation of numbers, randomnumber
generation and writing to files. The driver program is given to you and you are NOT
allowed to change it. Hence, the obvious overloaded operators will be output << which
need to be implemented.

Specific Instructions
This assignment should be written as a modular C++ program. You will develop a class
(module) called LottoTicket, with header (.h) and implementation (.cpp) files. The main
program should be in its own module and is given to you.

This is a 5-player game. The user inputs a minimum(min) value, maximum(max) value,
number of rounds(r) to be played. This information is used to generate a LottoTicket. A
LottoTicket has 15 slots, which has to be randomly filled between the min and max
value(inclusive). Once all tickets are generated, the game starts and will play ‘r’ rounds.
For each round, a random number between min and max(inclusive) is generated and this
value is searched in each ticket to see if it is found in any one of the 15 slots. Please note,
that if a number is repeated in a ticket, then only one occurrence of the number is
considered as found.

The winner is decided by the first player who has all 15 slots revealed/found before the ‘r’
rounds are completed, else the game is considered a draw. You are required to print the
Ticket details along with the status of each round to both the console and an output file.

Please note that a properly seeded random number generator will ensure different results
for each run.

To summarize the steps:


1. Get all required data from user
2. Generate a LottoTicket for each player
3. Start the game and play ‘r’ rounds
4. For each round, a random number is generated
5. Check for each ticket if the number is found
6. Check for any winners
7. If there is a winner, then go to step 9
8. If there are no winners, repeat steps 4-7 till ‘r’ rounds
9. Display the game status: Draw/Winner Details

Programming concepts that are expected in the solution:


1. Object oriented modular solution
2. File IO operation
3. Use of selection and repetition constructs
4. Use of random function
5. The class used should have constructors and overloaded output operators.
Issued: Wednesday, Sept 1, 2021
Due: Monday, Sept 13, 2021 with demo at specified time during the week
CS 501 Fall 2021
Assignment 1 University of Bridgeport

Driver program:

int main() {

int num_of_players = 5;
LottoTicket players[5];
int min = 0;
int max = 0;
int max_rounds = 0;
int winner = -1;
bool gameover;
string filename = "output.txt";
ofstream fout;
srand(time(NULL));

cout<<"Enter the output filename: ";


cin>>filename;
cout<<"Enter the minimum number: ";
cin>>min;
cout<<"Enter the maximum number: ";
cin>>max;
cout<<"Enter the number of rounds to be played: ";
cin>>max_rounds;
fout.open(filename, ofstream::app);

int i = 0;

while(i != num_of_players) {
LottoTicket player1(i);
player1.generateLotto(min,max);
players[i] = player1;
i++;
}

cout<<"\nGenerating Lottos for all players .... \n\n";


fout<<"************************************************";
fout<<"\nGenerating Lottos for all players .... \n\n";
for(LottoTicket lotto: players) {
cout<<lotto;
fout<<lotto;
}

Issued: Wednesday, Sept 1, 2021


Due: Monday, Sept 13, 2021 with demo at specified time during the week
CS 501 Fall 2021
Assignment 1 University of Bridgeport

i = 0;
cout<<"\n";
cout<<" ";
cout<<"\nGame is starting"<<endl;
cout<<"Round# \tLotto\tP#0\tP#1\tP#2\tP#3\tP#4"<<endl;
fout<<"\n";
fout<<" -";
fout<<"\nGame is starting"<<endl;

fout<<"Round# \tLotto\tP#0\tP#1\tP#2\tP#3\tP#4"<<endl;
for(int j = 0; j < max_rounds; j++) {
int current_lotto = generateRandom(min, max);

cout<<j<<"\t"<<current_lotto<<"\t";
fout<<j<<"\t"<<current_lotto<<"\t";
for(LottoTicket &lotto: players) {
gameover = lotto.play(current_lotto);
cout<<lotto.getLottosWonCount()<<"\t";
fout<<lotto.getLottosWonCount()<<"\t";
if(gameover) {
winner = lotto.getId();
break;
}
}
cout<<endl;
fout<<endl;

if(gameover) {
break;
}
}
cout<<" ";
cout<<"\n\tRESULT: \t";
fout<<" ";
fout<<"\n\tRESULT: \t";
if(winner >= 0) {
cout<<"Winner is: Player "<<winner<<endl;
fout<<"Winner is: Player "<<winner<<endl;
} else {
cout<<"This game is a draw"<<endl;
fout<<"This game is a draw"<<endl;
}

return 0;
}

Issued: Wednesday, Sept 1, 2021


Due: Monday, Sept 13, 2021 with demo at specified time during the week
CS 501 Fall 2021
Assignment 1 University of Bridgeport

Output:

Winning Game: (Winner Player 1) Draw game:

Winning Game (Winner: Player 2)

Issued: Wednesday, Sept 1, 2021


Due: Monday, Sept 13, 2021 with demo at specified time during the week

You might also like