金喜正规买球

Silverlight五子棋游戏

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

概述:

      注册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;
&nbsp;           
this.ImgContainer = canvas;
            ImgContainer.MouseLeftButtonUp 
+= new MouseButtonEventHandler(ImgContainer_MouseLeftButtonUp);
        }

        
void ImgContainer_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
&nbsp;           
if (ObjectClick != null)
                ObjectClick(
this, e);
        }
    }
2.棋盘的初始化

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

        
public MainPage()
        {
            InitializeComponent();
            InitContainer();

        }

        
/// <summary>
        
/// 棋盘初始化
        
/// </summary>
        private void InitContainer()
        {
          &nbsp; 
//List<Canvas> listChild = new List<Canvas>();
          &nbsp; 
//List<List<Canvas>> listParent = new&nbsp;List<List<Canvas>>();
            index = 1;
     &nbsp;      
const int Point_Width = 40;
    ;        
const int Point_Height = 40;
            LayoutRoot.Children.Clear();

  &nbsp;   &nbsp;     
for (int i = 0; i < 15; i++)
            {
               &nbsp;
for (int j = 0; j < 15; j++)
                {
                    ChessObject chess 
= null;
             &nbsp;      Canvas canvas 
= new Canvas();
&nbsp;                   ImageBrush img 
= new ImageBrush();
                   ; ImageSource imgsource 
= new BitmapImage(FindImg(i, j));
                 ;   img.ImageSource 
= imgsource;
       &nbsp;            canvas.Background 
= img;
                    canvas.Width 
= Point_Width;
      &nbsp;            &nbsp;canvas.Height 
= Point_Height;
                    canvas.SetValue(Canvas.LeftProperty,&nbsp;Convert.ToDouble(j 
* Point_Width));
                  &nbsp; canvas.SetValue(Canvas.TopProperty, Convert.ToDouble(i 
* Point_Height));
       ;             
//canvas.MouseLeftButtonUp += new MouseButtonEventHandler(canvas_MouseLeftButtonUp);
    &nbsp;               chess = new ChessObject(canvas);
 ;    &nbsp;              chess.ObjectClick 
+= new RoutedEventHandler(chess_ObjectClick);
          &nbsp;         arrayChessChild[i, j] 
= chess;
                    LayoutRoot.Children.Add(arrayChessChild[i, j].ImgContainer);
                }
             }
        }
3.定义对象的单击事件


        
void chess_ObjectClick(object sender, RoutedEventArgs e)
        {
            
if (!(sender is ChessObject))
          &nbsp;     
return;

         &nbsp;  ChessObject chess 
= sender as ChessObject;
            
if (chess.IsFill)
             &nbsp;  
return;

      &nbsp;     
if (index % 2 == 1)
            {
           ;     ImageBrush ;img 
= new ImageBrush();
  &nbsp;             img.ImageSource 
= new BitmapImage(new Uri("pngs/black.png", UriKind.Relative));
       &nbsp;&nbsp;       chess.ImgContainer.Background 
= img;
                chess.IsPrevious 
= true;
            }
      &nbsp;   &nbsp; 
else
            {
  &nbsp;             ImageBrush img 
= new ImageBrush();
      ;     &nbsp;    img.ImageSource 
= new BitmapImage(new Uri("pngs/white.png", UriKind.Relative));
  &nbsp;             chess.ImgContainer.Background 
= img;
      &nbsp;&nbsp;        chess.IsPrevious 
= false;
            }
            chess.IsFill&nbsp;
= true;
     &nbsp;      
if (IsOver() == 1)
            {
            &nbsp;   System.Windows.Controls.ChildWindow childwindow 
= new ChildWindow();
         &nbsp;      childwindow.Content 
= "黑方获得比赛的胜利!";
         &nbsp; &nbsp;    childwindow.Title 
= "比赛结束";
   &nbsp;            childwindow.Closed 
+= new EventHandler(childwindow_Closed);
                childwindow.Show();
          &nbsp;     
//System.Windows.Browser.HtmlPage.Window.Alert("black success.");
                
            }
  &nbsp;         
else if (IsOver() == 0)
            {
               &nbsp;System.Windows.Controls.ChildWindow childwindow 
= new ChildWindow();
           ;   &nbsp; childwindow.Content 
= "白方获得比赛的胜利!";
           &nbsp;    childwindow.Title 
= "比赛结束";
              &nbsp; childwindow.Closed 
+= new EventHandler(childwindow_Closed);
                childwindow.Show();
                
//System.Windows.Browser.HtmlPage.Window.Alert("white success.");
                
//InitContainer();
            }
       &nbsp; &nbsp;  
else
&nbsp;            ;   index
++;
        }

        
