updated GPL headers in source code
[dribble.git] / league.c
blobd26917ec77bc35c15f46eda28dce4fa470edbcb6
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 "defs.h"
24 struct league
26 int ndivs;
27 int *divisions;
28 const char *name;
31 static int lnum = 0;
32 static struct league *lvec;
34 int league_init(int elements)
36 lvec = calloc(elements, sizeof(*lvec));
38 if(lvec == NULL)
40 return -1;
42 else
44 return 0;
48 int league_generate(int country, const char *name)
50 int i;
52 lvec[lnum].ndivs = country_num_divisions(country);
53 lvec[lnum].divisions = calloc(lvec[lnum].ndivs, sizeof(*lvec[lnum].divisions));
55 for(i=0 ; i<lvec[lnum].ndivs ; i++)
57 int rank;
58 rank = country_division_rank(country, i);
59 lvec[lnum].divisions[i] = division_generate(country, rank);
62 lvec[lnum].name = name;
64 return lnum++;
67 int league_num_divs(int league)
69 return lvec[league].ndivs;
72 void league_free()
74 int i;
75 for(i=0 ; i<lnum ; i++)
77 free(lvec[i].divisions);
79 free(lvec);
82 #ifdef FTEST_LEAGUE
83 #include <assert.h>
84 #include <stdlib.h>
85 #include <string.h>
86 #include <time.h>
87 int main(int argc, char *argv[])
89 int italy;
90 int nteams;
91 int ndivs;
92 int liga;
94 srand(time(NULL));
96 italy = country_read(DATADIR "italy.txt");
97 nteams = country_num_teams_total(italy);
98 ndivs = country_num_divisions(italy);
100 assert(player_init((division_rand_players_base()+division_rand_players_top())*nteams) == 0);
101 assert(team_init(nteams) == 0);
102 assert(division_init(ndivs) == 0);
103 assert(league_init(1) == 0);
105 liga = league_generate(italy, "Italian league");
106 assert(league_num_divs(liga) == 2);
107 assert(strcmp(lvec[0].name, "Italian league") == 0);
109 country_free();
110 player_free();
111 team_free();
112 division_free();
113 league_free();
114 return 0;
116 #endif