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 mare_proiect_1
- {
- public partial class Form1 : Form
- {
- bool turn = true;
- int turn_count = 0;
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- }
- private void button_click(object sender, EventArgs e)
- {
- Button b = (Button)sender;
- if (turn) b.Text = "X";
- else b.Text = "0";
- turn = !turn;
- b.Enabled = false;
- turn_count++;
- checkForWinner();
- }
- private void checkForWinner()
- {
- bool win = false;
- if (button1.Text == button2.Text && button2.Text == button3.Text && !button1.Enabled) win = true;
- else if (button4.Text == button5.Text && button5.Text == button6.Text && !button4.Enabled) win = true;
- else if (button7.Text == button8.Text && button8.Text == button9.Text && !button7.Enabled) win = true;
- if (button1.Text == button4.Text && button4.Text == button7.Text && !button1.Enabled) win = true;
- else if (button2.Text == button5.Text && button5.Text == button8.Text && !button2.Enabled) win = true;
- else if (button3.Text == button6.Text && button6.Text == button9.Text && !button3.Enabled) win = true;
- else if (button1.Text == button5.Text && button5.Text == button9.Text && !button1.Enabled) win = true;
- else if (button3.Text == button5.Text && button5.Text == button7.Text && !button7.Enabled) win = true;
- if (win)
- {
- disableButtons();
- String winner = "";
- if (turn) winner = "0";
- else winner = "X";
- MessageBox.Show(winner + " a castigat!");
- }
- else
- {
- if (turn_count == 9) MessageBox.Show("Draw");
- }
- }
- private void disableButtons()
- {
- try
- {
- foreach(Control c in Controls)
- {
- Button b = (Button)c;
- b.Enabled = false;
- }
- }
- catch {}
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement