コード例 #1
0
        public async void DynamicButtonClick(object sender, RoutedEventArgs e)
        {
            Button clicked         = (Button)sender;
            int    clicked_i_Index = 0;
            int    clicked_j_Index = 0;
            string temp            = "";
            string playerStepAtFieldOrCases;
            string fieldSizeOrCases = "";

            string[] dataSetArr = new string[4];

            temp = clicked.Name.Remove(0, 1);
            int index = temp.IndexOf("J");

            if (index > 0)
            {
                clicked_i_Index = Convert.ToInt32(temp.Substring(0, index));
            }
            clicked_j_Index = Convert.ToInt32(clicked.Name.Substring(clicked.Name.IndexOf("J") + 1));

            if (ConnectionIsServer == true)
            {
                if (whomStep == "S2" || WhomStepTB.Text == "Step for Player: 2")
                {
                    return;
                }
                if (WorkingField[clicked_i_Index, clicked_j_Index] == 0)
                {
                    if (AngryRB.IsChecked == true)
                    {
                        clicked.Content = new Image
                        {
                            Source            = new BitmapImage(new Uri("pack://application:,,,/AngryToe;component/Resources/Icon_blue_brd_0.png")),
                            VerticalAlignment = VerticalAlignment.Center,
                            Height            = 50
                        };
                    }

                    if (PeppaRB.IsChecked == true)
                    {
                        clicked.Content = new Image
                        {
                            Source            = new BitmapImage(new Uri("pack://application:,,,/AngryToe;component/Resources/Icon_blue_Jorge_0.png")),
                            VerticalAlignment = VerticalAlignment.Center,
                            Height            = 50
                        };
                    }

                    if (ClassicRB.IsChecked == true)
                    {
                        clicked.Content = new Image
                        {
                            Source            = new BitmapImage(new Uri("pack://application:,,,/AngryToe;component/Resources/Icon_0.png")),
                            VerticalAlignment = VerticalAlignment.Center,
                            Height            = 50
                        };
                    }

                    WorkingField[clicked_i_Index, clicked_j_Index] = 1;


                    // Winner Check//-------------------------------------------------------------------------------------------
                    if (ThreeFieldRB.IsChecked == true)
                    {
                        if (WinnersCheck.Check_all_rows(WorkingField) == 1 || WinnersCheck.Check_all_diagonals3(WorkingField) == 1)
                        {
                            Server1Winner = true;
                            MessageBox.Show("Congratulation, Winner is Player #1!");
                        }
                        if (WinnersCheck.Check_all_rows(WorkingField) == 2 || WinnersCheck.Check_all_diagonals3(WorkingField) == 2)
                        {
                            Client2Winner = true;
                            MessageBox.Show("Congratulation, Winner is Player #2!");
                        }
                    }
                    if (FiveFieldRB.IsChecked == true)
                    {
                        if (WinnersCheck.DglRightToLeft5(WorkingField, 5) == 1 || WinnersCheck.DglLeftToRight5(WorkingField) == 1 || WinnersCheck.Check_all_rows5(WorkingField) == 1)
                        {
                            Server1Winner = true;
                            MessageBox.Show("Congratulation, Winner is Player #1!");
                        }
                        if (WinnersCheck.DglRightToLeft5(WorkingField, 5) == 2 || WinnersCheck.DglLeftToRight5(WorkingField) == 2 || WinnersCheck.Check_all_rows5(WorkingField) == 2)
                        {
                            Client2Winner = true;
                            MessageBox.Show("Congratulation, Winner is Player #2!");
                        }
                    }

                    if (Server1Winner == true)
                    {
                        fieldSizeOrCases = "WN"; Player1Scores++; ScoresPlayer1.Text = Player1Scores.ToString();
                    }
                    if (Server1Winner == false && Client2Winner == false)
                    {
                        playerStepAtFieldOrCases = clicked.Name;
                    }
                    if (FiveFieldRB.IsChecked == true && Server1Winner == false)
                    {
                        fieldSizeOrCases = "F5";
                    }
                    if (ThreeFieldRB.IsChecked == true && Server1Winner == false)
                    {
                        fieldSizeOrCases = "F3";
                    }

                    whomStep = "S2";
                    if (whomStep == "S2" && firstCycleServer == true)
                    {
                        WhomStepTB.Text = "Step for Player: 2";
                    }
                    if (whomStep == "S2" && firstCycleServer == false)
                    {
                        WhomStepTB.Text = "Step for Player:  " + Player2Name.Text;
                    }

                    string temp1 = await ServerTCP.SendDataAsync(whomStep + fieldSizeOrCases + clicked.Name);

                    if (temp1 != "")
                    {
                        dataSetArr = ServerTCP.DataParsing(temp1);
                        if (dataSetArr[0] == "S1")
                        {
                            WhomStepTB.Text = "Step for Player: " + Player1Name.Text;
                            whomStep        = "S1";
                        }
                        if (dataSetArr[1] == "F3")
                        {
                            Requested3SizeField = true;
                        }
                        if (dataSetArr[1] == "F5")
                        {
                            Requested3SizeField = false;
                        }
                        if (dataSetArr[2].IndexOf("I") == 0 && dataSetArr[2].IndexOf("J") == 2)
                        {
                            temp  = dataSetArr[2].Remove(0, 1);
                            index = temp.IndexOf("J");
                            if (index > 0)
                            {
                                clicked_i_Index = Convert.ToInt32(temp.Substring(0, index));
                            }
                            clicked_j_Index = Convert.ToInt32(dataSetArr[2].Substring(dataSetArr[2].IndexOf("J") + 1));

                            WorkingField[clicked_i_Index, clicked_j_Index] = 2;
                            Button myDynamicButton = new Button();
                            if (ThreeFieldRB.IsChecked == true)
                            {
                                myDynamicButton = ServerTCP.FindChild <Button>(InternalField_3x3_Grid, dataSetArr[2]);
                            }
                            if (FiveFieldRB.IsChecked == true)
                            {
                                myDynamicButton = ServerTCP.FindChild <Button>(InternalField_5x5_Grid, dataSetArr[2]);
                            }

                            if (AngryRB.IsChecked == true)
                            {
                                myDynamicButton.Content = new Image
                                {
                                    Source            = new BitmapImage(new Uri("pack://application:,,,/AngryToe;component/Resources/Icon_red_brd_X.png")),
                                    VerticalAlignment = VerticalAlignment.Center,
                                    Height            = 50
                                };
                            }

                            if (PeppaRB.IsChecked == true)
                            {
                                myDynamicButton.Content = new Image
                                {
                                    Source            = new BitmapImage(new Uri("pack://application:,,,/AngryToe;component/Resources/Icon_peppa_X.png")),
                                    VerticalAlignment = VerticalAlignment.Center,
                                    Height            = 50
                                };
                            }

                            if (ClassicRB.IsChecked == true)
                            {
                                myDynamicButton.Content = new Image
                                {
                                    Source            = new BitmapImage(new Uri("pack://application:,,,/AngryToe;component/Resources/Icon_X.png")),
                                    VerticalAlignment = VerticalAlignment.Center,
                                    Height            = 50
                                };
                            }
                        }

                        if (dataSetArr[1] == "WN")
                        {
                            Player2Scores++; ScoresPlayer2.Text = Player2Scores.ToString();
                            ContinueRequest myContinue = new ContinueRequest();
                            if (myContinue.ShowDialog() == true)
                            {
                                try
                                {
                                    WhomStepTB.Text = "Step for Player: " + Player2Name.Text;
                                    whomStep        = "S2";
                                    await ServerTCP.RequestToContinueAsync(whomStep + fieldSizeOrCases + "YESX", WorkingField, WhomStepTB, Player1Name);

                                    InternalField_3x3_Grid.Children.Clear();
                                    InternalField_5x5_Grid.Children.Clear();
                                    await StartButtonRunAsync();
                                }
                                catch (Exception ex)
                                {
                                    MessageBox.Show(ex.Message);
                                }
                            }
                            else
                            {
                                try
                                {
                                    string temp2 = await ServerTCP.SendDataAsync(whomStep + fieldSizeOrCases + "NOXX");

                                    ServerTCP.tcpClient.Close();
                                }
                                catch (Exception ex)
                                {
                                    MessageBox.Show(ex.Message);
                                }
                            }
                        }

                        if (dataSetArr[2] == "YESX")
                        {
                            ServerTCP.tcpClient.Close();
                            await StartButtonRunAsync();
                        }

                        if (dataSetArr[2] == "NOXX")
                        {
                            ClientTCP.client.Close();
                        }
                    }
                }

                else
                {
                    MessageBox.Show("Not correct step!");
                }
            }

            if (ConnectionIsServer == false /*&& ClientTB.Text == "Step for X"*/)
            {
                if (whomStep == "S1" || WhomStepTB.Text == "Step for Player: 1")
                {
                    return;
                }

                if (WorkingField[clicked_i_Index, clicked_j_Index] == 0)
                {
                    if (AngryRB.IsChecked == true)
                    {
                        clicked.Content = new Image
                        {
                            Source            = new BitmapImage(new Uri("pack://application:,,,/AngryToe;component/Resources/Icon_red_brd_X.png")),
                            VerticalAlignment = VerticalAlignment.Center,
                            Height            = 50
                        };
                    }

                    if (PeppaRB.IsChecked == true)
                    {
                        clicked.Content = new Image
                        {
                            Source            = new BitmapImage(new Uri("pack://application:,,,/AngryToe;component/Resources/Icon_peppa_X.png")),
                            VerticalAlignment = VerticalAlignment.Center,
                            Height            = 50
                        };
                    }

                    if (ClassicRB.IsChecked == true)
                    {
                        clicked.Content = new Image
                        {
                            Source            = new BitmapImage(new Uri("pack://application:,,,/AngryToe;component/Resources/Icon_X.png")),
                            VerticalAlignment = VerticalAlignment.Center,
                            Height            = 50
                        };
                    }

                    WorkingField[clicked_i_Index, clicked_j_Index] = 2;

                    // Winner Check//-------------------------------------------------------------------------------------------
                    if (ThreeFieldRB.IsChecked == true)
                    {
                        if (WinnersCheck.Check_all_rows(WorkingField) == 1 || WinnersCheck.Check_all_diagonals3(WorkingField) == 1)
                        {
                            Server1Winner = true;
                            MessageBox.Show("Congratulation, Winner is Player #1!");
                        }
                        if (WinnersCheck.Check_all_rows(WorkingField) == 2 || WinnersCheck.Check_all_diagonals3(WorkingField) == 2)
                        {
                            Client2Winner = true;
                            MessageBox.Show("Congratulation, Winner is Player #2!");
                        }
                    }
                    if (FiveFieldRB.IsChecked == true)
                    {
                        if (WinnersCheck.DglRightToLeft5(WorkingField, 5) == 1 || WinnersCheck.DglLeftToRight5(WorkingField) == 1 || WinnersCheck.Check_all_rows5(WorkingField) == 1)
                        {
                            Server1Winner = true;
                            MessageBox.Show("Congratulation, Winner is Player #1!");
                        }
                        if (WinnersCheck.DglRightToLeft5(WorkingField, 5) == 2 || WinnersCheck.DglLeftToRight5(WorkingField) == 2 || WinnersCheck.Check_all_rows5(WorkingField) == 2)
                        {
                            Client2Winner = true;
                            MessageBox.Show("Congratulation, Winner is Player #2!");
                        }
                    }

                    if (Client2Winner == true)
                    {
                        fieldSizeOrCases = "WN"; Player2Scores++; ScoresPlayer2.Text = Player2Scores.ToString();
                    }
                    if (Server1Winner == false && Client2Winner == false)
                    {
                        playerStepAtFieldOrCases = clicked.Name;
                    }
                    if (FiveFieldRB.IsChecked == true && Client2Winner == false)
                    {
                        fieldSizeOrCases = "F5";
                    }
                    if (ThreeFieldRB.IsChecked == true && Client2Winner == false)
                    {
                        fieldSizeOrCases = "F3";
                    }
                    whomStep = "S1"; if (whomStep == "S1")
                    {
                        WhomStepTB.Text = "Step for Player: " + Player1Name.Text;
                    }
                    if (firstCycle == true)
                    {
                        await ClientTCP.SendDataClientBest(whomStep + fieldSizeOrCases + clicked.Name + "&" + Player2Name.Text);
                    }
                    if (firstCycle == false)
                    {
                        await ClientTCP.SendDataClientBest(whomStep + fieldSizeOrCases + clicked.Name);
                    }

                    string temp1 = await ClientTCP.ReceiveDataClientBest();

                    if (temp1 != "")
                    {
                        dataSetArr = ServerTCP.DataParsing(temp1);
                        if (dataSetArr[0] == "S2")
                        {
                            WhomStepTB.Text = "Step for Player: " + Player2Name.Text;
                            whomStep        = "S2";
                        }
                        if (dataSetArr[1] == "F3")
                        {
                            Requested3SizeField = true;
                        }
                        if (dataSetArr[1] == "F5")
                        {
                            Requested3SizeField = false;
                        }
                        if (dataSetArr[2].IndexOf("I") == 0 && dataSetArr[2].IndexOf("J") == 2)
                        {
                            temp  = dataSetArr[2].Remove(0, 1);
                            index = temp.IndexOf("J");
                            if (index > 0)
                            {
                                clicked_i_Index = Convert.ToInt32(temp.Substring(0, index));
                            }
                            clicked_j_Index = Convert.ToInt32(dataSetArr[2].Substring(dataSetArr[2].IndexOf("J") + 1));

                            WorkingField[clicked_i_Index, clicked_j_Index] = 1;
                            //MessageBox.Show(WhomStepTB.Text + " " + Requested3SizeField.ToString() + " " + clicked_i_Index.ToString() + " " + clicked_j_Index.ToString());
                            firstCycle = false; // needed to pass name first time
                            Button myDynamicButton = new Button();
                            if (ThreeFieldRB.IsChecked == true)
                            {
                                myDynamicButton = ServerTCP.FindChild <Button>(InternalField_3x3_Grid, dataSetArr[2]);
                            }
                            if (FiveFieldRB.IsChecked == true)
                            {
                                myDynamicButton = ServerTCP.FindChild <Button>(InternalField_5x5_Grid, dataSetArr[2]);
                            }

                            if (AngryRB.IsChecked == true)
                            {
                                myDynamicButton.Content = new Image
                                {
                                    Source            = new BitmapImage(new Uri("pack://application:,,,/AngryToe;component/Resources/Icon_blue_brd_0.png")),
                                    VerticalAlignment = VerticalAlignment.Center,
                                    Height            = 50
                                };
                            }

                            if (PeppaRB.IsChecked == true)
                            {
                                myDynamicButton.Content = new Image
                                {
                                    Source            = new BitmapImage(new Uri("pack://application:,,,/AngryToe;component/Resources/Icon_blue_Jorge_0.png")),
                                    VerticalAlignment = VerticalAlignment.Center,
                                    Height            = 50
                                };
                            }

                            if (ClassicRB.IsChecked == true)
                            {
                                myDynamicButton.Content = new Image
                                {
                                    Source            = new BitmapImage(new Uri("pack://application:,,,/AngryToe;component/Resources/Icon_0.png")),
                                    VerticalAlignment = VerticalAlignment.Center,
                                    Height            = 50
                                };
                            }
                        }

                        if (dataSetArr[1] == "WN")
                        {
                            Player1Scores++; ScoresPlayer1.Text = Player1Scores.ToString();
                            ContinueRequest myContinue = new ContinueRequest();
                            if (myContinue.ShowDialog() == true)
                            {
                                try
                                {
                                    await ClientTCP.RequestToContinueAsync(whomStep + fieldSizeOrCases + "YESX");

                                    int threeCells = 3; int fiveCells = 5;
                                    InternalField_3x3_Grid.Children.Clear();
                                    InternalField_5x5_Grid.Children.Clear();
                                    Server1Winner = false; Client2Winner = false;
                                    if (ThreeFieldRB.IsChecked == true)
                                    {
                                        AddingButtonsToField(threeCells, InternalField_3x3_Grid);
                                        WorkingField = new int[3, 3];

                                        for (int i = 0; i < threeCells; i++)
                                        {
                                            for (int j = 0; j < threeCells; j++)
                                            {
                                                WorkingField[i, j] = 0;
                                            }
                                        }
                                    }
                                    if (FiveFieldRB.IsChecked == true)
                                    {
                                        AddingButtonsToField(fiveCells, InternalField_5x5_Grid);
                                        WorkingField = new int[5, 5];

                                        for (int i = 0; i < fiveCells; i++)
                                        {
                                            for (int j = 0; j < fiveCells; j++)
                                            {
                                                WorkingField[i, j] = 0;
                                            }
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                    MessageBox.Show(ex.Message);
                                }
                            }
                            else
                            {
                                try
                                {
                                    await ClientTCP.RequestToContinueAsync(whomStep + fieldSizeOrCases + "NOXX");

                                    ClientTCP.client.Close();
                                }
                                catch (Exception ex)
                                {
                                    MessageBox.Show(ex.Message);
                                }
                            }
                        }

                        if (dataSetArr[2] == "YESX")
                        {
                            InternalField_3x3_Grid.Children.Clear();
                            InternalField_5x5_Grid.Children.Clear();
                            Server1Winner = false; Client2Winner = false; firstCycle = true;
                            ClientTCP.client.Close();
                            ScoresPlayer1.Text = Player1Scores.ToString();
                            ScoresPlayer2.Text = Player2Scores.ToString();
                            //Thread.Sleep(2000);
                            await StartButtonRunAsync();

                            ScoresPlayer1.Text = Player1Scores.ToString();
                            ScoresPlayer2.Text = Player2Scores.ToString();
                        }

                        if (dataSetArr[2] == "NOXX")
                        {
                            ClientTCP.client.Close();
                        }
                    }
                }
                else
                {
                    MessageBox.Show("Not correct step!");
                }
            }
        }