void childwindow_Closed(object sender, EventArgs e)
        {
            InitContainer();
        }
4.判断游戏结束的逻辑

       /// <summary>
        
/// 判断游戏是否结束
        
/// </summary>
        
/// <returns></returns>
        private int IsOver()
        {
   &nbsp; ;       
for (int i = 0; i < 15; i++)
            {
          &nbsp;&nbsp;    
for (int j = 0; j < 15; j++)
                {
         &nbsp; &nbsp;        
if (arrayChessChild[i, j].IsFill)
                    {

                   ;     
if (arrayChessChild[i, j].IsPrevious)
                        {
            &nbsp;    &nbsp;          
int successCount = 1;
             &nbsp;          &nbsp;   
for (int z = 1; z < 5; z++)
                            {
                           &nbsp; &nbsp;  
if (i + z >= 15)
                           &nbsp;   &nbsp;    
break;
  &nbsp;             &nbsp;               
if ((!arrayChessChild[i + z, j].IsPrevious) || (!arrayChessChild[i + z, j].IsFill))
                                {
                          &nbsp;      &nbsp;  successCount 
= 1;
                                    
break;
                                }
    ;                    &nbsp;       
if (arrayChessChild[i + z, j].IsFill && arrayChessChild[i + z, j].IsPrevious)
       &nbsp;                       ;     successCount
++;
                                
if (successCount == 5)
       &nbsp;                            
return 1;
                            }
                   &nbsp;        successCount 
= 1;
            &nbsp;               
for (int z = 1; z < 5; z++)
                            {
    &nbsp;                           
if (i + z >= 15 || j + z >= 15)
                                    
break;
       &nbsp;       &nbsp;                
if ((!arrayChessChild[i + z, j + z].IsPrevious) || (!arrayChessChild[i + z, j + z].IsFill))
                                {
                                   &nbsp;successCount 
= 1;
                       ;             
break;
                                }
 &nbsp;                      &nbsp;       
if (arrayChessChild[i + z, j + z].IsFill && arrayChessChild[i + z, j + z].IsPrevious)
   &nbsp;                            &nbsp;   successCount
++;
             &nbsp;                  
if (successCount == 5)
                              &nbsp;     
return 1;
                            }
                    &nbsp;       successCount ;
= 1;
 &nbsp;                          
for (int z = 1; z < 5; z++)
                            {
          &nbsp;                     
if (j + z >= 15)
                          &nbsp;         
break;
                ;                
if ((!arrayChessChild[i, j + z].IsPrevious) || (!arrayChessChild[i, j + z].IsFill))
                                {
        ;                            successCount 
= 1;
    &nbsp;                               
break;
                                }
          &nbsp;   ;                  
if (arrayChessChild[i, j + z].IsFill && arrayChessChild[i, j + z].IsPrevious)
                         &nbsp;          successCount
++;
                           &nbsp;    
if (successCount == 5)
                                    
return 1;
                            }
                        }
&nbsp;                       
else
                        {
                   &nbsp;        
int successCount = 1;
           &nbsp;         &nbsp;      
for (int z = 1; z < 5; z++)
                            {
                            &nbsp;   
if (i + z >= 15)
         &nbsp;   &nbsp;                      
break;
               &nbsp;        &nbsp;       
if ((arrayChessChild[i + z, j].IsPrevious) || (!arrayChessChild[i + z, j].IsFill))
                                {
                                    successCount 
= 1;
                     &nbsp;              
break;
                                }
                      &nbsp;   &nbsp;     
if (arrayChessChild[i + z, j].IsFill && (!arrayChessChild[i + z, j].IsPrevious))
        ;                            successCount
++;
                      &nbsp;         
if (successCount == 5)
       &nbsp;                            
return 0;
                            }
                    ;        successCount 
= 1;
                            
for (int z = 1; z < 5; z++)
                            {
           &nbsp;                    
if (i + z >= 15 || j + z >= 15)
                           &nbsp;        
break;
             &nbsp;        &nbsp;         
if ((arrayChessChild[i + z, j + z].IsPrevious) || (!arrayChessChild[i + z, j + z].IsFill))
                                {
                                    successCount 
= 1;
&nbsp;                                   
break;
                                }
                  &nbsp;             
if (arrayChessChild[i + z, j + z].IsFill && (!arrayChessChild[i + z, j + z].IsPrevious))
                 &nbsp;                 &nbsp;successCount
++;
                   &nbsp;            
if (successCount == 5)
                        &nbsp;           
return 0;
                            }
      &nbsp;                     successCount 
= 1;
                           ; 
for (int z = 1; z < 5; z++)
                            {
                                
if (j + z >= 15)
            ;                        
break;
                  &nbsp;   ;          
if ((arrayChessChild[i, j + z].IsPrevious) || (!arrayChessChild[i, j + z].IsFill))
                                {
          &nbsp;                         successCount 
= 1;
          &nbsp;                         
break;
                                }
                          ;      
if (arrayChessChild[i, j + z].IsFill && (!arrayChessChild[i, j + z].IsPrevious))
                                   ; successCount
++;
         &nbsp;    ;                  
if (successCount == 5)
                   &nbsp;                
return 0;
                            }
                        }
                    }
                }
            }
   &nbsp;        
return -1;
        }
