Help to take away global varaibles and to make use of return value


Hello, i would like to ask for an advice on how to change my code so that i can take away my global variables but still being able to have the new values changing and following with each game. I need to communicate my data in between my functions.

I have created a reurn value "points" but i dont know how to make us of it so that i can take away the global variables(most importantly the global variable points). Can anyone give me a tipp on how to change it?

Thanks!



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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296

#include <iostream>
#include <ctime>         
#include <cstdlib>        
using namespace std;



int game_bet=0;       
int game_account=0;
int row1 = 0;
int row2 = 0;        
int row3 = 0;
int col1 = 0;
int col2 = 0;
int col3 = 0;
int dia1 = 0;
int dia2 = 0;
int points = 0;     


void instructions ();    
int game ();             
void game_start1();       
void game_start2();
void game_winn ();



int main()

{
    char svar = '\0';
    
    cout << "Welcome to one arm bandit!\n" << endl;
   
        game_start1();
        game_start2();
        instructions ();
        cout<<"Press a key followed by ENTER to play\n"<<endl;
        std::string wait;
        std::cin>>wait;
        game();                         // calling the game
    
        cout<<"Your points are\n"<<points<<endl;
        game_winn();                        
        cout << "Do you want to play again?, or do you choose to take all your money out, press s to play and a to take out your money\n" <<endl;              
        cin>>svar;
    
    
    while (game_account>0 && answer == 's')      
        
    {
        game_start2();
        instructions ();
        cout<<"Press a key followed by ENTER to play a set\n"<<endl;
        std::string wait;                      
        std::cin>>wait;
        game();                                        
        cout<<"Your points are\n"<<points<<endl;     // return value?
        game() 
        game_winn();
        
        cout << "Do you want to play again?, or do you choose to take all your money out, press s to play and a to take out your money"n"<<endl;
        cin>> svar;
        
   
       
        if (answer=='a')       //if player takes out money, game ends
        
         {
            cout<< "congrats, you go home with"<<endl;
            game_account=game_account+ game_bet;
            cout<< game_account<<endl;
            cout<<"goodbye"<<endl;
         }

     }
    
    
    while (game_account==0) 
    
    {
        
        cout<< "you lost"<<endl;
            
    }
    
    
    return 0;
    
}



// function when user initially decide how much he wants to put in the machine

void game_start1()

{


    cout<< "How much money do you want to put in the machine choose from 50,100,500kr"<<endl;
    cin>> game_account; 
    
    
    if (game_account== 50 ||game_account==100||game_account==500)
        
    {   
        cout<< "Du har nu i ditt konto\n"<< game_account<<"kr"<<endl;
    }
    
    
    else if (game_account!= 50 ||game_account!= 100 ||game_account!= 500) 
        
    {
        
        game_start1();
 
    }
    
}


//function where user decide how much he wants to bet for each game set
    
 void game_start2()
    
{
    
    cout<< "How much do you want to bet from your account on:"<<game_account<<"kr"<<endl;
     
    cout<< "choose between minimum 10kr and maximum your whole account\n"<<endl;
     
    cin>> game_bet;
     
    if (game_bet>=10|| game_bet==game_account)      
       
    {
        
        cout<< "You have choosen to bet\n" <<game_bet<<"kr"<<endl;
        cout<<"you kniw have left in the machine"<<endl;
        game_account= game_account-game_bet;
        cout<< game_account<<endl; 
        
    }
        
    else if (game_bet<10||game_bet>game_account)
        
    {
        
        game_start2();     

    }
 
    
        
}

//function that explains the game rules for the user

void instructions()

{

cout<<"\n"
"We will now start the game,the goal for you is to get as many rows, columns and diagonals as possible of the same symbol. Depending on how many you get, you win money that you can either choose to withdraw or continue to play for. If you do not get any row, column or diagonal of the same symbol, you lose your bet

\n\n"<<endl;

}




char array2D[3][3];    //declaring array 

int game()

