Thread: array for teams

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    307

    array for teams

    OK trying something to help out a local team....

    total players = 2 - 30

    each team (at first is equal players, unless there is an odd man, then they are in team two)

    game is played....

    at the end, losing team loses a player

    game played again

    losing team loses player, system repeats

    when the admin decides, all the players are re suffled to two even teams, then cycle repeats...

    using a 1d array, i can start the players, add a player, manually delete a player BUT onec a team loses too many, then probs start and players "dissapper" one teams...

    is a 1d array the right way to go?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <stddef.h>
    #include <time.h>
    #include <string.h> 
     
    int main(void)
    {    
     char array[30][255];
     int totalplayers;
     int choice;
     
     
     //GET TOTAL NUMBER OF PLAYERS//
     printf("Total Number of Players (1-30): ");
     scanf("%i",&totalplayers);
     
     //ENTER PLAYERS NAMES//
     for(int n=0;n<totalplayers;n++)
      {
       printf("Enter Player # %i`s Name: ",n+1);
       scanf("%s", &*array[n]);
      }
     //PRINT NAMES ENTERED//
     system("cls");
     printf("Current List of players\n");
     for(int n=0;n<totalplayers;n++)
      {
       printf("%s\n",array[n]);
      }
     system("pause");
     
     //PRINT MENU FOR CHOICES//
     do
      {
       system("cls");
       do
        {
         printf("Menu\n");
         printf("1.Randomize Teams\n");
         printf("2.Add Player to list\n");
         printf("3.View Teams\n");
         printf("4.Manually remove player\n");
         printf("5.Randomly Remove player from Team\n");
         printf("6.Exit\n");
         printf("Choice: ");
         scanf("%i",&choice);
        }while(choice>6||choice<1);
        switch(choice)
         {
          case 1://randomize teams
          srand ( time(NULL) );
          for (int i = totalplayers-1; i > 0; i--)     
           {         
            int j = rand() % (i+1);           
            if ((strcmp(array[j]," ")!=0)&&(strcmp(array[i]," ")!=0))
             {
              char temp[255]; 
              temp[0] = *array[i];     
              *array[i] = *array[j] ;     
              *array[j] = temp[0]; 
             }
            else
             {
              i=i-1;
             }
            
           }
          system("cls");
          printf("Team 1\t\t\tTeam2\n");
          for(int n=0;n<totalplayers;n++)
           {
            printf("%s\t\t\t",array[n]);
            n++;
            printf("%s\n",array[n]);
           }
          system("pause");
          break;
       
          case 2:
          
          if(totalplayers<30)
           {
            system("cls");
            totalplayers++;
            printf("Enter Player # %i`s Name: ",totalplayers);
            scanf("%s", &*array[totalplayers-1]);
     
            //PRINT NAMES ENTERED//
            system("cls");
            printf("Current List of players\n");
            for(int n=0;n<totalplayers;n++)
             {
              printf("%s\n",array[n]);
             }
            system("pause");
           }
          else
           {
            system("cls");
            printf("Already at max players!!!\n");
            system("pause");
           }
          break;
       
          case 3:
          system("cls");
          printf("Team 1\t\t\tTeam2\n");
          for(int n=0;n<totalplayers;n++)
           {
            printf("%s\t\t\t",array[n]);
            n++;
            printf("%s\n",array[n]);
           }
          system("pause");
          
          break;
       
          case 4://manually remove player from team
          system("cls");
          printf("Team 1\t\t\tTeam2\n");
          for(int n=0;n<totalplayers;n++)
           {
            printf("%i.%s\t\t\t",n+1,array[n]);
            n++;
            printf("%i.%s\n",n+1,array[n]);
           }
          
          
        
        
        
          do
           { 
            printf("Which player to delete (1-%i) ",totalplayers);
            scanf("%i",&choice);
           }while(choice<1||choice>totalplayers);
          *array[choice-1]=' ';
          for(int m=choice-1;m<totalplayers;m=m+2)
          *array[m]=*array[m+2];
          system("cls");
          printf("Team 1\t\t\tTeam2\n");
          for(int n=0;n<totalplayers;n++)
           {
            printf("%i.%s\t\t\t",n+1,array[n]);
            n++;
            printf("%i.%s\n",n+1,array[n]);
           }
          
           
          break;
          
          case 5:
          printf("5");
          break;
          
          case 6:
          printf("6");
          break;
         }
      }while(choice!=6);
     
     
     return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    char temp[255];
    temp[0] = *array[i];
    *array[i] = *array[j] ;
    *array[j] = temp[0];

    Well this doesn't swap two strings, if that's what you were planning.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    Quote Originally Posted by Salem View Post
    char temp[255];
    temp[0] = *array[i];
    *array[i] = *array[j] ;
    *array[j] = temp[0];

    Well this doesn't swap two strings, if that's what you were planning.
    well so far, it has been.....

    takes i (from a for loop 0 to total players-1) and switches that value from the array with the random one...

    try it it works perfect.

    my prob is seperating into two teams, where when one team still has lets say 15 people, and the second team starts losing people, the players dont start getting messed up

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Why have you declared an array of 255 elements if you only use the first element?
    Fact - Beethoven wrote his first symphony in C

  5. #5
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Huh, what you could do is use a linked list of player structures. Well, technically two. You keep track of the length of each structure (each element is a player) and you're trying to shoot for even teams. So when you lose a player you just take the modulo of that with respect to 2 and if its zero then cut one element of a structure and put it at the end of the other. That way you're just swapping around allocations and you can allow the memory to fit as is needed instead of being statically allocated.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Quote Originally Posted by Crossfire View Post
    well so far, it has been.....

    takes i (from a for loop 0 to total players-1) and switches that value from the array with the random one...

    try it it works perfect.

    my prob is seperating into two teams, where when one team still has lets say 15 people, and the second team starts losing people, the players dont start getting messed up
    Code:
    $ cat bar.c
    #include <stdio.h>
    #include <stdlib.h>
    #include <stddef.h>
    #include <time.h>
    #include <string.h>
    
    int main(void)
    {
      char array[30][255];
      strcpy(array[0],"hello");
      strcpy(array[1],"world");
      int i = 0, j = 1;
      printf("Before=%s %s\n", array[0], array[1] );
        {
        char temp[255];
        temp[0] = *array[i];
        *array[i] = *array[j] ;
        *array[j] = temp[0];
        }
      printf("After=%s %s\n", array[0], array[1] );
      return 0;
    }
    $ 
    $ gcc bar.c
    $ ./a.out 
    Before=hello world
    After=wello horld
    Unless you have a massively over complicated program for dealing with single character team names, your swap does NOT work.

    Please don't tell me your debug strategy is to write the whole thing in one go, then dump it on a forum for someone else to fix.

    This problem is both easy to spot by reading the code and easy to spot by single-stepping in a debugger.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    @JOHN: the teams dont rebalance untill the option is chosen, so there COULD be 10 on one side and 4 on the other, i tried the idea of having every even go to one, and every odd goto the other, but once it gets so far down, it starts being screwy, so i am thinking the first X go on team 1, and the rest go on team 2.

    @Salem: lol, i was a dumbass, i was TESTing it with all 30 players, and using a-z`s and 1-9`s. so i didnt notice the full names not working!!! my random code i used from another program with ints!!! And no, to both, debugging wasnt in all one shot, i make a section then test it debig it once it works i move on! and the second no was to the "wanting others to fix it" i have NEVER done that, nor will i ever! the code posted was for the group to see the idea of what i was doing, and how i was doing it, and hence the asking if this was a good way to go, and if not what would be the best.

    @everyone: it looks like i am going to use one array with a "middle" split, first half on 1 and rest on 2. ill update this when i redo it all!

    thanks all

  8. #8
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    fixed the swap, thank you never noticed it because i was testing with one char, not a whole word!

    Code:
    char temp[255];
    strcpy(temp,array[i]);
    strcpy(array[i],array[j]);
    strcpy(array[j],temp);
    yeah on emergency leave due to my grandma passing, so not noticing the little mistakes i was making, mind isnt all here 100%

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assigning teams
    By kenneykam in forum C Programming
    Replies: 3
    Last Post: 03-18-2013, 10:11 PM
  2. Replies: 2
    Last Post: 03-20-2012, 08:41 AM
  3. The last three teams
    By ivanovbg in forum C++ Programming
    Replies: 5
    Last Post: 10-04-2011, 11:33 AM
  4. Replies: 9
    Last Post: 08-23-2010, 02:31 PM
  5. Replies: 6
    Last Post: 11-09-2006, 03:28 AM