예제 #1
0
        public async Task StartButtonRunAsync()
        {
            int threeCells = 3; int fiveCells = 5; firstCycle = true; firstCycleServer = true;

            if (InternalField_3x3_Grid.Children.Count > 0)
            {
                InternalField_3x3_Grid.Children.Clear();
            }
            if (InternalField_5x5_Grid.Children.Count > 0)
            {
                InternalField_5x5_Grid.Children.Clear();
            }
            Server1Winner = false; Client2Winner = false; whomStep = "";
            if (ThreeFieldRB.IsChecked == true)
            {
                //MainGrid.Children.Remove(InternalField_5x5_Grid);
                InternalField_3x3_Grid = InternalField_3x3_Grid_Create(MainGrid);
                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)
            {
                //MainGrid.Children.Remove(InternalField_3x3_Grid);
                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;
                    }
                }
            }

            if (NewGameRB.IsChecked == true)//server
            {
            }
            if (ConnectRB.IsChecked == true)//client
            {
            }

            //MessageBox.Show(ServerTCP.GetLocalIPAddress());
            if (ConnectionIsServer == false)
            {
                if (UserNameTB.Text != "")
                {
                    ClientTCP.ClientUserName = UserNameTB.Text; Player2Name.Text = UserNameTB.Text; ScoresPlayer2.Text = "0";
                }
                if (UserNameTB.Text == "")
                {
                    MessageBox.Show("Please, enter Name!"); return;
                }

                try
                {
                    await ClientTCP.WriteLabelBestAsync(IpAddressData, port, WhomStepTB, Requested3SizeField, WorkingField, FiveFieldRB, ThreeFieldRB, ServerTCP.ServUserName, Player1Name);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            if (ConnectionIsServer == true)
            {
                if (UserNameTB.Text != "")
                {
                    ServerTCP.ServUserName = UserNameTB.Text; Player1Name.Text = UserNameTB.Text; ScoresPlayer1.Text = "0";
                }
                if (UserNameTB.Text == "")
                {
                    MessageBox.Show("Please, enter Name!"); return;
                }

                if (ThreeFieldRB.IsChecked == true)
                {
                    ServerWorkingField = 3;
                }
                if (FiveFieldRB.IsChecked == true)
                {
                    ServerWorkingField = 5; // To pass to Client and set correct field size
                }
                try
                {
                    WhomStepTB.Text = "Step for Player: 2";
                    var serverRun = await ServerTCP.RunBest(WhomStepTB, Requested3SizeField, WorkingField, FiveFieldRB, ThreeFieldRB, ServerTCP.ServUserName, ClientTCP.ClientUserName, Player2Name, whomStep);

                    Button myDynamicButton = new Button();
                    if (ThreeFieldRB.IsChecked == true)
                    {
                        myDynamicButton = ServerTCP.FindChild <Button>(InternalField_3x3_Grid, serverRun);
                    }
                    if (FiveFieldRB.IsChecked == true)
                    {
                        myDynamicButton = ServerTCP.FindChild <Button>(InternalField_5x5_Grid, serverRun);
                    }

                    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
                        };
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

                firstCycleServer = false;
            }
        }