Raised This Month: $51 Target: $400
 12% 

Solved Randomize Teams


Post New Thread Reply   
 
Thread Tools Display Modes
ish12321
Veteran Member
Join Date: May 2016
Old 04-22-2018 , 14:31   Re: Randomize Teams
Reply With Quote #11

PHP Code:
new players[MAX_PLAYERS], numplayer;
    
get_players_ex(playersnumGetPlayers_ExcludeHLTV);    
    
    for(new 
0numi++)
    {
        
player players[i];

        if(
cs_get_user_team(player) == CS_TEAM_UNASSIGNED)
        {
            continue;
        }

        
user_kill(player);
        
cs_set_user_team(playerCS_TEAM_SPECTATOR);

    }
    
    
get_players_ex(playersnumGetPlayers_ExcludeHLTV GetPlayers_MatchTeam"SPECTATOR");

    for(new 
0numi++)
    {
        
player players[i];
        
cs_set_user_team(playerCsTeams:random_num(12));
    }

    new 
players_CT[MAX_PLAYERS], players_T[MAX_PLAYERS], num_CTnum_T;
    
get_players_ex(players_CTnum_CTGetPlayers_ExcludeHLTV GetPlayers_MatchTeam"CT");
    
get_players_ex(players_Tnum_TGetPlayers_ExcludeHLTV GetPlayers_MatchTeam"TERRORIST");
    
    if(
num_CT num_T)
    {
        for(new 
0num_CTi++)
        {
            if((
num_CT num_T) <= 1)
            {
                break;
            }

            
player players_CT[i];

            
cs_set_user_team(playerCS_TEAM_T);
        }
    }
    else if(
num_T num_CT)
    {
        for(new 
0num_Ti++)
        {
            if((
num_T num_CT) <= 1)
            {
                break;
            }

            
player players_T[i];

            
cs_set_user_team(playerCS_TEAM_CT);
        }
    } 
This is the code I used to randomize teams. It works perfectly for most time ,but sometimes is it sending all players to T or CT. Could someone please check it and point me out the bug ?
__________________
['O|s|G'] | Death Wins a.k.a Ish Chhabra was here
ish12321 is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 04-22-2018 , 19:04   Re: Randomize Teams
Reply With Quote #12

Quote:
Originally Posted by ish12321 View Post
This is the code I used to randomize teams. It works perfectly for most time ,but sometimes is it sending all players to T or CT. Could someone please check it and point me out the bug ?
It's likely because when you're balancing them out you never subtract & add from each team count when moving a player to the opposite team. The for-loop continues for the entirety of num_CT or num_T since the if((num_TEAM1 - num_TEAM2) <= 1) condition never changes.
Code:
if(num_CT > num_T) {     for(new i = 0; i < num_CT; i++)     {         if((num_CT - num_T) <= 1)         {             break;         }                 player = players_CT[i];                 cs_set_user_team(player, CS_TEAM_T);
        num_CT--;
        num_T++;
    } } else if(num_T > num_CT) {     for(new i = 0; i < num_T; i++)     {         if((num_T - num_CT) <= 1)         {             break;         }                 player = players_T[i];                 cs_set_user_team(player, CS_TEAM_CT);
        num_T--;
        num_CT++;
    } }
__________________

Last edited by Bugsy; 04-22-2018 at 19:08.
Bugsy is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 13:26.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode