Advertisement
Guest User

X SI 0

a guest
Feb 19th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.51 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace mare_proiect_1
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         bool turn = true;
  16.         int turn_count = 0;
  17.  
  18.         public Form1()
  19.         {
  20.             InitializeComponent();
  21.         }
  22.  
  23.         private void Form1_Load(object sender, EventArgs e)
  24.         {
  25.  
  26.         }
  27.  
  28.         private void button_click(object sender, EventArgs e)
  29.         {
  30.             Button b = (Button)sender;
  31.             if (turn) b.Text = "X";
  32.             else b.Text = "0";
  33.  
  34.             turn = !turn;
  35.             b.Enabled = false;
  36.             turn_count++;
  37.  
  38.             checkForWinner();
  39.         }
  40.        
  41.         private void checkForWinner()
  42.         {
  43.             bool win = false;
  44.             if (button1.Text == button2.Text && button2.Text == button3.Text && !button1.Enabled) win = true;
  45.             else if (button4.Text == button5.Text && button5.Text == button6.Text && !button4.Enabled) win = true;
  46.             else if (button7.Text == button8.Text && button8.Text == button9.Text && !button7.Enabled) win = true;
  47.  
  48.             if (button1.Text == button4.Text && button4.Text == button7.Text && !button1.Enabled) win = true;
  49.             else if (button2.Text == button5.Text && button5.Text == button8.Text && !button2.Enabled) win = true;
  50.             else if (button3.Text == button6.Text && button6.Text == button9.Text && !button3.Enabled) win = true;
  51.  
  52.             else if (button1.Text == button5.Text && button5.Text == button9.Text && !button1.Enabled) win = true;
  53.             else if (button3.Text == button5.Text && button5.Text == button7.Text && !button7.Enabled) win = true;
  54.  
  55.             if (win)
  56.             {
  57.                 disableButtons();
  58.  
  59.                 String winner = "";
  60.                 if (turn) winner = "0";
  61.                 else winner = "X";
  62.  
  63.                 MessageBox.Show(winner + " a castigat!");
  64.             }
  65.             else
  66.             {
  67.                 if (turn_count == 9) MessageBox.Show("Draw");
  68.             }
  69.         }
  70.        
  71.         private void disableButtons()
  72.         {
  73.             try
  74.             {
  75.                 foreach(Control c in Controls)
  76.                 {
  77.                     Button b = (Button)c;
  78.                     b.Enabled = false;
  79.                 }
  80.             }
  81.             catch {}
  82.         }
  83.  
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement