int determine_matches(player * the_player, game * the_game, int which_card)
{
if (determine_two_kind(the_player->get_card(1), the_player->get_card(2)))
{
//cout << "your cards are the same" << endl;
return(3);
}
for (int i = 1; i < 6; i++)
{
if (the_player->get_card(which_card)->get_card_number() == the_game->get_card(i)->get_card_number())
{
for(int j = i+1; j < 6; j++)
{
if(the_player->get_card(which_card)->get_card_number() == the_game->get_card(j)->get_card_number())
{
return 7;
}
//cout << "DEBUG: in determine_two_kind, MATCH" << endl;
}
return (1 + which_card);
}
}
//cout << "DEBUG: In determine_two_kind, NO MATCH" << endl;
return 0;
}
basically the code checks for two of a kind and if it finds a match then checks for 3 of kind.
it works absolutely fine, I'm just concerned that its quite an ugly bit of code, any suggestions on optimizing/ doing it a different way.
I don't think there is a performance issue here. If the code works as you intended, the only reason to "optimize" it would be to make it easier to read/manage and possibly detect bugs.
Here's one suggestion (assuming that the return type of get_card_number() is int)
I really don't understand your return values, but this might be a possible way to simply detect if you have two or three of a kind (without the inner loop):
1 2 3 4 5 6 7 8 9 10 11 12 13
int matches = 1;
int number = the_player->get_card(which_card)->get_card_number();
for (int i = 1; i < 6; i++)
{
if (number == the_game->get_card(i)->get_card_number())
{
matches += 1;
}
}
// matches equals:
// 1 = only the player's card
// 2 = two of a kind
// 3 = three of a kind etc.
bool determine_hand(player * the_player, game * the_game) // this function determines if there is a match in either hands.
{
int a, b, c;
a = determine_matches(the_player, the_game, 1);
b = determine_matches(the_player, the_game, 2);
c = a + b;
// 4 possibilities, TT(5) TF(3) FT(2) FF(0)
switch (c)
{
case 7:
cout << the_player->get_name() << " has three of a kind" << endl;
the_player->set_hand_number(3);
returntrue;
break;
case 6:
cout << the_player->get_name() << "'s cards are the same" << endl;
the_player->set_hand_number(1);
returntrue;
break;
case 5:
cout << the_player->get_name() << " has two pairs" << endl;
the_player->set_hand_number(2);
returntrue;
break;
case 3:
cout << the_player->get_name() << " has a pair on the 2nd card" << endl;
the_player->set_hand_number(1);
returntrue;
break;
case 2:
cout << the_player->get_name() << " has a pair on the 1st card" << endl;
the_player->set_hand_number(1);
returntrue;
break;
case 0:
cout << the_player->get_name() << ", unlucky you have no pairs" << endl;
the_player->set_hand_number(0);
returnfalse;
break;
default:
cout << "DEBUG: ERROR1 in determine_hand" << endl;
returnfalse;
break;
}
cout << "DEBUG: ERROR2 in determine_hand" << endl;
returnfalse;
}
it performs determine_hand twice (once for each card in the players hand) and then adds the values together. It's an interesting way of solving things but its the first thing that came to mind.
And then replace the numbers in your code with the name. It generates the same code, but looks better. You don't really need to assign numbers in the enum either, because it automatically gets 0, 1, 2...