using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RoundRobin
{
    public partial class frm65RoundRobin : Form
    {
        public frm65RoundRobin()
        {
            InitializeComponent();
        }

        // Represents a bye.
        private const int Bye = -1;

        // Schedule the round robin.
        private void goButton_Click(object sender, EventArgs e)
        {
            // Get the schedule.
            int numTeams = (int)nudNumTeams.Value;
            int[,] results = GenerateRoundRobin(numTeams);

            // Display the result.
            string text = "";
            for (int round = 0; round <= results.GetUpperBound(1); round++)
            {
                text += "Round " + round + ":\r\n";
                for (int team = 0; team < numTeams; team++)
                {
                    if (results[team, round] == Bye)
                    {
                        text += "    " + team + " (bye)\r\n";
                    }
                    else if (team < results[team, round])
                    {
                        text += "    " + team + " v " + results[team, round] + "\r\n";
                    }
                }
            }

            resultsTextBox.Text = text;
        }

        // Return an array where results(i, j) gives the opponent of team i in round j.
        private int[,] GenerateRoundRobin(int numTeams)
        {
            if (numTeams % 2 == 0)
                return GenerateRoundRobinEven(numTeams);
            else
                return GenerateRoundRobinOdd(numTeams);
        }

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

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

            // Start the rounds.
            for (int round = 0; round < numTeams; 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;
        }

        // Return an array where results(i, j) gives opponent of team i in round j.
        // Note: numTeams must be even.
        private int[,] GenerateRoundRobinEven(int numTeams)
        {
            // Generate the result for one fewer teams.
            int[,] results = GenerateRoundRobinOdd(numTeams - 1);

            // Copy the results into a bigger array,
            // replacing the byes with the extra team.
            int[,] results2 = new int[numTeams, numTeams - 1];
            for (int team = 0; team < numTeams - 1; team++)
            {
                for (int round = 0; round < numTeams - 1; round++)
                {
                    if (results[team, round] == Bye)
                    {
                        // Change the bye to the new team.
                        results2[team, round] = numTeams - 1;
                        results2[numTeams - 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;
        }


        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.nudNumTeams = new System.Windows.Forms.NumericUpDown();
            this.resultsTextBox = new System.Windows.Forms.TextBox();
            this.goButton = new System.Windows.Forms.Button();
            this.Label1 = new System.Windows.Forms.Label();
            ((System.ComponentModel.ISupportInitialize)(this.nudNumTeams)).BeginInit();
            this.SuspendLayout();
            // 
            // nudNumTeams
            // 
            this.nudNumTeams.Location = new System.Drawing.Point(67, 15);
            this.nudNumTeams.Name = "nudNumTeams";
            this.nudNumTeams.Size = new System.Drawing.Size(53, 20);
            this.nudNumTeams.TabIndex = 6;
            this.nudNumTeams.Value = new decimal(new int[] {
            6,
            0,
            0,
            0});
            // 
            // resultsTextBox
            // 
            this.resultsTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.resultsTextBox.Location = new System.Drawing.Point(12, 41);
            this.resultsTextBox.Multiline = true;
            this.resultsTextBox.Name = "resultsTextBox";
            this.resultsTextBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
            this.resultsTextBox.Size = new System.Drawing.Size(235, 208);
            this.resultsTextBox.TabIndex = 8;
            // 
            // goButton
            // 
            this.goButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.goButton.Location = new System.Drawing.Point(172, 12);
            this.goButton.Name = "goButton";
            this.goButton.Size = new System.Drawing.Size(75, 23);
            this.goButton.TabIndex = 7;
            this.goButton.Text = "Go";
            this.goButton.Click += new System.EventHandler(this.goButton_Click);
            // 
            // Label1
            // 
            this.Label1.AutoSize = true;
            this.Label1.Location = new System.Drawing.Point(12, 17);
            this.Label1.Name = "Label1";
            this.Label1.Size = new System.Drawing.Size(49, 13);
            this.Label1.TabIndex = 9;
            this.Label1.Text = "# Teams";
            // 
            // frm65RoundRobin
            // 
            this.AcceptButton = this.goButton;
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(259, 261);
            this.Controls.Add(this.nudNumTeams);
            this.Controls.Add(this.resultsTextBox);
            this.Controls.Add(this.goButton);
            this.Controls.Add(this.Label1);
            this.Name = "frm65RoundRobin";
            this.Text = "RoundRobin";
            ((System.ComponentModel.ISupportInitialize)(this.nudNumTeams)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.NumericUpDown nudNumTeams;
        internal System.Windows.Forms.TextBox resultsTextBox;
        internal System.Windows.Forms.Button goButton;
        internal System.Windows.Forms.Label Label1;
    }
}