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
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/>.
37 struct match
**matches
;
41 static struct division
*dvec
;
43 static void assign_matches(struct division
*d
)
46 int matches_per_round
;
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
)
69 for(i
=0; i
<matches_per_round
; i
++)
72 c2
[i
] = total_rounds
-i
;
75 for(round
=0 ; round
<total_rounds
; round
++)
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
];
93 d
->matches
[round
][mn
].home
= c1
[mn
];
94 d
->matches
[round
][mn
].away
= c2
[mn
];
98 d
->matches
[round
][mn
].home
= c2
[mn
];
99 d
->matches
[round
][mn
].away
= c1
[mn
];
104 last
= c1
[matches_per_round
-1];
105 for(mn
=matches_per_round
-1 ; mn
>0; mn
--)
110 c2
[matches_per_round
] = last
;
111 for(mn
=1 ; mn
<matches_per_round
; mn
++)
117 for(round
=0 ; round
<total_rounds
; round
++)
119 for(mn
=0; mn
<matches_per_round
; mn
++)
123 home
= d
->matches
[round
][mn
].home
;
124 away
= d
->matches
[round
][mn
].away
;
132 int division_init(int elements
)
134 dvec
= calloc(elements
, sizeof(*dvec
));
146 int division_rand_players_base()
151 int division_rand_players_top()
156 int division_generate(int country
, int division_rank
)
162 d
->n_teams
= country_num_teams_in_division(country
, division_rank
);
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()));
174 int division_num_teams(int division
)
176 return dvec
[division
].n_teams
;
184 for(i
=0; i
<dnum
; i
++)
188 for(j
=0 ; j
<dvec
[i
].n_teams
-1 ; j
++)
190 free(dvec
[i
].matches
[j
]);
193 free(dvec
[i
].matches
);
199 #ifdef FTEST_DIVISION
203 int main(int argc
, char *argv
[])
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);