Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string.h> // merkkijonot
- #include <stdlib.h> // muisti malloc
- #include "player.h"
- #include "line.h"
- typedef struct Team_Struct{
- char name[33];
- Line lines[4]; // field lines has incompelet type ???
- Player *freep;
- int flen;
- }Team;
- /* Creates new team with given nameCreates new team with given name
- * Name of the team can be max 32
- * characters long. There must be 4 lines
- * in a team. Each line has its own id
- * number from 1 to 4. At the beginning
- * all lines and list of free players
- * are empty.
- * Returns new team or NULL if failed */
- Team* createTeam(const char* name){
- if(name==NULL || sizeof(name)>32)
- return NULL;
- Team *new = malloc(sizeof *new);
- strcpy(new->name,name);
- new->freep = NULL;
- new->flen=0;
- return new;
- }
- /* Deletes the team, all lines and
- * all players in the team.
- * Frees all memory allocated to the
- * team, lines or players. */
- void deleteTeam(Team* t){
- int i=0;
- while(i<=3){ // tuhoaa linja
- deleteLine(t->lines[i]);
- i++;
- }
- free(t->lines);
- for(i=0;t->freep[i] != NULL;i++){ // tuhoaa vapaat pelaajat
- free(t->freep[i]);
- }
- }
- free(t);
- }
- /* Prints the team name followed by allPrints the team name followed by all
- * lines and players in lines and finally
- * the list of free players.
- * Format:
- * <team name>
- * Line 1
- * <id> <player name>
- * ...
- * Line 2
- * <id> <player name>
- * ...
- * Free Players
- * <id> <player name>
- * ...
- */
- void printTeam(const Team* t){
- printf("%s\n",t->name);
- int i=0
- while(i<=3){ // printtaa linjat
- printf("Line %i\n",i+1);
- printPlayers(t->lines[i]);
- }
- printf("Free Players\n");
- i=0;
- while(t->freep[i]!=NULL){ // vapaat pelaajat
- printPlayerInfo(t->freep[i]);
- i++;
- }
- }
- /* Adds the player to the list of free players
- * Returns the unique id of the player or 0
- * on failure */
- int addPlayer(Team* t, Player* player){
- t->flen++;
- t->freep=realloc(freep,sizeof(Player)*(t->flen));
- t->freep[t->flen]=player;
- return getId(player);
- }
- /* Deletes the player with given id from the team
- * Removal can be done only if player is in the
- * list of free players.
- * Returns 0 on success or -1 if player was not
- * found or removal fails */
- int removePlayer(Team* t, int playerId);
- /* Assigns player from the list of free players
- * to the given line. If position is already
- * occupied, assignment fails.
- * Returns 0 on success and -1 on failure */
- int assignPlayerToLine(Team* t, int lineId, int playerId, int position);
- /* Removes player from given line and given position and
- * moves him to the list of free players */
- void scratchPlayerFromLine(Team* t, int lineId, int pos);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement