11 #include "ziggurat.hpp"
15 #define MAX_PLAYERS 16
17 float match_stddev = 70.0f;
23 struct highest_ranking {
25 bool operator() (const player &a, const player &b) const {
26 if (a.points != b.points) {
27 return a.points > b.points;
29 return a.margin > b.margin;
36 return rand() * (1.0f / RAND_MAX);
39 // FIXME: replace rejection method with a better method! this one can only ever go to [-5z, +5z]
41 float draw_gaussian(float stddev)
44 float z = draw_unit() * 10.0f - 5.0f;
45 float y = draw_unit();
52 float draw_gaussian(float mu, float stddev)
54 static bool inited = false;
55 static long unsigned seed = time(NULL);
57 static float fn[128], wn[128];
59 r4_nor_setup(kn, fn, wn);
63 return r4_nor(&seed, kn, fn, wn) * stddev + mu;
67 int main(int argc, char **argv)
69 int trials = atoi(argv[1]);
71 if (scanf("%f", &match_stddev) != 1) {
72 fprintf(stderr, "Could't read match stddev\n");
77 if (scanf("%d", &num_players) != 1) {
78 fprintf(stderr, "Could't read number of players\n");
82 if (num_players > MAX_PLAYERS) {
83 fprintf(stderr, "Max %d players supported\n", MAX_PLAYERS);
87 vector<string> players;
88 map<string, int> player_map;
89 float ratings[MAX_PLAYERS];
90 bool has_scores[MAX_PLAYERS][MAX_PLAYERS];
91 for (int pl1 = 0; pl1 < num_players; ++pl1) {
92 for (int pl2 = 0; pl2 < num_players; ++pl2) {
93 has_scores[pl1][pl2] = false;
97 int scores[MAX_PLAYERS][MAX_PLAYERS];
98 for (int i = 0; i < num_players; ++i) {
101 int ret = scanf("%s %f", buf, &rating);
103 fprintf(stderr, "Couldn't read player %d\n", i);
110 players.push_back(buf);
116 char pl1[256], pl2[256];
119 if (scanf("%s %s %d %d", pl1, pl2, &score1, &score2) != 4) {
123 if (player_map.count(pl1) == 0) {
124 fprintf(stderr, "Unknown player '%s'\n", pl1);
127 if (player_map.count(pl2) == 0) {
128 fprintf(stderr, "Unknown player '%s'\n", pl2);
132 scores[player_map[pl1]][player_map[pl2]] = score1;
133 scores[player_map[pl2]][player_map[pl1]] = score2;
134 has_scores[player_map[pl1]][player_map[pl2]] = true;
135 has_scores[player_map[pl2]][player_map[pl1]] = true;
138 int placements[MAX_PLAYERS][MAX_PLAYERS];
139 for (int i = 0; i < num_players; ++i) {
140 for (int j = 0; j < num_players; ++j) {
141 placements[i][j] = 0;
145 for (int i = 0; i < trials; ++i) {
146 // draw the missing matches
147 for (int pl1 = 0; pl1 < num_players; ++pl1) {
148 for (int pl2 = pl1 + 1; pl2 < num_players; ++pl2) {
149 if (has_scores[pl1][pl2]) {
153 float mu = ratings[pl1] - ratings[pl2];
155 int score = lrintf(draw_gaussian(mu, match_stddev));
156 scores[pl1][pl2] = score;
157 scores[pl2][pl1] = -score;
161 // sum up the points and margin for each player
162 player stats[MAX_PLAYERS];
163 for (int pl1 = 0; pl1 < num_players; ++pl1) {
164 stats[pl1].player_index = pl1;
165 stats[pl1].points = 0;
166 stats[pl1].margin = 0;
167 for (int pl2 = 0; pl2 < num_players; ++pl2) {
171 stats[pl1].margin += scores[pl1][pl2];
172 stats[pl1].margin -= scores[pl2][pl1];
173 if (scores[pl1][pl2] > scores[pl2][pl1]) {
174 stats[pl1].points += 2;
175 } else if (scores[pl1][pl2] == scores[pl2][pl1]) {
181 // order by points and then margin
182 sort(stats, stats + num_players, highest_ranking());
183 for (int j = 0; j < num_players; ++j) {
184 ++placements[stats[j].player_index][j];
188 for (int i = 0; i < num_players; ++i) {
189 printf("%-15s", players[i].c_str());
190 for (int j = 0; j < num_players; ++j) {
191 printf(" %5d", placements[i][j]);