private void SkipButton_Click(object sender, EventArgs e)
        {
            CGStudentProgress.NewSection();
            switch (CGStudentProgress.CurrentSection)
            {
            case 3:
            {
                UI_SecondSetup();
                break;
            }

            case 4:
            {
                UI_ThirdSetup();
                break;
            }

            case 5:
            {
                parent.UpdateFirstGameState(Coalition, YesRB_CG.Checked);
                this.Close();
                break;
            }

            case 6:
            {
                parent.UpdateFirstGameState(Coalition, false);
                this.Close();
                break;
            }

            default:
                throw new NotImplementedException();
            }
        }
        /// <summary>
        /// Finish this step and open next form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SkipButton_Click(object sender, EventArgs e)
        {
            CGStudentProgress.NewSection();
            TestForm F = new TestForm();

            F.Show();
            this.Close();
        }
        private void FinishBTN_Click(object sender, EventArgs e)
        {
            switch (CGStudentProgress.CurrentSection)
            {
            case 2:
                if (CheckStudentsStrategies())
                {
                    SkipBTN_Click(this, new EventArgs());
                }
                break;

            case 3:
            {
                SingleGame D = G.SingleGames[0].Dominate();
                if (CheckStudentDomination(D))
                {
                    SkipBTN_Click(this, new EventArgs());
                }
                else
                {
                    CGStudentProgress.GenerateError("Должны получиться матрицы размерностью " +
                                                    D.A.Count + "x" + D.A[0].Count, null);
                }
            }
            break;

            case 4:
                if (CheckStudentGameSolution())    //graphical 2x2
                {
                    SkipBTN_Click(this, new EventArgs());
                }
                break;

            case 5:
                if (CheckDivisionAndSufficiency(Coalition, YesRB_CG.Checked, YesRB_RD.Checked))
                {
                    SkipBTN_Click(this, new EventArgs());
                }
                break;

            case 6:
            {
                parent.UpdateFirstGameState(Coalition, true);
                this.Close();
            }
            break;

            default:
                throw new NotImplementedException();
            }
            this.Location = new Point((Screen.PrimaryScreen.Bounds.Width - this.Width) / 2,
                                      (Screen.PrimaryScreen.Bounds.Height - this.Height) / 2);
        }
        private void SkipBTN_Click(object sender, EventArgs e)
        {
            switch (CGStudentProgress.CurrentSection)
            {
            case 2:
            {
                CGStudentProgress.NewSection();
                SecondInterfaceSetup_Domination();
            }
            break;

            case 3:
            {
                CGStudentProgress.NewSection();         //4
                if (sender is Button)
                {
                    ThirdInterfaceSetup_GraphicalSolution(true);
                }
                else
                {
                    ThirdInterfaceSetup_GraphicalSolution(false);
                }
            }
            break;

            case 4:
            {
                CGStudentProgress.NewSection();             //5
                FourthInterfaceSetup_PayoffDistributionAndSufficiency();
            }
            break;

            case 5:
            {
                parent.UpdateFirstGameState(Coalition, YesRB_CG.Checked);
                CGStudentProgress.NewSection();             //6
                this.Close();
            }
            break;

            case 6:
            {
                parent.UpdateFirstGameState(Coalition, false);
                this.Close();
            }
            break;

            default:
                throw new NotImplementedException();
            }
        }
        private void FinishBTN_Click(object sender, EventArgs e)
        {
            switch (CGStudentProgress.CurrentSection)
            {
            case 7:
            {
                if (CheckEconStability())
                {
                    CGStudentProgress.NewSection();
                    SecondInterfaceSetup_PayoffDivision();
                }
            }
            break;

            case 8:
            {
                if (CheckDivision(RationalityCB.Checked))
                {
                    CGStudentProgress.NewSection();
                    ThirdInterfaceSetup_CGNecessity();
                }
            }
            break;

            case 9:
            {
                if (CheckNecessity())
                {
                    for (int i = 0; i < CGStudentProgress.FormsOpened.Count; i++)
                    {
                        CGStudentProgress.FormsOpened[i].Close();
                    }
                    CGStudentProgress.Finished = true;
                    TestSelectionForm TF = new TestSelectionForm();
                    TF.StartPosition = FormStartPosition.CenterScreen;
                    TF.Show();
                    this.Close();
                }
            }
            break;

            default:
                break;
            }
        }
        public bool CheckIncooperativeGame()
        {
            //Check fulfillment
            for (int i = 0; i < TB.Count; i++)
            {
                if (TB[i].NumericTB.Text == "")
                {
                    if (i == TB.Count - 1)
                    {
                        System.Windows.Forms.MessageBox.Show("Заполните поле целевой функции");
                    }
                    else
                    {
                        System.Windows.Forms.MessageBox.Show("Поле для " + (i + 1) + " игрока не заполнено.");
                    }
                    return(false);
                }
            }

            //If not empty
            for (int i = 0; i < TB.Count; i++)
            {
                double value = TB[i].Value(),
                       ActualValue;
                if (i < TB.Count - 1)
                {
                    ActualValue = Database.G.payoffs[i];
                }
                else
                {
                    ActualValue = Database.G.outcome;
                }

                if ((value > 1.1 * ActualValue) || (value < 0.9 * ActualValue))
                {
                    CGStudentProgress.GenerateError("Ошибка при подсчете выражения для " + (i + 1) + " игрока.", errorCounter);
                    return(false);
                }
            }

            return(true);
        }
        private bool CheckNecessity()
        {
            for (int i = 0; i < NTB.Count; i++)
            {
                double value = CGStudentProgress.ReadValue(NTB[i].Text);
                if (value == -1)
                {
                    return(false);
                }

                double ActualValue = Convert.ToDouble(TB[i].Text) - Database.G.payoffs[i];

                if ((value > 1.1 * ActualValue) || (value < 0.9 * ActualValue))
                {
                    CGStudentProgress.GenerateError("Ошибка при подсчете выражения для " + (i + 1) + " игрока.", null);
                    return(false);
                }
            }

            return(true);
        }
        private bool CheckEconStability()
        {
            double max = 0;

            for (int i = 0; i < CB.Count; i++)
            {
                max = Math.Max(max, (CB[i].Tag as BimatrixGame).outcome);
            }
            for (int i = 0; i < CB.Count; i++)
            {
                if (((CB[i].Checked) && ((CB[i].Tag as BimatrixGame).outcome != max)) ||
                    ((!CB[i].Checked) && ((CB[i].Tag as BimatrixGame).outcome == max)))
                {
                    CGStudentProgress.GenerateError("Ошибка при определении экономической устойчивости коалиции", null);
                    return(false);
                }
            }

            for (int i = 0; i < CB.Count; i++)
            {
                if (CB[i].Checked)
                {
                    G = (CB[i].Tag as BimatrixGame);
                    CB[i].Hide();
                }
                else
                {
                    CB[i].Parent.Dispose();
                }
            }

            for (int i = 0; i < CB.Count; i++)
            {
                CB[i].Dispose();
            }


            return(true);
        }
 public IncooperativeGameForm()
 {
     InitializeComponent();
     UI_FirstSetup(3);
     CGStudentProgress.NewSection();
 }
        public bool CheckDivisionAndSufficiency(string Coalition, bool Sufficient, bool DivisionRational)
        {
            List <List <int> > C = new List <List <int> >();

            for (int i = 0; i < Coalition.Length; i++)
            {
                if (Coalition[i] == '{')
                {
                    C.Add(new List <int>());
                }
                if ((Coalition[i] >= '0') && (Coalition[i] <= '9'))
                {
                    C.Last().Add(Convert.ToInt32(Coalition[i] - '0'));
                }
            }
            BimatrixGame G = Database.G.FindGame(Coalition);

            if (((Database.G.outcome > G.SingleGames[0].Ha + G.SingleGames[0].Hb) && (Sufficient)) ||
                ((Database.G.outcome < G.SingleGames[0].Ha + G.SingleGames[0].Hb) && (!Sufficient)))
            {
                CGStudentProgress.GenerateError("Ошибка при определении существенности", null);
                return(false);
            }

            bool Rational = true;

            for (int i = 0; i < PayoffDistributionTB.Count; i++)
            {
                double value = CGStudentProgress.ReadValue(PayoffDistributionTB[i].Text);
                if (value == -1)
                {
                    return(false);
                }

                double ActualValue;
                if (CGStudentProgress.DistributionType == 1)
                {
                    double IncooperativePrize      = Database.G.payoffs[i],
                           CoalitionPrize          = 0,
                           CoalitionPlayersPayoffs = 0;
                    int CoalitionIndex             = -1;
                    for (int p = 0; p < C.Count; p++)
                    {
                        for (int q = 0; q < C[p].Count; q++)
                        {
                            if (C[p][q] - 1 == i)
                            {
                                CoalitionIndex = p;
                            }
                        }
                    }
                    CoalitionPrize = G.payoffs[CoalitionIndex];
                    for (int j = 0; j < C[CoalitionIndex].Count; j++)
                    {
                        CoalitionPlayersPayoffs += Database.G.payoffs[C[CoalitionIndex][j] - 1];
                    }
                    ActualValue = IncooperativePrize * CoalitionPrize / CoalitionPlayersPayoffs;
                }
                else
                {
                    throw new NotImplementedException();
                }

                if ((value > 1.1 * ActualValue) || (value < 0.9 * ActualValue))
                {
                    CGStudentProgress.GenerateError("Ошибка при подсчете выражения для " + (i + 1) + " игрока.", null);
                    return(false);
                }
                if (ActualValue < Database.G.payoffs[i])
                {
                    Rational = false;
                }
            }

            if (((DivisionRational) && (!Rational)) || ((!DivisionRational) && (Rational)))
            {
                CGStudentProgress.GenerateError("Ошибка при определении рациональности дележа", null);
                return(false);
            }

            return(true);
        }
        private bool CheckStudentGameSolution()
        {
            double value       = CGStudentProgress.ReadValue(textBox1.Text),
                   ActualValue = Convert.ToDouble(A[1, 1].Value) -
                                 Convert.ToDouble(A[2, 1].Value) - Convert.ToDouble(A[1, 2].Value) +
                                 Convert.ToDouble(A[2, 2].Value);

            if (value == -1)
            {
                return(false);
            }
            if ((value > 1.1 * ActualValue) || (value < 0.9 * ActualValue))
            {
                CGStudentProgress.GenerateError("Ошибка в формуле выигрыша первого игрока", null);
                return(false);
            }

            value       = CGStudentProgress.ReadValue(textBox2.Text);
            ActualValue = Convert.ToDouble(A[2, 1].Value) -
                          Convert.ToDouble(A[2, 2].Value);
            if (value == -1)
            {
                return(false);
            }
            if ((value > 1.1 * ActualValue) || (value < 0.9 * ActualValue))
            {
                CGStudentProgress.GenerateError("Ошибка в формуле выигрыша первого игрока", null);
                return(false);
            }

            value       = CGStudentProgress.ReadValue(textBox3.Text);
            ActualValue = Convert.ToDouble(A[1, 2].Value) -
                          Convert.ToDouble(A[2, 2].Value);
            if (value == -1)
            {
                return(false);
            }
            if ((value > 1.1 * ActualValue) || (value < 0.9 * ActualValue))
            {
                CGStudentProgress.GenerateError("Ошибка в формуле выигрыша первого игрока", null);
                return(false);
            }

            value       = CGStudentProgress.ReadValue(textBox4.Text);
            ActualValue = Convert.ToDouble(A[2, 2].Value);
            if (value == -1)
            {
                return(false);
            }
            if ((value > 1.1 * ActualValue) || (value < 0.9 * ActualValue))
            {
                CGStudentProgress.GenerateError("Ошибка в формуле выигрыша первого игрока", null);
                return(false);
            }

            value       = CGStudentProgress.ReadValue(textBox5.Text);
            ActualValue = Convert.ToDouble(B[1, 1].Value) -
                          Convert.ToDouble(B[2, 1].Value) - Convert.ToDouble(B[1, 2].Value) +
                          Convert.ToDouble(B[2, 2].Value);
            if (value == -1)
            {
                return(false);
            }
            if ((value > 1.1 * ActualValue) || (value < 0.9 * ActualValue))
            {
                CGStudentProgress.GenerateError("Ошибка в формуле выигрыша первого игрока", null);
                return(false);
            }

            value       = CGStudentProgress.ReadValue(textBox6.Text);
            ActualValue = Convert.ToDouble(B[2, 1].Value) -
                          Convert.ToDouble(B[2, 2].Value);
            if (value == -1)
            {
                return(false);
            }
            if ((value > 1.1 * ActualValue) || (value < 0.9 * ActualValue))
            {
                CGStudentProgress.GenerateError("Ошибка в формуле выигрыша первого игрока", null);
                return(false);
            }

            value       = CGStudentProgress.ReadValue(textBox7.Text);
            ActualValue = Convert.ToDouble(B[1, 2].Value) -
                          Convert.ToDouble(B[2, 2].Value);
            if (value == -1)
            {
                return(false);
            }
            if ((value > 1.1 * ActualValue) || (value < 0.9 * ActualValue))
            {
                CGStudentProgress.GenerateError("Ошибка в формуле выигрыша первого игрока", null);
                return(false);
            }

            value       = CGStudentProgress.ReadValue(textBox8.Text);
            ActualValue = Convert.ToDouble(B[2, 2].Value);
            if (value == -1)
            {
                return(false);
            }
            if ((value > 1.1 * ActualValue) || (value < 0.9 * ActualValue))
            {
                CGStudentProgress.GenerateError("Ошибка в формуле выигрыша первого игрока", null);
                return(false);
            }

            return(true);
        }
        private void ThirdInterfaceSetup_GraphicalSolution(bool skipped)
        {
            Task2Panel.Hide();

            if (skipped)
            {
                List <int>    Drows    = new List <int>();
                List <int>    Dcolumns = new List <int>();
                List <string> Cheaders = new List <string>();
                List <string> Rheaders = new List <string>();
                SingleGame    D        = G.SingleGames[0].Dominate(Drows, Dcolumns);

                for (int i = 0; i < Drows.Count; i++)
                {
                    Rheaders.Add(A.Rows[Drows[i]].HeaderCell.Value.ToString());
                }
                for (int i = 0; i < Dcolumns.Count; i++)
                {
                    Cheaders.Add(A.Columns[Dcolumns[i]].HeaderCell.Value.ToString());
                }
                Ga             = new UI.StrategiesGrid(A, D.A.Count, D.A[0].Count);
                Gb             = new UI.StrategiesGrid(B, D.A.Count, D.A[0].Count);
                Ga.LimitedSize = true;
                Gb.LimitedSize = true;
                Ga.InitializeHeaders("", G.SingleGames[0].FirstPlayer, G.SingleGames[0].SecondPlayer, Database.G.S);
                Gb.InitializeHeaders("", G.SingleGames[0].FirstPlayer, G.SingleGames[0].SecondPlayer, Database.G.S);
                Ga.InitializeGrid(D.A);
                Gb.InitializeGrid(D.B);
                for (int i = 0; i < A.Rows.Count; i++)
                {
                    A.Rows[i].HeaderCell.Value = Rheaders[i];
                    B.Rows[i].HeaderCell.Value = Rheaders[i];
                }
                for (int j = 0; j < A.ColumnCount; j++)
                {
                    A.Columns[j].HeaderCell.Value = Cheaders[j];
                    B.Columns[j].HeaderCell.Value = Cheaders[j];
                }
            }

            if ((A.Rows.Count == 2) && (A.Columns.Count == 2))
            {
                Graphical2x2SolutionPanel.Show();

                UI.ControlsAligner gpanel = new UI.ControlsAligner(MatrixesPanel);
                gpanel.AddElement(A);
                gpanel.AddElement(B, false);
                gpanel.Align();

                UI.ControlsAligner form = new UI.ControlsAligner(this);
                form.AddElement(MatrixesPanel);
                form.AddElement(Graphical2x2SolutionPanel);
                form.AddElement(SkipBTN, true, "Left");
                form.AddElement(FinishBTN, false, "Right");
                form.Align();
            }
            else
            {
                CGStudentProgress.NewSection();
                FourthInterfaceSetup_PayoffDistributionAndSufficiency();
            }
        }
        private bool AnalyzeCell(string s, List <char> Letters, int CoalitionSize)
        {
            List <int> Combination = new List <int>();

            for (int j = 0; j < CoalitionSize; j++)
            {
                Combination.Add(-1);
            }
            int player = -1;

            for (int i = 0; i < Letters.Count; i++)
            {
                if (s[0] == Letters[i])
                {
                    player = i;
                }
            }
            if (player == -1)
            {
                System.Windows.Forms.MessageBox.Show
                    ("Первым символом ячейки должна быть буква, соответствующая игроку",
                    "Неправильный ввод");
                return(false);
            }

            string number = "";

            for (int i = 1; i < s.Length; i++)
            {
                if ((s[i] >= '0') && (s[i] <= '9'))
                {
                    number += s[i];
                }
                else
                {
                    if (number == "")
                    {
                        System.Windows.Forms.MessageBox.Show
                            ("Некорректно составленная стратегия",
                            "Неправильный ввод");
                        return(false);
                    }
                    Combination[player] = Convert.ToInt32(number);
                    number = "";
                    player = -1;
                    for (int j = 0; j < Letters.Count; i++)
                    {
                        if (s[i] == Letters[j])
                        {
                            player = j;
                        }
                    }
                    if (player == -1)
                    {
                        System.Windows.Forms.MessageBox.Show
                            ("Некорректно составленная стратегия",
                            "Неправильный ввод");
                        return(false);
                    }
                }
            }

            for (int i = 0; i < CoalitionStrategiesCombination.Count; i++)
            {
                if (CoalitionStrategiesCombination[i] == Combination)
                {
                    if (Matches[i] == false)
                    {
                        Matches[i] = true;
                    }
                    else
                    {
                        CGStudentProgress.GenerateError("Повторное заполнение одинаковой коалиции.", null);
                        return(false);
                    }
                    break;
                }
            }
            return(true);
        }
        public ViewCooperativeGameForm(TestForm parent, string Coalition)
        {
            InitializeComponent();
            G              = Database.G.FindGame(Coalition);
            this.parent    = parent;
            this.Coalition = Coalition;
            if (CGStudentProgress.CurrentSection == 6)
            {
                CreateArrays(0, 1);
                UI.ControlsAligner mpanel = new UI.ControlsAligner(MatrixesPanel);
                mpanel.AddElement(A);
                mpanel.AddElement(B, false);
                mpanel.Align();

                RP_v1.Text         += G.SingleGames[0].FirstPlayer + " = " + G.SingleGames[0].Ha.ToString("0.00");
                RP_v2.Text         += G.SingleGames[0].SecondPlayer + " = " + G.SingleGames[0].Hb.ToString("0.00");;
                RP_PriceLabel.Text += (G.SingleGames[0].Ha + G.SingleGames[0].Hb).ToString("0.00");
                for (int i = 0; i < G.SingleGames[0].x.Count; i++)
                {
                    RP_x.Text += G.SingleGames[0].x[i].ToString("0.00") + " ";
                }
                RP_x.Text += ")";

                for (int i = 0; i < G.SingleGames[0].y.Count; i++)
                {
                    RP_y.Text += G.SingleGames[0].y[i].ToString("0.00") + " ";
                }
                RP_y.Text += ")";


                UI.ControlsAligner rpanel = new UI.ControlsAligner(GameResultPanel);
                rpanel.AddElement(RP_PriceLabel);
                rpanel.AddElement(RP_v1);
                rpanel.AddElement(RP_v2, false);
                rpanel.AddElement(RP_x);
                rpanel.AddElement(RP_y);
                rpanel.Align();
                SkipBTN.Text    = "Несущественная";
                SkipBTN.Width   = 200;
                FinishBTN.Text  = "Существенная";
                FinishBTN.Width = SkipBTN.Width;
                GameResultPanel.Show();

                UI.ControlsAligner form = new UI.ControlsAligner(this);
                Task1Panel.Hide();
                form.AddElement(NavigationPanel);
                form.AddElement(MatrixesPanel);
                form.AddElement(GameResultPanel);
                form.AddElement(SkipBTN, true, "Left");
                form.AddElement(FinishBTN, false, "Right");
                form.Align();
                CreateNavigationPanel();
            }
            else
            {
                this.ControlBox = false;
                SecondInterfaceSetup_Domination();
                CGStudentProgress.NewSection();
                A.ColumnHeaderMouseClick += new DataGridViewCellMouseEventHandler(ColumnHeadersMouseClick);
                A.RowHeaderMouseClick    += new DataGridViewCellMouseEventHandler(RowHeaderMouseClick);
                B.ColumnHeaderMouseClick += new DataGridViewCellMouseEventHandler(ColumnHeadersMouseClick);
                B.RowHeaderMouseClick    += new DataGridViewCellMouseEventHandler(RowHeaderMouseClick);
            }
        }
        private bool CheckDivision(bool DivisionRational)
        {
            string             Coalition = PanelsPanel.Controls[0].Controls[0].Text;
            List <List <int> > C         = new List <List <int> >();

            for (int i = 0; i < Coalition.Length; i++)
            {
                if (Coalition[i] == '{')
                {
                    C.Add(new List <int>());
                }
                if ((Coalition[i] >= '0') && (Coalition[i] <= '9'))
                {
                    C.Last().Add(Convert.ToInt32(Coalition[i] - '0'));
                }
            }

            bool Rational = true;

            for (int i = 0; i < TB.Count; i++)
            {
                double value = CGStudentProgress.ReadValue(TB[i].Text);
                if (value == -1)
                {
                    return(false);
                }

                double ActualValue;
                double IncooperativePrize      = Database.G.payoffs[i],
                       CoalitionPrize          = 0,
                       CoalitionPlayersPayoffs = 0;
                int CoalitionIndex             = -1;
                for (int p = 0; p < C.Count; p++)
                {
                    for (int q = 0; q < C[p].Count; q++)
                    {
                        if (C[p][q] - 1 == i)
                        {
                            CoalitionIndex = p;
                        }
                    }
                }
                CoalitionPrize = G.payoffs[CoalitionIndex];
                for (int j = 0; j < C[CoalitionIndex].Count; j++)
                {
                    CoalitionPlayersPayoffs += Database.G.payoffs[C[CoalitionIndex][j] - 1];
                }
                ActualValue = IncooperativePrize * CoalitionPrize / CoalitionPlayersPayoffs;

                if ((value > 1.1 * ActualValue) || (value < 0.9 * ActualValue))
                {
                    CGStudentProgress.GenerateError("Ошибка при подсчете выражения для " + (i + 1) + " игрока.", null);
                    return(false);
                }
                if (ActualValue < Database.G.payoffs[i])
                {
                    Rational = false;
                }
            }

            if (((DivisionRational) && (!Rational)) || ((!DivisionRational) && (Rational)))
            {
                CGStudentProgress.GenerateError("Ошибка при определении рациональности дележа", null);
                return(false);
            }

            return(true);
        }