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.Windows.Forms;
- namespace TicTacToe
- {
- public partial class Form1 : Form
- {
- bool turn = true;
- int turn_count = 0;
- public Form1()
- {
- InitializeComponent();
- }
- private void beendenToolStripMenuItem_Click(object sender, EventArgs e)
- {
- Application.Exit();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- }
- private void button_Click(object sender, EventArgs e)
- {
- Button b = (Button)sender;
- b.Enabled = false;
- b.Text = (turn) ? "X" : "O";
- turn = !turn;
- turn_count++;
- Check_for_winner();
- }
- private void Check_for_winner()
- {
- bool win = false;
- if ((A1.Text == A2.Text) && (A1.Text == A3.Text) && !A1.Enabled)
- win = true;
- else if ((B1.Text == B2.Text) && (B1.Text == B3.Text) && !B1.Enabled)
- win = true;
- else if ((C1.Text == C2.Text) && (C1.Text == C3.Text) && !C1.Enabled)
- win = true;
- else if ((A1.Text == B1.Text) && (A1.Text == C1.Text) && !A1.Enabled)
- win = true;
- else if ((A2.Text == B2.Text) && (A2.Text == C2.Text) && !A2.Enabled)
- win = true;
- else if ((A3.Text == B3.Text) && (A3.Text == C3.Text) && !A3.Enabled)
- win = true;
- else if ((A1.Text == B2.Text) && (A1.Text == C3.Text) && !A1.Enabled)
- win = true;
- else if ((A3.Text == B2.Text) && (A3.Text == C1.Text) && !C1.Enabled)
- win = true;
- if (win)
- {
- DisableButtons();
- string winner = (turn) ? "O" : "X";
- MessageBox.Show(winner + " Wins!","We have a Winner");
- }
- else
- {
- if (turn_count == 9)
- {
- MessageBox.Show("Draw!");
- }
- }
- }
- private void neuesSpielToolStripMenuItem_Click(object sender, EventArgs e)
- {
- foreach (Control c in Controls)
- {
- try
- {
- Button b = (Button)c;
- b.Text = "";
- b.Enabled = true;
- }
- catch { }
- }
- turn = true;
- turn_count = 0;
- }
- private void DisableButtons()
- {
- foreach (Control c in Controls)
- {
- try
- {
- Button b = (Button)c;
- b.Enabled = false;
- }
- catch { }
- }
- }
- private void button_MouseEnter(object sender, EventArgs e)
- {
- Button b = (Button)sender;
- if (b.Enabled)
- b.Text = (turn) ? "X" : "O";
- }
- private void button_MouseLeave(object sender, EventArgs e)
- {
- Button b = (Button)sender;
- if (b.Enabled)
- b.Text = "";
- }
- private void buttonComputer_Click(object sender, EventArgs e)
- {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement