1
\$\begingroup\$

I am new to coding and am trying to understand events what is an annoying stage but super important I guess. I just made a Tic Tac Toe game and it is working but not really "beautiful" coded. I really have problems in using the events.

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;


namespace Tic_Tac_Toe
{

    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {


        public MainWindow()
        {
            InitializeComponent();
        }
            public int Feld1G = 0;
            public int Feld1Y = 0;
            public int Feld2G = 0;
            public int Feld2Y = 0;
            public int Feld3G = 0;
            public int Feld3Y = 0;
            public int Feld4G = 0;
            public int Feld4Y = 0;
            public int Feld5G = 0;
            public int Feld5Y = 0;
            public int Feld6G = 0;
            public int Feld6Y = 0;
            public int Feld7G = 0;
            public int Feld7Y = 0;
            public int Feld8G = 0;
            public int Feld8Y = 0;
            public int Feld9G = 0;
            public int Feld9Y = 0;
            public int Greenwincount = 0;
            public int Yellowwincount = 0;


            /*i have 9 buttons(3x3) which are the play field */ 
        private void Feld1_Click(object sender, RoutedEventArgs e)
        {
            // in my game each player have 1 Radiobutton so they check a RButton and then its their turn
            if (Player1RButton.IsChecked == true)
            {
                // i dont wanted to use "X" or "O" so i chose the colors green and yellow
                Feld1.Background = Brushes.Green;
                // Feld1G is for example that Player1 (green) is "owning" this 
                // field/button so i can later check who won the game
                Feld1G = 1;
                Feld1Y = 0;

            }
            if (Player2RButton.IsChecked == true)
            {
                //here is the same thing happening like in the example for green
                Feld1.Background = Brushes.Yellow;
                Feld1Y = 1;
                Feld1G = 0;
            }
        }

        private void Feld2_Click(object sender, RoutedEventArgs e)
        {
            if (Player1RButton.IsChecked == true)
            {
                Feld2.Background = Brushes.Green;
                Feld2G = 1;
                Feld2Y = 0;
            }
            if (Player2RButton.IsChecked == true)
            {
                Feld2.Background = Brushes.Yellow;
                Feld2Y = 1;
                Feld2G = 0;
            }
        }

        private void Feld3_Click(object sender, RoutedEventArgs e)
        {
            if (Player1RButton.IsChecked == true)
            {
                Feld3.Background = Brushes.Green;
                Feld3G = 1;
                Feld3Y = 0;
            }
            if (Player2RButton.IsChecked == true)
            {
                Feld3.Background = Brushes.Yellow;
                Feld3Y = 1;
                Feld3G = 0;
            }
        }

        private void Feld4_Click(object sender, RoutedEventArgs e)
        {
            if (Player1RButton.IsChecked == true)
            {
                Feld4.Background = Brushes.Green;
                Feld4G = 1;
                Feld4Y = 0;
            }
            if(Player2RButton.IsChecked == true)
            {
                Feld4.Background = Brushes.Yellow;
                Feld4Y = 1;
                Feld4G = 0;
            }

        }

        private void Feld5_Click(object sender, RoutedEventArgs e)
        {
            if (Player1RButton.IsChecked == true)
            {
                Feld5.Background = Brushes.Green;
                Feld5G = 1;
                Feld5Y = 0;
            }
            if (Player2RButton.IsChecked == true)
            {
                Feld5.Background = Brushes.Yellow;
                Feld5Y = 1;
                Feld5G = 0;
            }
        }

        private void Feld6_Click(object sender, RoutedEventArgs e)
        {
            if (Player1RButton.IsChecked == true)
            {
                Feld6.Background = Brushes.Green;
                Feld6G = 1;
                Feld6Y = 0;
            }
            if (Player2RButton.IsChecked == true)
            {
                Feld6.Background = Brushes.Yellow;
                Feld6Y = 1;
                Feld6G = 0;
            }
        }

        private void Feld7_Click(object sender, RoutedEventArgs e)
        {
            if (Player1RButton.IsChecked == true)
            {
                Feld7.Background = Brushes.Green;
                Feld7G = 1;
                Feld7Y = 0;
            }
            if (Player2RButton.IsChecked == true)
            {
                Feld7.Background = Brushes.Yellow;
                Feld7Y = 1;
                Feld7G = 0;
            }
        }

        private void Feld8_Click(object sender, RoutedEventArgs e)
        {
            if (Player1RButton.IsChecked == true)
            {
                Feld8.Background = Brushes.Green;
                Feld8G = 1;
                Feld8Y = 0;
            }
            if (Player2RButton.IsChecked == true)
            {
                Feld8.Background = Brushes.Yellow;
                Feld8Y = 1;
                Feld8G = 0;
            }
        }

        private void Feld9_Click(object sender, RoutedEventArgs e)
        {
            if (Player1RButton.IsChecked == true)
            {
                Feld9.Background = Brushes.Green;
                Feld9G = 1;
                Feld9Y = 0;
            }
            if (Player2RButton.IsChecked == true)
            {
                Feld9.Background = Brushes.Yellow;
                Feld9Y = 1;
                Feld9G = 0;
            }
        }
        public void OnClick (EventArgs e)
        {
            this.Feld1.Click += new RoutedEventHandler(this.OnClick);
        }
        private void Player1RButton_Checked(object sender, RoutedEventArgs e)
        {

        }

        private void Player2RButton_Checked(object sender, RoutedEventArgs e)
        {

        }

        private void New_Game_Click(object sender, RoutedEventArgs e)
        {
            Feld1.Background = Brushes.Gray;
            Feld2.Background = Brushes.Gray;
            Feld3.Background = Brushes.Gray;
            Feld4.Background = Brushes.Gray;
            Feld5.Background = Brushes.Gray;
            Feld6.Background = Brushes.Gray;
            Feld7.Background = Brushes.Gray;
            Feld8.Background = Brushes.Gray;
            Feld9.Background = Brushes.Gray;

            if (Feld1G == 1 && Feld2G == 1 && Feld3G == 1)
            {
                Winnertb.Text = "Player1";
                Winnertb.Background = Brushes.Green;
                Greenwincount++;
            }
           else if (Feld4G == 1 && Feld5G == 1 && Feld6G == 1)
            {
                Winnertb.Text = "Player1";
                Winnertb.Background = Brushes.Green;
                Greenwincount++;
            }
            else if (Feld7G == 1 && Feld8G == 1 && Feld9G == 1)
            {
                Winnertb.Text = "Player1";
                Winnertb.Background = Brushes.Green;
                Greenwincount++;
            }
            else if (Feld1G == 1 && Feld4G == 1 && Feld7G == 1)
            {
                Winnertb.Text = "Player1";
                Winnertb.Background = Brushes.Green;
                Greenwincount++;
            }
            else if (Feld2G == 1 && Feld5G == 1 && Feld8G == 1)
            {
                Winnertb.Text = "Player1";
                Winnertb.Background = Brushes.Green;
                Greenwincount++;
            }
            else if (Feld3G == 1 && Feld6G == 1 && Feld9G == 1)
            {
                Winnertb.Text = "Player1";
                Winnertb.Background = Brushes.Green;
                Greenwincount++;
            }
            else if (Feld1G == 1 && Feld5G == 1 && Feld9G == 1)
            {
                Winnertb.Text = "Player1";
                Winnertb.Background = Brushes.Green;
                Greenwincount++;
            }
            else if (Feld3G == 1 && Feld5G == 1 && Feld7G == 1)
            {
                Winnertb.Text = "Player1";
                Winnertb.Background = Brushes.Green;
                Greenwincount++;
            }

            else if(Feld1Y==1 && Feld2Y==1 && Feld3Y==1)
            {
                Winnertb.Text = "Player2";
                Winnertb.Background = Brushes.Yellow;
                Yellowwincount++;
            }
            else if (Feld4Y == 1 && Feld5Y == 1 && Feld6Y == 1)
            {
                Winnertb.Text = "Player2";
                Winnertb.Background = Brushes.Yellow;
                Yellowwincount++;
            }
            else if (Feld7Y == 1 && Feld8Y == 1 && Feld9Y == 1)
            {
                Winnertb.Text = "Player2";
                Winnertb.Background = Brushes.Yellow;
                Yellowwincount++;
            }
            else if (Feld1Y == 1 && Feld4Y == 1 && Feld7Y == 1)
            {
                Winnertb.Text = "Player2";
                Winnertb.Background = Brushes.Yellow;
                Yellowwincount++;
            }
            else if (Feld8Y == 1 && Feld5Y == 1 && Feld2Y == 1)
            {
                Winnertb.Text = "Player2";
                Winnertb.Background = Brushes.Yellow;
                Yellowwincount++;
            }
            else if (Feld9Y == 1 && Feld6Y == 1 && Feld3Y == 1)
            {
                Winnertb.Text = "Player2";
                Winnertb.Background = Brushes.Yellow;
                Yellowwincount++;
            }
            else if (Feld1Y == 1 && Feld5Y == 1 && Feld9Y == 1)
            {
                Winnertb.Text = "Player2";
                Winnertb.Background = Brushes.Yellow;
                Yellowwincount++;
            }
            else if (Feld3Y == 1 && Feld5Y == 1 && Feld7Y == 1)
            {
                Winnertb.Text = "Player2";
                Winnertb.Background = Brushes.Yellow;
                Yellowwincount++;
            }
            else
            {
                Winnertb.Text = "Draw";
                Winnertb.Background = Brushes.White;


            }
            Greenwins.Text = Convert.ToString(Greenwincount);
            Yellowwins.Text = Convert.ToString(Yellowwincount);

            Feld1G = 0;
            Feld1Y = 0;
            Feld2G = 0;
            Feld2Y = 0;
            Feld3G = 0;
            Feld3Y = 0;
            Feld4G = 0;
            Feld4Y = 0;
            Feld5G = 0;
            Feld5Y = 0;
            Feld6G = 0;
            Feld6Y = 0;
            Feld7G = 0;
            Feld7Y = 0;
            Feld8G = 0;
            Feld8Y = 0;
            Feld9G = 0;
            Feld9Y = 0;

    }
    }
}

