// // Created by pranshav on 11/18/24. // #include "scores.h" Scores::Scores() : size(0), capacity(4) { array = new ScoreNode[capacity]; } Scores::~Scores() { delete[] array; } void Scores::addScore(char player) { for (int i = 0; i < size; i++) { if (array[i].player == player) { return; } } if (size == capacity) { growArray(); } array[size].player = player; array[size].score = 0; size++; } void Scores::updateScore(char player) { for (int i = 0; i < size; i++) { if (array[i].player == player) { array[i].score++; return; } } } ScoreNode Scores::findPlayerScore(char player) { for (int i = 0; i < size; i++) { if (array[i].player == player) { return array[i]; } } } void Scores::printResults() { // Sort players alphabetically std::sort(array, array + size, [](const ScoreNode& a, const ScoreNode& b) { return a.player < b.player; }); // Find max score int maxScore = 0; for (int i = 0; i < size; i++) { if (array[i].score > maxScore) { maxScore = array[i].score; } } // Count players with max score int countMaxScore = 0; for (int i = 0; i < size; i++) { if (array[i].score == maxScore) { countMaxScore++; } } // Print results for (int i = 0; i < size; i++) { std::cout << "Player " << array[i].player << " has " << array[i].score; if (array[i].score == maxScore) { std::cout << (countMaxScore > 1 ? " (tie)" : " (win)"); } std::cout << "." << std::endl; } } void Scores::growArray() { capacity *= 2; ScoreNode* temp = new ScoreNode[capacity]; for (int i = 0; i < size; i++) { temp[i] = array[i]; } delete[] array; array = temp; }