3

I am not sure how to have them play each other once and only once. For example, here is a sample of teams (I will add scoring later, looking for mostly logic help):

class MyTeams {
    String teamName;
    int wins;
    int losses;
}

public class BasketallSeason {

    public static void main(String[] args) {

        MyTeams aoTeams[] = new MyTeams[9];

        aoTeams[0].teamName = "Utah";
        aoTeams[1].teamName = "USC";
        aoTeams[2].teamName = "Saint Mary's";
        aoTeams[3].teamName = "Oregon";
        aoTeams[4].teamName = "San Diego";
        aoTeams[5].teamName = "San Francisco";
        aoTeams[6].teamName = "UCLA";
        aoTeams[7].teamName = "Washington";
        aoTeams[8].teamName = "Loyola";
    }
}
Jess
  • 23,901
  • 21
  • 124
  • 145
K2SO Ghost Spirit
  • 1,291
  • 3
  • 11
  • 11

8 Answers8

5

Here's a basic algorithm for you

  • Take the first team of the array
  • have that team play every other team of the array
  • remove that team from the array, or otherwise mark that team as having already played everybody, so that you ignore it.
  • repeat
3

I actually spent some time and coded up the answer. It is a pretty fun question. There are many ways to solve this:

  1. Fancy iteration
  2. Recursion
  3. Iteration while popping off a team
  4. Use a separate structure to 'mark' teams as having been processed

This picture might help:

  0 1 2 3
0 - A A A
1 B - A A
2 B B - A
3 B B B -

The X and Y axis of this matrix shows the answers that you want. Either Set A or Set B will give you the answer. Note that you don't want teams playing themselves as indicated by a dash in the matrix. Below are 3 options that use iteration:

public class BBall {

public static void main(String args[]) {

    List<String> teams = new ArrayList<String>();
    teams.add("Boston");
    teams.add("LA");
    teams.add("New York");
    teams.add("Chicago");
    teams.add("Dallas");

    // This option might be a little easier to read.
    int index1 = 0;
    System.out.println("Easy to read:");
    for (String team1 : teams) {
        index1++;
        for (int index2 = index1; index2 < teams.size(); ++index2) {
            System.out.println(team1 + " plays " + teams.get(index2));
        }
    }
    System.out.println("This is set A:");
    for (int x = 1; x < teams.size(); x++) {
        for (int y = x - 1; y >= 0; y--) {
            System.out.println(teams.get(x) + " plays " + teams.get(y));
        }
    }
    System.out.println("This is set B:");
    for (int x = 0; x < teams.size() - 1; x++) {
        for (int y = x + 1; y < teams.size(); y++) {
            System.out.println(teams.get(x) + " plays " + teams.get(y));
        }
    }
}
}

The output:

Easy to read:

    Boston plays LA
    Boston plays New York
    Boston plays Chicago
    Boston plays Dallas
    LA plays New York
    LA plays Chicago
    LA plays Dallas
    New York plays Chicago
    New York plays Dallas
    Chicago plays Dallas

This is set A:

    LA plays Boston
    New York plays LA
    New York plays Boston
    Chicago plays New York
    Chicago plays LA
    Chicago plays Boston
    Dallas plays Chicago
    Dallas plays New York
    Dallas plays LA
    Dallas plays Boston

This is set B:

    Boston plays LA
    Boston plays New York
    Boston plays Chicago
    Boston plays Dallas
    LA plays New York
    LA plays Chicago
    LA plays Dallas
    New York plays Chicago
    New York plays Dallas
    Chicago plays Dallas
Jess
  • 23,901
  • 21
  • 124
  • 145
1

Define two nested for loops that each loop over your teams. Normally, this would have every team play every other team twice, and each team would play itself once.

You don't want that, so initialize the looping variable for the inner for loop to be one plus the current value of the looping variable for the outer for loop. This ensures that each combination of teams is iterated exactly once, and a team doesn't play itself.

Then call whatever game playing logic you have inside the inner for loop.

rgettman
  • 176,041
  • 30
  • 275
  • 357
1

Loop like so:

for(int i = 0; i < aoTeams.length - 1; i++) {
     for(int j = i+1; j < aoTeams.length; j++) {
         aoTeams[i].playAgainst(aoTeams[j]);
     }
}
G. Bach
  • 3,869
  • 2
  • 25
  • 46
1

Should be right:

for (int i = aoTeams.length-1; i >= 0; i--)
    for (int j = i-1; j >= 0; j--)
        System.out.println(aoTeams[j].teamName + " : "+ aoTeams[i].teamName);

Don't forget to use at first captial letter for classes in java and write fields always in camelcase starting with a lower letter ;-)

Blizzer
  • 260
  • 4
  • 22
0

Maintain a java.util.BitSet reference in your class myTeams and instantiate it with a number equaling the number of teams. By default all values are unset (false) in the beginning.

As a team tries to play a match with another team in the aoTeams check the value in the BitSet at the array index of the opponent. If it's not set let it play and set the value at that index. Do the same for the other team as well.

An example implementation might look like:

class Team {

    String teamName;
    int wins;
    int losses;
    BitSet playRecord;

    public Team(String name, int size) {
        this.teamName = name;
        playRecord = new BitSet(size);
    }

    public boolean hasPlayed(int index) {
        return playRecord.get(index);
    }

    public void finishedPlaying(int index) {
        playRecord.set(index);
    }

}

And this is how you use it:

public static void main(String[] args) {
    int size = 9;
    Team aoTeams[] = new Team[size];
    aoTeams[0] = new Team("Utah", size);
    aoTeams[1] = new Team("USC", size);
    play(aoTeams, 0, 1);
}

private static void play(Team[] teams, int indexA, int indexB) {
    if (teams[indexA].hasPlayed(indexB)) {
        // Teams have already played together
    } else {
        // Teams playing for the first time
        teams[indexA].finishedPlaying(indexB);
        teams[indexB].finishedPlaying(indexA);
    }
}
0

Create a boolean array(say alreadyPlayed) in class myTeams, initialise them with all false in the constructor, when team1 plays with team2, set alreadyPassed[2] = true(in team1's object) and set alreadyPassed[1] = true in team2's object.

A human being
  • 1,220
  • 8
  • 18
0

A simple nested loop. Iterate through both lists and emit pairs, starting the second loop at 1+ the first loop index.

Hot Licks
  • 47,103
  • 17
  • 93
  • 151