Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace TicTacToe
- {
- public class MesDonnees : INotifyPropertyChanged
- {
- private string monTexte;
- private int rangee;
- private int colonne;
- public MesDonnees() { }
- public MesDonnees(int rangee, int colonne)
- {
- this.monTexte = "";
- this.rangee = rangee;
- this.colonne = colonne;
- }
- public int Rangee { get { return rangee; } }
- public int Colonne { get { return colonne; } }
- public String MonTexte
- {
- get { return monTexte; }
- set
- {
- monTexte = value;
- OnPropertyChanged("MonTexte");
- }
- }
- public override bool Equals(object obj)
- {
- if (!(obj is MesDonnees))
- return false;
- var other = obj as MesDonnees;
- return (monTexte.Equals(other.monTexte));
- }
- public static bool operator ==(MesDonnees x, MesDonnees y)
- {
- return x.Equals(y);
- }
- public static bool operator !=(MesDonnees x, MesDonnees y)
- {
- return !(x == y);
- }
- public bool EstVide()
- {
- return monTexte.Length == 0;
- }
- public event PropertyChangedEventHandler PropertyChanged;
- private void OnPropertyChanged(string info)
- {
- PropertyChangedEventHandler handler = PropertyChanged;
- if (handler != null)
- {
- handler(this, new PropertyChangedEventArgs(info));
- }
- }
- }
- public partial class MainWindow : Window
- {
- string[] XO = { "X", "O" };
- MesDonnees[,] tableau = new MesDonnees[3, 3];
- int[] matchGagne = { 0, 0 };
- int joueurCourant = 1;
- SolidColorBrush defaultSolidColorBrush;
- SolidColorBrush courantSolidColorBrush;
- int coup = 0;
- bool gagnant = false;
- public MainWindow()
- {
- InitializeComponent();
- defaultSolidColorBrush = (SolidColorBrush) textBlockJoueur1.Background;
- courantSolidColorBrush = new SolidColorBrush(Colors.PowderBlue);
- PremierePartie();
- ChoisirLeProchainJoueur();
- }
- private void PremierePartie()
- {
- for (int i = 0; i < tableau.GetLength(0); i++)
- {
- for (int j = 0; j < tableau.GetLength(1); j++)
- {
- tableau[i, j] = new MesDonnees(i, j);
- }
- }
- Binding myBinding = new Binding("MonTexte");
- myBinding.Source = tableau[0, 0];
- bouton_0_0.SetBinding(Button.ContentProperty, myBinding);
- myBinding = new Binding("MonTexte");
- myBinding.Source = tableau[0, 1];
- bouton_0_1.SetBinding(Button.ContentProperty, myBinding);
- myBinding = new Binding("MonTexte");
- myBinding.Source = tableau[0, 2];
- bouton_0_2.SetBinding(Button.ContentProperty, myBinding);
- myBinding = new Binding("MonTexte");
- myBinding.Source = tableau[1, 0];
- bouton_1_0.SetBinding(Button.ContentProperty, myBinding);
- myBinding = new Binding("MonTexte");
- myBinding.Source = tableau[1, 1];
- bouton_1_1.SetBinding(Button.ContentProperty, myBinding);
- myBinding = new Binding("MonTexte");
- myBinding.Source = tableau[1, 2];
- bouton_1_2.SetBinding(Button.ContentProperty, myBinding);
- myBinding = new Binding("MonTexte");
- myBinding.Source = tableau[2, 0];
- bouton_2_0.SetBinding(Button.ContentProperty, myBinding);
- myBinding = new Binding("MonTexte");
- myBinding.Source = tableau[2, 1];
- bouton_2_1.SetBinding(Button.ContentProperty, myBinding);
- myBinding = new Binding("MonTexte");
- myBinding.Source = tableau[2, 2];
- bouton_2_2.SetBinding(Button.ContentProperty, myBinding);
- }
- private void NouvellePartie()
- {
- for (int i = 0; i < tableau.GetLength(0); i++)
- {
- for (int j = 0; j < tableau.GetLength(1); j++)
- {
- tableau[i, j].MonTexte = "";
- }
- }
- coup = 0;
- gagnant = false;
- }
- private void ChoisirLeProchainJoueur()
- {
- joueurCourant = (joueurCourant + 1) % 2;
- textBlockJoueur1.Background = (joueurCourant == 0) ? courantSolidColorBrush : defaultSolidColorBrush;
- textBlockJoueur2.Background = (joueurCourant == 1) ? courantSolidColorBrush : defaultSolidColorBrush;
- }
- private void ProcesserBouton(int rangee, int colonne)
- {
- // si le joueur a choisi une case qui contient déjà un X ou un O, on l'ignore.
- if (tableau[rangee, colonne].EstVide())
- {
- if (coup == 9)
- {
- return;
- }
- if (gagnant == true)
- {
- MessageBox.Show("There`s already a winner. Restart it pls");
- return;
- }
- tableau[rangee, colonne].MonTexte = XO[joueurCourant];
- coup++;
- // Vérifier ligne par ligne
- // Rangées
- for (int i = 0; i < tableau.GetLength(0); i = i + 1)
- {
- if (!tableau[0, 0].EstVide())
- {
- if ((tableau[0, 0] == tableau[0, 1]) && (tableau[0, 0] == tableau[0, 2]))
- {
- MessageBox.Show("J'ai un gagnant");
- gagnant = true;
- matchGagne[joueurCourant]++;
- if (joueurCourant == 0)
- {
- textBlockScore1.Text = Convert.ToString(matchGagne[joueurCourant]);
- }
- else
- {
- textBlockScore2.Text = Convert.ToString(matchGagne[joueurCourant]);
- }
- break;
- }
- }
- if (!tableau[1, 0].EstVide())
- {
- if ((tableau[1, 0] == tableau[1, 1]) && (tableau[1, 0] == tableau[1, 2]))
- {
- MessageBox.Show("J'ai un gagnant");
- gagnant = true;
- matchGagne[joueurCourant]++;
- if (joueurCourant == 0)
- {
- textBlockScore1.Text = Convert.ToString(matchGagne[joueurCourant]);
- }
- else
- {
- textBlockScore2.Text = Convert.ToString(matchGagne[joueurCourant]);
- }
- break;
- }
- }
- if (!tableau[2, 0].EstVide())
- {
- if ((tableau[2, 0] == tableau[2, 1]) && (tableau[2, 0] == tableau[2, 2]))
- {
- MessageBox.Show("J'ai un gagnant");
- gagnant = true;
- matchGagne[joueurCourant]++;
- if (joueurCourant == 0)
- {
- textBlockScore1.Text = Convert.ToString(matchGagne[joueurCourant]);
- }
- else
- {
- textBlockScore2.Text = Convert.ToString(matchGagne[joueurCourant]);
- }
- break;
- }
- }
- }
- // Diagonales
- for (int i = 0; i < tableau.GetLength(1); i = i + 1)
- {
- if (!tableau[0, 0].EstVide())
- {
- if ((tableau [0, 0] == tableau [1, 1]) && (tableau [0, 0] == tableau [2, 2]))
- {
- MessageBox.Show("J'ai un gagnant");
- gagnant = true;
- matchGagne[joueurCourant]++;
- if (joueurCourant == 0)
- {
- textBlockScore1.Text = Convert.ToString(matchGagne[joueurCourant]);
- }
- else
- {
- textBlockScore2.Text = Convert.ToString(matchGagne[joueurCourant]);
- }
- break;
- }
- }
- if (!tableau[0, 2].EstVide())
- {
- if ((tableau[0, 2] == tableau[1, 1]) && (tableau[0, 2] == tableau[2, 0]))
- {
- MessageBox.Show("J'ai un gagnant");
- gagnant = true;
- matchGagne[joueurCourant]++;
- if (joueurCourant == 0)
- {
- textBlockScore1.Text = Convert.ToString(matchGagne[joueurCourant]);
- }
- else
- {
- textBlockScore2.Text = Convert.ToString(matchGagne[joueurCourant]);
- }
- break;
- }
- }
- }
- // Colonnes
- for (int i = 0; i < tableau.GetLength(1); i = i + 1)
- {
- if (!tableau[0, 0].EstVide())
- {
- if ((tableau [0, 0] == tableau [1, 0]) && (tableau [0, 0] == tableau [2, 0]))
- {
- MessageBox.Show("J'ai un gagnant");
- gagnant = true;
- matchGagne[joueurCourant]++;
- if (joueurCourant == 0)
- {
- textBlockScore1.Text = Convert.ToString(matchGagne[joueurCourant]);
- }
- else
- {
- textBlockScore2.Text = Convert.ToString(matchGagne[joueurCourant]);
- }
- break;
- }
- }
- if (!tableau[0, 1].EstVide())
- {
- if ((tableau[0, 1] == tableau[1, 1]) && (tableau[0, 1] == tableau[2, 1]))
- {
- MessageBox.Show("J'ai un gagnant");
- gagnant = true;
- matchGagne[joueurCourant]++;
- if (joueurCourant == 0)
- {
- textBlockScore1.Text = Convert.ToString(matchGagne[joueurCourant]);
- }
- else
- {
- textBlockScore2.Text = Convert.ToString(matchGagne[joueurCourant]);
- }
- break;
- }
- }
- if (!tableau[0, 2].EstVide())
- {
- if ((tableau[0, 2] == tableau[1, 2]) && (tableau[0, 2] == tableau[2, 2]))
- {
- MessageBox.Show("J'ai un gagnant");
- gagnant = true;
- matchGagne[joueurCourant]++;
- if (joueurCourant == 0)
- {
- textBlockScore1.Text = Convert.ToString(matchGagne[joueurCourant]);
- }
- else
- {
- textBlockScore2.Text = Convert.ToString(matchGagne[joueurCourant]);
- }
- break;
- }
- }
- }
- /*
- * Ici vous allez rajouter la logique
- * pour permettre à 2 joueurs de jouer
- * au tic-tac-toe
- */
- ChoisirLeProchainJoueur();
- }
- }
- private void CliquerBouton(object sender, RoutedEventArgs e)
- {
- Button btn = sender as Button;
- if (btn != null)
- {
- // Le même code sera exécuté si on appuie sur une des 9 boutons.
- BindingExpression expr = BindingOperations.GetBindingExpression(btn, Button.ContentProperty);
- ProcesserBouton(((MesDonnees)expr.DataItem).Rangee, ((MesDonnees)expr.DataItem).Colonne);
- }
- }
- private void CliquerNouvellePartie(object sender, RoutedEventArgs e)
- {
- NouvellePartie();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement