I have a lot of code (probably about 1/2 of all of my 891 lines) dedicated to just making players and teams.
Here's the code that makes the players and teams:
private void makeTeams() {
toPlay = maps.get(random.nextInt(maps.size())); // I have a method that I shortened to make maps
pName = toPlay.islands().get(you).getTeam().players.get(0).getName();
// red's players
ArrayList<Player> redPlayers = new ArrayList<>();
if(toPlay.allPlayers().size() == 8) { // making players for solo
// making player
Player toAdd = toPlay.allPlayers().get(random.nextInt(toPlay.allPlayers().size()));
// adding players
redPlayers.add(toAdd);
// removing the player
toPlay.allPlayers().remove(toAdd);
} // what color the team is
ColorOfTeam cotred = new ColorOfTeam(Color.red);
// the team
Team red = new Team(redPlayers, cotred);
// adding the team
teams.add(red);
// blue's players
ArrayList<Player> bluePlayers = new ArrayList<>();
if(toPlay.allPlayers().size() == 7) { // making players
// making player
Player toAdd = toPlay.allPlayers().get(random.nextInt(toPlay.allPlayers().size()));
// adding players
bluePlayers.add(toAdd);
// removing the player
toPlay.allPlayers().remove(toAdd);
} // what color the team is
ColorOfTeam cotblue = new ColorOfTeam(Color.blue);
// the team
Team blue = new Team(bluePlayers, cotblue);
// adding the team
teams.add(blue);
// red's players
ArrayList<Player> greenPlayers = new ArrayList<>();
if(toPlay.allPlayers().size() == 6) { // making players for solo
// making player
Player toAdd = toPlay.allPlayers().get(random.nextInt(toPlay.allPlayers().size()));
// adding players
greenPlayers.add(toAdd);
// removing the player
toPlay.allPlayers().remove(toAdd);
} // what color the team is
ColorOfTeam cotgreen = new ColorOfTeam(Color.green);
// the team
Team green = new Team(greenPlayers, cotgreen);
// adding the team
teams.add(green);
// blue's players
ArrayList<Player> yellPlayers = new ArrayList<>();
if(toPlay.allPlayers().size() == 5) { // making players
// making player
Player toAdd = toPlay.allPlayers().get(random.nextInt(toPlay.allPlayers().size()));
// adding players
yellPlayers.add(toAdd);
// removing the player
toPlay.allPlayers().remove(toAdd);
} // what color the team is
ColorOfTeam cotyell = new ColorOfTeam(Color.yellow);
// the team
Team yell = new Team(yellPlayers, cotyell);
// adding the team
teams.add(yell);
// aqua's players
ArrayList<Player> aquaPlayers = new ArrayList<>();
if(toPlay.allPlayers().size() == 4) { // making players for solo
// making player
Player toAdd = toPlay.allPlayers().get(random.nextInt(toPlay.allPlayers().size()));
// adding players
aquaPlayers.add(toAdd);
// removing the player
toPlay.allPlayers().remove(toAdd);
} // what color the team is
ColorOfTeam cotaqua = new ColorOfTeam(Color.cyan);
// the team
Team aqua = new Team(aquaPlayers, cotaqua);
// adding the team
teams.add(aqua);
// white's players
ArrayList<Player> whitePlayers = new ArrayList<>();
if(toPlay.allPlayers().size() == 3) { // making players
// making player
Player toAdd = toPlay.allPlayers().get(random.nextInt(toPlay.allPlayers().size()));
// adding players
whitePlayers.add(toAdd);
// removing the player
toPlay.allPlayers().remove(toAdd);
} // what color the team is
ColorOfTeam cotwhite = new ColorOfTeam(Color.lightGray);
// the team
Team white = new Team(whitePlayers, cotwhite);
// adding the team
teams.add(white);
// pink's players
ArrayList<Player> pinkPlayers = new ArrayList<>();
if(toPlay.allPlayers().size() == 2) { // making players for solo
// making player
Player toAdd = toPlay.allPlayers().get(random.nextInt(toPlay.allPlayers().size()));
// adding players
pinkPlayers.add(toAdd);
// removing the player
toPlay.allPlayers().remove(toAdd);
} // what color the team is
ColorOfTeam cotpink = new ColorOfTeam(Color.magenta);
// the team
Team pink = new Team(pinkPlayers, cotpink);
// adding the team
teams.add(pink);
// gray's players
ArrayList<Player> grayPlayers = new ArrayList<>();
if(toPlay.allPlayers().size() == 1) { // making players
// making player
Player toAdd = toPlay.allPlayers().get(random.nextInt(toPlay.allPlayers().size()));
// adding players
grayPlayers.add(toAdd);
// removing the player
toPlay.allPlayers().remove(toAdd);
} // what color the team is
ColorOfTeam cotgray = new ColorOfTeam(Color.gray);
// the team
Team gray = new Team(grayPlayers, cotgray);
// adding the team
teams.add(gray);
// red's players
ArrayList<Player> redPlayersrd = new ArrayList<>();
if(toPlay.allPlayers().size() == 16) {
// making player
Player toAdd = toPlay.allPlayers().get(random.nextInt(toPlay.allPlayers().size()));
// adding players
redPlayersrd.add(toAdd);
// removing the player
toPlay.allPlayers().remove(toAdd);
// making player
Player toAdd2 = toPlay.allPlayers().get(random.nextInt(toPlay.allPlayers().size()));
// adding players
redPlayersrd.add(toAdd2);
// removing the player
toPlay.allPlayers().remove(toAdd2);
} // what color the team is
ColorOfTeam cotredRD = new ColorOfTeam(Color.red);
// the team
Team redRD = new Team(redPlayersrd, cotredRD);
// adding the team
teams.add(redRD);
// red's players
ArrayList<Player> bPlayersrd = new ArrayList<>();
if(toPlay.allPlayers().size() == 14) {
// making player
Player toAdd = toPlay.allPlayers().get(random.nextInt(toPlay.allPlayers().size()));
// adding players
bPlayersrd.add(toAdd);
// removing the player
toPlay.allPlayers().remove(toAdd);
// making player
Player toAdd2 = toPlay.allPlayers().get(random.nextInt(toPlay.allPlayers().size()));
// adding players
bPlayersrd.add(toAdd2);
// removing the player
toPlay.allPlayers().remove(toAdd2);
} // what color the team is
ColorOfTeam cotbRD = new ColorOfTeam(Color.blue);
// the team
Team bRD = new Team(bPlayersrd, cotbRD);
// adding the team
teams.add(bRD);
// red's players
ArrayList<Player> gPlayersrd = new ArrayList<>();
if(toPlay.allPlayers().size() == 12) {
// making player
Player toAdd = toPlay.allPlayers().get(random.nextInt(toPlay.allPlayers().size()));
// adding players
gPlayersrd.add(toAdd);
// removing the player
toPlay.allPlayers().remove(toAdd);
// making player
Player toAdd2 = toPlay.allPlayers().get(random.nextInt(toPlay.allPlayers().size()));
// adding players
gPlayersrd.add(toAdd2);
// removing the player
toPlay.allPlayers().remove(toAdd2);
} // what color the team is
ColorOfTeam cotgRD = new ColorOfTeam(Color.green);
// the team
Team gRD = new Team(gPlayersrd, cotgRD);
// adding the team
teams.add(gRD);
// red's players
ArrayList<Player> yPlayersrd = new ArrayList<>();
if(toPlay.allPlayers().size() == 10) {
// making player
Player toAdd = toPlay.allPlayers().get(random.nextInt(toPlay.allPlayers().size()));
// adding players
yPlayersrd.add(toAdd);
// removing the player
toPlay.allPlayers().remove(toAdd);
// making player
Player toAdd2 = toPlay.allPlayers().get(random.nextInt(toPlay.allPlayers().size()));
// adding players
yPlayersrd.add(toAdd2);
// removing the player
toPlay.allPlayers().remove(toAdd2);
} // what color the team is
ColorOfTeam cotyRD = new ColorOfTeam(Color.yellow);
// the team
Team yRD = new Team(yPlayersrd, cotyRD);
// adding the team
teams.add(yRD);
// red's players
ArrayList<Player> aquPlayersrd = new ArrayList<>();
if(toPlay.allPlayers().size() == 8) {
// making player
Player toAdd = toPlay.allPlayers().get(random.nextInt(toPlay.allPlayers().size()));
// adding players
aquPlayersrd.add(toAdd);
// removing the player
toPlay.allPlayers().remove(toAdd);
// making player
Player toAdd2 = toPlay.allPlayers().get(random.nextInt(toPlay.allPlayers().size()));
// adding players
redPlayersrd.add(toAdd2);
// removing the player
toPlay.allPlayers().remove(toAdd2);
} // what color the team is
ColorOfTeam cotaquRD = new ColorOfTeam(Color.cyan);
// the team
Team aquRD = new Team(aquPlayersrd, cotaquRD);
// adding the team
teams.add(aquRD);
// red's players
ArrayList<Player> wPlayersrd = new ArrayList<>();
if(toPlay.allPlayers().size() == 6) {
// making player
Player toAdd = toPlay.allPlayers().get(random.nextInt(toPlay.allPlayers().size()));
// adding players
wPlayersrd.add(toAdd);
// removing the player
toPlay.allPlayers().remove(toAdd);
// making player
Player toAdd2 = toPlay.allPlayers().get(random.nextInt(toPlay.allPlayers().size()));
// adding players
wPlayersrd.add(toAdd2);
// removing the player
toPlay.allPlayers().remove(toAdd2);
} // what color the team is
ColorOfTeam cotwRD = new ColorOfTeam(Color.white);
// the team
Team wRD = new Team(wPlayersrd, cotwRD);
// adding the team
teams.add(wRD);
// red's players
ArrayList<Player> pPlayersrd = new ArrayList<>();
if(toPlay.allPlayers().size() == 4) {
// making player
Player toAdd = toPlay.allPlayers().get(random.nextInt(toPlay.allPlayers().size()));
// adding players
pPlayersrd.add(toAdd);
// removing the player
toPlay.allPlayers().remove(toAdd);
// making player
Player toAdd2 = toPlay.allPlayers().get(random.nextInt(toPlay.allPlayers().size()));
// adding players
pPlayersrd.add(toAdd2);
// removing the player
toPlay.allPlayers().remove(toAdd2);
} // what color the team is
ColorOfTeam cotpRD = new ColorOfTeam(Color.magenta);
// the team
Team pRD = new Team(pPlayersrd, cotpRD);
// adding the team
teams.add(pRD);
// red's players
ArrayList<Player> graPlayersrd = new ArrayList<>();
if(toPlay.allPlayers().size() == 2) {
// making player
Player toAdd = toPlay.allPlayers().get(random.nextInt(toPlay.allPlayers().size()));
// adding players
graPlayersrd.add(toAdd);
// removing the player
toPlay.allPlayers().remove(toAdd);
// making player
Player toAdd2 = toPlay.allPlayers().get(random.nextInt(toPlay.allPlayers().size()));
// adding players
graPlayersrd.add(toAdd2);
// removing the player
toPlay.allPlayers().remove(toAdd2);
} // what color the team is
ColorOfTeam cotgraRD = new ColorOfTeam(Color.gray);
// the team
Team graRD = new Team(graPlayersrd, cotgraRD);
// adding the team
teams.add(graRD);
System.out.println("The map is " + toPlay.getName() + '-' + toPlay.maxPlayerAm());
int u = random.nextInt(teams.size()-1);
youAreOn = teams.get(u);
System.out.println("You are on team " + youAreOn.getName());
for(int i=0; i<youAreOn.getPlayers().size(); i++) {
System.out.print("The players are " + youAreOn.getPlayers().get(i).getName());
}
}
As you can see, this is very lengthy (I calculated that it takes up 438 lines of code), being lengthy because I wrote this before I got good at using for and while loops. My question is: can anyone find a way to shorten this? It got out of hand and is now hard to shorten for me. I've looked at it, and can't find a way to shorten it.
The main reason I wanted to shorten it is so that it runs faster: I have a run() method that counts the ticks and frames of every second; the ticks and frames should be 60, because that's what I set my fps to, but they're always 0.