The UI:

1

I want to make it a bit more clearly here: I want all the functions run from the method above and not in each button event for itself.

What do I have to do to make this work the way I explained above to make one method for all of my buttons?

\$\endgroup\$
8
  • \$\begingroup\$ so my questions: 1. what do I have to do to make this s*** work Does your programm currently work? \$\endgroup\$
    – yuri
    Commented May 29, 2017 at 11:06
  • \$\begingroup\$ yes it is doing what i want but i a workmate told me i should try to change it that way cause its not good coding and for learning more about the events \$\endgroup\$
    – Pr0gr4mm3r
    Commented May 29, 2017 at 11:09
  • \$\begingroup\$ You should probably rephrase the question. See this for some help How to Ask \$\endgroup\$
    – yuri
    Commented May 29, 2017 at 11:10
  • 4
    \$\begingroup\$ You shouldn't use events in WPF, and instead use Commands: wpf-tutorial.com/commands/using-commands \$\endgroup\$
    – BCdotWEB
    Commented May 29, 2017 at 14:12
  • 2
    \$\begingroup\$ My advice is to look up DRY Imagine writing all your code fresh and every time you'd want to copy and paste something, don't. Store your grid in an array or List and pass parameters to functions. Also don't assume coders are "boys" \$\endgroup\$ Commented May 29, 2017 at 14:49

1 Answer 1

1
\$\begingroup\$

Looking to your last version, you need some improvements to check all conditions.

I designed a solution using your version and based on this resource.

using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace TicTacToe
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public int[,] gridValues = new int[3, 3];
    int playerTurn;
    int currentStarted;

    public MainWindow()
    {
        InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        foreach (var control in GridTicTacToe.Children.OfType<Button>())
        {
            if (!control.Name.Equals("btnNewGame"))
                control.Click += MakeChoice;
        }

        playerTurn = 1;
        currentStarted = playerTurn;
    }

    private void btnNewGame_Click(object sender, RoutedEventArgs e)
    {
        StartNewGame();
    }

    private void MakeChoice(object sender, RoutedEventArgs e)
    {
        // Safe cast the sender to type Button.
        var button = sender as Button;
        if (button == null) return;

        double buttonChosen = 0;
        double.TryParse(button.Name, out buttonChosen);

        // if buttonChosen is 0, the name of the button is not correct. Let all 9x9 buttons names as grid positions: 0.0, 0.1, 0.2, 1.0, 1.1, 1.2...
        if (buttonChosen == 0)
        {
            MessageBox.Show("Error! Please, contact support.");
            return;
        }
        var names = button.Name.ToCharArray();

        int firstValue = names.First();
        int secondValue = names.Last();

        // Change the background color of the button
        // yellow or green based on which radio button
        // is checked.

        if (playerTurn == 1)
        {
            button.Background = Brushes.Green;
            button.IsEnabled = false;
            gridValues[firstValue, secondValue] = 1;
            Player1RButton.IsChecked = false;
            Player2RButton.IsChecked = true;
            playerTurn = 2;
        }
        else
        {
            button.Background = Brushes.Yellow;
            button.IsEnabled = false;
            gridValues[firstValue, secondValue] = 2;
            Player1RButton.IsChecked = true;
            Player2RButton.IsChecked = false;
            playerTurn = 1;
        }

        checkAllRows();
        checkAllColumns();
        checkAllDiagonals();
    }

    private void StartNewGame()
    {
        foreach (var control in GridTicTacToe.Children.OfType<Button>())
        {
            control.Background = Brushes.Gray;
            control.IsEnabled = true;
        }

        for (int i = 0; i < 3; i++)
            for (int j = 0; j < 3; j++)
                gridValues[i, j] = 0;

        if (currentStarted == 1)
            playerTurn = 2;
        else
            playerTurn = 1;

        currentStarted = playerTurn;
    }

    private void checkAllColumns()
    {
        int countForP1 = 0;
        int countForP2 = 0;

        for (int i = 0; i < 3; i++)
        {

            countForP1 = 0;
            countForP2 = 0;

            for (int j = 0; j < 3; j++)
            {
                if (gridValues[j, i] == 1)
                {
                    countForP1++;
                }

                if (gridValues[j, i] == 2)
                {
                    countForP2++;
                }

                if (countForP1 == 3)
                {
                    MessageBox.Show("Player 1 Wins !!");
                    StartNewGame();
                    break;
                }
                if (countForP2 == 3)
                {
                    MessageBox.Show("Player 2 Wins !!");
                    StartNewGame();
                    break;
                }
            }
        }
    }

    private void checkAllDiagonals()
    {
        int countforP1 = 0;
        int countforP2 = 0;

        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                if (i == j && gridValues[j, i] == 1)
                {
                    countforP1++;
                }

                if (i == j && gridValues[j, i] == 2)
                {
                    countforP2++;
                }

                if (countforP1 == 3)
                {
                    MessageBox.Show("Player 1 Wins !!");
                    StartNewGame();
                    break;
                }
                if (countforP2 == 3)
                {
                    MessageBox.Show("Player 2 Wins !!");
                    StartNewGame();
                    break;
                }
            }
        }
    }

    private void checkAllRows()
    {
        int countforP1 = 0;
        int countforP2 = 0;

        for (int i = 0; i < 3; i++)
        {
            countforP1 = 0;
            countforP2 = 0;

            for (int j = 0; j < 3; j++)
            {
                if (gridValues[i, j] == 1)
                {
                    countforP1++;
                }
                if (gridValues[i, j] == 2)
                {
                    countforP2++;
                }

                if (countforP1 == 3)
                {
                    MessageBox.Show("Player 1 Wins !!");
                    StartNewGame();
                    break;
                }
                if (countforP2 == 3)
                {
                    MessageBox.Show("Player 2 Wins !!");
                    StartNewGame();
                    break;
                }
            }
        }
    }
}
}

To use this, your button's names should follow grid positions pattern: first button: 0.0, second button: 0.1...

I wrote this blindly and didn't try, so you can try and change what is wrong, if something is wrong.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.