#include "racelistgenerator.h" RaceListGenerator::RaceListGenerator(vector driverIds, vector carIds) { this->driverIds = driverIds; this->carIds = carIds; } bool RaceListGenerator::matchCombination(struct Combination x1, struct Combination x2) { // returns true if combination is possible; means drivers as well as cars // differs if (x1.car != x2.car) { if (x1.driver != x2.driver) { return true; } return false; } return false; } bool RaceListGenerator::matchList(vector x1, vector x2) { if (x1.size() != x2.size()) { return false; } for (size_t i = 0; i < x1.size(); i++) { if (!matchCombination(x1.at(i), x2.at(i))) { // no match; driver or car used twice return false; } } return true; } // returns a valid list for a race // structur: shellcar, shelldriver, deacar, deadriver vector> RaceListGenerator::getList() { std::srand(unsigned(std::time(nullptr))); vector drivers = this->driverIds; vector cars = this->carIds; vector shellList; vector deaList; for (int driver : drivers) { for (int car : cars) { struct Combination x = {driver, car}; shellList.push_back(x); deaList.push_back(x); } } // shuffle shell list std::random_shuffle(shellList.begin(), shellList.end(), myrandom); while (1) { std::random_shuffle(deaList.begin(), deaList.end(), myrandom); if (matchList(shellList, deaList) == true) { break; } } vector> vecToReturn; vector subVec; for (unsigned long i = 0; i < shellList.size(); i++) { subVec.push_back(shellList.at(i).car); subVec.push_back(shellList.at(i).driver); subVec.push_back(deaList.at(i).car); subVec.push_back(deaList.at(i).driver); vecToReturn.push_back(subVec); // print(vecToReturn.back()); subVec.clear(); } return vecToReturn; } /* int main() { vector drivers = {0, 1, 2}; vector cars = {0, 1, 2, 3, 4}; vector> list = getList(); // std::for_each(list.begin(), list.end(), print); return 0; } */ void RaceListGenerator::print(vector v) { std::for_each(v.begin(), v.end(), [](int vec) { cout << vec << " "; }); cout << " " << endl; } int RaceListGenerator::myrandom(int i) { return std::rand() % i; }