using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{int c=0; //宣告c
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int r;
c = c + 1;
r = c % 2; //宣告r為c/2的餘數 為了判斷要下X或O
if (r == 1)
{ button1.Text = "O"; } //餘數為1就下O
else
{ button1.Text = "X"; }//餘數為0就下X
button1.Enabled = false;
if ((button1.Text == button2.Text && button2.Text == button3.Text) ||
(button1.Text == button5.Text && button5.Text == button9.Text) ||
(button1.Text == button4.Text && button4.Text == button7.Text))
{ MessageBox.Show("win"); } //如果判斷有三個相同就表示連線 顯示win
}
private void Form1_Load(object sender, EventArgs e)
{
button1.Text = "";
button2.Text = "";
button3.Text = "";
button4.Text = "";
button5.Text = "";
button6.Text = "";
button7.Text = "";
button8.Text = "";
button9.Text = ""; //先把button變為空白
}
private void button2_Click(object sender, EventArgs e)
{
int r;
c = c + 1;
r = c % 2;
if (r == 1)
{ button2.Text = "O"; }
else
{ button2.Text = "X"; } //在button2下 O或X
button2.Enabled = false;
if ((button2.Text == button1.Text && button1.Text == button3.Text) ||
(button2.Text == button5.Text && button5.Text == button8.Text))
//如果跟button1&3或button5&8 相同
{ MessageBox.Show("win"); } //就顯示win
}
private void button5_Click(object sender, EventArgs e)
{
int r;
c = c + 1;
r = c % 2;
if (r == 1)
{ button5.Text = "O"; }
else
{ button5.Text = "X"; }
button5.Enabled = false;
if ((button5.Text == button4.Text && button4.Text == button6.Text) ||
(button5.Text == button2.Text && button2.Text == button8.Text) ||
(button5.Text == button1.Text && button1.Text == button9.Text) ||
(button5.Text == button3.Text && button3.Text == button7.Text))
{ MessageBox.Show("win"); } //判斷方法同button2寫法
}
private void button8_Click(object sender, EventArgs e)
{
}
private void button7_Click(object sender, EventArgs e)
{
int r;
c = c + 1;
r = c % 2;
if (r == 1)
{ button7.Text = "O"; }
else
{ button7.Text = "X"; }
button7.Enabled = false;
if ((button7.Text == button1.Text && button1.Text == button4.Text) ||
(button7.Text == button8.Text && button8.Text == button9.Text) ||
(button7.Text == button5.Text && button5.Text == button3.Text))
{ MessageBox.Show("win"); }
}
private void button6_Click(object sender, EventArgs e)
{
int r;
c = c + 1;
r = c % 2;
if (r == 1)
{ button6.Text = "O"; }
else
{ button6.Text = "X"; }
button6.Enabled = false;
if ((button3.Text == button2.Text && button2.Text == button1.Text) ||
(button3.Text == button6.Text && button6.Text == button9.Text) ||
(button3.Text == button5.Text && button5.Text == button7.Text))
{ MessageBox.Show("win"); }
}
private void button9_Click(object sender, EventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
int r;
c = c + 1;
r = c % 2;
if (r == 1)
{ button4.Text = "O"; }
else
{ button4.Text = "X"; }
button4.Enabled = false;
if ((button4.Text == button5.Text && button5.Text == button6.Text) ||
(button4.Text == button1.Text && button1.Text == button7.Text))
{ MessageBox.Show("win"); }
}
private void button3_Click(object sender, EventArgs e)
{
int r;
c = c + 1;
r = c % 2;
if (r == 1)
{ button3.Text = "O"; }
else
{ button3.Text = "X"; }
button3.Enabled = false;
if ((button3.Text == button2.Text && button2.Text == button1.Text) ||
(button3.Text == button6.Text && button6.Text == button9.Text) ||
(button3.Text == button5.Text && button5.Text == button7.Text))
{ MessageBox.Show("win"); }
}
}
}
方法2:用JUDGE (UNDONE)
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 WindowsFormsApplication3
{
public partial class Form1 : Form
{int c=0;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
judge();
int r;
c = c + 1;
r = c % 2;
if (r == 1)
{ button1.Text = "O"; }
else
{ button1.Text = "X"; } button1.Enabled = false;
}
private void Form1_Load(object sender, EventArgs e)
{
button1.Text = "";
button2.Text = "";
button3.Text = "";
button4.Text = "";
button5.Text = "";
button6.Text = "";
button7.Text = "";
button8.Text = "";
button9.Text = "";
}
private void button2_Click(object sender, EventArgs e)
{ int r;
c = c + 1;
r = c % 2;
if (r==1)
{ button2.Text = "O"; }
else
{ button2.Text = "X"; } button2.Enabled = false;
judge();
}
private void judge()
{
if (button1.Text == "X"&&button2.Text=="X"&&button3.Text=="X"){MessageBox.Show("Win!");}
}
private void button5_Click(object sender, EventArgs e)
{
judge();
int r;
c = c + 1;
r = c % 2;
if (r == 1)
{ button5.Text = "O"; }
else
{ button5.Text = "X"; } button5.Enabled = false;
}
private void button8_Click(object sender, EventArgs e)
{
judge();
int r;
c = c + 1;
r = c % 2;
if (r == 1)
{ button8.Text = "O"; }
else
{ button8.Text = "X"; } button8.Enabled = false;
}
private void button7_Click(object sender, EventArgs e)
{
judge();
int r;
c = c + 1;
r = c % 2;
if (r == 1)
{ button7.Text = "O"; }
else
{ button7.Text = "X"; } button7.Enabled = false;
}
private void button6_Click(object sender, EventArgs e)
{
judge();
int r;
c = c + 1;
r = c % 2;
if (r == 1)
{ button6.Text = "O"; }
else
{ button6.Text = "X"; } button6.Enabled = false;
}
private void button9_Click(object sender, EventArgs e)
{
judge();
int r;
c = c + 1;
r = c % 2;
if (r == 1)
{ button9.Text = "O"; }
else
{ button9.Text = "X"; } button9.Enabled = false;
}
private void button4_Click(object sender, EventArgs e)
{
judge();
int r;
c = c + 1;
r = c % 2;
if (r == 1)
{ button4.Text = "O"; }
else
{ button4.Text = "X"; } button4.Enabled = false;
}
private void button3_Click(object sender, EventArgs e)
{
judge();
int r;
c = c + 1;
r = c % 2;
if (r == 1)
{ button3.Text = "O"; }
else
{ button3.Text = "X"; } button3.Enabled = false;
}
}
}
沒有留言:
張貼留言