1
votes

I'm writing a fantasy basketball team program in Netbeans where 10 teams play 2 games against each team and I'm trying to keep track of each team's number of wins and losses. The ten teams are stored to an array list, and I have two loops set to iterate through the array without repeats, but I can't figure out for the life of me to how to keep track of the wins and losses for each team and store them in the correct place in my array. Code is as follows

for (x = 0; x < alCTeams.size() - 1; x++) {
    for (y = x + 1; y < alCTeams.size(); y++) {
        for (iGameCount = 0; iGameCount < 2; iGameCount++) {
            do {
                iHTScore = rndGenerator.nextInt(100);
                iVTScore = rndGenerator.nextInt(100);

                if (iGameCount % 2 == 0)
                    iHTScore += 5;
                else
                    iVTScore += 5;

            } while (iHTScore == iVTScore);

            if (iHTScore > iVTScore) {
                iHTWins++;
                iVTLosses++;
            } else {
                iHTLosses++;
                iVTWins++;
            }
        }
        alCTeams.get(y).setWins(iVTWins);
        alCTeams.get(y).setWins(iVTLosses);
    }
    alCTeams.get(x).setWins(iHTWins);
    alCTeams.get(x).setLosses(iHTLosses);
}
2
Looks like code begging for OOP-flavored refactoring. :)Hovercraft Full Of Eels

2 Answers

0
votes

You should retrieve the current number of wins/losses for each team inside the respective loops. Otherwise you are just increasing some general win-loss-counts and store them randomly to teams:

for (x = 0; x < alCTeams.size() - 1; x++) {
    // retrieve current wins/losses for team x
    iHTWins = alCTeams.get(x).getWins();
    iHTLosses = alCTeams.get(x).getLosses();

    for (y = x + 1; y < alCTeams.size(); y++) {
        // retrieve current wins/losses for team y
        iVTWins = alCTeams.get(y).getWins();
        iVTLosses = alCTeams.get(y).getLosses();

        for (iGameCount = 0; iGameCount < 2; iGameCount++) {
            // do 2 game stuff
        }
        // store wins/losses for team y back to list
        alCTeams.get(y).setWins(iVTWins);
        alCTeams.get(y).setLosses(iVTLosses);  // set losses, not wins
    }
    // store wins/losses for team x back to list
    alCTeams.get(x).setWins(iHTWins);
    alCTeams.get(x).setLosses(iHTLosses);
}
0
votes

Check below code: I've added comments to describe.

for (x = 0; x < alCTeams.size()-1; x++) {
        for (y = x+1; y < alCTeams.size(); y++) {

            // Before game begins, reset the stats to zero
            iHTWins   = 0;
            iVTLosses = 0;
            iHTLosses = 0;
            iVTWins   = 0;

            for (iGameCount = 0; iGameCount < 2; iGameCount++) {
                do {
                    iHTScore = rndGenerator.nextInt(100); 
                    iVTScore = rndGenerator.nextInt(100); 

                    if (iGameCount % 2 == 0) {
                        iHTScore += 5;
                    } else {
                        iVTScore += 5;
                    }
                } while (iHTScore == iVTScore);

                if (iHTScore > iVTScore) {
                    iHTWins++;
                    iVTLosses++;
                } else {
                    iHTLosses++;
                    iVTWins++;
                }
            }
            alCTeams.get(y).setWins(alCTeams.get(y).getWins() + iVTWins);
            alCTeams.get(y).setLosses(alCTeams.get(y).getLosses() + iVTLosses);

            // Moved inside loop
            alCTeams.get(x).setWins(alCTeams.get(x).getWins() + iHTWins);
            alCTeams.get(x).setLosses(alCTeams.get(x).getLosses() + iHTLosses);
        }
    }