Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Making my C++ code more efficient

Status
Not open for further replies.

q-bertsuit

Junior Member level 3
Joined
Nov 29, 2011
Messages
29
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,283
Activity points
1,559
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.

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;
		}

	}
 }
 

Getting rid of redundant C++ code

Hi,

I tried posting yesterday, but I though I could explain myself better than in the last post with this pseudo code.

struct Hand{
-----
-----
string PlayerHand;
}

Hand sPlayer1, sPlayer2, sPlayer3, sPlayer4, sPlayer5, sPlayer6, sPlayer7, sPlaer8;

void sorting::CreateHands(string LineRead)
{
int pos1;
sPlayer1.PlayerHand = LineRead;
pos1 = sPlayer1.find.(",");
sPlayer1.PlayerHand.erase(0, pos1 + 1);
}

So my question is, can I initialize all the structures sPlayer1 to sPlayer8 strings with a loop or dynamic memory, without having to copy paste the content of sorting::CreateHands() 8 times and changing it to sPlayer2 etc. ?

Thank you for your time.
 

Make the function more generic and pass by reference or pointer.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top