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
- Add `leaderboard::print_board(std::ostream& out, const std::vector<Score>& scores)`, which writes the high score table for the cabinet's attract-mode screen. `scores` is what `Leaderboard::top()` returns: 0 to 10 entries, already in table order (highest first).
- The table is the header line `== HIGH SCORES ==` followed by exactly one row per entry of `scores`, in the order given, and nothing else. The entry at index 0 gets rank 1, the next rank 2, and so on.
- Each row is built by the new `format_row(rank, score)`: the rank right-aligned in 2 columns, `. `, the initials, a space, and the points right-aligned in 9 columns, for example ` 1. AAA 123450`. Every line ends with `\n`.
- When `scores` is empty, the table is the header followed by the single line ` NO SCORES YET` (two leading spaces).
Files touched
- src/leaderboard/leaderboard.hpp
- src/leaderboard/leaderboard.cpp
- tests/leaderboard_test.cpp
--- 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);
+}
+