Seydie

Tic Tac Toe

Feb 18th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.04 KB | None | 0 0
  1. namespace WindowsFormsApp2
  2. {
  3.     public partial class Form : System.Windows.Forms.Form
  4.     {
  5.         bool Player1 = true;
  6.         int ScoreX = 0;
  7.         int ScoreO = 0;
  8.  
  9.         public Form()
  10.         {
  11.             InitializeComponent();
  12.             b1.Click += ClickCheck;
  13.             b2.Click += ClickCheck;
  14.             b3.Click += ClickCheck;
  15.             b4.Click += ClickCheck;
  16.             b5.Click += ClickCheck;
  17.             b6.Click += ClickCheck;
  18.             b7.Click += ClickCheck;
  19.             b8.Click += ClickCheck;
  20.             b9.Click += ClickCheck;
  21.         }
  22.  
  23.         private void bexit_Click(object sender, EventArgs e)
  24.         {
  25.             Application.Exit();
  26.         }
  27.  
  28.         private void babout_Click(object sender, EventArgs e)
  29.         {
  30.             MessageBox.Show("By Seydie");
  31.         }
  32.  
  33.         private void ClickCheck(object sender, EventArgs e)
  34.         {
  35.             if (sender is Button)
  36.             {
  37.                 Button b = sender as Button;
  38.  
  39.                 if (Player1 == true && b.Text == "")
  40.                 {
  41.                     b.Text = "X";
  42.                     Player1 = false;
  43.                 }
  44.  
  45.                 else if (b.Text == "X" || b.Text == "O")
  46.                 {
  47.                 }
  48.  
  49.                 else
  50.                 {
  51.                     b.Text = "O";
  52.                     Player1 = true;
  53.                 }
  54.  
  55.                 if ((b1.Text == "X" && b2.Text == "X" && b3.Text == "X") || (b1.Text == "X" && b4.Text == "X" && b7.Text == "X") || (b1.Text == "X" && b5.Text == "X" && b9.Text == "X") || (b2.Text == "X" && b5.Text == "X" && b8.Text == "X") || (b3.Text == "X" && b5.Text == "X" && b7.Text == "X") || (b3.Text == "X" && b6.Text == "X" && b9.Text == "X") || (b7.Text == "X" && b8.Text == "X" && b9.Text == "X") || (b4.Text == "X" && b5.Text == "X" && b6.Text == "X"))
  56.                 {
  57.                     MessageBox.Show("Player 1 won!");
  58.                     ScoreX++;
  59.                     EmptyBoard();
  60.                     Player1 = false;
  61.                 }
  62.  
  63.                 if ((b1.Text == "O" && b2.Text == "O" && b3.Text == "O") || (b1.Text == "O" && b4.Text == "O" && b7.Text == "O") || (b1.Text == "O" && b5.Text == "O" && b9.Text == "O") || (b2.Text == "O" && b5.Text == "O" && b8.Text == "O") || (b3.Text == "O" && b5.Text == "O" && b7.Text == "O") || (b3.Text == "O" && b6.Text == "O" && b9.Text == "O") || (b7.Text == "O" && b8.Text == "O" && b9.Text == "O") || (b4.Text == "O" && b5.Text == "O" && b6.Text == "O"))
  64.                 {
  65.                     MessageBox.Show("Player 2 won!");
  66.                     ScoreO++;
  67.                     EmptyBoard();
  68.                     Player1 = true;
  69.                 }
  70.                 if (b1.Text != "" && b2.Text != "" && b3.Text != "" && b4.Text != "" && b5.Text != "" && b6.Text != "" && b7.Text != "" && b8.Text != "" && b9.Text != "")
  71.                 {
  72.                     DialogResult dialogResult = MessageBox.Show("Do you want to play another game?", "No winner", MessageBoxButtons.YesNo);
  73.                     if (dialogResult == DialogResult.Yes)
  74.                     {
  75.                         EmptyBoard();
  76.                         Player1 = true;
  77.                     }
  78.  
  79.                     else if (dialogResult == DialogResult.No)
  80.                     {
  81.                         Application.Exit();
  82.                     }
  83.                 }
  84.             }
  85.  
  86.             else
  87.             {
  88.                 MessageBox.Show("Oops! Something went wrong!");
  89.                 Application.Exit();
  90.             }
  91.             score.Text = "Score: X = " + ScoreX + " | O = " + ScoreO;
  92.         }
  93.  
  94.         private void bretry_Click(object sender, EventArgs e)
  95.         {
  96.             EmptyBoard();
  97.             Player1 = true;
  98.         }
  99.  
  100.         public void EmptyBoard()
  101.         {
  102.             b1.Text = "";
  103.             b2.Text = "";
  104.             b3.Text = "";
  105.             b4.Text = "";
  106.             b5.Text = "";
  107.             b6.Text = "";
  108.             b7.Text = "";
  109.             b8.Text = "";
  110.             b9.Text = "";
  111.         }
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment