checking a vector

hey guys, i need to check my vector for a run of 3 or 4 cards (ace, 2 3 or 4, 5,6 etc), here is what i have so far, however it makes my program crash:

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
void checkrun1 ()
            {
                for (unsigned j = 0; j < hand1.size() - 2; ++j)
                {
                    for (unsigned q = 0; q < hand1.size() - 2; ++q)
                    {
                        for (unsigned m = 0; m < hand1.size() - (j + q);)
                        {
                    if (hand1[j]->getvalue() == hand1[j+q]->getvalue() + 1)
                    {
                        if (hand1[j+q]->getvalue() == hand1[j+q+m]->getvalue() + 1)
                        {
                            tscore1 += 3;
                            if (hand1[j + 2]->getvalue() == hand1[j+3]->getvalue() + 1)
                            {
                                tscore1 += 1;
                                cout << "run of 4 = 4 points" << endl;
                                break;
                            }

                            else
                            {
                                cout << "run of 3 = 3 points" << endl;
                            }
                        }
                    }
                    }
                }
                }
            }


the vector of cards has already been sorted based on value prior to this, hence the only checking for a card higher rather than lower
You're accessing uninitialized data with calls like
1
2
3
hand1[j+q]
//and
hand1[j+q+m]

Go over your loops again and make sure you're accessing what you need to.
Do you even need 3 loops?
they are initialized in the for loop, and they are needed due to the amount of combinations there could be

for instance, the hand could be: ace, 2, 2, 3, jack, i need it to recognise that there are two straights of 3 in that hand
Last edited on
I mean hand1 at j+q doesn't exist.
For example if hand1.size() = 10,
then j goes up to 9-2 = 7, and so
does q, so you're trying to access
hand1[14] which is uninitialized.
oh i see, so maybe hand1.size() - j for q
it compiles and runs now thank you, however it doesnt recognise runs still
Topic archived. No new replies allowed.