0
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

So you make a weird jump:

if(toPlay.allPlayers().size() == 1) { // making players
    ...
    toPlay.allPlayers().remove(toAdd);
} // what color the team is
...
if(toPlay.allPlayers().size() == 16) {

I'm working under the assumption toPlay.allPlayers().size() is 8 or 16 and you meant to do an if..else. This is also not knowing exactly what your other code is. I'd create an ArrayList of all the different players arrays. Then use a while loop to iterate while toPlay.allPlayers() has a player. You use a lot of repeat code that could be used once. I make a generic ColorOfTeam cot that based on which player is being sorted currently gets a corresponding color.

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();

    // player lists
    ArrayList<ArrayList<Player>> players = new ArrayList<>();
    ArrayList<Player> redPlayers = new ArrayList<>();
    ArrayList<Player> bluePlayers = new ArrayList<>();
    ArrayList<Player> greenPlayers = new ArrayList<>();
    ArrayList<Player> yellPlayers = new ArrayList<>();
    ArrayList<Player> aquaPlayers = new ArrayList<>();
    ArrayList<Player> whitePlayers = new ArrayList<>();
    ArrayList<Player> pinkPlayers = new ArrayList<>();
    ArrayList<Player> grayPlayers = new ArrayList<>();
    players.addAll(Arrays.asList(redPlayers,bluePlayers,greenPlayers,yellPlayers,aquaPlayers,whitePlayers,pinkPlayers,grayPlayers));

    int numberOfPlayers=toPlay.allPlayers().size();
    while(toPlay.allPlayers().size()>0){
        Player toAdd = toPlay.allPlayers().get(random.nextInt(toPlay.allPlayers().size()));
        // adding players
        players.get(numberOfPlayers-toPlay.allPlayers().size()).add(toAdd);
        // what color the team is
        ColorOfTeam cot;
        //this is under the assumption number of players is 8 or 16, otherwise I'd switch to a if...else chain
        //probably would then use %8==0 else %8==1, etc
        switch(numberOfPlayers-toPlay.allPlayers().size()){
            case 8:
            case 0:
                cot=new ColorOfTeam(Color.red);
                break;
            case 9:
            case 1:
                cot=new ColorOfTeam(Color.blue);
                break;
            case 10:
            case 2:
                cot=new ColorOfTeam(Color.green);
                break;
            case 11:
            case 3:
                cot=new ColorOfTeam(Color.yellow);
                break;
            case 12:
            case 4:
                cot=new ColorOfTeam(Color.cyan);
                break;
            case 13:
            case 5:
                cot=new ColorOfTeam(Color.lightGray);
                break;
            case 14:
            case 6:
                cot=new ColorOfTeam(Color.magenta);
                break;
            case 15:
            case 7:
                cot=new ColorOfTeam(Color.gray);
                break;
        }
        // the team
        Team t = new Team(players.get(numberOfPlayers-toPlay.allPlayers().size()), cot);
        // adding the team
        teams.add(t);
        // removing the player
        toPlay.allPlayers().remove(toAdd);
    }

            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());
    }
}
\$\endgroup\$
  • \$\begingroup\$ Just one error that I fixed: the while-loop needs to have && numberOfPlayers-toPlay.allPlayers.size()<8. If it doesn't, there's an error on players.get(numberOfPlayers-toPlay.allPlayers.size()).add(toAdd); \$\endgroup\$ – TheJavaNub May 5 '18 at 12:26
1
\$\begingroup\$

Well... now that you've learned how to use loops, why don't you just throw it all away and try to write it from scratch.

Often it's easier to rewrite the entire thing instead of trying to improve the existing code.

Don't be afraid to delete the code. You've learned a lot by writing it for the first time, it will not go to waste - you'll be smarter now, and it'll be much easier to write it the second time. You can back up the original code, so you can always safely return to it when your rewrite fails for some reason. There's nothing to loose.

\$\endgroup\$
  • 1
    \$\begingroup\$ this might be better as a comment \$\endgroup\$ – depperm Apr 27 '18 at 15:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.