using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if (button1.Text != "O")
{
button1.Text = "O";
if (defined()) MessageBox.Show("O win");
}
else
{
button1.Text = "X";
if (defined()) MessageBox.Show("X win");
}
//----------------------------------------------------------------
}
private void button2_Click(object sender, EventArgs e)
{
if (button2.Text != "O")
{
button2.Text = "O";
if (defined()) MessageBox.Show("O win");
}
else
{
button2.Text = "X";
if (defined()) MessageBox.Show("X win");
}
}
private void button3_Click(object sender, EventArgs e)
{
if (button3.Text != "O")
{
button3.Text = "O";
if (defined()) MessageBox.Show("O win");
}
else
{
button3.Text = "X";
if (defined()) MessageBox.Show("X win");
}
}
private void button4_Click(object sender, EventArgs e)
{
if (button4.Text != "O")
{
button4.Text = "O";
if (defined()) MessageBox.Show("O win");
}
else
{
button4.Text = "X";
if (defined()) MessageBox.Show("X win");
}
}
private void button5_Click(object sender, EventArgs e)
{
if (button5.Text != "O")
{
button5.Text = "O";
if (defined()) MessageBox.Show("O win");
}
else
{
button5.Text = "X";
if (defined()) MessageBox.Show("X win");
}
}
private void button6_Click(object sender, EventArgs e)
{
if (button6.Text != "O")
{
button6.Text = "O";
if (defined()) MessageBox.Show("O win");
}
else
{
button6.Text = "X";
if (defined()) MessageBox.Show("X win");
}
}
private void button7_Click(object sender, EventArgs e)
{
if (button7.Text != "O")
{
button7.Text = "O";
if (defined()) MessageBox.Show("O win");
}
else
{
button7.Text = "X";
if (defined()) MessageBox.Show("X win");
}
}
private void button8_Click(object sender, EventArgs e)
{
if (button8.Text != "O")
{
button8.Text = "O";
if (defined()) MessageBox.Show("O win");
}
else
{
button8.Text = "X";
if (defined()) MessageBox.Show("X win");
}
}
private void button9_Click(object sender, EventArgs e)
{
if (button9.Text != "O")
{
button9.Text = "O";
if (defined()) MessageBox.Show("O win");
}
else
{
button9.Text = "X";
if (defined()) MessageBox.Show("X win");
}
}
public bool defined()
{
bool A1 = (button1.Text == button2.Text && button2.Text == button3.Text && button3.Text == button1.Text);
bool A2 = (button4.Text == button5.Text && button4.Text == button6.Text && button5.Text == button6.Text);
bool A3 = (button7.Text == button8.Text && button7.Text == button9.Text && button7.Text == button8.Text);
bool B1 = (button1.Text == button4.Text && button4.Text == button7.Text && button1.Text == button7.Text);
bool B2 = (button2.Text == button5.Text && button5.Text == button8.Text && button7.Text == button8.Text);
bool B3 = (button3.Text == button6.Text && button6.Text == button9.Text && button3.Text == button9.Text);
bool C1 = (button1.Text == button5.Text && button5.Text == button9.Text && button1.Text == button9.Text);
bool C2 = (button3.Text == button5.Text && button5.Text == button7.Text && button7.Text == button3.Text);
return (A1 || A2 || A3 || B1 || B2 || B3 || C1 || C2);
}
}
}

沒有留言:
張貼留言