Deal or No Deal with arrays

I have a homework assignment and I feel like half way done with it but it does not run properly.

here is assignment txt:
/******************************************

Project Deal or No Deal Here is what your program will do: Program first welcomes the user. It explains how the game will be played. There are 10 briefcases with different amounts of money in them. We have 3 cases that contain no money. We also have one case with $1, one with $10, one with $100, one - $1,000, one - $10,000, one - 100,000, and finally one with 1 million dollars. These different amounts are randomly assigned to cases. The user does not know which case contains what amount of money. The user can open up to 3 cases. After the case is open - user is shown its content. Then the program asks - Deal or No Deal? If the user chooses Deal - he/she gets to keep the money in the recently opened case and the game is over. If the user chooses No Deal - then he/she can
open another case. After 3 cases have been opened - the user is shown the final amount of money (amounts are not added - only the last case counts), and the game is over. Prompt the user to enter the player’s name and display it together with the final amount; repeat the game for more players. Do not forget to re shuffle the brief cases.

Input: Keyboard

Output: Display the output to the screen. Write to a file the name of each player and the
final amount (at least 5 players).

Design: Your program should include several functions in addition to main()
// NOTE: You must write documentation for each function!


Pseudo-code:
1. User starts the game: display info about the game // this could be a function
2. Create 10 brief cases with money (from $0 to $1000000).
// define and initialize an array in main()
3. Open the output file players.txt
4. Shuffle the brief cases. // this could be a function
5. Game play begins // function
Prompt the user to enter his/her name

loop ( up to three times, as long as the user wants to continue playing )
Prompt the user to enter a brief case number (0 to 9) // validation
// this could be a function
Display the value stored in that brief case
Ask the user if it is deal or not
end loop

Display the name and the final amount to the screen
Display the name and the final amount to the file
6. Display "Game over for …. name of the player"
7. Ask the user if there is another player. If there is, repeat steps 4, 5, 6 and 7 until done
(no more player).
8. Close the output file.
9. Game finishes: display "Game over" // this could be a function

********************/
my code does not run for more than 3-4 users. After entering extra user after like third or forth it is not asking to enter box number it just state the prize and Game over function. Please help! What am I missing?


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){}
Last edited on
I don't see that 'tries' gets reset to 0 when a new player starts.

If I'm a player and am told there are 10 boxes, I'd expect to be able to enter a 1 through 10 as valid box numbers. The code is checking for a range of 0 to 9, which I get, since those are the array elements, but I think I'd try to present those as 1 to 10 for the user.
Good point! Thank you so much for your help!


fixed it this way

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

#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+1;
            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;
    cout<< "\nIs there another player?"<<endl;
    cin >> answer;

		if (answer == 'y' || answer == 'Y')
			status = true;
		else if (answer == 'n' || answer == 'N')
			status = false;
		else
			cout << "Not 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<<"....";
            }

        while (tries <3)
        {
            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
                {

                    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
                        tries++;
                        continue;


                }

        }

        int prize = array[boxSelected];

    cout<< Name<< " won " << prize<< endl;
    outputFile << Name<< " won "<<prize<<endl;

    cout<<"\nGame over for "<<Name<< endl;


}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;
}


void wait(int seconds)
{
    clock_t endwait;
    endwait=clock()+seconds+CLOCKS_PER_SEC;
    while(clock()<endwait){}
}

Topic archived. No new replies allowed.