Insert round robin result in database in ASP.Net using C#

nauna
 
on Mar 25, 2021 11:32 PM
1116 Views

In this method i want to loop team name and round number one by one and insert int to sql table

please advice

        public void roundrobingenerator(string participants)
        {
            // Get the teams.
            string all_teams = "A, B, C, D".Replace(",", "\r");
            char[] separators = { '\r', '\n' };
            string[] team_names = all_teams.Split(separators,
                StringSplitOptions.RemoveEmptyEntries);

            // Get the schedule.
            int num_teams = team_names.Length;
            int[,] results = GenerateRoundRobin(num_teams);

            // Display the result.
            string txt = "";
            for (int round = 0; round <= results.GetUpperBound(1); round++)
            {
                txt += "Round " + round + ":<br/>";
                for (int team = 0; team < num_teams; team++)
                {
                    if (results[team, round] == BYE)
                    {
                        txt += "    " + team_names[team] + " (bye)<br/>";
                    }
                    else if (team < results[team, round])
                    {
                        txt += "    " + team_names[team] + " v " +
                            team_names[results[team, round]] + "<br/>";
                    }


                    ///Insert Method to loop thru round and team name and insert to sql column here
                    //// sql table name: matchdetails
                    ///matchid
                    ///teamname
                    ///roundnumber
                }
            }           
        }

 

        private const int BYE = -1;

        // Return an array where results(i, j) gives
        // the opponent of team i in round j.
        // Note: num_teams must be odd.
        private int[,] GenerateRoundRobinOdd(int num_teams)
        {
            int n2 = (int)((num_teams - 1) / 2);
            int[,] results = new int[num_teams, num_teams];

            // Initialize the list of teams.
            int[] teams = new int[num_teams];
            for (int i = 0; i < num_teams; i++) teams[i] = i;

            // Start the rounds.
            for (int round = 0; round < num_teams; round++)
            {
                for (int i = 0; i < n2; i++)
                {
                    int team1 = teams[n2 - i];
                    int team2 = teams[n2 + i + 1];
                    results[team1, round] = team2;
                    results[team2, round] = team1;
                }

                // Set the team with the bye.
                results[teams[0], round] = BYE;

                // Rotate the array.
                RotateArray(teams);
            }

            return results;
        }


        private int[,] GenerateRoundRobinEven(int num_teams)
        {
            // Generate the result for one fewer teams.
            int[,] results = GenerateRoundRobinOdd(num_teams - 1);

            // Copy the results into a bigger array,
            // replacing the byes with the extra team.
            int[,] results2 = new int[num_teams, num_teams - 1];
            for (int team = 0; team < num_teams - 1; team++)
            {
                for (int round = 0; round < num_teams - 1; round++)
                {
                    if (results[team, round] == BYE)
                    {
                        // Change the bye to the new team.
                        results2[team, round] = num_teams - 1;
                        results2[num_teams - 1, round] = team;
                    }
                    else
                    {
                        results2[team, round] = results[team, round];
                    }
                }
            }

            return results2;
        }

        // Rotate the entries one position.
        private void RotateArray(int[] teams)
        {
            int tmp = teams[teams.Length - 1];
            Array.Copy(teams, 0, teams, 1, teams.Length - 1);
            teams[0] = tmp;
        }

        private int[,] GenerateRoundRobin(int num_teams)
        {
            if (num_teams % 2 == 0)
                return GenerateRoundRobinEven(num_teams);
            else
                return GenerateRoundRobinOdd(num_teams);
        }

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Mar 26, 2021 03:02 AM

Hi nauna,

Refer below code.

public void roundrobingenerator(string participants = null)
{
    // Get the teams.
    string all_teams = "A, B, C, D".Replace(",", "\r");
    char[] separators = { '\r', '\n' };
    string[] team_names = all_teams.Split(separators, StringSplitOptions.RemoveEmptyEntries);

    // Get the schedule.
    int num_teams = team_names.Length;
    int[,] results = GenerateRoundRobin(num_teams);

    int match = 0;
    // Display the result.
    string txt = "";
    for (int round = 0; round <= results.GetUpperBound(1); round++)
    {
        txt += "Round " + round + ":<br/>";
        for (int team = 0; team < num_teams; team++)
        {
            if (results[team, round] == BYE)
            {
                txt += "    " + team_names[team] + " (bye)<br/>";
            }
            else if (team < results[team, round])
            {
                txt += "    " + team_names[team] + " v " + team_names[results[team, round]] + "<br/>";

                match++;
                ///Insert Method to loop thru round and team name and insert to sql column here
                this.InsertMatchDetail("Match " + match, team_names[team] + " v " + team_names[results[team, round]], "Round " + round);
            }
        }
    }

    Response.Write(txt);
}

private void InsertMatchDetail(string id, string name, string round)
{
    string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    string query = "INSERT INTO matchdetails VALUES(@Id,@TeamName,@Round)";
    using (SqlConnection con = new SqlConnection(conString))
    {
        SqlCommand cmd = new SqlCommand(query);
        cmd.Parameters.AddWithValue("@Id", id);
        cmd.Parameters.AddWithValue("@TeamName", name);
        cmd.Parameters.AddWithValue("@Round", round);
        cmd.Connection = con;
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
    }
}
private const int BYE = -1;

// Return an array where results(i, j) gives
// the opponent of team i in round j.
// Note: num_teams must be odd.
private int[,] GenerateRoundRobinOdd(int num_teams)
{
    int n2 = (int)((num_teams - 1) / 2);
    int[,] results = new int[num_teams, num_teams];

    // Initialize the list of teams.
    int[] teams = new int[num_teams];
    for (int i = 0; i < num_teams; i++) teams[i] = i;

    // Start the rounds.
    for (int round = 0; round < num_teams; round++)
    {
        for (int i = 0; i < n2; i++)
        {
            int team1 = teams[n2 - i];
            int team2 = teams[n2 + i + 1];
            results[team1, round] = team2;
            results[team2, round] = team1;
        }

        // Set the team with the bye.
        results[teams[0], round] = BYE;

        // Rotate the array.
        RotateArray(teams);
    }

    return results;
}


private int[,] GenerateRoundRobinEven(int num_teams)
{
    // Generate the result for one fewer teams.
    int[,] results = GenerateRoundRobinOdd(num_teams - 1);

    // Copy the results into a bigger array,
    // replacing the byes with the extra team.
    int[,] results2 = new int[num_teams, num_teams - 1];
    for (int team = 0; team < num_teams - 1; team++)
    {
        for (int round = 0; round < num_teams - 1; round++)
        {
            if (results[team, round] == BYE)
            {
                // Change the bye to the new team.
                results2[team, round] = num_teams - 1;
                results2[num_teams - 1, round] = team;
            }
            else
            {
                results2[team, round] = results[team, round];
            }
        }
    }

    return results2;
}

// Rotate the entries one position.
private void RotateArray(int[] teams)
{
    int tmp = teams[teams.Length - 1];
    Array.Copy(teams, 0, teams, 1, teams.Length - 1);
    teams[0] = tmp;
}

private int[,] GenerateRoundRobin(int num_teams)
{
    if (num_teams % 2 == 0)
        return GenerateRoundRobinEven(num_teams);
    else
        return GenerateRoundRobinOdd(num_teams);
}