Index

Hello!

I just need some help with showing me why I sometimes get the same card over again. When I have declared them and also done a if statement if the variable had been used before.

So what's the problem.
- I want a unique card to only show once, only one red king, only one red ten and so on.

Here is the code:

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
#include <iostream>
using namespace std;

       class BlackJack{
       public:
       bool hjarter[13];
       bool spader[13];
       bool ruter[13];
       bool klover[13];
       string tempF, tempK;
       int farg, summa, svar2;
       
           BlackJack(){

	        for ( int i = 0; i < 13; i++ ){

		    hjarter[i] = false;
            spader[i] = false;
            ruter[i] = false;
            klover[i] = false;

            }
            
            summa = 0;
            
            }
            
            void nyttKort(){

	        // slumpa fram färg
	       
           farg = rand()%3;
           int varde;
                     
               do{      
                     
	           if(farg == 0){ // 1
                
                   tempF = "hjärter";
                         
	               varde = rand()%13;
	
               if ( hjarter[varde] == false ){ // 2

		       if ( varde <= 8 ) {
                    
                    cout << tempF << " " << varde + 2;         
                    summa = summa + varde + 2;                                                             
			     
               }  
               
               else if ( varde == 9  ){
                    
                    cout << tempF << " knekt";
                    summa = summa + 10;
                    
               }
               
               else if ( varde == 10 ){
                    
                    cout << tempF << " dam";
                    summa = summa + 10;
                    
               }
               
               else if ( varde == 11 ){
                    
                    cout << tempF << " kung";
                    summa = summa + 10;
                    
               }
               
               else    
               
               ;
               
               } // End 2
               
               } // End 1
               
               }while ( hjarter[varde] == true );
               
               hjarter[varde] == true;
               
               ///////////////////////////////////////// NY FÄRG
               
                              do{      
                     
	           if(farg == 1){ // 1
                
                   tempF = "spader";
                         
	               varde = rand()%13;
	
               if ( spader[varde] == false ){ // 2

		       if ( varde <= 8 ) {
                    
                    cout << tempF << " " << varde + 2;         
                    summa = summa + varde + 2;                                                             
			     
               }  
               
               else if ( varde == 9  ){
                    
                    cout << tempF << " knekt";
                    summa = summa + 10;
                    
               }
               
               else if ( varde == 10 ){
                    
                    cout << tempF << " dam";
                    summa = summa + 10;
                    
               }
               
               else if ( varde == 11 ){
                    
                    cout << tempF << " kung";
                    summa = summa + 10;
                    
               }
               
               else
               
               ;
               
               } // End 2
               
               } // End 1
               
               }while ( spader[varde] == true );
               
               spader[varde] == true;
               
               ///////////////////////////////////////// NY FÄRG
               
                              do{      
                     
	           if(farg == 2){ // 1
                
                   tempF = "klöver";
                         
	               varde = rand()%13;
	
               if ( klover[varde] == false ){ // 2

		       if ( varde <= 8 ) {
                    
                    cout << tempF << " " << varde + 2;         
                    summa = summa + varde + 2;                                                             
			     
               }  
               
               else if ( varde == 9  ){
                    
                    cout << tempF << " knekt";
                    summa = summa + 10;
                    
               }
               
               else if ( varde == 10 ){
                    
                    cout << tempF << " dam";
                    summa = summa + 10;
                    
               }
               
               else if ( varde == 11 ){
                    
                    cout << tempF << " kung";
                    summa = summa + 10;
                    
               }
               
               else
               
               ;
               
               } // End 2
               
               } // End 1
               
               }while ( klover[varde] == true );
               
               klover[varde] == true;

	           ///////////////////////////////////////// NY FÄRG
               
                              do{      
                     
	           if(farg == 3){ // 1
                
                   tempF = "ruter";
                         
	               varde = rand()%13;
	
               if ( ruter[varde] == false ){ // 2

		       if ( varde <= 8 ) {
                    
                    cout << tempF << " " << varde + 2;         
                    summa = summa + varde + 2;                                                             
			     
               }  
               
               else if ( varde == 9  ){
                    
                    cout << tempF << " knekt";
                    summa = summa + 10;
                    
               }
               
               else if ( varde == 10 ){
                    
                    cout << tempF << " dam";
                    summa = summa + 10;
                    
               }
               
               else if ( varde == 11 ){
                    
                    cout << tempF << " kung";
                    summa = summa + 10;
                    
               }
               
               else
               
               ;
               
               } // End 2
               
               } // End 1
               
               }while ( ruter[varde] == true );
               
               ruter[varde] == true;

	}

    void summaK(){
         
         cout << "\nDin summa :" << summa;
         
    }
		




};

int main(void){
        
        int svar;
        srand(time(0));
        BlackJack spelare;
        
        do {
        cin >> svar;
        
        if ( svar == 1 ){
             
        spelare.nyttKort();
        spelare.summaK();
        cout << "\n";
        
        }
        
        }while ( svar != 0 );
    
        system("pause");
        return 0;
    
}



I don't see what the error is, but in general, this is not the best approach.
If you want to have a random sequence, literally, have a random sequence. Declare a struct Card{ int farg, varde; }; (I'm just guessing what those words mean). Declare an array of 52 Cards and fill it in a for loop. Then use std::random_shuffle (defined in <algorithm>) to shuffle the deck. Finally have an integer which marks the top of the deck and move it every time you draw a card.
Yeah but I'm doing a school project so I haven't learned all the things you said there =)
Well of course I can compile it an run it, but I for example get red king more than 1 time when it's not supposed to, when I only want it 1 time. It's like it doesn't check the list or something :)

Thanks for your answer!
The algorithm to check whether a card has been played, is not vary simple (not the way you do it right now anyway). What if you roll farg = 0 but all hjarter cards have already been drawn? You'll get stuck in an infinite loop.

You could do like I said. If you don't understand something about that method, ask.

Another way, more similar to yours would be to generate a single random number 'i' with rand()%52. Have an array 'deck' of 52 booleans. Regenerate 'i' in a loop until deck[i] is false (just like you do now). When you do that, 'i' will be the index of a unique card. then farg = i/13 and varde = i%13. After that you can do with farg and varde whatever you need without any kind of checking.
Topic archived. No new replies allowed.