Open Side Menu Go to the Top

02-15-2009 , 12:06 PM
Hi,

I am trying to make a texas hold 'em simulator using c++. Currently I am using the twoplustwo / RayW evaluator to evaluate the hands, this is really my fiirst poker project so I don't know a huge amount about it. Please move this post if it is in the wrong forum, I just assumed this was right since this is where the hand evaluator source was.

My code is adapted from the code posted on codingthewheel.com
its very similar to the test program that was given there, just made a few changes for looping and testing hole cards and community cards. It needs the handranks.dat to run.

Here is my code:
Code:
#include <windows.h>
#include <tchar.h>
#include <iostream>
#include <cstdlib>
#include "../RandGen/MersenneTwister.h"
using namespace std;

// The handranks lookup table- loaded from HANDRANKS.DAT.
int HR[32487834];
int deck[51];
MTRand mtrand1;


int LookupHand(int* pCards, int* holeCards)
{
    int p = HR[53 + *holeCards++];
    p = HR[p + *holeCards++];
    p = HR[p + *pCards++];
    p = HR[p + *pCards++];
    p = HR[p + *pCards++];
    p = HR[p + *pCards++];
	return HR[p + *pCards++];
}

void gen_deck(){
	for (int i=0;i<52; i++){
		deck[i] = i;
	}
	deck[1] = 0;
	deck[14] = 0;
}


int _tmain(int argc, _TCHAR* argv[])
{


	printf("Testing the Two Plus Two 7-Card Evaluator\n");
	printf("-----------------------------------------\n\n");

	// Load the HandRanks.DAT file and map it into the HR array
	printf("Loading HandRanks.DAT file...");
	memset(HR, 0, sizeof(HR));
	FILE * fin = fopen("HandRanks.dat", "rb");
	if (!fin)
	    return false;
	size_t bytesread = fread(HR, sizeof(HR), 1, fin);	// get the HandRank Array
	fclose(fin);
	printf("complete.\n\n");

	printf("Using a Monte-Carlo simulation to find the odds of AA winning in a heads up game\n\n\n");

	int myHoleHand[2] = {13, 26};
	int commCards[5];
	int oppHoleHand[2];
	int winCounter = 0;
	int lossCounter = 0;
	for(int loopy=0; loopy<100000; loopy++){
		gen_deck();
		for(int p=0; p<5; p++){
			int randCommCard = mtrand1.randInt( 52 );
			if (deck[randCommCard] == 0) { p--; }
			else { commCards[p] = randCommCard; }
		}
		for (int j=0; j<2; j++){
			int randHoleCard = mtrand1.randInt( 52 );
			if (deck[randHoleCard] == 0) { j--; }
			else {oppHoleHand[j] = randHoleCard;}
		}
	
		int oppHandValue = LookupHand(commCards, oppHoleHand);
		int myHandValue = LookupHand(commCards, myHoleHand);
		if (myHandValue < oppHandValue){
			++winCounter;
		}
		else {
			++lossCounter;
		}

	}
	printf("I won %d times \n", winCounter);
	printf("I lost %d times \n", lossCounter);

	std::cin.get();
	return 0;
}
So the first problem is that this does not seem to be giving me the correct answer... When I run it with as is (hole cards AA), I only win about 53% of the time, surely that is incorrect, I can't seem to work out why this is happening though. I assume that the actual hand-eval part is correct since it is from the coding the wheel site, but the rest of it I'm not so sure about...

On that note, am I using the correct method for the monte-carlo, using new hole and community cards for every iteration?

Next problem is that my code is super-cumbersome, I'm not great at c++ and I am sure there's a much better / nicer / faster way to do all this, especiallly the bits where I generate a new deck every iteration and also the way I check if I've used a card (setting the array value to zero). Can anyone suggest a better way to do this stuff?

Thanks for any help
coding hand simulation Quote
coding hand simulation
150% up to $2,000 Welcome Bonus on CoinPoker
Join the action now
Daily Rewards • Splash Pots • CoinRaces
coding hand simulation

      
m