Skip to content

Commit

Permalink
refactor: cleanup generator
Browse files Browse the repository at this point in the history
  • Loading branch information
Disservin committed Sep 29, 2024
1 parent 0f1c70c commit 325cfcd
Showing 1 changed file with 30 additions and 16 deletions.
46 changes: 30 additions & 16 deletions app/src/matchmaking/tournament/roundrobin/match_generator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,28 @@

namespace fastchess {

/**
* Generate matches for a round-robin tournament.
* Everytime a new match is required the next() function is called.
*/
class MatchGenerator {
public:
MatchGenerator(book::OpeningBook* opening_book, int initial_matchcount)
: i(0), j(1), k(0), g(0), engine_configs_size(config::EngineConfigs.get().size()) {
: ecidx_one(0), ecidx_two(1), round_id(0), pair_id(0), engine_configs_size(config::EngineConfigs.get().size()) {
opening_book_ = opening_book;
offset_ = initial_matchcount / config::TournamentConfig.get().games;
k = offset_;
offset_ = initial_matchcount / games;
round_id = offset_;
}

std::optional<std::tuple<std::size_t, std::size_t, std::size_t, int, std::optional<std::size_t>>> next() {
if (i >= engine_configs_size || j >= engine_configs_size) return std::nullopt;
if (ecidx_one >= engine_configs_size || ecidx_two >= engine_configs_size) return std::nullopt;

if (g == 0) {
if (pair_id == 0) {
// Fetch a new opening only once per round
opening = opening_book_->fetchId();
}

auto match = std::make_tuple(i, j, k, g, opening);
auto match = std::make_tuple(ecidx_one, ecidx_two, round_id, pair_id, opening);

advance();

Expand All @@ -36,20 +40,30 @@ class MatchGenerator {
private:
std::optional<std::size_t> opening;
book::OpeningBook* opening_book_;
std::size_t i, j, k, g;

// 1 - 2
const std::size_t games = config::TournamentConfig.get().games;
const std::size_t rounds = config::TournamentConfig.get().rounds;

// Engine configuration indices
std::size_t ecidx_one, ecidx_two;
std::size_t round_id;

// 0 - 1
std::size_t pair_id;
std::size_t engine_configs_size;
int offset_;

std::size_t games = config::TournamentConfig.get().games;
std::size_t rounds = config::TournamentConfig.get().rounds;
int offset_;

void advance() {
if (++g >= games) {
g = 0;
if (++k >= rounds) {
k = offset_;
if (++j >= engine_configs_size) {
j = ++i + 1;
if (++pair_id >= games) {
pair_id = 0;

if (++round_id >= rounds) {
round_id = offset_;

if (++ecidx_two >= engine_configs_size) {
ecidx_two = ++ecidx_one + 1;
}
}
}
Expand Down

0 comments on commit 325cfcd

Please sign in to comment.