Silverlight五子棋游戏

原创|其它|编辑:郝浩|2009-09-25 13:44:19.000|阅读 426 次

概述:

      注册cnblog已经有很长的一段时间了。经常在园子里看高手的文章,使我这个小虫成长的很快。今天闲来无事搞点东西,也是出于以前一个朋友的创意。

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>

      注册cnblog已经有很长的一段时间了。经常在园子里看高手的文章,使我这个小虫成长的很快。今天闲来无事搞点东西,也是出于以前一个朋友的创意。
先说下思路,这个小程序是使用Silverlight3开发的。首先我定义了一个棋盘的子对象,用于存放棋盘的格子和棋子。然后用一个二维的数组存放对象。其余的操作就都是逻辑的了。大家看代码吧。

下面给出代码:
1.定义对象


 /// <summary>
    
/// 棋盘子对象
    
/// </summary>
    public class ChessObject
    {
        
public event RoutedEventHandler ObjectClick;
        
//Canvas容器
        public Canvas ImgContainer { getset; }
        
//判断是否先手
        public bool IsPrevious { getset; }
        
//判断此点是否有棋子
        public bool IsFill { getset; }
        
//构造
        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);
        }
    }
2.棋盘的初始化

public ChessObject[,] arrayChessChild = new ChessObject[1515];

        
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);
                }
             }
        }
3.定义对象的单击事件


        
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();
        }
4.判断游戏结束的逻辑

       /// <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 >= 15)
                                    
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 >= 15 || j + z >= 15)
                                    
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 >= 15)
                                    
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;
                            }
                        }
                        
else
                        {
                            
int successCount = 1;
                            
for (int z = 1; z < 5; z++)
                            {
                                
if (i + z >= 15)
                                    
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 >= 15 || j + z >= 15)
                                    
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 >= 15)
                                    
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;
                            }
                        }
                    }
                }
            }
            
return -1;
        }
5.还有一个初始化棋盘的函数

     /// <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;
        }
鉴于本小虫水平有限,说不出什么大道理,代码就简单介绍到这。
本人的代码水平有限,也想在平时多积累一些写代码的经验,今天把东西放上来献丑了。。很希望有大侠对我的代码批评指正,不胜感激
标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com

文章转载自:博客园

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP