井字遊戲是一簡單又好玩的小東西,剛好上星期新同事在看有關Graphics的部份,
便出這個題目給他練習一下繪圖並完成小作品,北七也抽空寫了一個小範例。
程式說明
region 陣列用來紀錄以下9個位子是否有被填入O或X,
region陣列內若為1,則會繪上O,為2則會繪上X。
透過覆寫OnPaint 事件,會依照region以及bingo內的結果作繪圖。
至於Form1_MouseMove中取得滑鼠位置,這點需要注意一下,
北七一開始用如下方式取得滑鼠座標
int x= MousePosition.X;
int y= MousePosition.Y;
取出來的位置與Form上所用的座標不同,以下面這一段CODE舉例
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
mouse_location_x = e.Location.X;
mouse_location_y = e.Location.Y;
int x= MousePosition.X;
int y= MousePosition.Y;
Console.WriteLine(mouse_location_x+"\t"+mouse_location_y+"\n"+x+"\t"+y);
}
輸出結果如下,可以看到差距是頗大的,這一點需要注意一下。
256 340
514 620
附上完整程式碼
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 井字遊戲
{
public partial class Form1 : Form
{
//紀錄九宮格狀態
int[] region = new int[9];
public Form1()
{
InitializeComponent();
this.Width = 561;
this.Height = 561;
}
//計算每一個欄的寬度
float column;
//計算每一個列的高度
float row ;
//O先
bool o_x = true;
//滑鼠X
int mouse_location_x;
//滑鼠Y
int mouse_location_y;
//是否有勝利連線
int[] bingo = new int[8];
//已結束
bool finish = false;
//繪圖
protected override void OnPaint(PaintEventArgs pe)
{
//取得Graphics
Graphics g = pe.Graphics;
//取得Pen
Pen p=new Pen(Color.Black);
//取得畫面大小
float width = this.Width-20;
float height = this.Height-20;
//計算出要畫的範圍
column = width / 3;
row = height / 3;
//畫橫線
g.DrawLine(p, 0, row, width, row);
g.DrawLine(p, 0, row*2, width, row*2);
//畫直線
g.DrawLine(p, column, 0, column, height);
g.DrawLine(p, column * 2, 0, column*2, height);
//判斷是否需要畫OX
for (int i = 0; i < region.Length; i++)
{
if (region[i] == 1)
{
g.DrawEllipse(p, (i % 3) * (column) + 30, (i / 3) * (row) + 30, column - 60, row - 60);
}
if (region[i] == 2)
{
g.DrawLine(p, (i % 3) * column + 20, (i / 3) * row + 20, ((i % 3) + 1) * column - 20, (i / 3 + 1) * row - 20);
g.DrawLine(p, ((i % 3) + 1) * column - 20, (i / 3) * row + 20, (i % 3) * column + 20, ((i / 3) + 1) * row - 20);
}
}
//畫出勝利直線
for (int i = 0; i < bingo.Length; i++)
{
//0,1,2
if (bingo[0]==1)
{
g.DrawLine(p, 0,row/2,width,row/2);
finish = true;
}
//3,4,5
if (bingo[1] == 1)
{
g.DrawLine(p, 0, row*2 -row/2, width, row*2-row/2);
finish = true;
}
//6,7,8
if (bingo[2] == 1)
{
g.DrawLine(p, 0, row * 3 - row / 2, width, row * 3 - row / 2);
finish = true;
}
//0,3,6
if (bingo[3] == 1)
{
g.DrawLine(p, column-(column/2), 0, column-(column/2),height);
finish = true;
}
//1,4,7
if (bingo[4] == 1)
{
g.DrawLine(p, column*2 - (column / 2), 0, column *2- (column / 2), height);
finish = true;
}
//2,5,8
if (bingo[5] == 1)
{
g.DrawLine(p, column * 3 - (column / 2), 0, column * 3 - (column / 2), height);
finish = true;
}
//0,4,8
if (bingo[6] == 1)
{
g.DrawLine(p, 0 , 0, width , height);
finish = true;
}
//2,4,6
if (bingo[7] == 1)
{
g.DrawLine(p,width, 0,0,height);
finish = true;
}
}
}
//滑鼠點下去後要做的事
private void Form1_Click(object sender, EventArgs e)
{
//取得滑鼠座標
int x = mouse_location_x;
int y = mouse_location_y;
if (finish == false)
{
//判斷點下的區域是那一個
//左邊第一排0,3,6
if (x < column)
{
//0
if (y > 0 && y < row)
{
if (region[0] == 0)
{
region[0] = o_x == true ? 1 : 2;
o_x = !o_x;
}
}
//3
if (y > row && y < row * 2)
{
if (region[3] == 0)
{
region[3] = o_x == true ? 1 : 2;
o_x = !o_x;
}
}
//6
if (y > row * 2 && y < row * 3)
{
if (region[6] == 0)
{
region[6] = o_x == true ? 1 : 2;
o_x = !o_x;
}
}
}
//中間那一排
if (x > column && x < column * 2)
{
//1
if (y > 0 && y < row)
{
if (region[1] == 0)
{
region[1] = o_x == true ? 1 : 2;
o_x = !o_x;
}
}
//4
if (y > row && y < row * 2)
{
if (region[4] == 0)
{
region[4] = o_x == true ? 1 : 2;
o_x = !o_x;
}
}
//7
if (y > row * 2 && y < row * 3)
{
if (region[7] == 0)
{
region[7] = o_x == true ? 1 : 2;
o_x = !o_x;
}
}
}
//右邊那一排
if (x > column * 2 && x < column * 3)
{
//2
if (y > 0 && y < row)
{
if (region[2] == 0)
{
region[2] = o_x == true ? 1 : 2;
o_x = !o_x;
}
}
//5
if (y > row && y < row * 2)
{
if (region[5] == 0)
{
region[5] = o_x == true ? 1 : 2;
o_x = !o_x;
}
}
//8
if (y > row * 2 && y < row * 3)
{
if (region[8] == 0)
{
region[8] = o_x == true ? 1 : 2;
o_x = !o_x;
}
}
}
}
//檢查是否有連線(8種組合)
//第一列
if (region[0] == region[1] && region[1]== region[2] && region[0]!=0)
{
bingo[0] = 1;
}
//第二列
else if (region[3] == region[4] && region[4] == region[5] && region[3] != 0)
{
bingo[1] = 1;
}
//第三列
else if (region[6] == region[7] && region[7] == region[8] && region[6] != 0)
{
bingo[2] = 1;
}
//第一行
else if (region[0] == region[3] && region[3] == region[6] && region[0] != 0)
{
bingo[3] = 1;
}
//第二行
else if (region[1] == region[4] && region[4] == region[7] && region[1] != 0)
{
bingo[4] = 1;
}
//第三行
else if (region[2] == region[5] && region[5] == region[8] && region[2] != 0)
{
bingo[5] = 1;
}
// \連線
else if (region[0] == region[4] && region[4] == region[8] && region[0] != 0)
{
bingo[6] = 1;
}
// /連線
else if (region[2] == region[4] && region[4] == region[6] && region[2] != 0)
{
bingo[7] = 1;
}
this.Refresh();
//檢查和局
int check=0;
for (int i = 0; i < region.Length; i++)
{
if (region[i] > 0)
check++;
}
if (check == 9)
{
for (int i = 0; i < bingo.Length; i++)
{
if (bingo[i] > 0)
{
check--;
}
}
}
if (check == 9)
{
DialogResult dialogResult = MessageBox.Show("平手,要不要再來一局?", "分出勝負", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
for (int i = 0; i < region.Length; i++)
{
region[i] = 0;
}
for (int i = 0; i < bingo.Length; i++)
{
bingo[i] = 0;
}
o_x = true;
finish = false;
this.Refresh();
}
else if (dialogResult == DialogResult.No)
{
this.Close();
}
}
//已分出勝負
if (finish)
{
DialogResult dialogResult = MessageBox.Show(o_x ? "X獲勝,要不要再來一局?" : "O獲勝,要不要再來一局?", "分出勝負", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
for (int i = 0; i < region.Length; i++)
{
region[i] = 0;
}
for (int i = 0; i < bingo.Length; i++)
{
bingo[i] = 0;
}
o_x = true;
finish = false;
this.Refresh();
}
else if (dialogResult == DialogResult.No)
{
this.Close();
}
}
}
//取得滑鼠位置
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
mouse_location_x = e.Location.X;
mouse_location_y = e.Location.Y;
}
}
}