Advertisement
vvvaaalll

OOP_lv7_analiza

Jan 20th, 2020
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.79 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 LV7_analiza
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.  
  16.         bool turn = true; //true - X; False - O
  17.         int turnCount = 0;
  18.  
  19.         public Form1()
  20.         {
  21.             InitializeComponent();
  22.         }
  23.  
  24.         private void exitToolStripMenuItem_Click(object sender, EventArgs e)
  25.         {
  26.             Application.Exit();
  27.         }
  28.  
  29.         private void Button_click(object sender, EventArgs e)
  30.         {
  31.             Button b = (Button)sender;
  32.             if (turn)
  33.                 b.Text = "X";
  34.             else
  35.                 b.Text = "O";
  36.             turn = !turn;
  37.  
  38.             b.Enabled = false;
  39.  
  40.             checkForWin();
  41.         }
  42.  
  43.        private void checkForWin() {
  44.  
  45.             bool thereIs = false;
  46.  
  47.             if ((A1.Text == A2.Text) && (A2.Text == A3.Text) && (!A1.Enabled))  thereIs = true;
  48.  
  49.             else if ((B1.Text == B2.Text) && (B2.Text == B3.Text) && (!B1.Enabled))  thereIs = true;
  50.  
  51.             else if ((C1.Text == C2.Text) && (C2.Text == C3.Text) && (!C1.Enabled))  thereIs = true;
  52.  
  53.  
  54.  
  55.  
  56.             else if ((A1.Text == B1.Text) && (B1.Text == C1.Text) && (!A1.Enabled))  thereIs = true;
  57.             else if ((A2.Text == B2.Text) && (B2.Text == C2.Text) && (!A2.Enabled))  thereIs = true;
  58.             else if ((A3.Text == B3.Text) && (B3.Text == C3.Text) && (!A3.Enabled))  thereIs = true;
  59.  
  60.  
  61.             else if ((A1.Text == B2.Text) && (B2.Text == C3.Text) && (!A1.Enabled))  thereIs = true;
  62.             else if ((A3.Text == B2.Text) && (B2.Text == C1.Text) && (!A3.Enabled))  thereIs = true;
  63.  
  64.  
  65.             if (thereIs) {
  66.                 string winner = "";
  67.                 if (turn) winner = "O";
  68.                 else winner = "X";
  69.  
  70.                 MessageBox.Show(winner + " has won!");
  71.                 disableButtons();
  72.             }
  73.         }
  74.  
  75.         private void disableButtons()
  76.         {
  77.             try
  78.             {
  79.                 foreach (Control c in Controls)
  80.                 {
  81.                     Button b = (Button)c;
  82.                     b.Enabled = false;
  83.  
  84.                 }
  85.  
  86.             }
  87.  
  88.             catch { }
  89.         }
  90.  
  91.  
  92.         private void NewGame_click(object sender, EventArgs e)
  93.         {
  94.             bool turn = true; //true - X; False - O
  95.             int turnCount = 0;
  96.  
  97.  
  98.             try
  99.             {
  100.                 foreach (Control c in Controls)
  101.                 {
  102.                     Button b = (Button)c;
  103.                     b.Enabled = true;
  104.                     b.Text = "";
  105.                 }
  106.  
  107.             }
  108.  
  109.             catch { }
  110.         }
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement