Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.58 KB | None | 0 0
  1. namespace TicTacToe
  2. {
  3. public class MesDonnees : INotifyPropertyChanged
  4. {
  5. private string monTexte;
  6. private int rangee;
  7. private int colonne;
  8.  
  9. public MesDonnees() { }
  10.  
  11. public MesDonnees(int rangee, int colonne)
  12. {
  13. this.monTexte = "";
  14. this.rangee = rangee;
  15. this.colonne = colonne;
  16. }
  17.  
  18. public int Rangee { get { return rangee; } }
  19. public int Colonne { get { return colonne; } }
  20. public String MonTexte
  21. {
  22. get { return monTexte; }
  23. set
  24. {
  25. monTexte = value;
  26. OnPropertyChanged("MonTexte");
  27. }
  28. }
  29.  
  30. public override bool Equals(object obj)
  31. {
  32. if (!(obj is MesDonnees))
  33. return false;
  34.  
  35. var other = obj as MesDonnees;
  36.  
  37. return (monTexte.Equals(other.monTexte));
  38. }
  39.  
  40. public static bool operator ==(MesDonnees x, MesDonnees y)
  41. {
  42. return x.Equals(y);
  43. }
  44.  
  45. public static bool operator !=(MesDonnees x, MesDonnees y)
  46. {
  47. return !(x == y);
  48. }
  49.  
  50. public bool EstVide()
  51. {
  52. return monTexte.Length == 0;
  53. }
  54.  
  55. public event PropertyChangedEventHandler PropertyChanged;
  56.  
  57. private void OnPropertyChanged(string info)
  58. {
  59. PropertyChangedEventHandler handler = PropertyChanged;
  60. if (handler != null)
  61. {
  62. handler(this, new PropertyChangedEventArgs(info));
  63. }
  64. }
  65. }
  66. public partial class MainWindow : Window
  67. {
  68. string[] XO = { "X", "O" };
  69. MesDonnees[,] tableau = new MesDonnees[3, 3];
  70. int[] matchGagne = { 0, 0 };
  71. int joueurCourant = 1;
  72. SolidColorBrush defaultSolidColorBrush;
  73. SolidColorBrush courantSolidColorBrush;
  74. int coup = 0;
  75. bool gagnant = false;
  76. public MainWindow()
  77. {
  78. InitializeComponent();
  79. defaultSolidColorBrush = (SolidColorBrush) textBlockJoueur1.Background;
  80. courantSolidColorBrush = new SolidColorBrush(Colors.PowderBlue);
  81. PremierePartie();
  82. ChoisirLeProchainJoueur();
  83. }
  84.  
  85. private void PremierePartie()
  86. {
  87. for (int i = 0; i < tableau.GetLength(0); i++)
  88. {
  89. for (int j = 0; j < tableau.GetLength(1); j++)
  90. {
  91. tableau[i, j] = new MesDonnees(i, j);
  92. }
  93. }
  94.  
  95. Binding myBinding = new Binding("MonTexte");
  96. myBinding.Source = tableau[0, 0];
  97. bouton_0_0.SetBinding(Button.ContentProperty, myBinding);
  98.  
  99. myBinding = new Binding("MonTexte");
  100. myBinding.Source = tableau[0, 1];
  101. bouton_0_1.SetBinding(Button.ContentProperty, myBinding);
  102.  
  103. myBinding = new Binding("MonTexte");
  104. myBinding.Source = tableau[0, 2];
  105. bouton_0_2.SetBinding(Button.ContentProperty, myBinding);
  106.  
  107. myBinding = new Binding("MonTexte");
  108. myBinding.Source = tableau[1, 0];
  109. bouton_1_0.SetBinding(Button.ContentProperty, myBinding);
  110.  
  111. myBinding = new Binding("MonTexte");
  112. myBinding.Source = tableau[1, 1];
  113. bouton_1_1.SetBinding(Button.ContentProperty, myBinding);
  114.  
  115. myBinding = new Binding("MonTexte");
  116. myBinding.Source = tableau[1, 2];
  117. bouton_1_2.SetBinding(Button.ContentProperty, myBinding);
  118.  
  119. myBinding = new Binding("MonTexte");
  120. myBinding.Source = tableau[2, 0];
  121. bouton_2_0.SetBinding(Button.ContentProperty, myBinding);
  122.  
  123. myBinding = new Binding("MonTexte");
  124. myBinding.Source = tableau[2, 1];
  125. bouton_2_1.SetBinding(Button.ContentProperty, myBinding);
  126.  
  127. myBinding = new Binding("MonTexte");
  128. myBinding.Source = tableau[2, 2];
  129. bouton_2_2.SetBinding(Button.ContentProperty, myBinding);
  130. }
  131. private void NouvellePartie()
  132. {
  133. for (int i = 0; i < tableau.GetLength(0); i++)
  134. {
  135. for (int j = 0; j < tableau.GetLength(1); j++)
  136. {
  137. tableau[i, j].MonTexte = "";
  138. }
  139. }
  140. coup = 0;
  141. gagnant = false;
  142. }
  143.  
  144. private void ChoisirLeProchainJoueur()
  145. {
  146. joueurCourant = (joueurCourant + 1) % 2;
  147.  
  148. textBlockJoueur1.Background = (joueurCourant == 0) ? courantSolidColorBrush : defaultSolidColorBrush;
  149. textBlockJoueur2.Background = (joueurCourant == 1) ? courantSolidColorBrush : defaultSolidColorBrush;
  150. }
  151.  
  152. private void ProcesserBouton(int rangee, int colonne)
  153. {
  154.  
  155. // si le joueur a choisi une case qui contient déjà un X ou un O, on l'ignore.
  156. if (tableau[rangee, colonne].EstVide())
  157. {
  158. if (coup == 9)
  159. {
  160. return;
  161. }
  162.  
  163. if (gagnant == true)
  164. {
  165. MessageBox.Show("There`s already a winner. Restart it pls");
  166. return;
  167. }
  168.  
  169. tableau[rangee, colonne].MonTexte = XO[joueurCourant];
  170. coup++;
  171.  
  172. // Vérifier ligne par ligne
  173. // Rangées
  174. for (int i = 0; i < tableau.GetLength(0); i = i + 1)
  175. {
  176.  
  177. if (!tableau[0, 0].EstVide())
  178. {
  179.  
  180.  
  181. if ((tableau[0, 0] == tableau[0, 1]) && (tableau[0, 0] == tableau[0, 2]))
  182. {
  183. MessageBox.Show("J'ai un gagnant");
  184. gagnant = true;
  185. matchGagne[joueurCourant]++;
  186. if (joueurCourant == 0)
  187. {
  188. textBlockScore1.Text = Convert.ToString(matchGagne[joueurCourant]);
  189. }
  190. else
  191. {
  192. textBlockScore2.Text = Convert.ToString(matchGagne[joueurCourant]);
  193. }
  194. break;
  195. }
  196. }
  197. if (!tableau[1, 0].EstVide())
  198. {
  199. if ((tableau[1, 0] == tableau[1, 1]) && (tableau[1, 0] == tableau[1, 2]))
  200. {
  201. MessageBox.Show("J'ai un gagnant");
  202. gagnant = true;
  203. matchGagne[joueurCourant]++;
  204. if (joueurCourant == 0)
  205. {
  206. textBlockScore1.Text = Convert.ToString(matchGagne[joueurCourant]);
  207. }
  208. else
  209. {
  210. textBlockScore2.Text = Convert.ToString(matchGagne[joueurCourant]);
  211. }
  212. break;
  213. }
  214. }
  215. if (!tableau[2, 0].EstVide())
  216. {
  217. if ((tableau[2, 0] == tableau[2, 1]) && (tableau[2, 0] == tableau[2, 2]))
  218. {
  219. MessageBox.Show("J'ai un gagnant");
  220. gagnant = true;
  221. matchGagne[joueurCourant]++;
  222. if (joueurCourant == 0)
  223. {
  224. textBlockScore1.Text = Convert.ToString(matchGagne[joueurCourant]);
  225. }
  226. else
  227. {
  228. textBlockScore2.Text = Convert.ToString(matchGagne[joueurCourant]);
  229. }
  230. break;
  231. }
  232. }
  233.  
  234. }
  235. // Diagonales
  236. for (int i = 0; i < tableau.GetLength(1); i = i + 1)
  237. {
  238. if (!tableau[0, 0].EstVide())
  239. {
  240. if ((tableau [0, 0] == tableau [1, 1]) && (tableau [0, 0] == tableau [2, 2]))
  241. {
  242. MessageBox.Show("J'ai un gagnant");
  243. gagnant = true;
  244. matchGagne[joueurCourant]++;
  245. if (joueurCourant == 0)
  246. {
  247. textBlockScore1.Text = Convert.ToString(matchGagne[joueurCourant]);
  248. }
  249. else
  250. {
  251. textBlockScore2.Text = Convert.ToString(matchGagne[joueurCourant]);
  252. }
  253. break;
  254. }
  255. }
  256. if (!tableau[0, 2].EstVide())
  257. {
  258. if ((tableau[0, 2] == tableau[1, 1]) && (tableau[0, 2] == tableau[2, 0]))
  259. {
  260. MessageBox.Show("J'ai un gagnant");
  261. gagnant = true;
  262. matchGagne[joueurCourant]++;
  263. if (joueurCourant == 0)
  264. {
  265. textBlockScore1.Text = Convert.ToString(matchGagne[joueurCourant]);
  266. }
  267. else
  268. {
  269. textBlockScore2.Text = Convert.ToString(matchGagne[joueurCourant]);
  270. }
  271. break;
  272. }
  273. }
  274. }
  275. // Colonnes
  276. for (int i = 0; i < tableau.GetLength(1); i = i + 1)
  277. {
  278. if (!tableau[0, 0].EstVide())
  279. {
  280. if ((tableau [0, 0] == tableau [1, 0]) && (tableau [0, 0] == tableau [2, 0]))
  281. {
  282. MessageBox.Show("J'ai un gagnant");
  283. gagnant = true;
  284. matchGagne[joueurCourant]++;
  285. if (joueurCourant == 0)
  286. {
  287. textBlockScore1.Text = Convert.ToString(matchGagne[joueurCourant]);
  288. }
  289. else
  290. {
  291. textBlockScore2.Text = Convert.ToString(matchGagne[joueurCourant]);
  292. }
  293. break;
  294. }
  295. }
  296. if (!tableau[0, 1].EstVide())
  297. {
  298. if ((tableau[0, 1] == tableau[1, 1]) && (tableau[0, 1] == tableau[2, 1]))
  299. {
  300. MessageBox.Show("J'ai un gagnant");
  301. gagnant = true;
  302. matchGagne[joueurCourant]++;
  303. if (joueurCourant == 0)
  304. {
  305. textBlockScore1.Text = Convert.ToString(matchGagne[joueurCourant]);
  306. }
  307. else
  308. {
  309. textBlockScore2.Text = Convert.ToString(matchGagne[joueurCourant]);
  310. }
  311. break;
  312. }
  313. }
  314. if (!tableau[0, 2].EstVide())
  315. {
  316. if ((tableau[0, 2] == tableau[1, 2]) && (tableau[0, 2] == tableau[2, 2]))
  317. {
  318. MessageBox.Show("J'ai un gagnant");
  319. gagnant = true;
  320. matchGagne[joueurCourant]++;
  321. if (joueurCourant == 0)
  322. {
  323. textBlockScore1.Text = Convert.ToString(matchGagne[joueurCourant]);
  324. }
  325. else
  326. {
  327. textBlockScore2.Text = Convert.ToString(matchGagne[joueurCourant]);
  328. }
  329. break;
  330. }
  331. }
  332. }
  333. /*
  334. * Ici vous allez rajouter la logique
  335. * pour permettre à 2 joueurs de jouer
  336. * au tic-tac-toe
  337. */
  338. ChoisirLeProchainJoueur();
  339. }
  340. }
  341. private void CliquerBouton(object sender, RoutedEventArgs e)
  342. {
  343. Button btn = sender as Button;
  344. if (btn != null)
  345. {
  346. // Le même code sera exécuté si on appuie sur une des 9 boutons.
  347. BindingExpression expr = BindingOperations.GetBindingExpression(btn, Button.ContentProperty);
  348. ProcesserBouton(((MesDonnees)expr.DataItem).Rangee, ((MesDonnees)expr.DataItem).Colonne);
  349. }
  350. }
  351.  
  352. private void CliquerNouvellePartie(object sender, RoutedEventArgs e)
  353. {
  354. NouvellePartie();
  355. }
  356. }
  357. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement