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:
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?
so my questions: 1. what do I have to do to make this s*** work
Does your programm currently work? \$\endgroup\$