Double Circle Tournament

I am developing a sports torunament in java based on a round scheduling algorithm. For n teams, I want to generate 2 (n-1) rounds with n / 2 matches. That is, each team must play a match in a round, and every two teams meet twice, once and once at home. I managed to implement the algorithm, except for the home / remote part. I can create rounds, but I can’t “change” teams in the second half of the rounds so that they play both at home and at home.

Here is what I still have:

import java.util.Arrays;
import java.util.Scanner;

public class sports {

public static void main(String[] args) {

    //obtain the number of teams from user input
    Scanner input = new Scanner(System.in);
    System.out.print("How many teams should the fixture table have?");

    int teams;
    teams = input.nextInt();


    // Generate the schedule using round robin algorithm.
    int totalRounds = (teams - 1)*2;
    int matchesPerRound = teams / 2;
    String[][] rounds = new String[totalRounds][matchesPerRound];

    for (int round = 0; round < totalRounds; round++) {
        for (int match = 0; match < matchesPerRound; match++) {
            int home = (round + match) % (teams - 1);
            int away = (teams - 1 - match + round) % (teams - 1);

            // Last team stays in the same place while the others
            // rotate around it.
            if (match == 0) {
                away = teams - 1;
            }

            // Add one so teams are number 1 to teams not 0 to teams - 1
            // upon display.
            rounds[round][match] = ("team " + (home + 1) + " plays against team " + (away + 1));
        }
    }

    // Display the rounds    
    for (int i = 0; i < rounds.length; i++) {
        System.out.println("Round " + (i + 1));
        System.out.println(Arrays.asList(rounds[i]));
        System.out.println();
    }

}

}

Do not mind the even / odd number of teams, at the moment I am only interested in the number of teams. Any help is appreciated, thanks :)

+4
source share
3

True Soft ,

            String roundString;
            if (round < halfRoundMark) {
                roundString = ("team " + (home + 1)
                        + " plays against team " + (away + 1));
            } else {
                roundString = ("team " + (away + 1)
                        + " plays against team " + (home + 1));
            }
            rounds[round][match] = roundString;

int halfRoundMark = (totalRounds/2);
+1

, , .

0

Hi, I had to provide products to sellers, I wrote the following algo in Pseudo code check

package RoundRobin;

import java.util.ArrayList;
import java.util.List;

public class RoundRobin {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        List seller = new ArrayList();
        seller.add("a");
        seller.add("b");
        // seller.add("c");
        List products = new ArrayList();
        products.add("1");
        products.add("2");
        products.add("3");
        products.add("4");
        products.add("5");
        products.add("6");
        products.add("7");

        List SellerProducts = new ArrayList();

        int sellerLength = seller.size();
        int productLength = products.size();
        System.out.println(sellerLength + "<-selleL , prodLength->" + productLength);
        int divide = productLength / sellerLength;
        int mod = productLength % sellerLength;

        for (int sel = 1; sel <= divide; sel++) {
            for (int sCount = 0; sCount < sellerLength; sCount++) {
                // System.out.println("Scount" + sCount);
                if (sel == 1) {
                    SellerProducts.add(seller.get(sCount) + "-" + products.get(sCount));
                } else if (sel > 1) {
                    int next = sCount + ((sel * sellerLength) - sellerLength);
                    // System.out.println(next);
                    SellerProducts.add(seller.get(sCount) + "-" + products.get(next));
                }
            }
        }

        // for Remaining
        for (int p = 0
                + (sellerLength * divide), initCountForSeller = 0; p < productLength; p++, initCountForSeller++) {
            SellerProducts.add(seller.get(initCountForSeller) + "-" + products.get(p));
        }
        System.out.println("Seller : " + seller);
        System.out.println("products : " + products);
        System.out.println("seller product " + SellerProducts);
    }

}
-2
source

All Articles