Question

There are 52C2 * 50C2 / 2 = 812175 possible head-to-head match ups in Hold 'em. Assuming I have an array with each card, how can I enumerate all these match ups?

For example, to enumerate all possible starting hands is:

for (int a = 0; a < 51; ++a) {
  for (int b = a + 1; b < 52; ++b) {
    println(cards[a] + "," + cards[b]);
  }
}

I worked out can have all match ups twice with (get both As,Ah vs Kc,Kd and Kc,Kd vs As,Ah):

long total = 0;
for (int a = 0; a < 51; ++a) {
  for (int b = a + 1; b < 52; ++b) {
    for (int c = 0; c < 51; ++c) {
      for (int d = c + 1; d < 52; ++d) {
        total++;
      }
    }
  }
}
Was it helpful?

Solution

Your code prints the correct result, but doesn't iterate over all the cards correctly. a and c should loop up to 52. The extra hands need to be removed with an if statement:

for (int a = 0; a < 52; ++a) {
  for (int b = a + 1; b < 52; ++b) {
    for (int c = 0; c < 52; ++c) {
      for (int d = c + 1; d < 52; ++d) {
        if (c != a && c != b && d != a && d != b) {
          total++;
        }
      }
    }
  }
}

This can then be modified to eliminate the duplicate hands:

for (int a = 0; a < 52; ++a) {
  for (int b = a + 1; b < 52; ++b) {
    for (int c = a + 1; c < 52; ++c) {
      for (int d = c + 1; d < 52; ++d) {
        if (c != b && d != b) {
          total++;
        }
      }
    }
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top