SHARE
TWEET

Untitled

a guest Sep 8th, 2017 123 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <string.h> // merkkijonot
  2. #include <stdlib.h> // muisti malloc
  3. #include "player.h"
  4. #include "line.h"
  5.  
  6.  
  7.  
  8. typedef struct Team_Struct{
  9.  
  10.     char name[33];
  11.     Line lines[4]; // field lines has incompelet type ???
  12.     Player *freep;
  13.     int flen;
  14.  
  15.     }Team;
  16.  
  17. /* Creates new team with given nameCreates new team with given name
  18.  * Name of the team can be max 32
  19.  * characters long. There must be 4 lines
  20.  * in a team. Each line has its own id
  21.  * number from 1 to 4. At the beginning
  22.  * all lines and list of free players
  23.  * are empty.
  24.  * Returns new team or NULL if failed */
  25. Team* createTeam(const char* name){
  26.  
  27.     if(name==NULL || sizeof(name)>32)
  28.     return NULL;
  29.  
  30.     Team *new = malloc(sizeof *new);
  31.     strcpy(new->name,name);
  32.  
  33.     new->freep = NULL;
  34.     new->flen=0;
  35.  
  36.  
  37.     return new;
  38.  
  39.     }
  40.  
  41. /* Deletes the team, all lines and
  42.  * all players in the team.
  43.  * Frees all memory allocated to the
  44.  * team, lines or players. */
  45. void deleteTeam(Team* t){
  46.     int i=0;
  47.     while(i<=3){            // tuhoaa linja
  48.        deleteLine(t->lines[i]);
  49.         i++;
  50.         }
  51.     free(t->lines);
  52.     for(i=0;t->freep[i] != NULL;i++){  // tuhoaa vapaat pelaajat
  53.         free(t->freep[i]);
  54.  
  55.         }
  56.     }
  57.     free(t);
  58.  
  59.  
  60.     }
  61.  
  62. /* Prints the team name followed by allPrints the team name followed by all
  63.  * lines and players in lines and finally
  64.  * the list of free players.
  65.  * Format:
  66.  * <team name>
  67.  * Line 1
  68.  * <id> <player name>
  69.  * ...
  70.  * Line 2
  71.  * <id> <player name>
  72.  * ...
  73.  * Free Players
  74.  * <id> <player name>
  75.  * ...
  76.  */
  77. void printTeam(const Team* t){
  78.  
  79.     printf("%s\n",t->name);
  80.     int i=0
  81.     while(i<=3){ // printtaa linjat
  82.        printf("Line %i\n",i+1);
  83.        printPlayers(t->lines[i]);
  84.         }
  85.     printf("Free Players\n");
  86.     i=0;
  87.     while(t->freep[i]!=NULL){ // vapaat pelaajat
  88.         printPlayerInfo(t->freep[i]);
  89.         i++;
  90.         }
  91.  
  92.  
  93.     }
  94.  
  95. /* Adds the player to the list of free players
  96.  * Returns the unique id of the player or 0
  97.  * on failure */
  98. int addPlayer(Team* t, Player* player){
  99.     t->flen++;
  100.     t->freep=realloc(freep,sizeof(Player)*(t->flen));
  101.     t->freep[t->flen]=player;
  102.     return getId(player);
  103.  
  104.  
  105.  
  106.     }
  107.  
  108. /* Deletes the player with given id from the team
  109.  * Removal can be done only if player is in the
  110.  * list of free players.
  111.  * Returns 0 on success or -1 if player was not
  112.  * found or removal fails  */
  113. int removePlayer(Team* t, int playerId);
  114.  
  115.  
  116. /* Assigns player from the list of free players
  117.  * to the given line. If position is already
  118.  * occupied, assignment fails.
  119.  * Returns 0 on success and -1 on failure */
  120. int assignPlayerToLine(Team* t, int lineId, int playerId, int position);
  121.  
  122.  
  123. /* Removes player from given line and given position and
  124.  * moves him to the list of free players */
  125. void scratchPlayerFromLine(Team* t, int lineId, int pos);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top