Files
Rennbahn/racelistgenerator.cpp

97 lines
2.6 KiB
C++

#include "racelistgenerator.h"
RaceListGenerator::RaceListGenerator(vector<int> driverIds,
vector<int> 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<struct Combination> x1,
vector<struct Combination> 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<vector<int>> RaceListGenerator::getList() {
std::srand(unsigned(std::time(nullptr)));
vector<int> drivers = this->driverIds;
vector<int> cars = this->carIds;
vector<struct Combination> shellList;
vector<struct Combination> 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<vector<int>> vecToReturn;
vector<int> 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<int> drivers = {0, 1, 2};
vector<int> cars = {0, 1, 2, 3, 4};
vector<vector<int>> list = getList();
// std::for_each(list.begin(), list.end(), print);
return 0;
}
*/
void RaceListGenerator::print(vector<int> v) {
std::for_each(v.begin(), v.end(), [](int vec) { cout << vec << " "; });
cout << " " << endl;
}
int RaceListGenerator::myrandom(int i) {
return std::rand() % i;
}