Skip to content

Instantly share code, notes, and snippets.

@Tarrasch
Created September 29, 2013 08:30
Show Gist options
  • Select an option

  • Save Tarrasch/6750456 to your computer and use it in GitHub Desktop.

Select an option

Save Tarrasch/6750456 to your computer and use it in GitHub Desktop.
Hosting a tournament with Electronic Chessboards? Do you only have a few of them, but still want all contestants to get to play on them at least once? Then try this program! :)
// LiveBordShuffler.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
const int RONDER = 5;
const int LIVEB = 5;
const int SPELBORD = 6;
const int BERGERLISTA6[6][5] =
{
{5, 1, 2, 3, 4},
{4, 0, 5, 2, 3},
{3, 4, 0, 1, 5},
{2, 5, 4, 0, 1},
{1, 2, 3, 5, 0},
{0, 3, 1, 4, 2},
};
typedef vector < int > VI;
typedef vector < VI > VVI;
typedef pair < int, int > II;
typedef vector < II > VII;
#define for(type, var, min, max) for(type var=min; var<max; var++)
#define all(c) c.begin(), c.end()
int _tmain(int argc, _TCHAR* argv[])
{
VII partier;
//Kontrollera bergerlista
for(int, spelare, 0, SPELBORD){
for(int, rond, 0, RONDER){
if(BERGERLISTA6[BERGERLISTA6[spelare][rond]][rond]!=spelare){
cout << "Bergerlistaerror!";
cin.get();
return 0;
}
}
}
//Skapa partilista
for(int, rond, 0, RONDER)
for(int, spelare, 0, SPELBORD)
if(BERGERLISTA6[spelare][rond]>spelare)
partier.push_back(II(spelare, BERGERLISTA6[spelare][rond]));
if(partier.size()!=SPELBORD*RONDER/2){
cout << "PartierERROR!";
cin.get();
return 0;
}
VI nLiveGivetBordsNR(SPELBORD, 0);
for(int, i, 0, SPELBORD){
nLiveGivetBordsNR[i]=LIVEB*RONDER/SPELBORD + (LIVEB*RONDER%SPELBORD>i);
}
int whiler =0;
while(whiler <10){
VVI matcherGivetBordsNR;
for(int, bord, 0, SPELBORD){
VI RoundUsed(RONDER, false);
VI PlayerUsed(RONDER+1, false);
VI matcher;
//randomStartNumber = (randomStartNumber+5)%15;
for(int, liveb, 0, nLiveGivetBordsNR[bord]){
bool found = false;
int randomStartNumber = rand()%partier.size();
for(int, matchforer, randomStartNumber, (partier.size()+randomStartNumber)){
int match = matchforer%partier.size();
if(!RoundUsed[match/3]){
if(!PlayerUsed[partier[match].first] && !PlayerUsed[partier[match].second]){
RoundUsed[match/3] = true;
PlayerUsed[partier[match].first] = PlayerUsed[partier[match].second] = true;
found = true;
matcher.push_back(match);
if((liveb == 2) + (find(all(PlayerUsed), false) != PlayerUsed.end()) == 1){
if(liveb == 2){
for(int, i, 0, PlayerUsed.size()){
PlayerUsed[i]=false;
}
}
}
else{
cout << "Bad matches!";
cin.get();
return 0;
}
break;
}
}
}
if(!found){
cout << "No match found!";
cin.get();
return 0;
}
}
//sort(all(matcher));
matcherGivetBordsNR.push_back(matcher);
}
bool badshuffle=false;
VI rond_use(RONDER, 0);
VI partiused(partier.size(), 0);
VI lagused(RONDER+1, 0);
for(int, i, 0, matcherGivetBordsNR.size()){
for(int, j, 0, matcherGivetBordsNR[i].size()){
if(++rond_use[matcherGivetBordsNR[i][j]/3]>LIVEB)
badshuffle=true;
if(++partiused[matcherGivetBordsNR[i][j]]>2)
badshuffle=true;
lagused[partier[matcherGivetBordsNR[i][j]].first]++;
lagused[partier[matcherGivetBordsNR[i][j]].second]++;
}
}
for(int, i, 0, RONDER+1)
if(lagused[i]<8)
badshuffle=true;
if(badshuffle)
continue;
whiler++;
string indents;
cout << "Iteration: " << whiler << "\n\n(";
for(int, i , 0, lagused.size())
cout << lagused[i] << ", ";
cout << ")\n\n";
for(int, rond, 0, RONDER){
indents = "";
cout << indents << "Rond " << rond+1 << ":" << endl;
for(int, bord, 0, SPELBORD){
indents = "\t";
cout << indents << "Bord " << bord+1 << ": ";
for(int, i, 0, matcherGivetBordsNR[bord].size()){
if(matcherGivetBordsNR[bord][i]/3==rond){
cout << "Lag " << partier[matcherGivetBordsNR[bord][i]].first+1 << " mot lag " << partier[matcherGivetBordsNR[bord][i]].second+1;
}
}
cout << endl;
}
}
cout << "___________________________________________________________________________\n";
if(whiler ==5)
cin.get();
}
cin.get();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment