
Here, just did one small game for my wife and my baby with their photos( That's the reason they happy).
WPF , VS2010,Microsoft Expression Blend 4, InstallAware Studio Admin 9, I used.
Also I doing Silverlight 4 edition at the same time.
For Algorithm the core codes like below:
////////////////////////////////////////////////////////////////////////
//
// Kai Zhou Copyright (C) 2010
//
// created date : 2010-7-15
// developer : Kai Zhou
//
//
////////////////////////////////////////////////////////////////////////
using System;
using System.Net;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
//using System.Windows.Shapes;
using System.Windows.Media.Imaging;
using System.IO;
using System.Diagnostics;
namespace GuoGuoGo.Bll
{
public class Engine
{
public const int LevelCount = 7;
public static Size ItemSize = new Size(36,36);
static int SelectedCount = 0;
private static string starPath = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
enum ItemDer
{
LineX,
LineY,
LTRB,
LBRT
};
public static Point MaxBackGridSplits
{
get
{
return new Point(12, 8);
}
}
static Pojo.ImgItem CurSelectedItem = null;
static double ItemCount //= Config.SLConfig.BackGridSplits.X * Config.SLConfig.BackGridSplits.Y;
{
get;
set;
}
public delegate void ItemLinked(Pojo.ImgItem item);
public static event ItemLinked ItemLinkedHanlder;
public static int CurLevelIndex
{
get;
set;
}
public static Grid GameBackGrid
{
get;
set;
}
public static Pojo.GameLevel CurLevel = null;
public static Pojo.ImgItem[][] AllItems
{
get;
set;
}
public delegate void delegateRefreshGrid();
public static event delegateRefreshGrid RefreshGridHanlder;
///
public delegate void delegateItemCompleted(Pojo.GameLevel level);
public static event delegateItemCompleted GameCompletedHanlder;
public static void InitEngine()
{
ClearItemsCach();
}
public delegate void delegateSetLevel();
public static event delegateSetLevel SetLevelEventHanlder;
public static void SetLevel(int level)
{
int TotalScore = 0;
if (CurLevel != null) TotalScore = CurLevel.Score;
if (level == 0) level = 1;
else if (level > LevelCount)
{
TotalScore = 0;
}
CurLevel = new GuoGuoGo.Pojo.GameLevel();
CurLevel.Level = level;
CurLevel.BackGridSplits = MaxBackGridSplits;
CurLevel.Score = TotalScore;
//int imgIndex = Config.SLConfig.AllGameLevelImages.Length < itemimages =" new" i =" 0;" imagesource =" new" itemcount =" CurLevel.BackGridSplits.X" selectedcount =" 0;" showgridlines =" false;" i =" 0;" cd =" new" width =" new" i =" 0;" rd =" new" height =" new" width =" CurLevel.BackGridSplits.X" height =" CurLevel.BackGridSplits.Y"> CreatedItems = new List
Random rand = new Random();
for (int i = 0; i < index =" rand.Next(CurLevel.ItemImages.Length" item =" new" itemimg =" CurLevel.ItemImages[index];" item2 =" new" itemimg =" item.ItemImg" i =" 0;" x =" (int)(i" y =" (int)(i" index =" rand.Next(CreatedItems.Count" item =" CreatedItems[index];" location =" new" background =" GameBackGrid;" curselecteditem =" null;"> lastItems = GetExistsItems();
if (lastItems.Count == 0)
{
FillGrid();
return;
}
//ClearItemsCach();
Random rand = new Random();
if (lastItems.Count <= 0) return; for (int x = 0; x < y =" 0;" index =" rand.Next(lastItems.Count" item =" lastItems[index];" item =" new" itemimg =" item.ItemImg" location =" new" background =" GameBackGrid;" score =" 0;" allitems =" new" i =" 0;"> GetExistsItems()
{
List
for (int i = 0; i < x =" (int)(i" y =" (int)(i" curselecteditem =" null;">= ItemCount)
{
if (GameCompletedHanlder != null) GameCompletedHanlder(CurLevel);
}
}
else
{
if (CurSelectedItem != null)
{
CurSelectedItem.CnacelSelect();
}
item.Select();
CurSelectedItem = item;
return true;
}
return false;
}
public static bool IsCanLink(Pojo.ImgItem item1, Pojo.ImgItem item2)
{
if (item1.ItemImg != item2.ItemImg || item1 == item2) return false;
ItemDer itemder = GetItemDer(item1, item2);
switch (itemder)
{
case ItemDer.LineX:
{
return CheckLineX(item1, item2);
}
case ItemDer.LineY:
{
return CheckLineY(item1, item2);
}
case ItemDer.LTRB:
{
return CheckLTRB(item1, item2);
}
case ItemDer.LBRT:
{
return CheckLBRT(item1, item2);
}
}
return false;
}
private static bool CheckLineX(Pojo.ImgItem item1, Pojo.ImgItem item2)
{
if (CheckInPointsIsEmpty(item1.Location, item2.Location, ItemDer.LineX) || (item1.Location.Y == 0 && item2.Location.Y == 0) ||
(item1.Location.Y == CurLevel.BackGridSplits.Y - 1 && item2.Location.Y == CurLevel.BackGridSplits.Y - 1))
{
return true;
}
for (int i = (int)item1.Location.Y - 1; i >= 0 ; i--)
{
Point p1=new Point(item1.Location.X, i);
Point p2=new Point(item2.Location.X, i);
if (!CheckPointIsEmpty(p1) || !CheckPointIsEmpty(p2)) break;
if (CheckInPointsIsEmpty(p1, p2, ItemDer.LineX))
{
return true;
}
if (i == 0)
{
return true;
}
}
for (int i = (int)item1.Location.Y + 1; i <= CurLevel.BackGridSplits.Y - 1; i++) { Point p1 = new Point(item1.Location.X, i); Point p2 = new Point(item2.Location.X, i); if (!CheckPointIsEmpty(p1) || !CheckPointIsEmpty(p2)) break; if (CheckInPointsIsEmpty(p1, p2, ItemDer.LineX)) { return true; } if (i == CurLevel.BackGridSplits.Y - 1) { return true; } } return false; } private static bool CheckLineY(Pojo.ImgItem item1, Pojo.ImgItem item2) { if (CheckInPointsIsEmpty(item1.Location, item2.Location, ItemDer.LineY) || (item1.Location.X == 0 && item2.Location.X == 0) || (item1.Location.X == CurLevel.BackGridSplits.X - 1 && item2.Location.X == CurLevel.BackGridSplits.X - 1)) { return true; } for (int i = (int)item1.Location.X - 1; i >= 0; i--)
{
Point p1 = new Point(i, item1.Location.Y);
Point p2 = new Point(i, item2.Location.Y);
if (!CheckPointIsEmpty(p1) || !CheckPointIsEmpty(p2)) break;
if (CheckInPointsIsEmpty(p1, p2, ItemDer.LineY))
{
return true;
}
if (i == 0)
{
return true;
}
}
for (int i = (int)item1.Location.X + 1; i <= CurLevel.BackGridSplits.X - 1; i++) { Point p1 = new Point(i, item1.Location.Y); Point p2 = new Point(i, item2.Location.Y); if (!CheckPointIsEmpty(p1) || !CheckPointIsEmpty(p2)) break; if (CheckInPointsIsEmpty(p1, p2, ItemDer.LineY)) { return true; } if (i == CurLevel.BackGridSplits.X - 1) { return true; } } return false; } private static bool CheckLTRB(Pojo.ImgItem item1, Pojo.ImgItem item2) { if (item1.Location.X > item2.Location.X)
{
Pojo.ImgItem itemtmp = item1;
item1 = item2;
item2 = itemtmp;
}
for (int i = (int)item2.Location.X - 1; i >= 0; i--)
{
Point p1 = new Point(i, item2.Location.Y);
Point p2 = new Point(i, item1.Location.Y);
if (!CheckPointIsEmpty(p1))
{
break;
}
if (p2 != item1.Location && !CheckPointIsEmpty(p2))
{
if (i < i ="="" i =" (int)item1.Location.X" p1 =" new" p2 =" new"> item2.Location.X)
{
break;
}
continue;
}
if (CheckInPointsIsEmpty(p2, item2.Location, ItemDer.LineX) && CheckInPointsIsEmpty(p1, p2, ItemDer.LineY))
{
return true;
}
else if (i == CurLevel.BackGridSplits.X - 1)
{
return true;
}
}
for (int i = (int)item2.Location.Y - 1; i >= 0; i--)
{
Point p1 = new Point(item2.Location.X,i);
Point p2 = new Point(item1.Location.X,i);
if (!CheckPointIsEmpty(p1))
{
break;
}
if (p2 != item1.Location && !CheckPointIsEmpty(p2))
{
if (i < i ="="" i =" (int)item1.Location.Y" p1 =" new" p2 =" new"> item2.Location.Y)
{
break;
}
continue;
}
if (CheckInPointsIsEmpty(p2, item2.Location, ItemDer.LineY) && CheckInPointsIsEmpty(p1, p2, ItemDer.LineX))
{
return true;
}
else if (i == CurLevel.BackGridSplits.Y - 1)
{
return true;
}
}
return false;
}
private static bool CheckLBRT(Pojo.ImgItem item1, Pojo.ImgItem item2)
{
if (item1.Location.X > item2.Location.X)
{
Pojo.ImgItem itemtmp = item1;
item1 = item2;
item2 = itemtmp;
}
for (int i = (int)item2.Location.X - 1; i >= 0; i--)
{
Point p1 = new Point(i, item2.Location.Y);
Point p2 = new Point(i, item1.Location.Y);
if (!CheckPointIsEmpty(p1))
{
break;
}
if (p2 != item1.Location && !CheckPointIsEmpty(p2))
{
if (i < i ="="" i =" (int)item1.Location.X" p1 =" new" p2 =" new"> item2.Location.X)
{
break;
}
continue;
}
if (CheckInPointsIsEmpty(p2, item2.Location, ItemDer.LineX) && CheckInPointsIsEmpty(p1, p2, ItemDer.LineY))
{
return true;
}
else if (i == CurLevel.BackGridSplits.X - 1)
{
return true;
}
}
for (int i = (int)item1.Location.Y - 1; i >= 0; i--)
{
Point p1 = new Point(item1.Location.X, i);
Point p2 = new Point(item2.Location.X, i);
if (!CheckPointIsEmpty(p1))
{
break;
}
if (p2 != item2.Location && !CheckPointIsEmpty(p2))
{
if (i < i ="="" i =" (int)item2.Location.Y" p1 =" new" p2 =" new"> item1.Location.Y)
{
break;
}
continue;
}
if (CheckInPointsIsEmpty(p2, item1.Location, ItemDer.LineY) && CheckInPointsIsEmpty(p1, p2, ItemDer.LineX))
{
return true;
}
else if (i == CurLevel.BackGridSplits.Y - 1)
{
return true;
}
}
return false;
}
private static bool CheckInPointsIsEmpty(Point p1, Point p2,ItemDer iDer)
{
if (iDer == ItemDer.LineX)
{
if (Math.Abs(p1.X - p2.X) == 1 || (p1.X - p2.X) == 0)
{
return true;
}
int offsetx = (int)(p1.X - p2.X);
if (offsetx < i =" 1;" i =" 1;" ider ="="" y ="="" offsety =" (int)(p1.Y" i =" 1;" i =" 1;" x ="="" y ="=""> 0)
{
return ItemDer.LTRB;
}
else
return ItemDer.LBRT;
}
#endregion
}
}