High score table for the arcade cabinet

C++ · C++ · beginner · modification

Adds the high score table that the attract-mode screen will show between demo loops. `leaderboard.hpp` declares two new functions: `format_row`, which lays out one row (rank, initials, points) with `std::format`, and `print_board`, which writes the header and then one row per entry of the list `Leaderboard::top()` returns, or a `NO SCORES YET` line when nobody has played yet. `leaderboard.cpp` implements them next to the existing `submit` and `top`, which are unchanged. `tests/leaderboard_test.cpp` gains tests for the new table code.

C++20, built with CMake (existing, unchanged; it compiles every `src/**/*.cpp` into the `cabinet` library and every `tests/*_test.cpp` into one test binary) by Apple clang 17 locally and GCC 13 in CI, with `-Wall -Wextra -Wconversion` treated as errors in CI; standard library only. The cabinet runs a single-threaded game loop, so nothing here is shared between threads. `Score` and `Leaderboard` are existing and unchanged: `Score::initials` is always exactly 3 characters `A` to `Z` (the entry screen enforces it) and `Score::points` is 0 to 999,999,999; `Leaderboard::top()` returns a copy of the table, at most `kMaxEntries` (10) entries, highest first. The tests use the repo's existing helper `tests/check.hpp` (`TEST(name)` registers a test, `CHECK(expr)` and `CHECK_EQ(a, b)` record a failure, and `tests/check_main.cpp` runs every registered test and exits non-zero on any failure). The attract-mode screen and the operator's service menu call `print_board(display_stream, board.top())` in a follow-up PR; until then nothing calls it outside the tests.

Requirements

Files touched

--- src/leaderboard/leaderboard.hpp
 #include <cstddef>
 #include <cstdint>
+#include <iosfwd>
 #include <string>
 #include <vector>
 };
 
+// One table row: rank right-aligned in 2 columns, ". ", initials, " ",
+// points right-aligned in 9 columns, e.g. " 1. AAA    123450".
+std::string format_row(std::size_t rank, const Score& score);
+
+// Writes the high score table for `scores`, already in table order.
+void print_board(std::ostream& out, const std::vector<Score>& scores);
+
 }  // namespace leaderboard
 
--- src/leaderboard/leaderboard.cpp
 
 #include <algorithm>
+#include <format>
+#include <ostream>
 #include <utility>
 
 }
 
+std::string format_row(std::size_t rank, const Score& score) {
+    return std::format("{:>2}. {} {:>9}", rank, score.initials, score.points);
+}
+

Review this PR

C++ practice