先说下思路,这个小程序是使用Silverlight3开发的。首先我定义了一个棋盘的子对象,用于存放棋盘的格子和棋子。然后用一个二维的数组存放对象。其余的操作就都是逻辑的了。大家看代码吧。
下面给出代码:
1.定义对象
/// <summary>
/// 棋盘子对象
/// </summary>
public class ChessObject
{
public event RoutedEventHandler ObjectClick;
//Canvas容器
public Canvas ImgContainer { get; set; }
//判断是否先手
public bool IsPrevious { get; set; }
//判断此点是否有棋子
public bool IsFill { get; set; }
//构造
public ChessObject(Canvas canvas)
{
this.IsFill = false;
this.ImgContainer = canvas;
ImgContainer.MouseLeftButtonUp += new MouseButtonEventHandler(ImgContainer_MouseLeftButtonUp);
}
void ImgContainer_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (ObjectClick != null)
ObjectClick(this, e);
}
}
public ChessObject[,] arrayChessChild = new ChessObject[15, 15];
public MainPage()
{
InitializeComponent();
InitContainer();
}
/// <summary>
/// 棋盘初始化
/// </summary>
private void InitContainer()
{
//List<Canvas> listChild = new List<Canvas>();
//List<List<Canvas>> listParent = new List<List<Canvas>>();
index = 1;
const int Point_Width = 40;
const int Point_Height = 40;
LayoutRoot.Children.Clear();
for (int i = 0; i < 15; i++)
{
for (int j = 0; j < 15; j++)
{
ChessObject chess = null;
Canvas canvas = new Canvas();
ImageBrush img = new ImageBrush();
ImageSource imgsource = new BitmapImage(FindImg(i, j));
img.ImageSource = imgsource;
canvas.Background = img;
canvas.Width = Point_Width;
canvas.Height = Point_Height;
canvas.SetValue(Canvas.LeftProperty, Convert.ToDouble(j * Point_Width));
canvas.SetValue(Canvas.TopProperty, Convert.ToDouble(i * Point_Height));
//canvas.MouseLeftButtonUp += new MouseButtonEventHandler(canvas_MouseLeftButtonUp);
chess = new ChessObject(canvas);
chess.ObjectClick += new RoutedEventHandler(chess_ObjectClick);
arrayChessChild[i, j] = chess;
LayoutRoot.Children.Add(arrayChessChild[i, j].ImgContainer);
}
}
}
void chess_ObjectClick(object sender, RoutedEventArgs e)
{
if (!(sender is ChessObject))
return;
ChessObject chess = sender as ChessObject;
if (chess.IsFill)
return;
if (index % 2 == 1)
{
ImageBrush img = new ImageBrush();
img.ImageSource = new BitmapImage(new Uri("pngs/black.png", UriKind.Relative));
chess.ImgContainer.Background = img;
chess.IsPrevious = true;
}
else
{
ImageBrush img = new ImageBrush();
img.ImageSource = new BitmapImage(new Uri("pngs/white.png", UriKind.Relative));
chess.ImgContainer.Background = img;
chess.IsPrevious = false;
}
chess.IsFill = true;
if (IsOver() == 1)
{
System.Windows.Controls.ChildWindow childwindow = new ChildWindow();
childwindow.Content = "黑方获得比赛的胜利!";
childwindow.Title = "比赛结束";
childwindow.Closed += new EventHandler(childwindow_Closed);
childwindow.Show();
//System.Windows.Browser.HtmlPage.Window.Alert("black success.");
}
else if (IsOver() == 0)
{
System.Windows.Controls.ChildWindow childwindow = new ChildWindow();
childwindow.Content = "白方获得比赛的胜利!";
childwindow.Title = "比赛结束";
childwindow.Closed += new EventHandler(childwindow_Closed);
childwindow.Show();
//System.Windows.Browser.HtmlPage.Window.Alert("white success.");
//InitContainer();
}
else
index++;
}
void childwindow_Closed(object sender, EventArgs e)
{
InitContainer();
}
/// <summary>
/// 判断游戏是否结束
/// </summary>
/// <returns></returns>
private int IsOver()
{
for (int i = 0; i < 15; i++)
{
for (int j = 0; j < 15; j++)
{
if (arrayChessChild[i, j].IsFill)
{
if (arrayChessChild[i, j].IsPrevious)
{
int successCount = 1;
for (int z = 1; z < 5; z++)
{
if (i + z > 14)
break;
if ((!arrayChessChild[i + z, j].IsPrevious) || (!arrayChessChild[i + z, j].IsFill))
{
successCount = 1;
break;
}
if (arrayChessChild[i + z, j].IsFill && arrayChessChild[i + z, j].IsPrevious)
successCount++;
if (successCount == 5)
return 1;
}
successCount = 1;
for (int z = 1; z < 5; z++)
{
if (i + z > 14 || j + z > 14)
break;
if ((!arrayChessChild[i + z, j + z].IsPrevious) || (!arrayChessChild[i + z, j + z].IsFill))
{
successCount = 1;
break;
}
if (arrayChessChild[i + z, j + z].IsFill && arrayChessChild[i + z, j + z].IsPrevious)
successCount++;
if (successCount == 5)
return 1;
}
successCount = 1;
for (int z = 1; z < 5; z++)
{
if (j + z > 14)
break;
if ((!arrayChessChild[i, j + z].IsPrevious) || (!arrayChessChild[i, j + z].IsFill))
{
successCount = 1;
break;
}
if (arrayChessChild[i, j + z].IsFill && arrayChessChild[i, j + z].IsPrevious)
successCount++;
if (successCount == 5)
return 1;
}
successCount = 1;
for (int z = 1; z < 5; z++)
{
if (i + z > 14 || j - z < 0)
break;
if ((!arrayChessChild[i + z, j - z].IsPrevious) || (!arrayChessChild[i + z, j - z].IsFill))
{
successCount = 1;
break;
}
if (arrayChessChild[i + z, j - z].IsFill && arrayChessChild[i + z, j - z].IsPrevious)
successCount++;
if (successCount == 5)
return 1;
}
}
else
{
int successCount = 1;
for (int z = 1; z < 5; z++)
{
if (i + z > 14)
break;
if ((arrayChessChild[i + z, j].IsPrevious) || (!arrayChessChild[i + z, j].IsFill))
{
successCount = 1;
break;
}
if (arrayChessChild[i + z, j].IsFill && (!arrayChessChild[i + z, j].IsPrevious))
successCount++;
if (successCount == 5)
return 0;
}
successCount = 1;
for (int z = 1; z < 5; z++)
{
if (i + z > 14 || j + z > 14)
break;
if ((arrayChessChild[i + z, j + z].IsPrevious) || (!arrayChessChild[i + z, j + z].IsFill))
{
successCount = 1;
break;
}
if (arrayChessChild[i + z, j + z].IsFill && (!arrayChessChild[i + z, j + z].IsPrevious))
successCount++;
if (successCount == 5)
return 0;
}
successCount = 1;
for (int z = 1; z < 5; z++)
{
if (j + z > 14)
break;
if ((arrayChessChild[i, j + z].IsPrevious) || (!arrayChessChild[i, j + z].IsFill))
{
successCount = 1;
break;
}
if (arrayChessChild[i, j + z].IsFill && (!arrayChessChild[i, j + z].IsPrevious))
successCount++;
if (successCount == 5)
return 0;
}
successCount = 1;
for (int z = 1; z < 5; z++)
{
if (i + z > 14 || j - z < 0)
break;
if ((arrayChessChild[i + z, j - z].IsPrevious) || (!arrayChessChild[i + z, j - z].IsFill))
{
successCount = 1;
break;
}
if (arrayChessChild[i + z, j - z].IsFill && (!arrayChessChild[i + z, j - z].IsPrevious))
successCount++;
if (successCount == 5)
return 0;
}
}
}
}
}
return -1;
/// <summary>
/// 棋盘图片填充
/// </summary>
/// <param name="i"></param>
/// <param name="j"></param>
/// <returns></returns>
public Uri FindImg(int i, int j)
{
Uri uri = null;
if (i == 0 && j == 0)
uri = new Uri("pngs/lefttop.png", UriKind.Relative);
else if (i == 0 && j == 14)
uri = new Uri("pngs/righttop.png", UriKind.Relative);
else if (i == 14 && j == 0)
uri = new Uri("pngs/leftbottom.png", UriKind.Relative);
else if (i == 14 && j == 14)
uri = new Uri("pngs/rightbottom.png", UriKind.Relative);
else if (j == 0)
uri = new Uri("pngs/left.png", UriKind.Relative);
else if (i == 0)
uri = new Uri("pngs/top.png", UriKind.Relative);
else if (i == 14)
uri = new Uri("pngs/bottom.png", UriKind.Relative);
else if (j == 14)
uri = new Uri("pngs/right.png", UriKind.Relative);
else
uri = new Uri("pngs/center.png", UriKind.Relative);
return uri;
}
本人的代码水平有限,也想在平时多积累一些写代码的经验,今天把东西放上来献丑了。。很希望有大侠对我的代码批评指正,不胜感激
在线DEMO 源码下载