C# Tic-Tac-Toe
資管二甲 U1035128 郭璟塘
C# 程式作業 井字遊戲
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
int tmp = 0; //設一個全域變數
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
a1.Text = ""; //把按鈕設成空白
a2.Text = "";
a3.Text = "";
b1.Text = "";
b2.Text = "";
b3.Text = "";
c1.Text = "";
c2.Text = "";
c3.Text = "";
button10.Text = "再玩一次";
}
private void button1_Click_1(object sender, EventArgs e)
{
int r;
tmp = tmp + 1; //每按一下 c就+1
r = tmp % 2; //把c除2取餘數 就會剩下0或1 諾是0按鈕就顯示O 諾是1按鈕就顯示 X
if (r == 0)
{
a1.Text = "O";
}
else
{
a1.Text = "X";
}
a1.Enabled = false; //下好離手 點下後就不能再更改
judge(); //呼叫函式
}
private void button2_Click_1(object sender, EventArgs e)
{
int r;
tmp = tmp + 1;
r = tmp % 2;
if (r == 0)
{
a2.Text = "O";
}
else
{
a2.Text = "X";
}
a2.Enabled = false;
judge();
}
private void button3_Click_1(object sender, EventArgs e)
{
int r;
tmp = tmp + 1;
r = tmp % 2;
if (r == 0)
{
a3.Text = "O";
}
else
{
a3.Text = "X";
}
a3.Enabled = false;
judge();
}
private void button4_Click_1(object sender, EventArgs e)
{
int r;
tmp = tmp + 1;
r = tmp % 2;
if (r == 0)
{
b1.Text = "O";
}
else
{
b1.Text = "X";
}
b1.Enabled = false;
judge();
}
private void button5_Click_1(object sender, EventArgs e)
{
int r;
tmp = tmp + 1;
r = tmp % 2;
if (r == 0)
{
b2.Text = "O";
}
else
{
b2.Text = "X";
}
b2.Enabled = false;
judge();
}
private void button6_Click_1(object sender, EventArgs e)
{
int r;
tmp = tmp + 1;
r = tmp % 2;
if (r == 0)
{
b3.Text = "O";
}
else
{
b3.Text = "X";
}
b3.Enabled = false;
judge();
}
private void button7_Click_1(object sender, EventArgs e)
{
int r;
tmp = tmp + 1;
r = tmp % 2;
if (r == 0)
{
c1.Text = "O";
}
else
{
c1.Text = "X";
}
c1.Enabled = false;
judge();
}
private void button8_Click_1(object sender, EventArgs e)
{
int r;
tmp = tmp + 1;
r = tmp % 2;
if (r == 0)
{
c2.Text = "O";
}
else
{
c2.Text = "X";
}
c2.Enabled = false;
judge();
}
private void button9_Click_1(object sender, EventArgs e)
{
int r;
tmp = tmp + 1;
r = tmp % 2;
if (r == 0)
{
c3.Text = "O";
}
else
{
c3.Text = "X";
}
c3.Enabled = false;
judge();
}
private void button10_Click_1(object sender, EventArgs e) //重新開始
{
a1.Text = "";
a2.Text = "";
a3.Text = "";
b1.Text = "";
b2.Text = "";
b3.Text = "";
c1.Text = "";
c2.Text = "";
c3.Text = "";
a1.Enabled = true;
a2.Enabled = true;
a3.Enabled = true;
b1.Enabled = true;
b2.Enabled = true;
b3.Enabled = true;
c1.Enabled = true;
c2.Enabled = true;
c3.Enabled = true;
tmp = 0;
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void Form1_Load_1(object sender, EventArgs e)
{
}
private void judge() //設立一個函式 判斷輸贏
{
if (a1.Text == a2.Text && a2.Text == a3.Text && a1.Text == "X")
{
MessageBox.Show("玩家一 X 贏了");
}
if (a1.Text == a2.Text && a2.Text == a3.Text && a1.Text == "O")
{
MessageBox.Show("玩家二 O 贏了");
}
if (b1.Text == b2.Text && b2.Text == b3.Text && b1.Text == "X")
{
MessageBox.Show("玩家一 X 贏了");
}
if (b1.Text == b2.Text && b2.Text == b3.Text && b1.Text == "O")
{
MessageBox.Show("玩家二 O 贏了");
}
if (c1.Text == c2.Text && c2.Text == c3.Text && c1.Text == "X")
{
MessageBox.Show("玩家一 X 贏了");
}
if (c1.Text == c2.Text && c2.Text == c3.Text && c1.Text == "O")
{
MessageBox.Show("玩家二 O 贏了");
}
if (a1.Text == b1.Text && b1.Text == c1.Text && a1.Text == "X")
{
MessageBox.Show("玩家一 X 贏了");
}
if (a1.Text == b1.Text && b1.Text == c1.Text && a1.Text == "O")
{
MessageBox.Show("玩家二 O 贏了");
}
if (a2.Text == b2.Text && b2.Text == c2.Text && a2.Text == "X")
{
MessageBox.Show("玩家一 X 贏了");
}
if (a2.Text == b2.Text && b2.Text == c2.Text && a2.Text == "O")
{
MessageBox.Show("玩家二 O 贏了");
}
if (a3.Text == b3.Text && b3.Text == c3.Text && a3.Text == "X")
{
MessageBox.Show("玩家一 X 贏了");
}
if (a1.Text == b2.Text && b2.Text == c3.Text && a1.Text == "X")
{
MessageBox.Show("玩家一 X 贏了");
}
if (a1.Text == b2.Text && b2.Text == c3.Text && a1.Text == "O")
{
MessageBox.Show("玩家二 O 贏了");
}
if (a3.Text == b2.Text && b2.Text == c1.Text && a3.Text == "X")
{
MessageBox.Show("玩家一 X 贏了");
}
if (a3.Text == b2.Text && b2.Text == c1.Text && a3.Text == "O")
{
MessageBox.Show("玩家二 O 贏了");
}
}
}
}