5.还有一个初始化棋盘的函数

     /// <summary>
        
/// 棋盘图片填充
        
/// </summary>
        
/// <param name="i"></param>
        
/// <param name="j"></param>
        
/// <returns></returns>
        public Uri FindImg(int i, int j)
        {
          &nbsp; Uri uri 
= null;
       &nbsp;    
if (i == 0 && j == 0)
     &nbsp;          uri 
= new Uri("pngs/lefttop.png", UriKind.Relative);
&nbsp;      &nbsp;    
else if (i == 0 && j == 14)
             &nbsp;  uri 
= new Uri("pngs/righttop.png", UriKind.Relative);
     ;       
else if (i == 14 && j == 0)
    &nbsp;           uri 
= new Uri("pngs/leftbottom.png", UriKind.Relative);
      &nbsp;     
else if (i == 14 && j == 14)
&nbsp;               uri 
= new Uri("pngs/rightbottom.png", UriKind.Relative);
&nbsp;      ;     
else if (j == 0)
                uri 
= new Uri("pngs/left.png", UriKind.Relative);
        &nbsp;   ;
else if (i == 0)
     ;       &nbsp;   uri 
= new Uri("pngs/top.png", UriKind.Relative);
     ;     ;  
else if (i == 14)
  ; &nbsp;            uri 
= new Uri("pngs/bottom.png", UriKind.Relative);
            
else if (j == 14)
            &nbsp;   uri 
= new Uri("pngs/right.png", UriKind.Relative);
            ;
else
&nbsp;            &nbsp;  uri 
= new Uri("pngs/center.png", UriKind.Relative);
  &nbsp;         
return uri;
        }
鉴于本小虫水平有限,说不出什么大道理,代码就简单介绍到这。
本人的代码水平有限,也想在平时多积累一些写代码的经验,今天把东西放上来献丑了。。很希望有大侠对我的代码批评指正,不胜感激
标签:

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

文章转载自:博客园

为你推荐

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


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP