fix printScore

This commit is contained in:
Sandipsinh Rathod 2024-10-11 17:11:40 -04:00
parent 2acdae42e4
commit 4f65ec59e8

@ -277,22 +277,6 @@ int normalize(char c) {
return toLowerCase(c) - 'a'; return toLowerCase(c) - 'a';
} }
void swap(myPair<char, myPair<int, bool> > *a, int i, int j) {
myPair<char, myPair<int, bool> > temp = a[i];
a[i] = a[j];
a[j] = temp;
}
void bubbleSort(myPair<char, myPair<int, bool> > *a) {
for (int i = 0; i < 25; i++) {
for (int j = 0; j < 25 - i; j++) {
if (a[j].second.first > a[j + 1].second.first)
swap(a, j, j + 1);
}
}
}
void printScores(int *points, char blacklist) { void printScores(int *points, char blacklist) {
myPair<char, myPair<int, bool> > *arr = static_cast<myPair<char, myPair<int, bool> > *>(alloc( myPair<char, myPair<int, bool> > *arr = static_cast<myPair<char, myPair<int, bool> > *>(alloc(
26 * sizeof(myPair<char, myPair<int, bool> >))); 26 * sizeof(myPair<char, myPair<int, bool> >)));
@ -307,31 +291,32 @@ void printScores(int *points, char blacklist) {
arr[i] = p; arr[i] = p;
} }
int maxScore = -1;
int maxCount = 0;
for (int i = 0; i < 26; i++) { for (int i = 0; i < 26; i++) {
if (arr[i].second.first == -1) { if (arr[i].second.first > maxScore) {
continue; maxScore = arr[i].second.first;
} maxCount = 1;
for (int j = 0; j < 26; j++) { } else if (arr[i].second.first == maxScore) {
if (arr[j].second.first == -1 || arr[j].first == arr[i].first) { maxCount++;
continue;
}
if (arr[i].second.first == arr[j].second.first) {
arr[i].second.second = true;
arr[j].second.second = true;
}
} }
} }
bubbleSort(arr); for (int i = 0; i < 26; i++) {
for (int i = 25; i > -1; i--) {
if (arr[i].second.first != -1) { if (arr[i].second.first != -1) {
pt "Player " << arr[i].first << " has " << arr[i].second.first << " boxes" << ( pt "Player " << arr[i].first << " has " << arr[i].second.first << (arr[i].second.first < 2
(i == 25 || arr[i].second.second) && arr[i].first != blacklist ? " box"
? arr[i].second.second : " boxes");
? " (tie)\n" if (arr[i].second.first == maxScore) {
: " (win)\n" if (maxCount > 1) {
: nl); pt " (tie)";
} else if (arr[i].first != blacklist) {
pt " (win)";
}
}
pt nl;
} }
} }