Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chapter 10 - Project 3 #122

Open
ShaunakRawat opened this issue Jan 3, 2024 · 0 comments
Open

Chapter 10 - Project 3 #122

ShaunakRawat opened this issue Jan 3, 2024 · 0 comments

Comments

@ShaunakRawat
Copy link

Issue in the solution for Project 3 of Chapter 10:

In the following code chunk from the solution, after reading a three-of-a-kind, the loop reads it again from the second card and sees a pair. The program thus detects a full house.

int matches = 0;
    for (int i = 0; i < NUM_CARDS; i++) {
        matches = 0;
        for (int j = i + 1; j < NUM_CARDS; j++) {
            if (hand[j][0] == hand[i][0])
                matches++;
        }

        if (matches == 1) pairs++;
        if (matches == 2) three = true;
        if (matches == 3) four = true;
    }

Here's a working solution. The following code chunk is from KNKing's online solutions for select exercises (http://knking.com/books/c2/answers/c10.html):

/* check for 4-of-a-kind, 3-of-a-kind, and pairs by
     looking for "runs" of cards with identical ranks */
  card = 0;
  while (card < NUM_CARDS) {
    rank = hand[card][RANK];
    run = 0;
    do {
      run++;
      card++;
    } while (card < NUM_CARDS && hand[card][RANK] == rank);
    switch (run) {
      case 2: pairs++;      break;
      case 3: three = true; break;
      case 4: four = true;  break;
    }
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant