Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Fair Teams Algorithm

807589Dec 14 2008 — edited Dec 14 2008
Hey and thanks for any help in advance. I am having a trouble finding an algorithm to make fair teams. Basically each "Gamer" has a score from 0-1000. I want to take a group of players in the lobby Vector and add them to the team Vectors so that the teams are as fair as possible. By fair i mean that the combined GamerScores for each of the Team vectors will be as close to equal as they can be. I dont expect anyone to code the makeFairTeams() method for me but maybe just an explanation of how it could be done. I made example classes of Gamer and a Test program. In my example i only have 5 Gamers but there could be hundreds so i want a functional way to put the players into the team vectors fairly.
/*
	This is a cutdown driver program.
	I have a lobby class that handles adding removing and making teams.
*/

import java.util.Vector;

public class FairTeams
{
	//The Teams...
	Vector<Gamer> team1= new Vector<Gamer>();
	Vector<Gamer> team2= new Vector<Gamer>();
	
	//The Players Lobby
	Vector<Gamer> playersInLobby= new Vector<Gamer>();
	
	//The Players and their rating.
	Gamer haloPlayer= new Gamer(400);
	Gamer simsPlayer= new Gamer(700);
	Gamer cod4Player= new Gamer(999);
	Gamer mistPlayer= new Gamer(633);
	Gamer leetPlayer= new Gamer(555);
	
	public FairTeams()
	{
		//All the players are in the lobby
		playersInLobby.add(haloPlayer);
		playersInLobby.add(simsPlayer);
		playersInLobby.add(cod4Player);
		playersInLobby.add(mistPlayer);
		playersInLobby.add(leetPlayer);
		
		/*Now I want to take all the players in the Lobby and make
		/    fair teams based on their rating. 
		/	  By making Fair Teams I mean adding them to the team vectors
		/    so they have the closest possible total score.
		*/		
		makeFairTeams();
	}
	
	public void makeFairTeams()
	{
		//No idea.
	}
	public static void main(String[] args)
	{
		FairTeams FT= new FairTeams();
	}
}
The Gamer Class
/*This is my Gamer Class torn down to only the 
	essentials needed for my problem. 
*/


public class GamerExample
{
	//gamerScore is a positive Integer <=1000.
	private int gamerScore;
	private boolean inTeam;
	
	public GamerExample()
	{
		gamerScore=0;
		inTeam=false;
	}
	
	//Constructor with score peram.
	public GamerExample(int gS)
	{
		if(gS>=0 && gS<=1000)
			gamerScore=gS;
		else{
			gamerScore=0;
			inTeam=false;
		}			
	}
	
	//Sets gamerScore if peram is valid.
	public void setGamerScore(int gS)
	{
		if(gS>=0 && gS<=1000)
			gamerScore=gS;
	}
	public int getGamerScore()
	{
		return(gamerScore);
	}
	
	
	//Sets Team status.True=in a team
	public void setTeamStatus(boolean b)
	{
		inTeam=b;
	}
	public boolean getTeamStatus()
	{
		return(inTeam);
	}
}
Again my problem is trying to find a functional way to put the "Gamers" into the team vectors so the total "GamerScore" for each team vector is as equal as possible. I tried to make this as clear as possible and thanks for any help.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 11 2009
Added on Dec 14 2008
2 comments
1,100 views