Why does this always return same number?

Pages: 12
Ty I have just fixed some stuff and make it a little harder to win but how can I disable making same cards multiple times?

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
#include <cstdlib>
#include <iostream>
#include <time.h>
#include <string>

using namespace std;

void Random ();
int points=0;


int main(int argc, char *argv[])
{   
    srand((unsigned)time(0) ); 
    Random ();           
    system("PAUSE");
    return EXIT_SUCCESS;
        
}


void
Random ()
{
    //INITIALIZE
    int color,value,PC_points;
    string value_str,color_str,t;
    

       
    //RANDOM COLOR
    color = rand() % 4 + 1; 
   
    switch(color)
{
    case 1:
        color_str = "Heart";
        break;
    case 2:
        color_str = "Spade";
        break;
   case 3:
        color_str = "Diamond";
        break;
   default:
        color_str = "Club";
        break;
        }
   
   
     
    //RANDOM VALUE
    value = rand() % 13 + 1;
    switch(value)
{
    case 1:
        value_str = "Ace";
        points = points + 11;
        break;
    case 2:
        value_str = "2";
        points = points + 2;
        break;
   case 3:
        value_str = "3";
        points = points + 3;
        break;
case 4:
        value_str = "4";
        points = points + 4;
        break;
case 5:
        value_str = "5";
        points = points + 5;
        break;
case 6:
        value_str = "6";
        points = points + 6;
        break;
case 7:
        value_str = "7";
        points = points + 7;
        break;
case 8:
        value_str = "8";
        points = points + 8;
        break;
case 9:
        value_str = "9";
        points = points + 9;
        break;
case 10:
        value_str = "10";
        points = points + 10;
        break;
case 11:
        value_str = "Jack";
        points = points + 10;
        break;
case 12:
        value_str = "Queen";
        points = points + 10;
        break;
default:
        value_str = "King";
        points = points + 10;
        break;
}


    
        
    //DISPLAY
    if (points <= 21)
    cout << color_str << " " << value_str << endl;
    else
    {
    cout << "Points :" << points << endl;
    cout << "Looser!" << endl;
    return;
    }
    
    

       
    //HIT OR STAY
    cout << "Points :" << points << endl;
    cout << "Hit (t) or Stay (s)?" << endl;
    cin >> t;
    if (t == "t")
    Random ();
    if (t == "s")
    {
      do
      PC_points = rand() % 25 + 1;
      while(PC_points < 17);
      cout << "PC Points: " << PC_points << endl;
      if (PC_points > 21 || points > PC_points)
      {
      cout << "Winner!" << endl;
      return;
      }
      if (points < PC_points )
      cout << "Looser!" << endl;
      else 
      cout << "Draw!" << endl;
      } 
}    
Last edited on
You can store the value in a vector or something like that and if it is equal to those already store discard it.
Uhm... Did I mention that I am noob?
Could you possibly give me tutorial for vector usage?
Ty
The simplest way I can think is this:

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
#include <vector>
#include <algorithm>
//...
vector<string> cards;
//...

void Random ()
{

        //...

       //After "RANDOM VALUE" (line 108)
       //Before "DISPLAY" (line 113)
       if ( count(cards.begin(),cards.end(),color_str+'\t'+value_str)  )
       {
            //Values exists so repeat the function
            Random();
            return;
       }else
       {
            //insert the value in the vector
            cards.push_back(color_str+'\t'+value_str);
            if ( cards.size()==52 ) cards.clear();//if it contains all the cards (which are 52) delete all vectors elements
       }

      //...

}

With these modifications your program shouldn't repeat until all the cards were given



http://www.cplusplus.com/reference/stl/vector/
http://www.cplusplus.com/reference/algorithm/count.html
Last edited on
I know I am extremely annoying but what exactly does cards.begin and cards.end calculate(what are their values =S)?

-EDIT-
What I meant is "Can you explain me this line in details so I won't have to bother ppl each time I have to use this... I would like to learn at beginning...
if ( count(cards.begin(),cards.end(),color_str+'\t'+value_str) )

Ty (Lol,this is like 14th Ty in 17 posts xD)
Last edited on
They are the iterators (an iterator is like a pointer).
If you think the vector to be like an array, they are like this:
1
2
3
int array[5];
array == vector.begin();
array+5 == vector.end()


http://www.cplusplus.com/reference/std/iterator/
http://www.cplusplus.com/reference/stl/vector/begin.html
http://www.cplusplus.com/reference/stl/vector/end.html
I've read all of those tuts and still don't get why do I have to write ( count(cards.begin(),cards.end() and not just color_str+'\t'+value_str) and compare it to...Well, something xD
count is one of the STL algorithms and they are meant to be general so you have so specify the iterators.

From http://www.cplusplus.com/reference/algorithm/ :
The header <algorithm> defines a collection of functions especially designed to be used on ranges of elements.
A range is any sequence of objects that can be accessed through iterators or pointers, such as an array or an instance of some of the STL containers.



If you want to compare you will need a loop
eg:
1
2
3
4
5
6
7
8
9
10
11
12
for(unsigned i=0; i<cards.size(); i++)
   if( cards[i] == color_str+'\t'+value_str )
   {
        Random();
        return;
   }
/*
as in the loop you have a return statement if it finds a value in cards
which is the same as color_str+'\t'+value_str the function will be stopped
and you won't get here, if Random will continue from here that means that
it wasn't found
*/
Last edited on
Ok ty and this is FINALLY totally done! *phew* xD
This is my last question in this thread I swear...
What does this mean?
cards.push_back(color_str+'\t'+value_str);
Last edited on
Ok sry this could be closed now...
I've read that vector tutorial once more and now i understand it all.
I would like to thank once more to everyone who helped me. =)
Topic archived. No new replies allowed.
Pages: 12