C# 连连看 游戏 连线 代码.doc
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Timers;
namespace LLKAN
{
class Game
{
///
/// 游戏是否正在进行
///
public bool Playflag;
public bool CanPlay;
public bool PlayWin;
public const int Row = 12;
public const int Column = 17;
public GamePic[,] GamePicList = new GamePic[Row, Column];
private GamePath MyPath = new GamePath();
public Game()
{
this.CanPlay =false;
this.PlayWin = false;
}
///
/// 游戏开始
///
public void GameStart(int pn)
{
// 游戏开始,产生随机图片
stdFunc PubstdFunc = new stdFunc();
int[] PicNumber = new int[Row * Column];
//int pn;
PicNumber = PubstdFunc.GetRandomPic(pn, (Row - 2) * (Column - 2));
for (int i = 1; i<=Row-2; i++)
{
for (int j = 1; j <= Column-2; j++)
{
GamePic MyPic = new GamePic(j * 45, i * 41, PicNumber[(i-1) *
(Column-2) + (j-1)], 1);
GamePicList[i, j] = MyPic;
}
}
//产生第一行
for (int i = 0; i < Column; i++)
{
GamePic MyPic = new GamePic(i * 45, 0,0, 2);
GamePicList[0, i] = MyPic;
}
//最后一行
for (int i = 0; i < Column; i++)
{
GamePic MyPic = new GamePic(i * 45, (Row-1)*41, 0, 2);
GamePicList[Row-1, i] = MyPic;
}
//第一列
for (int i = 0; i < Row; i++)
{
GamePic MyPic = new GamePic(0, i * 41, 0, 2);
GamePicList[i, 0] = MyPic;
}
//最一列
for (int i = 0; i < Row; i++)
{
GamePic MyPic = new GamePic((Column-1)*45, i * 41, 0, 2);
GamePicList[i, Column-1] = MyPic;
}
Playflag = true;
CanPlay = true;
}
///
/// 检查游戏状况
///
public void CheckPlay()
{
if (GameWin())
{
PlayWin = true;
return;
}
}
///
/// 得到两点之间的交叉点
///
public GamePath GetGamePath
{
get
{
return MyPath;
}
}
public void DrawLine(Graphics g, Color DrawColor)
{
Point PL,PH;
stdFunc MystdFunc = new stdFunc();
for (int i = 0; i < MyPath.GetPathList.Count; i+=2)
{
PL = new Point(
GamePicList[MyPath.GetPathList[i].X,MyPath.GetPathList[i].Y].X,
GamePicList[MyPath.GetPathList[i].X,MyPath.GetPathList[i].Y].Y);
PH = new Point(
GamePicList[MyPath.GetPathList[i + 1].X, MyPath.GetPathList[i +
1].Y].X,
GamePicList[MyPath.GetPathList[i + 1].X, MyPath.GetPathList[i +
1].Y].Y);
MystdFunc.DrawLine(g, PL, PH, 5, DrawColor);
}
}
///
/// 检查两个图标是否可以消
///
///
第一个图标的座标
///
第二个图标的座标
///
是否可以消除
public bool CheckPath(Point P1, Point P2,bool AddPath)
{
MyPath.Clear();
if (P1 == P2) return false;
// 开始搜索路径
// 1 检查水平
if (P1.X == P2.X)
{
if (CheckHorizontal(P1, P2, AddPath))
return true;
}
MyPath.Clear();
// 2 检查垂直
if (P1.Y == P2.Y)
{
if (CheckVertical(P1, P2, AddPath))
return true;
}
MyPath.Clear();
// 3 检查一折
if (CheckOneCorner(P1, P2, AddPath,true))
return true;
MyPath.Clear();
// 4 检查二折
if (CheckTwoCorner(P1, P2, AddPath))
return true;
return false;
}
///
/// 检查二折
///
///
第一个点
///
第二个点
///
是否要保存路径
///
private bool CheckTwoCorner(Point P1, Point P2, bool isAddPath)
{
Point MinRow, MaxRow;
MinRow = P1.X < P2.X ? P1 : P2;
MaxRow = P1.X < P2.X ? P2 : P1;
if (CheckTwoCornerUP(P1, P2, isAddPath))
{
return true;
}
MyPath.Clear();
if (CheckTwoCornerDown(P1, P2, isAddPath))
{
return true;
}
MyPath.Clear();
if (CheckTwoCornerLeft(P1, P2, isAddPath))
{
return true;
}
MyPath.Clear();
if (CheckTwoCornerRight(P1, P2, isAddPath))
{
return true;
}
return false;
}
private bool CheckTwoCornerUP(Point P1, Point P2, bool isAddPath)
{
for (int i = P1.X - 1; i > -1; i--)
{
Point StartPoint = new Point(i, P1.Y);
if (GamePicList[StartPoint.X, StartPoint.Y].State == 2)
{
if (isAddPath)
{
MyPath.Clear();
CPath xPath = new CPath(P1);
MyPath.Push(xPath);
CPath yPath = new CPath(StartPoint);
MyPath.Push(yPath);
}
if (CheckOneCorner(StartPoint, P2, isAddPath, false))
{
return true;
}
}
else
{
return false;
}
}
return false;
}
private bool CheckTwoCornerRight(Point P1, Point P2, bool isAddPath)
{
for (int i = P1.Y + 1; i < Game.Column; i++)
{
Point StartPoint = new Point(P1.X, i);
if (GamePicList[StartPoint.X, StartPoint.Y].State == 2)
{
if (isAddPath)
{
MyPath.Clear();
CPath xPath = new CPath(P1);
MyPath.Push(xPath);
CPath yPath = new CPath(StartPoint);
MyPath.Push(yPath);
}
if (CheckOneCorner(StartPoint, P2, isAddPath,false))
{
return true;
}
}
else
{
return false;
}
}
return false;
}
private bool CheckTwoCornerLeft(Point P1, Point P2, bool isAddPath)
{
for (int i = P1.Y - 1; i > -1; i--)
{
Point StartPoint = new Point(P1.X, i);
if (GamePicList[StartPoint.X, StartPoint.Y].State == 2)
{
if (isAddPath)
{
MyPath.Clear();
CPath xPath = new CPath(P1);
MyPath.Push(xPath);
CPath yPath = new CPath(StartPoint);
MyPath.Push(yPath);
}
if (CheckOneCorner(StartPoint, P2, isAddPath,false))
{
return true;
}
}
else
{
return false;
}
}
return false;
}
private bool CheckTwoCornerDown(Point P1, Point P2, bool isAddPath)
{
for (int i = P1.X + 1; i < Game.Row; i++)
{
Point StartPoint = new Point(i, P1.Y);
if (GamePicList[StartPoint.X, StartPoint.Y].State == 2)
{
if (isAddPath)
{
MyPath.Clear();
CPath xPath = new CPath(P1);
MyPath.Push(xPath);
CPath yPath = new CPath(StartPoint);
MyPath.Push(yPath);
}
if (CheckOneCorner(StartPoint, P2, isAddPath,false))
{
return true;
}
}
else
{
return false;
}
}
return false;
}
// 3 检查一折
private bool CheckOneCorner(Point P1, Point P2, bool isAddPath,bool ClearPath)
{
Point MinRow, MaxRow; //小行,大行
Point CrossX, CrossY; //交叉点
MinRow = P1.X < P2.X ? P1 : P2;
MaxRow = P1.X < P2.X ? P2 : P1;
if (MinRow.Y < MaxRow.Y)
{
CrossX = new Point(MinRow.X,MaxRow.Y);
CrossY = new Point(MaxRow.X,MinRow.Y);
}
else
{
CrossX = new Point(MinRow.X,MaxRow.Y);
CrossY = new Point(MaxRow.X,MinRow.Y);
}
//搜索 MinRow ---> CrossX--->MaxRow
if (GamePicList[CrossX.X, CrossX.Y].State == 2)
{
if (CheckHorizontal(MinRow, CrossX, false))
{
if (CheckVertical(CrossX, MaxRow, false))
{
if (isAddPath)
{
CPath xPath = new CPath(MinRow);
MyPath.Push(xPath);
CPath yPath = new CPath(CrossX);
//---- 增加两次 ------
MyPath.Push(yPath);
MyPath.Push(yPath);
CPath zPath = new CPath(MaxRow);
MyPath.Push(zPath);
}
return true;
}
}
}
if (isAddPath && ClearPath)
{
MyPath.Clear();
}
//搜索 MinRow ---> CrossY--->MaxRow
if (GamePicList[CrossY.X, CrossY.Y].State == 2)
{
if (CheckVertical(MinRow, CrossY,false))
{
if (CheckHorizontal(CrossY, MaxRow,false))
{
if (isAddPath)
{
CPath xPath = new CPath(MinRow);
MyPath.Push(xPath);
CPath yPath = new CPath(CrossY);
MyPath.Push(yPath);
MyPath.Push(yPath);
CPath zPath = new CPath(MaxRow);
MyPath.Push(zPath);
}
return true;
}
}
}
return false;
}
private bool CheckVertical(Point P1, Point P2, bool AddPath)
{
Point Px, Py;
Px = P1.X < P2.X ? P1 : P2;
Py = P1.X < P2.X ? P2 : P1;
for (int i = Px.X + 1; i < Py.X; i++)
{
if (GamePicList[i, Px.Y].State != 2)
{
return false;
}
}
if (AddPath)
{
CPath PA = new CPath(Px);
CPath PB = new CPath(Py);
MyPath.Push(PA);
MyPath.Push(PB);
}
return true;
}
private bool CheckHorizontal(Point P1, Point P2,bool AddPath)
{
Point Px, Py;
Px = P1.Y < P2.Y ? P1 : P2;
Py = P1.Y < P2.Y ? P2 : P1;
for (int i = Px.Y + 1; i < Py.Y; i++)
{
if (GamePicList[Px.X, i].State != 2)
{
return false;
}
}
if (AddPath)
{
CPath PA = new CPath(Px);
CPath PB = new CPath(Py);
MyPath.Push(PA);
MyPath.Push(PB);
}
return true;
}
private bool GameWin()
{
for (int i = 1; i < Game.Row - 1;i++ )
{
for (int j = 1; j < Game.Column - 1; j++)
{
if (GamePicList[i,j].State==1)
return false;
}
}
return true;
}
}
}