using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace 连连看
{
public partial class Form1 : Form
{
private Bitmap Source;
private int W = 50;
private int GameSize=10;
private bool Select_first = false;
private int x1, y1;
private int x2, y2;
Point z1, z2;
private int m_nCol = 10;
private int m_nRow = 10;
private int[] m_map = new int[10*10];
public enum LinkType {LineType,OneCornerType,TwoCornerType};
LinkType LType;
// public int LType;
private int level = 1;
private int length = 20;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Source = (Bitmap)Image.FromFile("D:\\MyProject\\C#\\WindowsFormsApplication4\\WindowsFormsApplication4\\animal.bmp");
this.pictureBox1.Height = W * (m_nRow+2);
this.pictureBox1.Width = W * (m_nCol+2);
this.pictureBox1.Top = 23;
this.pictureBox1.Left = 0;
int d = (this.Height - this.ClientRectangle.Height-this.menuStrip1.Height);
this.Height = this.pictureBox1.Height + this.pictureBox1.Top+d;
this.Width = this.pictureBox1.Width + this.pictureBox1.Left;
StartNewGame();
Init_Graphic();
}
private void StartNewGame()
{
length = 500;
for(int i=0;i<(m_nCol*m_nRow);i++)
{
m_map[i]=-1;
}
Random r = new Random();
ArrayList tmp=new ArrayList ();
for(int i=0;i<(m_nCol*m_nRow)/4;i++)
for(int j=0;j<4;j++)
tmp.Add(i);
for (int i = 0; i < m_nRow * m_nCol; i++)
{
int n = r.Next() % tmp.Count;
m_map[i] = (int)tmp[n];
tmp.RemoveAt(n);
}
}
private void RebuildGame()
{
Random
r = new Random();
ArrayList tmp = new ArrayList();
int count = 0;
for (int i = 0; i < (m_nCol * m_nRow); i++)
{
if (m_map[i] != -1)
count++;
}
if (count % 4 == 0)
{
for (int i = 0; i < count / 4; i++)
for (int j = 0; j < 4; j++)
tmp.Add(i);
for (int i = 0; i < m_nRow * m_nCol; i++)
{
if (tmp.Count != 0)
{
int n = r.Next() % tmp.Count;
if (m_map[i] != -1)
{
m_map[i] = (int)tmp[n];
tmp.RemoveAt(n);
}
}
}
}
else
{
for (int i = 0; i < 2; i++)
{
if (m_map[i] != -1) m_map[i] = 25;
}
int temp=0;
for (int i = 0; i < (count - 2) / 4; i++)
{ temp++;
for (int j = 0; j < 4; j++)
tmp.Add(i);
}
for (int m = 0; m < m_nRow * m_nCol; m++)
{
if (tmp.Count == 0)
break;
int n = r.Next() % tmp.Count;
if (m_map[m] != -1)
{
m_map[m] = (int)tmp[n];
tmp.RemoveAt(n);
}
}
}
}
private void Init_Graphic()
{
Graphics g = get_Graphic();
g.DrawString("第" + level + "关", new Font("宋体", 10, FontStyle.Regular), Brushes.White, (float)10, (float)20);
// g.DrawRectangle(new Pen(Color.Green, 2), 20, 20, length, 20);
for (int i = 0; i< 10 * 10; i++)
{
g.DrawImage(create_image(m_map[i]), W * (i % GameSize)+W, W * (i / GameSize)+W, W, W);
}
}
private Graphics get_Graphic()
{
//if (pictureBox1.Image == null)
pictureBox1.Image = new Bitmap(pictureBox1.Width, pictureBox1.Height);
Graphics g = Graphics.FromImage(pictureBox1.Image);
return g;
}
public Graphics GetGraphicsObject(ref PictureBox pic)
{
System.Drawing.Graphics g;
Bitmap bmp = new Bitmap(pic.Width, pic.Height);
pic.Image = bmp;
g = Graphics.FromImage(bmp);
return g;
}
private Bitmap create_image(int n)
{
Bitmap bit = new Bitmap(W, W);
Graphics g = Graphics.FromImage(bit);
Rectangle a = new Rectangle(0, 0, W, W);
Rectangle b = new Rectangle(0, n * 39, 39, 39);
g.DrawImage(Source, a, b, GraphicsUnit.Pixel);
return bit;
}
bool IsWin()
{
if (length == 0)
return false;
for(int i=0;iy2)
{
int n=y1;
y1=y2;
y2=n;
}
for(int i=y1+1;i<=y2;i++)
{
if(i==y2)
return true;
if(m_map[i*m_nCol+x]!=-1)
break;
}
return false;
}
bool Y_Link(int x1,int x2,int y)
{
if(x1>x2)
{
int x=x1;
x1=x2;
x2=x;
}
for(int i=x1+1;i<=x2;i++)
{
if(i==x2)
return true;
if(m_map[y*m_nCol+i]!=-1)
break;
}
return false;
}
bool OneCornerLink(int x1, int y1,int x2, int y2)
{
if (x1 > x2)
{
int n=x1;
x1=x2;
x2=n;
n=y1;
y1=y2;
y2=n;
}
if (y2 < y1)
{
if (m_map[y1 * m_nCol + x2] == -1)
{
if (Y_Link(x1, x2, y1) && X_Link(x2, y1, y2))
{
z1.X = x2; z1.Y = y1;
return true;
}
}
if (m_map[y2 * m_nCol + x1] ==-1)
{
if (Y_Link(x2 , x1, y2) && X_Link(x1, y2, y1))
{
z1.X = x1; z1.Y = y2;
return true;
}
}
return false;
}
else
{
if (m_map[y2 * m_nCol + x1] ==-1)
{
if (Y_Link(x1, x2, y2) && X_Link(x1, y1, y2 ))
{
z1.X = x1; z1.Y = y2;
return true;
}
}
if (m_map[y1 * m_nCol + x2] == -1)
{
if (Y_Link(x1 , x2, y1) && X_Link(x2, y1, y2))
{
z1.X = x2; z1.Y = y1;
return true;
}
}
return false;
}
}
bool TwoCornerLink(int x1, int y1, int x2, int y2)
{
if (x1 > x2)
{
int n = x1;
x1 = x2;
x2 = n;
n = y1;
y1 = y2;
y2 = n;
}
int x, y;
for (x = x1 + 1; x <= m_nCol; x++)
{
if (x == m_nCol)
if (Xtoboard(x2 + 1, y2, true))
{
z2.X = m_nCol; z2.Y = y1;
z1.X = m_nCol; z1.Y = y2;
return true;
}
else
break ;
if (m_map[y1 * m_nCol + x] != -1)
break;
if (OneCornerLink(x, y1, x2, y2))
{
z2.X = x; z2.Y = y1;
return true;
}
}
for (x = x1 - 1; x >=-1; x--)
{
if (x == -1)
if (Xtoboard(x2 - 1, y2, false))
{
z2.X = -1; z2.Y = y1;
z1.X = -1; z1.Y = y2;
return true;
}
else
break;
if (m_map[y1 * m_nCol + x] !=-1)
break;
if (OneCornerLink(x, y1, x2, y2))
{
z2.X = x; z2.Y = y1;
return true;
}
}
for (y = y1 - 1; y >=-1; y--)
{
if (y == -1)
if (Ytoboard(x2, y2 - 1, false))
{
z2.X = x1; z2.Y = -1;
z1.X = x2; z1.Y = -1;
return true;
}
else
break;
if (m_map[y * m_nCol + x1] !=-1)
break;
if (OneCornerLink(x1, y, x2, y2))
{
z2.X = x1; z2.Y = y;
return true;
}
}
for (y = y1 + 1; y <=m_nRow; y++)
{
if (y == m_nRow)
if (Ytoboard(x2, y2 + 1, true))
{
z2.X = x1; z2.Y = m_nRow;
z1.X = x2; z1.Y = m_nRow;
return true;
}
else
break;
if (m_map[y * m_nCol + x1] !=-1)
break;
if (OneCornerLink(x1, y, x2, y2))
{
z2.X = x1; z2.Y = y;
return true;
}
}
return false;
}
bool Xtoboard(int x, int y, bool bAdd)
{
if (bAdd)
{
for (int i = x; i < m_nCol; i++)
if (m_map[y * m_nCol + i] !=-1)
return false;
}
else
{
for (int i = 0; i <= x; i++)
if (m_map[y * m_nCol + i] !=-1)
return false;
}
return true;
}
bool Ytoboard(int x, int y, bool bAdd)
{
if(bAdd)
{
for(int i=y;i DateTime.Now)
{
}
}
private void DrawLinkLine(int x1, int y1, int x2, int y2,LinkType LType)
{
Graphics g = this.pictureBox1.CreateGraphics();
Pen p=new Pen(Color.Red,3);
Point p1 = new Point(x1 * W + W / 2+W, y1 * W + W / 2+W);
Point p2 = new Point(x2 * W + W / 2+W, y2 * W + W / 2+W);
if (LType == LinkType.LineType)
g.DrawLine(p, p1, p2);
if (LType == LinkType.OneCornerType)
{
Point pixel_z1 = new Point(z1.X * W + W / 2+W, z1.Y * W + W / 2+W);
g.DrawLine(p, p1, pixel_z1);
g.DrawLine(p, pixel_z1, p2);
}
if (LType == LinkType.TwoCornerType)
{
Point pixel_z1 = new Point(z1.X * W + W / 2+W, z1.Y * W + W / 2+W);
Point pixel_z2 = new Point(z2.X * W + W / 2+W, z2.Y * W + W / 2+W);
if (!(p1.X == pixel_z2.X || p1.Y == pixel_z2.Y))
{
Point c;
c = pixel_z1;
pixel_z1 = pixel_z2;
pixel_z2 = c;
}
g.DrawLine(p, p1, pixel_z2);
g.DrawLine(p, pixel_z2, pixel_z1);
g.DrawLine(p, pixel_z1, p2);
}
}
private void UnDrawLinkLine(int x1, int y1, int x2, int y2, LinkType LType)
{
Graphics g = this.pictureBox1.CreateGraphics();
Pen p = new Pen(this.BackColor , 3);
Point p1 = new Point(x1 * W + W / 2 + W, y1 * W + W / 2 + W);
Point p2 = new Point(x2 * W + W / 2 + W, y2 * W + W / 2 + W);
if (LType == LinkType.LineType)
g.DrawLine(p, p1, p2);
if (LType == LinkType.OneCornerType)
{
Point pixel_z1 = new Point(z1.X * W + W / 2 + W, z1.Y * W + W / 2 + W);
g.DrawLine(p, p1, pixel_z1);
g.DrawLine(p, pixel_z1, p2);
}
if (LType == LinkType.TwoCornerType)
{
Point pixel_z1 = new Point(z1.X * W + W / 2 + W, z1.Y * W + W / 2 + W);
Point pixel_z2 = new Point(z2.X * W + W / 2 + W, z2.Y * W + W / 2 + W);
if (!(p1.X == pixel_z2.X || p1.Y == pixel_z2.Y))
{
Point c;
c = pixel_z1;
pixel_z1 = pixel_z2;
pixel_z2 = c;
}
g.DrawLine(p, p1, pixel_z2);
g.DrawLine(p, pixel_z2, pixel_z1);
g.DrawLine(p, pixel_z1, p2);
}
}
private void DrawSelectedBlock(int x, int y, Pen myPen, Graphics g)
{
Rectangle b1 = new Rectangle(x * W + 1+W, y * W + 1+W, W - 3, W - 3);
g.DrawRectangle(myPen, b1);
}
private void ClearSelectedBlock(int x, int y, Graphics g)
{
SolidBrush myBrush = new SolidBrush(this.BackColor);
Rectangle b1 = new Rectangle(x * W+W, y * W+W, W, W);
g.FillRectangle(myBrush, b1);
}
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar ==(char) Keys.F1)
{
RebuildGame();
Init_Graphic();
}
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
Cursor.Current = Cursors.Hand;
Graphics g = this.pictureBox1.CreateGraphics();
Pen myPen = new Pen(Color.Red, 3);
int x, y;
if (e.Button == MouseButtons.Left)
{
x = (e.X - W) / W;
y = (e.Y - W) / W;
if (m_map[y * m_nCol + x] == -1) return;
if (Select_first == false)
{
x1 = x; y1 = y;
DrawSelectedBlock(x1, y1, myPen,g);
Select_first = true;
}
else
{
x2 = x; y2 = y;
if ((x1 == x2) && (y1 == y2)) return;
DrawSelectedBlock(x2, y2, myPen, g);
if (IsSame(x1, y1, x2, y2) && IsLink(x1, y1, x2, y2))
{
DrawLinkLine(x1, y1, x2, y2,LType);
System.Threading.Thread.Sleep(500);
ClearSelectedBlock(x1, y1, g);
ClearSelectedBlock(x2, y2, g);
m_map[y1 * m_nCol + x1] = -1;
m_map[y2 * m_nCol + x2] =-1;
Select_first = false;
UnDrawLinkLine(x1, y1, x2, y2, LType);
if (IsWin())
{ MessageBox.Show("闯关成功,恭喜进入下一关");
level++;
StartNewGame();
Init_Graphic();
}
if (level == 2)
{
int n = 0;
for (n = 0; n< m_nCol - x1-1 ; n++)
m_map[y1 * m_nCol + x1 + n] = m_map[y1 * m_nCol + x1 + n + 1];
if ((x1 + n) < m_nCol)
m_map[y1 * m_nCol + x1 + n ] = -1;
for (int i = 0; i < m_nCol - x2 - 1; i++)
m_map[y2 * m_nCol + x2 + i] = m_map[y2 * m_nCol + x2 + i + 1];
if((x2 + n) < m_nCol)
m_map[y2 * m_nCol + x2 + n ] = -1;
Init_Graphic();
}
if (level == 3)
{
}
if (level == 4)
{
}
}
else
{
int i = y1 * m_nCol + x1;
g.DrawImage(create_image(m_map[i]), W * (i % GameSize)+W,
W * (i / GameSize)+W, W, W);
x1 = x; y1 = y;
//myPen = new Pen(Color.Red, 3);
//Rectangle b2 = new Rectangle(x1 * W + 1, y1 * W + 1, W - 3, W - 3);
//g.DrawRectangle(myPen, b2);
Select_first = true;
}
}
}
if (e.Button == MouseButtons.Right)
{
if (!Find2Block())
{
MessageBox.Show("没有连通的方块了!!");
RebuildGame();
Init_Graphic();
}
}
}
private void 控制ToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void 刷新ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (MessageBox.Show("重新开始吗?", "重新开始", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
StartNewGame();
Init_Graphic();
}
}
private void 洗牌ToolStripMenuItem_Click(object sender, EventArgs e)
{
RebuildGame();
Init_Graphic();
}
private void 提示ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (!Find2Block())
{
MessageBox.Show("没有连通的方块了!!");
RebuildGame();
Init_Graphic();
}
}
private void timer1_Tick(object sender, EventArgs e)
{
Graphics g = this.pictureBox1.CreateGraphics();
length--;
g.DrawRectangle(new Pen(Color.LimeGreen, 2), 50, 20, length, 20);
if (length == 0)
{
MessageBox.Show("时间到!");
StartNewGame();
Init_Graphic();
}
}
private void 第1关ToolStripMenuItem_Click(object sender, EventArgs e)
{
level = 1;
if (MessageBox.Show("重新开始吗?", "重新开始", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
StartNewGame();
Init_Graphic();
}
}
private void 第2关ToolStripMenuItem_Click(object sender, EventArgs e)
{
level = 2;
if (MessageBox.Show("重新开始吗?", "重新开始", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
StartNewGame();
Init_Graphic();
}
}
private void 第3关ToolStripMenuItem_Click(object sender, EventArgs e)
{
level = 3;
if (MessageBox.Show("重新开始吗?", "重新开始", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
StartNewGame();
Init_Graphic();
}
}
private void 第4关ToolStripMenuItem_Click(object sender, EventArgs e)
{
level = 4;
if (MessageBox.Show("重新开始吗?", "重新开始", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
StartNewGame();
Init_Graphic();
}
}
}
}