/// <summary>
/// 获取指定的 CardPoint 的 Y 轴方向上的所有 ImageName 为 Global.EmptyImageName 的 CardPoint 集合
/// </summary>
private List<CardPoint> GetYPositions(CardPoint p)
{
var result = new List<CardPoint>() { p };
for ( int i = 0; i < Global.ContainerRows; i++)
{
var point = new CardPoint(i * Global.ContainerColumns + p.X);
if (_cards[point.Position].ImageName == Global.EmptyImageName)
result.Add(point);
}
return result;
}
/// <summary>
/// 执行消去两个卡片的任务
/// 参数为:两个卡片对象和消去路径的四个点(此四个点需按路径方向依次传入)
/// </summary>
private void RemoveCard(CardModel c1, CardModel c2, CardPoint p1, CardPoint p2, CardPoint p3, CardPoint p4)
{
_cards[c1.Position] = new CardModel(Global.EmptyImageName, c1.Position);
_cards[c2.Position] = new CardModel(Global.EmptyImageName, c2.Position);
Points.Clear();
Points.Add(CardPoint2Point(p1));
Points.Add(CardPoint2Point(p2));
Points.Add(CardPoint2Point(p3));
Points.Add(CardPoint2Point(p4));
Thread thread = new Thread
(
x =>
{
Thread.Sleep(100);
_syncContext.Post
(
y =>
{
Points.Clear();
},
null
);
}
);
thread.Start();
}
/// <summary>
/// CardPoint 转换成坐标位置 Point
/// </summary>
private Point CardPoint2Point(CardPoint cardPoint)
{
// 38 - 每个正方形卡片的边长
// 19 - 边长 / 2
// cardPoint.X * 2 - 卡片的 Padding 为 1 ,所以卡片间的间距为 2
var x = cardPoint.X * 38 + 19 + cardPoint.X * 2;
var y = cardPoint.Y * 38 + 19 + cardPoint.Y * 2;
return new Point(x, y);
}
/// <summary>
/// 检查当前是否仍然有可消除的卡片
/// </summary>
public bool IsActive()
{
var currentCards = _cards.Where(p => p.ImageName != Global.EmptyImageName).ToList();
for ( int i = 0; i < currentCards.Count() - 1; i++)
{
for ( int j = i + 1; j < currentCards.Count(); j++)
{
if (Match(currentCards[i], currentCards[j], false))
return true;
}
}
return false;
}
/// <summary>
/// 重新排列当前卡片集合
/// 并保证有可消卡片
/// </summary>
public void Replace()
{
Random r = new Random();
var currentCards = _cards.Where(p => p.ImageName != Global.EmptyImageName).ToList();
var count = currentCards.Count;
if (count == 0)
return;
var targetImageNames = currentCards.Select(p => p.ImageName).ToList();
var targetPositions = currentCards.Select(p => p.Position).ToList();
for ( int i = 0; i < count; i++)
{
var index = r.Next(0, count - i);
var targetImageName = targetImageNames.Skip(index).First();
var targetPosition = targetPositions.Skip(i).First();
_cards[targetPosition] = new CardModel(targetImageName, targetPosition);
targetImageNames.RemoveAt(index);
}
while (!IsActive())
{
Replace();
}
}
/// <summary>
/// 清除卡片集合
/// </summary>
public void Clear()
{
_cards.Clear();
Points.Clear();
}
/// <summary>
/// 当前卡片数量
/// </summary>
public int CardCount
{
get { return _cards.Where(p => p.ImageName != Global.EmptyImageName).Count(); }
}
/// <summary>
/// 连连看的卡片集合
/// </summary>
public ObservableCollection<CardModel> Cards
{
get { return _cards; }
}
private PointCollection _points;
/// <summary>
/// 可消两卡片的连接路径上的 4 个点的集合
/// </summary>
public PointCollection Points
{
get
{
if (_points == null)
_points = new PointCollection();
return _points;
}
set
{
_points = value;
if (PropertyChanged != null)
PropertyChanged( this, new PropertyChangedEventArgs( "Points"));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
}
/// 获取指定的 CardPoint 的 Y 轴方向上的所有 ImageName 为 Global.EmptyImageName 的 CardPoint 集合
/// </summary>
private List<CardPoint> GetYPositions(CardPoint p)
{
var result = new List<CardPoint>() { p };
for ( int i = 0; i < Global.ContainerRows; i++)
{
var point = new CardPoint(i * Global.ContainerColumns + p.X);
if (_cards[point.Position].ImageName == Global.EmptyImageName)
result.Add(point);
}
return result;
}
/// <summary>
/// 执行消去两个卡片的任务
/// 参数为:两个卡片对象和消去路径的四个点(此四个点需按路径方向依次传入)
/// </summary>
private void RemoveCard(CardModel c1, CardModel c2, CardPoint p1, CardPoint p2, CardPoint p3, CardPoint p4)
{
_cards[c1.Position] = new CardModel(Global.EmptyImageName, c1.Position);
_cards[c2.Position] = new CardModel(Global.EmptyImageName, c2.Position);
Points.Clear();
Points.Add(CardPoint2Point(p1));
Points.Add(CardPoint2Point(p2));
Points.Add(CardPoint2Point(p3));
Points.Add(CardPoint2Point(p4));
Thread thread = new Thread
(
x =>
{
Thread.Sleep(100);
_syncContext.Post
(
y =>
{
Points.Clear();
},
null
);
}
);
thread.Start();
}
/// <summary>
/// CardPoint 转换成坐标位置 Point
/// </summary>
private Point CardPoint2Point(CardPoint cardPoint)
{
// 38 - 每个正方形卡片的边长
// 19 - 边长 / 2
// cardPoint.X * 2 - 卡片的 Padding 为 1 ,所以卡片间的间距为 2
var x = cardPoint.X * 38 + 19 + cardPoint.X * 2;
var y = cardPoint.Y * 38 + 19 + cardPoint.Y * 2;
return new Point(x, y);
}
/// <summary>
/// 检查当前是否仍然有可消除的卡片
/// </summary>
public bool IsActive()
{
var currentCards = _cards.Where(p => p.ImageName != Global.EmptyImageName).ToList();
for ( int i = 0; i < currentCards.Count() - 1; i++)
{
for ( int j = i + 1; j < currentCards.Count(); j++)
{
if (Match(currentCards[i], currentCards[j], false))
return true;
}
}
return false;
}
/// <summary>
/// 重新排列当前卡片集合
/// 并保证有可消卡片
/// </summary>
public void Replace()
{
Random r = new Random();
var currentCards = _cards.Where(p => p.ImageName != Global.EmptyImageName).ToList();
var count = currentCards.Count;
if (count == 0)
return;
var targetImageNames = currentCards.Select(p => p.ImageName).ToList();
var targetPositions = currentCards.Select(p => p.Position).ToList();
for ( int i = 0; i < count; i++)
{
var index = r.Next(0, count - i);
var targetImageName = targetImageNames.Skip(index).First();
var targetPosition = targetPositions.Skip(i).First();
_cards[targetPosition] = new CardModel(targetImageName, targetPosition);
targetImageNames.RemoveAt(index);
}
while (!IsActive())
{
Replace();
}
}
/// <summary>
/// 清除卡片集合
/// </summary>
public void Clear()
{
_cards.Clear();
Points.Clear();
}
/// <summary>
/// 当前卡片数量
/// </summary>
public int CardCount
{
get { return _cards.Where(p => p.ImageName != Global.EmptyImageName).Count(); }
}
/// <summary>
/// 连连看的卡片集合
/// </summary>
public ObservableCollection<CardModel> Cards
{
get { return _cards; }
}
private PointCollection _points;
/// <summary>
/// 可消两卡片的连接路径上的 4 个点的集合
/// </summary>
public PointCollection Points
{
get
{
if (_points == null)
_points = new PointCollection();
return _points;
}
set
{
_points = value;
if (PropertyChanged != null)
PropertyChanged( this, new PropertyChangedEventArgs( "Points"));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
}
OK
[源码下载]
本文转自webabcd 51CTO博客,原文链接:
http://blog.51cto.com/webabcd/345623
,如需转载请自行联系原作者