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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
|
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <stdio.h>
#include <fstream>
using namespace std;
int saveOpenedBoxes[10]={0};
void wait(int seconds);
void clean(int);
void shuffle(int[],int);
bool alreadyOpened(int);
void displayRules();
bool playAgain(char);
bool range (int);
void gamePlay (int array []);
void gameOver();
/*****************************
This Function Cleans Box Values
******************************/
void clean(int *saveOpenedBoxes)
{
for(int i=0;i<10;i++)
{
saveOpenedBoxes[i]=0;
}
}
/*************************************
This Function is Shuffles the briefcases
*************************************/
void shuffle(int Boxes[],int N)
{
int index;
int temp;
srand(time(NULL));
for(int i=0;i<N;i++)
{
index=rand()%10;
temp=Boxes[i];
Boxes[i]=Boxes[index];
Boxes[index]=temp;
}
}
/*************************************
This Function check if entered box was
already selected
*************************************/
bool alreadyOpened(int i)
{
if(saveOpenedBoxes[i]==1)
return true;
else
{
saveOpenedBoxes[i]=1;
return false;
}
}
/*************************************
This Function explains rules to user
*************************************/
void displayRules()
{
cout<<"**********************************************************\n";
cout<<"\n There are 10 briefcases with different amounts of money in them.\n We have 3 cases that contain money. \n";
cout<<" We also have one case with 1 one with 10 one with 100 one 1000\n one 10000 one 100000 and finally one with 1 million dollars.\n";
cout<<" These different amounts are randomly assigned to cases. \n You can open up to 3 cases.\n";
cout<<" After the case is open You will be shown its content.\n\n Then You have to select Deal or No Deal?\n\n" ;
cout<<" If You choose Deal:\n\n You get to keep the money in the recently opened case and the game is over.\n \n";
cout<<" If choose No Deal: \n\n Then You can open another case. After 3 cases have been opened\n You will be shown the final amount of money \n";
cout<<" (amounts are not added only the last case counts) and the game is over.\n\n";
cout<<"*********************************************************\n\n";
}
/*************************************
This Function asks the user if there is
another player and repeats the game with
positive answer
*************************************/
bool playAgain(char answer)
{
bool status;
if (answer == 'y' || answer == 'Y')
status = true;
else if (answer == 'n' || answer == 'N')
status = false;
else
cout << "Please enter a valid response"<<endl;
return status;
}
/*************************************
This Function checks if chosen box
is in the range of 0 to 9
*************************************/
bool range (int number)
{
bool status;
if (number <0 || number >9)
status = true;
else
status = false;
return status;
}
/*************************************
This Function is Prompt the user to enter his/her name,
prompt user to enter brief case, validates case number
and display the value in that brief case. Up to 3 times.
Asks user if it is deal or not.
*************************************/
void gamePlay (int array [])
{
string Name;
int boxSelected,deal;
int tries = 0;
char answer;
ofstream outputFile;
outputFile.open("players.txt");
if (!outputFile) // validating output file
{
cout << "Error: unable to open the output file!" << endl;
}
do
{
cout<<"Enter Your Name :";
cin>>Name;
cout<<"\n****************\n";
cout<<"Shuffling Boxes..";
shuffle(array,10);
clean(saveOpenedBoxes);
for(int K=0;K<2;K++)
{
wait(1);
cout<<"....";
}
do
{
cout<<"\nChose Box No:";
cin>>boxSelected;
if(alreadyOpened(boxSelected))
{
cout<<"Box Already Selected ";
}
else if (range(boxSelected))
cout << "Error! Not in the range!"<<endl;
else
{
tries++;
cout<<"You Just Opened $ "<<array[boxSelected]<<endl;
cout<<" 1.Deal? \n 2. No Deal? \n";
cin>>deal;
if (deal !=1 && deal != 2)
{
cout << "Please enter 1 or 2" << endl;
cin.clear();
cin.ignore(10000,'\n');
}
else if(deal==1)
{
cout<<"You won "<<array[boxSelected]<<" $ ! \n";
break;
}
else
continue;
}
}while (tries <3);
int prize = array[boxSelected];
cout<< Name<< " won " << prize<< endl;
outputFile << Name<< " won "<<prize<<endl;
cout<<"\nGame over for "<<Name<< endl;
cout<< "\nIs there another player?"<<endl;
cin >> answer;
}while(playAgain (answer));
outputFile.close();
}
int main(int argc, char *argv[])
{
string Name;
int Boxes[]={0,0,0,1,10,100,1000,10000,100000,1000000};
displayRules();
gamePlay ( Boxes);
gameOver();
return 0;
}
/*************************************
This Function displays the Game Over
message
*************************************/
void gameOver()
{
cout<< "***********************************"<< endl;
cout<< "*********GAME OVER*****************"<< endl;
cout<< "***********************************"<< endl;
}
void wait(int seconds)
{
clock_t endwait;
endwait=clock()+seconds+CLOCKS_PER_SEC;
while(clock()<endwait){}
|