1

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++;
      }
    }
  }
}
grom
  • 15,842
  • 19
  • 64
  • 67

1 Answers1

0

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++;
        }
      }
    }
  }
}
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • 1
    actually you don't need to loop to 52, because b = a + 1. So when a = 51.. b = 52 so the loop for b when a = 51 is skipped. – grom Dec 18 '12 at 00:19
  • I was so close to the answer too... I was doing c = a + 2. – grom Dec 18 '12 at 01:13