updated GPL headers in source code
[dribble.git] / division.c
blob39700ead40828e3cf4ca45fbbd04eb2ca816b789
1 /*
2 dribble - a football manager game
3 Copyright (C) 2008 Jonas Sandberg
5 This file is part of dribble.
7 dribble is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 any later version.
12 dribble is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with dribble. If not, see <http://www.gnu.org/licenses/>.
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <strings.h>
23 #include "defs.h"
25 struct match
27 int home;
28 int away;
31 struct division
33 int country;
34 int division;
35 int n_teams;
36 int *teams;
37 struct match **matches;
40 static int dnum = 0;
41 static struct division *dvec;
43 static void assign_matches(struct division *d)
45 int total_rounds;
46 int matches_per_round;
47 int *c1;
48 int *c2;
49 int round;
50 int mn;
51 int i;
53 total_rounds = d->n_teams-1;
54 matches_per_round = d->n_teams/2;
55 c1 = calloc(total_rounds, sizeof(*c1));
56 c2 = calloc(total_rounds, sizeof(*c2));
57 d->matches = malloc(sizeof(*d->matches)*total_rounds);
59 for(i=0 ; i<total_rounds ; i++)
61 d->matches[i] = malloc(sizeof(**d->matches)*matches_per_round);
64 if(d->matches == NULL)
66 perror("malloc");
69 for(i=0; i<matches_per_round; i++)
71 c1[i] = i;
72 c2[i] = total_rounds-i;
75 for(round=0 ; round<total_rounds; round++)
77 int last;
79 for(mn=0; mn<matches_per_round; mn++)
81 if(mn == 0 && round%2 == 0)
83 d->matches[round][mn].home = c1[mn];
84 d->matches[round][mn].away = c2[mn];
86 else if(mn == 0 && round%2 == 1)
88 d->matches[round][mn].home = c2[mn];
89 d->matches[round][mn].away = c1[mn];
91 else if(round%2 == 0)
93 d->matches[round][mn].home = c1[mn];
94 d->matches[round][mn].away = c2[mn];
96 else if(round%2 == 1)
98 d->matches[round][mn].home = c2[mn];
99 d->matches[round][mn].away = c1[mn];
103 /* Fixture shift */
104 last = c1[matches_per_round-1];
105 for(mn=matches_per_round-1 ; mn>0; mn--)
107 c1[mn] = c1[mn-1];
109 c1[0] = c2[1];
110 c2[matches_per_round] = last;
111 for(mn=1 ; mn<matches_per_round; mn++)
113 c2[mn] = c2[mn+1];
117 for(round=0 ; round<total_rounds; round++)
119 for(mn=0; mn<matches_per_round; mn++)
121 int home;
122 int away;
123 home = d->matches[round][mn].home;
124 away = d->matches[round][mn].away;
128 free(c1);
129 free(c2);
132 int division_init(int elements)
134 dvec = calloc(elements, sizeof(*dvec));
136 if(dvec == NULL)
138 return -1;
140 else
142 return 0;
146 int division_rand_players_base()
148 return 18;
151 int division_rand_players_top()
153 return 5;
156 int division_generate(int country, int division_rank)
158 struct division *d;
159 int i;
161 d = &dvec[dnum];
162 d->n_teams = country_num_teams_in_division(country, division_rank);
163 assign_matches(d);
164 d->teams = calloc(d->n_teams, sizeof(*d->teams));
166 for(i=0 ; i<d->n_teams ; i++)
168 d->teams[i] = team_generate(country, division_rank, i, division_rand_players_base() + (rand() % division_rand_players_top()));
171 return dnum++;
174 int division_num_teams(int division)
176 return dvec[division].n_teams;
179 void division_free()
181 int i;
182 int j;
184 for(i=0; i<dnum; i++)
186 free(dvec[i].teams);
188 for(j=0 ; j<dvec[i].n_teams-1 ; j++)
190 free(dvec[i].matches[j]);
193 free(dvec[i].matches);
196 free(dvec);
199 #ifdef FTEST_DIVISION
200 #include <assert.h>
201 #include <stdlib.h>
202 #include <time.h>
203 int main(int argc, char *argv[])
205 int italy;
206 int nteams;
207 int ndivs;
208 int serie_a;
209 int serie_b;
211 srand(time(NULL));
213 italy = country_read(DATADIR "italy.txt");
214 nteams = country_num_teams_total(italy);
215 ndivs = country_num_divisions(italy);
217 assert(player_init((division_rand_players_base()+division_rand_players_top())*nteams) == 0);
218 assert(team_init(nteams) == 0);
219 assert(division_init(ndivs) == 0);
221 serie_a = division_generate(italy, 1);
222 assert(division_num_teams(serie_a) == 20);
223 serie_b = division_generate(italy, 2);
224 assert(division_num_teams(serie_b) == 22);
225 country_free();
226 player_free();
227 team_free();
228 division_free();
229 return 0;
231 #endif