Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace LV7_analiza
- {
- public partial class Form1 : Form
- {
- bool turn = true; //true - X; False - O
- int turnCount = 0;
- public Form1()
- {
- InitializeComponent();
- }
- private void exitToolStripMenuItem_Click(object sender, EventArgs e)
- {
- Application.Exit();
- }
- private void Button_click(object sender, EventArgs e)
- {
- Button b = (Button)sender;
- if (turn)
- b.Text = "X";
- else
- b.Text = "O";
- turn = !turn;
- b.Enabled = false;
- checkForWin();
- }
- private void checkForWin() {
- bool thereIs = false;
- if ((A1.Text == A2.Text) && (A2.Text == A3.Text) && (!A1.Enabled)) thereIs = true;
- else if ((B1.Text == B2.Text) && (B2.Text == B3.Text) && (!B1.Enabled)) thereIs = true;
- else if ((C1.Text == C2.Text) && (C2.Text == C3.Text) && (!C1.Enabled)) thereIs = true;
- else if ((A1.Text == B1.Text) && (B1.Text == C1.Text) && (!A1.Enabled)) thereIs = true;
- else if ((A2.Text == B2.Text) && (B2.Text == C2.Text) && (!A2.Enabled)) thereIs = true;
- else if ((A3.Text == B3.Text) && (B3.Text == C3.Text) && (!A3.Enabled)) thereIs = true;
- else if ((A1.Text == B2.Text) && (B2.Text == C3.Text) && (!A1.Enabled)) thereIs = true;
- else if ((A3.Text == B2.Text) && (B2.Text == C1.Text) && (!A3.Enabled)) thereIs = true;
- if (thereIs) {
- string winner = "";
- if (turn) winner = "O";
- else winner = "X";
- MessageBox.Show(winner + " has won!");
- disableButtons();
- }
- }
- private void disableButtons()
- {
- try
- {
- foreach (Control c in Controls)
- {
- Button b = (Button)c;
- b.Enabled = false;
- }
- }
- catch { }
- }
- private void NewGame_click(object sender, EventArgs e)
- {
- bool turn = true; //true - X; False - O
- int turnCount = 0;
- try
- {
- foreach (Control c in Controls)
- {
- Button b = (Button)c;
- b.Enabled = true;
- b.Text = "";
- }
- }
- catch { }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement