//++. This is a mini Game, allowing three players to contest. Each Player will have to open three boxes from given one hundred boxes in 3 turns. There is a secret number in each box. In each turn, the secret number inside the selected box will add-up to the total score of player. At the end, it will display the summary score of all players.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
class Player {
private:
char name[20];
int score;
public:
Player(){
strcpy(name, "\0");
score = 0;
//cout<<"Constructor Called";
}
~Player(){
//cout<<"Destructor Called";
}
void setName(){
cin>>this->name;
}
char* getName(){
return this->name;
}
void setScore(int score){
this->score = score;
}
int getScore(){
return this->score;
}
};
void displayInformation () {
cout<<"This is a mini Game, allowing three players to contest.";
cout<<"Each Player will have to open three boxes from given one hundred boxes in 3 turns.";
cout<<"There is a secret number in each box. In each turn number, the secret number inside the selected box will add-up to the total score of player. At the end, it will display the summary of score of each player.\n\n";
}
void fillBoard(int array[10][10]){
srand(time(0));
for(int i = 0; i < 10; i++){
for(int j = 0; j < 10; j++){
array[i][j] = rand()%100+1;
}
}
}
void displayBoard(int array[10][10]){
int *ptr = *array;
system("cls");
displayInformation();
for(int i = 1; i <= 100; i++){
if(*ptr != 0){
//cout<<i<<"("<<*ptr<<")\t";
cout<<i<<"\t";
}
else {
cout<<"X\t";
}
ptr++;
if(i%10 == 0)
cout<<endl;
}
cout<<endl;
ptr = NULL;
}
int openBox(int grid[10][10]){
int *ptr = *grid, choice = 0, secretNumber = 0;
cout<<"Enter the number of box to open it: ";
cin>>choice;
ptr = ptr+choice-1;
secretNumber = *ptr;
*ptr = 0;
return secretNumber;
}
void sortPlayers(Player players[], int size){
// put your code here to sort the players in descending order on the bases of score
// Example Input
// Uzma 233
// Ali 280
// bc123456899 250
// Desired Output
// Ali 280
// bc123456899 250
// Uzma 233
}
main(){
int size = 3, board[10][10] = {0};
Player players[size];
displayInformation();
for(int x = 0; x < size; x++) {
fillBoard(board);
cout<<"Enter name of player "<<x+1<<" of "<<size<<": ";
players[x].setName();
for(int i = 1; i <= 3; i++){
displayBoard(board);
cout<<"Turn "<<i<<" of 3"<<endl;
if(i < 3) {
players[x].setScore(players[x].getScore()+openBox(board));
}
else{
char choice = '\0';
int offer = 25+rand()%51;
cout<<"Special offer take: "<<offer<<endl<<endl;
cout<<"You can take special offer or open last box."<<endl;
cout<<"To take special offer press y or Y and any other character to open your last box: ";
cin>>choice;
if(choice == 'y' || choice == 'Y'){
players[x].setScore(players[x].getScore()+offer);
}
else{
players[x].setScore(players[x].getScore()+openBox(board));
}
}
cout<<endl<<endl;
}
cout<<"Your Score: "<<players[x].getScore()<<endl<<endl;
}
sortPlayers(players, size);
cout<<"---------- Result Summary -------------- "<<endl;
for(int k = 0; k < size; k++) {
cout<<players[k].getName()<<"\t Score: "<<players[k].getScore()<<endl;
}
cout<<endl;
system("pause");
}
|
In this program, we have created a Player class which will keep record of individual player’s data i.e. name and score. In main() function, we created three objects of Player class in the form of array. Doing this will allow three players to play the game.
A loop in main function is iterated to get name and score of three players. After getting player name and score, we are displaying result summary at the end of main function.
In this program, we have created five functions to perform all necessary tasks.
• displayInformation() function is used to display the information about the game on top of output window.
• fillBoard() function is taking a 2D array as argument and filling each element/box of array with random number from 1 to 100. We are re-filling 2D array for each player. Element of this array are also referred as box.
• displayBoard() function is also taking 2D array as argument and displaying box number from 1 to 100.
We are doing this for player to easily remember box number he/she want to open or already opened. It will show already opened box with character X indicating this box is already selected.
• openBox() function is taking 2D array as argument. This function will get box number from player, add secret number stored at selected box into total score of player and set value of box to zero. Setting secret number of box to zero is allowing us to mark the box with X, so in next turn this box should not use again.
• sortPlayers() function is taking array of player and size of array as arguments. It is your task to write the code of this function.
You are required to write the implementation of sortPlayers() function to sort the players array in descending order on the base of player score.