1

I'm trying to make a program, which puts a given amount of players in a given amount of teams. Afterwards they should be randomly chosen (eg. you roll the "dice" and Team 3's Player 42 and shall fight against Team 4's Player 22 (All the players are randomly put in the different teams, which are limited to the choice of the Gamemaster)).

In my code I have the basic output and structure. It says something like:

Team 1 now owns Player 43 Team 2 now owns Player 12 Team 4 now owns Player 1 Team 3 now owns Player 54

But my question is, how - based on the code - I could save this information and how can I (afterwards) let the players randomly fight? Members of the same team should NOT be able to fight each other and after each fight I want the players to be somehow on a "blacklist" where they can't be rolled anymore.

My code so far

#include <stdio.h>
#include <stdlib.h>

int main() 
{
    int mitglieder, teams, teameins = 0, teamzwei = 0, teamdrei = 0, teamvier = 0;

    printf("Teamcreation\n");
    printf("\nNumber of Players: ");
    scanf("%d", &mitglieder);
    printf("\nNumber of Teams: ");
    scanf("%d", &teams);
    printf("\nThere are ");
    printf("%d", mitglieder);
    printf(" Player in ");
    printf("%d", teams);
    printf(" Teams. \n");

    int array[mitglieder];

    for (int i = 0; i < mitglieder; i++) 
    {     // fill array
        array[i] = i;               
    }

    printf("The Player are in the following Teams: \n ");

    for (int i = 0; i < mitglieder; i++) 
    {    // shuffle array
        int temp = array[i];
        int randomIndex = rand() % mitglieder;

        array[i]           = array[randomIndex];
        array[randomIndex] = temp;
    }

    for (int i = 0; i < mitglieder; i++) 
    {    // print array
        int random_number = rand() % teams + 1;
        int tp = random_number;

        if(tp == 1) 
        {
            teameins+1;
        }
        else if(tp == 2) 
        {
            teamzwei+1;
        }
        else if(tp == 3) 
        {
            teamdrei+1;
        }
        else if(tp == 4) 
        {
            teamvier+1;                         
        }

        printf("Team %d - Spieler: %d\n ",random_number,array[i] + 1);      
    }

    if( (teamvier == 0) && (teamdrei == 0) ) 
    {
        printf("\n%d Mitglieder in Team 1 und %d Mitglieder in Team2",teameins,teamzwei);
    }
    else if((teamvier == 0) && (teamdrei < 0)) 
    {
        printf("\n%d Mitglieder in Team 1, %d Mitglieder in Team2 und %d Mitglieder in Team3.",teameins,teamzwei,teamdrei);
    }
    else if(teamvier < 0) 
    {
        printf("\n%d Mitglieder in Team 1, %d Mitglieder in Team2, %d Mitglieder in Team 3 und %d Mitglieder in Team4.",teameins,teamzwei,teamdrei,teamvier);
    }

    return 0;
}
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • 3
    You do not need the "good evening" - It might be the middle of the day where the person is reading this! – Ed Heal Dec 30 '15 at 20:55
  • Sorry, it's a bad habit of mine. Thank you for the information! – M. Torvalds Dec 30 '15 at 20:58
  • 2
    "how I could save this information", you need to design a data structure to store the generated data. For example you can have an array of teams where each array entry is a list of players. – kaylum Dec 30 '15 at 21:00
  • I already thought of this. So I'd need to say if they're Team One, they need to be saved to Team One's array, am I right? Sorry, I'm a bit new to all this. – M. Torvalds Dec 30 '15 at 21:02
  • Yes that'll be a good start. – kaylum Dec 30 '15 at 21:03
  • Could you read the information from a text file in the first place? It would be so much easier than typing it in every time you run the program. – Weather Vane Dec 30 '15 at 21:29

1 Answers1

0

Before basing something on the given code, you should correct the errors in it, which are within the counting of team members and the printing of the counts. While you're at it, you could make it work also for more than four teams, e. g.:

#include <string.h>
…
    int Spielerteam[mitglieder];                // team of the player
    int Teamstaerke[teams];                     // team member counts
    memset(Teamstaerke, 0, sizeof Teamstaerke); // zero the counts

    for (int i = 0; i < mitglieder; i++) 
    {    // print array
        int random_number = rand() % teams;
        int tp = random_number + 1;
        Spielerteam[i] = tp;            // save player's team information
        Teamstaerke[random_number]++;   // count the member
        printf("Team %d - Spieler: %d\n ", tp, array[i] + 1);      
    }

    for (int i = 0; i < teams; i++) 
        printf("%s%d Mitglieder in Team %d",
                i ? i+1 == teams ? " und " : ", " : "\n", Teamstaerke[i], i+1);
    puts("");

After that, the team each player array[i] + 1 belongs to is saved in Spielerteam[i].

Armali
  • 18,255
  • 14
  • 57
  • 171