Determining a straight in poker

solved!!
Last edited on
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
#include <iostream>
#include <string>
#include <algorithm>

struct Card
{
    std::string face;
	std::string suit;
};

constexpr std::size_t NCARDS = 5 ;
using hand = Card[NCARDS] ;

bool is_straight( const hand& h )
{
    static const std::string face[] { "Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };

    // get the face values as integers 1(Ace), 2, ... 13(King)
    int values[NCARDS] ;
    for( std::size_t i = 0 ; i < NCARDS ; ++i )
        values[i] = std::find( std::begin(face), std::end(face), h[i].face ) - std::begin(face) + 1 ;

    // sort face values in ascending order
    std::sort( values, values+NCARDS ) ;

    // check if the sorted sequence is a straight
    // http://en.cppreference.com/w/cpp/algorithm/search
    // http://en.cppreference.com/w/cpp/algorithm/equal
    static const int straight[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };
    static const int straight2[] { 1, 10, 11, 12, 13 }; // straight: Ace, Ten, Jack, Queen, King
    return ( std::search( std::begin(straight), std::end(straight), values, values+NCARDS ) != std::end(straight) )
             || std::equal( std::begin(straight2), std::end(straight2), values, values+NCARDS )  ;
}

int main()
{
    const hand a { { "Five", "Clubs" }, { "Four", "Spades" }, { "Six", "Diamonds" }, { "Three", "Clubs" }, { "Seven", "Hearts" } };
    std::cout << std::boolalpha << is_straight(a) << '\n' ; // true

    const hand b { { "Queen", "Clubs" }, { "Ten", "Spades" }, { "Jack", "Diamonds" }, { "Ace", "Clubs" }, { "King", "Hearts" } };
    std::cout << std::boolalpha << is_straight(b) << '\n' ; // true

    const hand c { { "Queen", "Clubs" }, { "Two", "Spades" }, { "Jack", "Diamonds" }, { "Ace", "Clubs" }, { "King", "Hearts" } };
    std::cout << std::boolalpha << is_straight(c) << '\n' ; // false
}

http://coliru.stacked-crooked.com/a/e800f47f107c52a1
JLBorges,

Thank you for your help! Unfortunately, we didn't cover anything from the algorithms header, so I don't think I'll be able to reference the code (this is why I don't have vectors in my code - we never discussed them in class so we can't use them).

However, the functions you provided were extremely useful for future projects, so thanks once again. It's also kind of frustrating that this can be done much simpler using built in functions...
Here's a 'do not use the library' version: specially written for career teachers:
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
#include <iostream>
#include <string>

struct Card
{
    std::string face;
    std::string suit;
};

constexpr std::size_t NCARDS = 5 ;
using hand = Card[NCARDS] ;
using face_values = int[NCARDS] ;

// get the face values as integers 1(Ace), 2, ... 13(King)
int face_to_value( const std::string& f )
{
    static constexpr int NFACES = 13 ;
    static const std::string face[NFACES] { "Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };

    for( int i = 0 ; i < NFACES ; ++i ) if( face[i] == f ) return i+1 ;
    return -1 ; // invalid face
}

void sort( face_values& values ) // ascending
{
    for( std::size_t i = 0 ; i < NCARDS ; ++i )
        for( std::size_t j = i+1 ; j < NCARDS ; ++j )
            if( values[i] > values[j] )
            {
                int temp = values[i] ;
                values[i] = values[j] ;
                values[j] = temp ;
            }
}

bool is_straight( const hand& h )
{
    // get the face values as integers 1(Ace), 2, ... 13(King)
    int values[NCARDS] ;
    for( std::size_t i = 0 ; i < NCARDS ; ++i ) values[i] = face_to_value( h[i].face ) ;

    sort(values) ; // sort face values in ascending order

    // check if the sorted sequence is a straight
    // special case for the first value: take care of Ace, Ten, Jack, Queen, King
    if( values[1] == ( values[0] + 1 ) || ( values[0] == 1 && values[1] == 10 ) )
    {
        for( std::size_t i = 2 ; i < NCARDS ; ++i ) // for values[1] onwards
            if( values[i] != (values[i-1] + 1 ) ) return false ;

        return true ;
    }

    return false ;
}

int main()
{
    const hand a { { "Five", "Clubs" }, { "Four", "Spades" }, { "Six", "Diamonds" }, { "Three", "Clubs" }, { "Seven", "Hearts" } };
    std::cout << std::boolalpha << is_straight(a) << '\n' ; // true

    const hand b { { "Queen", "Clubs" }, { "Ten", "Spades" }, { "Jack", "Diamonds" }, { "Ace", "Clubs" }, { "King", "Hearts" } };
    std::cout << std::boolalpha << is_straight(b) << '\n' ; // true

    const hand c { { "Queen", "Clubs" }, { "Two", "Spades" }, { "Jack", "Diamonds" }, { "Ace", "Clubs" }, { "King", "Hearts" } };
    std::cout << std::boolalpha << is_straight(c) << '\n' ; // false
}

http://coliru.stacked-crooked.com/a/df0a8f89eca1c05c
why i cant compile it???im noob so i just want to see how it actually work.
what i did
copy the coding
make new file and choose empty project
then i add at the source file: joker.cpp
paste the coding
run

the weird thing is i dont see anything...
do i need to do something so i can see how it run???ty
Edit:i found it. but how to fix this?
'Project2.exe' (Win32): Loaded 'C:\Program Files\AVAST Software\Avast\aswhookx.dll'. Cannot find or open the PDB file.
i already check the dll is still there. but my avast is off atm(not disable). how to fix this?
even after activate my avast the problem still persist...
Last edited on
> the weird thing is i dont see anything...
> do i need to do something so i can see how it run???ty

Not sure what the problem really is; try adding this line at the end (as the last line of) of main:
std::cin.get() ;
jblsx8 wrote:

// check for 3 of a kind
else if (secondp[0] == secondp[1] && secondp[0] == secondp[2] ||
secondp[0] == secondp[1] && secondp[0] == secondp[3] ||
secondp[0] == secondp[1] && secondp[0] == secondp[4] ||
secondp[0] == secondp[3] && secondp[0] == secondp[4] ||

secondp[1] == secondp[2] && secondp[1] == secondp[3] ||
secondp[1] == secondp[2] && secondp[1] == secondp[4] ||
secondp[1] == secondp[3] && secondp[1] == secondp[4] ||
secondp[2] == secondp[3] && secondp[2] == secondp[4] )


cout << "\n3 of a kind" << endl;


also you might want to check some of your other comparisons for edge cases. Three of a kind is an example of the mathematical "5 choose 3" -- there should be 10 of these but you only check for 8. In terms of indices (0-4):
0, 1, 2
0, 1, 3
0, 1, 4
0, 2, 3
0, 2, 4

0, 3, 4
1, 2, 3
1, 2, 4
1, 3, 4
2, 3, 4

Similarly, for check of "one pair" you have many more comparisons than needed. 5 choose 2 is also 10. You check if a[0]==a[4], but later also check if a[4]==a[0], which isn't needed.

Always try to think about the mathematical equivalent to show all combinations. Add some unit tests, sorta like what JLBorges did to test his is_straight() method, but on a grander scale to test many possibilities, asserting for example "Ten, Ten, Ten, Ten, Jack" is a Four of a Kind or loudly print out "Fail".

Looks great so far! Should work out well if you continue in the same vein, checking best hands first, so that it'd never look inside Three of a Kind logic if it already found a Four of a Kind.
Last edited on
> the weird thing is i dont see anything...
> do i need to do something so i can see how it run???ty
it refers to the running screen, as it only showed up and then closed up immediately after that
but once i did as what u suggested
i got a display like this after inserting std::cin.get() ;

true
true
false

:/ i dont see the point of all this though. if it just to display this 3 output might as well practice hello world.
(btw i didnt copy OP coding, i only copy urs; the one "do not use library" version)
which one should i replaced OP coding with urs coding if i what did earlier is just not enough coding to run it
[i had no intention to plagia or something here, I'm just spending my free time to learn by reverse learning successful coding since im having a hard time learning it a normal way

about the avast thing
'Project2.exe' (Win32): Loaded 'C:\Program Files\AVAST Software\Avast\aswhookx.dll'. Cannot find or open the PDB file.
i still dunno how to make it load the symbol. already google it, most answer seems doesnt understand the problem itself and said theres nothing wrong with it.
previously not only i had that problem but i also had this problem which is some problems involved with multiple of PDB that cannot be found or open
which is solved by ticking Microsoft Symbol Server in Tools>Option>Debugging
Alas it still doesnt solved the avast one
some said it can be solved by disable it, some said if u dont not have the respective antivirus involved[showed in the debugging list] ur pc contain running malware.
my antivirus is running fine atm i even check the respective file location and it is there but why it written cannot find or open the PDB file???
as for 15 sec antivirus[i also read that once it could find and open the aswhookx.dll, this is all it does but...], it does occur once i activate again my antivirus despite not clearing the issue shows in the debugging list.

[but lets just ignore this problem since the programming had no problem running along with it unless u happen to solve it before]
valiantZero wrote:
:/ i dont see the point of all this though. if it just to display this 3 output might as well practice hello world.

uh, JLBorges wrote a method to check for a Straight in poker and then he wrote a few test cases to make sure the method returns the expected output for his tests. He expected "true, true, false" and got "true, true, false" (method appears to be working so far).

If you want to output more things, then maybe add more things? lol. The method returns a boolean and the tests output those boolean results as text (it'd otherwise output 0's and 1's) -- what did you expect?

As for your antivirus -- it sounds like it was somehow set to actively monitor new processes, but when you try to run "Project2", the antivirus has trouble loading itself because it's somehow disabled. PDB files generally accompany libraries built in debug mode. Maybe if you could try to find out the name of the PDB file it was trying to load...? Perhaps reinstall your AVAST.
Last edited on
JLBorges,
Thank you once again!! I did not know it was possible to "convert" my string into integer values, very neat and just what I was looking for :)

Icy1,
thank you for picking that up! I "hard" coded my hand similar to JLBorges did and did not pick up I was missing two statements. Also, I ran into a problem on the previous assignment where I did not order my code correctly and I kept getting the wrong output. Now I know :)

Valiantzero, i'm not sure why you are getting those errors, although I do not use AVAST. Maybe try reinstalling it?

Topic archived. No new replies allowed.