q-bertsuit
Junior Member level 3
Hi,
I'm making a program that reads in a list of 1000 Texas Hold 'em poker hands and decides which hand is the best. One line looks like this: 120,9C AH 4H JH 7H, 7C AS, 8S QH, 9S 3H, 2S 3S, QS 3D, 6H 6D, TD 3C, JC KS
Where 120 is the hand number, the first 5 cards (9C AH 4H JH 7H) are the community cards and the other 8 pairs are the player cards.
I have created a class with a function that creates 8 strings, one for each player, that contains his 7 cards that can be used to create the best 5 card hand. (i.e "9C AH 4H JH 7H 7C AS")
It's working fine, but I feel there is a lot of redundant code. I think it would be more efficient to use dynamic memory and pointers instead of the way it's currently done.
Any input would be greatly appreciated.
I'm making a program that reads in a list of 1000 Texas Hold 'em poker hands and decides which hand is the best. One line looks like this: 120,9C AH 4H JH 7H, 7C AS, 8S QH, 9S 3H, 2S 3S, QS 3D, 6H 6D, TD 3C, JC KS
Where 120 is the hand number, the first 5 cards (9C AH 4H JH 7H) are the community cards and the other 8 pairs are the player cards.
I have created a class with a function that creates 8 strings, one for each player, that contains his 7 cards that can be used to create the best 5 card hand. (i.e "9C AH 4H JH 7H 7C AS")
It's working fine, but I feel there is a lot of redundant code. I think it would be more efficient to use dynamic memory and pointers instead of the way it's currently done.
Any input would be greatly appreciated.
Code:
class sorting
{
private:
int PlayerNumber;
int pos1;
string ComunityCards;
public:
string Player1, Player2, Player3, Player4, Player5, Player6, Player7, Player8;
void CreateHands(string LineRead);
};
void sorting::CreateHands(string LineRead)
{
//Find comunitycards
ComunityCards = LineRead;
pos1 = ComunityCards.find (",");
ComunityCards.erase(0, pos1 + 1);
pos1 = ComunityCards.find (",");
ComunityCards.erase(pos1);
cout << ComunityCards << endl;
PlayerNumber = 0;
//Find player hands
while(PlayerNumber != 8){
PlayerNumber ++;
switch (PlayerNumber)
{
case 1:
Player1 = LineRead;
for (int i = 0; i < PlayerNumber + 1; i++){
pos1 = Player1.find (",");
Player1.erase(0, pos1 + 1);
}
pos1 = Player1.find (",");
Player1.erase(pos1);
Player1 = ComunityCards + Player1;
cout << Player1 << endl;
break;
case 2:
Player2 = LineRead;
for (int i = 0; i < PlayerNumber + 1; i++){
pos1 = Player2.find (",");
Player2.erase(0, pos1 + 1);
}
pos1 = Player2.find (",");
Player2.erase(pos1);
Player2 = ComunityCards + Player2;
cout << Player2 << endl;
break;
case 3:
Player3 = LineRead;
for (int i = 0; i < PlayerNumber + 1; i++){
pos1 = Player3.find (",");
Player3.erase(0, pos1 + 1);
}
pos1 = Player3.find (",");
Player3.erase(pos1);
Player3 = ComunityCards + Player3;
cout << Player3 << endl;
break;
case 4:
Player4 = LineRead;
for (int i = 0; i < PlayerNumber + 1; i++){
pos1 = Player4.find (",");
Player4.erase(0, pos1 + 1);
}
pos1 = Player4.find (",");
Player4.erase(pos1);
Player4 = ComunityCards + Player4;
cout << Player4 << endl;
break;
case 5:
Player5 = LineRead;
for (int i = 0; i < PlayerNumber + 1; i++){
pos1 = Player5.find (",");
Player5.erase(0, pos1 + 1);
}
pos1 = Player5.find (",");
Player5.erase(pos1);
Player5 = ComunityCards + Player5;
cout << Player5 << endl;
break;
case 6:
Player6 = LineRead;
for (int i = 0; i < PlayerNumber + 1; i++){
pos1 = Player6.find (",");
Player6.erase(0, pos1 + 1);
}
pos1 = Player6.find (",");
Player6.erase(pos1);
Player6 = ComunityCards + Player6;
cout << Player6 << endl;
break;
case 7:
Player7 = LineRead;
for (int i = 0; i < PlayerNumber + 1; i++){
pos1 = Player7.find (",");
Player7.erase(0, pos1 + 1);
}
pos1 = Player7.find (",");
Player7.erase(pos1);
Player7 = ComunityCards + Player7;
cout << Player7 << endl;
break;
case 8:
Player8 = LineRead;
for (int i = 0; i < PlayerNumber + 1; i++){
pos1 = Player8.find (",");
Player8.erase(0, pos1 + 1);
}
Player8 = ComunityCards + Player8;
cout << Player8 << endl;
break;
default:
cout << "Unknown";
break;
}
}
}