for (int i = 0; i < hand_size; i++) if (suits[i] == 'c') clubs++; else if (suits[i] == 'd') diamonds++; else if (suits[i] == 'h') hearts++; else if (suits[i] == 's') spades++; if (clubs >= 3) points = points + clubs; if (diamonds >= 3) points = points + diamonds; if (hearts >= 3) points = points + hearts; if (spades >= 3) points = points + spades;
Confusing runs with n-of-a-kind?
// Converts the letter of the suit to uppercase. void convertsuitup(char *suits); { while ( *suits != '\0' ) { if (islower(*suits)) *suits = toupper(*suits); ++suits; } }
for (int i = 0; suits[i] != '\0'; i++) // check suits suits[i] = toupper(suits[i]);
suits
is not a string.
It doesn't end with a null byte.
for (int i = 0; i < s; ++i) if ((sute[i] == sute[i + 1]) && (i + 1 < s)) ++streak; else if ((sute[i] != sute[i + 1]) && (i + 1 < s)) return 0;
They don't inspire confidence.
A bad habit to accquire.
for (int i = 0; i < s; ++i) if ((i + 1 < s) && (sute[i] == sute[i + 1])) ++streak; else if ((i + 1 < s) && (sute[i] != sute[i + 1])) return 0;
void bubbleSort(int ranks[], int hand_size); { int hold = 0; // blah blah blah }
It's a prototype declaration followed by a compound statement.
int count_hand(int ranks[], char suits[], int hand_size) { void bubbleSort(int ranks[], int hand_size); { // blah blah blah } }
int c_counter = 0, d_counter = 0, h_counter = 0, s_counter = 0; for (int i = 0; i < hand_size; i++) { if (suits[i] == 'c' || suits[i] == 'C') ++c_counter; if (suits[i] == 'd' || suits[i] == 'D') ++d_counter; if (suits[i] == 'h' || suits[i] == 'H') ++h_counter; if (suits[i] == 's' || suits[i] == 'S') ++s_counter; } if ((c_counter == hand_size) || (d_counter == hand_size) || (h_counter == hand_size) || (s_counter == hand_size)) return hand_size; else return 0;
for (int i = 0; i < s; ++i) if ((i + 1 < s) && (sute[i] == sute[i + 1])) ++streak; else if ((i + 1 < s) && (sute[i] != sute[i + 1])) return 0;
(i + 1 < s)
is repeated code.
for (int i = 0; i < s - 1; ++i) if (sute[i] == sute[i + 1]) ++streak; else if (sute[i] != sute[i + 1]) return 0;
for (int i = 0; i < s - 1; ++i) if (sute[i] == sute[i + 1]) ++streak; else return 0;
for (int i = 0; i < s - 1; ++i) if (sute[i] != sute[i + 1]) return 0; return s;
freq
for?
int freq[52]; freq[z] = 0; ++freq[r[i]]; if (freq[r[i]] > maxfreq) maxfreq = freq[r[i]]; while (freq[count] > 0) { if ( freq[count+1] >= 1 ) { if ((freq[count] >= max_freq_streak) && (freq[count] > 1) ) freq_streak += freq[count];
// Sort the cards int current_card = 0; // Number of sorted items while (current_card < hand_size) { int pos = findsmallest (ranks, current_card, hand_size); // Temporary holder int temp = ranks[current_card]; // Start swap ranks[current_card] = ranks[pos]; ranks[pos] = temp; temp = suits[current_card]; suits[current_card] = suits[pos]; suits[pos] = temp; current_card++; } // Dynamically allocate special arrays to keep track // of the properties of each card (node) and initialize these arrays. int* parent = new int[hand_size]; int* len_of_run = new int[hand_size]; // Initialize the property arrays for (i = 0; i < hand_size; i++) { // Initially the parent of a node is itself parent[i] = ranks[i]; // Initial length of a run = 1, the node itself len_of_run[i] = 1; } // Populate the property arrays. Find both the parent of each node // and the length of the runs for each root node for (i = 0; i < hand_size; i++) { for (j = i + 1; j < hand_size; j++) { // Set the parent if (ranks[j] == ranks[j - 1]) parent[j] = parent[j - 1]; else if (ranks[j] == ranks[j - 1] + 1) parent[j] = ranks[j - 1]; // Set the run length for each root node; // each node whose parent is itself if ((ranks[j] == ranks[i] + len_of_run[i]) && (parent[i] == ranks[i])) len_of_run[i]++; } } // Use the properties to calculate the number of // runs for each root node and the total number of points. int num_of_runs = 0, num_of_children = 0; int curr_parent = 0, my_parent = 0, wrong_parent = 0; for (i = 0; i < hand_size; i++) { // Only process nodes that are the non-repeated roots // of legal runs if (len_of_run[i] > 2) { num_of_runs = 1; curr_parent = ranks[i]; wrong_parent = curr_parent + len_of_run[i] - 1; // Check each card after the i-th for (k = i + 1; k < hand_size; k++) { // If the ranks are the same it must be a repeated root node // so go to the next one. if (ranks[i] == ranks[k]) continue; my_parent = parent[k]; // If the parent is higher than the length of the // run then we must have reached a different // (higher) run so quit. if (my_parent >= wrong_parent) break; // If the parent of a given node = the current // parent, then it must be a child if (my_parent == curr_parent) { num_of_children++; } // Else if the parent is greater than the current // parent then it must be a grandchild of the // current parent; calculate the num of // runs, and then re-init else if (my_parent > curr_parent) { // Calculate num of runs num_of_runs = num_of_runs * num_of_children; // Initialize curr_parent = my_parent; // The current node is the first child of the // new current parent num_of_children = 1; } } // Increase the num of runs to account for the last set // of children num_of_runs = num_of_runs * num_of_children; points += len_of_run[i] * num_of_runs; // Re-initalize num_of_children = 0; num_of_runs = 0; } } // Clean up delete parent; delete len_of_run; |
int runs(int ranks[], int hand_size) { // Calculates the total points from all runs in a hand. int hold, consec_card_counter = 1, run_size = 1, number_of_runs = 1, rank_counter[13] = {0}; for (int pass = 0; pass < hand_size - 1; pass++) for (int i = 0; i < hand_size - 1; i++) if (ranks[i] > ranks[i + 1]) { hold = ranks[i]; ranks[i] = ranks[i + 1]; ranks[i + 1] = hold; } for (int i = 1; i < hand_size; i++) if (ranks[i - 1] <= ranks[i]) ++consec_card_counter; for (int i = 1; i < consec_card_counter; i++) if (ranks[i - 1] == ranks[i] - 1) ++run_size; for (int j = 0; j < 13; j++) for (int i = 0; i < consec_card_counter; i++) if (ranks[i] == j + 1) ++rank_counter[j]; for (int j = 0; j < 13; j++) if (rank_counter[j] >= 1) number_of_runs *= rank_counter[j]; if (run_size >= 3 && run_size <= hand_size) return run_size * number_of_runs; else return 0; } |
This page last modified on 19 June 2001.