{

    srand (time(0));
    
    
    for (int x=0; x<3; x++){  
        for (int y=0; y<3; y++){                      
            array2D[x][y] = "AZM"[rand() % 3];  

        }
    }
    
    for (int x=0; x<3; x++){           
        for (int y=0; y<3; y++){
        cout << array2D[x][y];}
        cout << endl;
      }
    
    cout << endl;
    
    row1 = array2D[0][0]+ array2D[0][1]+ array2D[0][2];  
    row2 = array2D[1][0]+ array2D[1][1]+ array2D[1][2];  
    row3 = array2D[2][0]+ array2D[2][1]+ array2D[2][2];
    col1 = array2D[0][0]+ array2D[1][0]+ array2D[2][0];
    col2 = array2D[0][1]+ array2D[1][1]+ array2D[2][1];
    col3 = array2D[0][2]+ array2D[1][2]+ array2D[2][2];
    dia1 = array2D[0][0]+ array2D[1][1]+ array2D[2][2];
    dia2 = array2D[2][0]+ array2D[1][1]+ array2D[0][2];
    
    points=0;   //points initiates to 0 after each game
    
    
    if (row1 == 270 || row1 == 231 || row1 == 195) 
        points++;                                                         //A,Z,M, when A= 65 (3*65=195), Z=90(3*90=270), M= 77(3*77=231)
    if (row2 == 270 || row2 == 231 || row2 == 195)
        points++;
    if (row3 == 270 || row3 == 231 || row3 == 195)
        points++;
    if (col1 == 270 || col1 == 231 || col1 == 195)      
        points++;
    if (col2 == 270 || col2 == 231 || col2 == 195)
        points++;
    if (col3 == 270 || col3 == 231 || col3 == 195)
        points++;
    if (dia1 == 270 || dia1 == 231 || dia1 == 195)
        points++;                                   
    if (dia2 == 270 || dia2 == 231 || dia2 == 195)
        points++;
   
    
    return points;        //my return value is the points user won in game

}




 //here it decides how much the the user won and the value gets deposited into game_account
    

 void game_winn ()
   
{
    
    
    if (points==0)    
        
    {
        cout<<"You got 0 equal symbols and lost your bet,you know have left to play for\n" << game_account <<endl;
    }
    

    
    if (points==1)
        
    {  game_account=game_account+ game_bet* 2;
        
        cout<<"You won 1 row/diagonal and 2 times your bet, you now have in the machine\n"<< game_account<<endl;
    }
    
    
    if (points==2)
        
    {  game_account=game_account+ game_bet* 4;
        
        cout<<"You won 2 rows and 4 times your bet, you now have in the machine\n" << game_account<<endl;
    }
    
        
    if (points==3)
        
        
    {  game_account=game_account+game_bet* 8;
        
        cout<<"You won 3 rows and 8 times your bet, you now have in the machine \n"<< game_account<<endl;
    }
    
        
    if (points==4)
    
    {  game_account=game_account+game_bet* 16;
        
        cout<<"You won 4 rows and 16 times your bet, you now have in the machine\n"<< game_account<<endl;
    }

    
    if (points>4)
    
    {  game_account=game_account+game_bet* 128;
        
        cout<<"You won all the rows and 128 times your bet, you now have in the machine\n"<< game_account<<endl;
    }
    
    
    
}
 






Well, you need to look at each function, and decide which are purely local variables (not used anywhere else in the program), which are inputs (to be passed as parameters) and what is the output, if any. In the example below, I've used return for the output value, but if there was more than one value to be returned, you might instead pass the variable by reference.

For illustration only, a very simplified version to show some of the ideas:
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
#include <iostream>
#include <ctime>         
#include <cstdlib>     
   
using namespace std;
           
int game_start1();       
int game_winn(int points, int game_account, int game_bet); 

int main()
{  
    cout << "Welcome to one arm bandit!\n" << endl;
 
    int game_account;
    int points;
    int game_bet;
    
    game_account = game_start1();

    points   = 2;
    game_bet = 1;
    
    cout << "Your points are\n" << points<<endl;
    game_account = game_winn(points, game_account, game_bet);                        
    
    return 0;
}

int  game_start1()
{
    int amount = 0;
    
    do
    {
        cout << "How much money do you want to put in the machine choose from 50,100,500kr"<<endl;
        cin >> amount; 
    } while (amount!=50 && amount!=100 && amount!=500);
       
    cout<< "You now have in your account\n" << amount << "kr"<<endl;

    return amount;
}
 
int game_winn(int points, int game_account, int game_bet) 
{
   
    if (points == 1)
    {  
        game_account = game_account + game_bet * 2;
        cout<<"You won 1 row/diagonal and 2 times your bet, you now have in the machine\n"<< game_account<<endl;
    }
    
    return game_account;
}
@Chervil
But is the extensive use of global variables deprecated?
The use of global values is not deprecated. It's still legal in the language.
It's just a poor coding practice.

Accessing global variables all over your program makes it very hard to debug and maintain as your program gets larger and more complex.

For example, points should really be an attribute of a Game object. points are initialized when the Game object is instantiated and go away when the Game instance goes out of scope. Any change to points should be done via calls to Game functions.



Thanks Anon, I didn't know that. I'll keep that in mind.
It could be considered a matter of style or preference.

I've programmed in languages where all variables are global. You can live with it. Having done things that way in the past, I don't mind having one or two key variables as global. But most variables (such as loop counters and so on) don't need to be global, and passing variables as parameters allows tighter control, by permitting any individual function the ability to modify only those variables which it actually needs to modify in order to fulfil its purpose. I guess for me its a way of doing two things,
a) keeping things simple (any piece of code has access to only the data it needs)
and b) giving some degree of protection from idiotic mistakes (which I'll admit I do make)..
Topic archived. No new replies allowed.