使用C#实现中国象棋棋子走法的判定

使用以下代码请遵循协议.

 

将和帅的走法实现:

ExpandedBlockStart.gif King
class  King : PieceBase
{
    
public   override   bool  CanMoveTo(Point coordinate,  char [,] boardstatus)
    {
        
if  (Math.Pow( this .Coordinate.X  -  coordinate.X,  2 +  Math.Pow( this .Coordinate.Y  -  coordinate.Y,  2 !=   1 )
            
return   false ;

        
if  (coordinate.X  >   5   |  coordinate.X  <   3 )
            
return   false ;
        
if  (coordinate.Y  >   2   &  coordinate.Y  <   7 )
            
return   false ;

        
return   true ;
    }
}

 

士和仕的走法实现:

ExpandedBlockStart.gif Advisor
class  Advisor : PieceBase
{
    
public   override   bool  CanMoveTo(Point coordinate,  char [,] boardstatus)
    {
        
if  (Math.Pow( this .Coordinate.X  -  coordinate.X,  2 +  Math.Pow( this .Coordinate.Y  -  coordinate.Y,  2 !=   2 )
            
return   false ;

        
if  (coordinate.X  >   5   |  coordinate.X  <   3 )
            
return   false ;
        
if  (coordinate.Y  >   2   &  coordinate.Y  <   7 )
            
return   false ;

        
return   true ;
    }
}

 

象和相的走法实现:  

ExpandedBlockStart.gif Elephant
class  Elephant : PieceBase
{
    
public   override   bool  CanMoveTo(Point coordinate,  char [,] boardstatus)
    {
        
if  (Math.Pow( this .Coordinate.X  -  coordinate.X,  2 +  Math.Pow( this .Coordinate.Y  -  coordinate.Y,  2 !=   8 )
            
return   false ;

        
if  ( this .Side  ==  PieceSide.Red  &  coordinate.Y  <   5 ) {  return   false ; }
        
if  ( this .Side  ==  PieceSide.Black  &  coordinate.Y  >   4 ) {  return   false ; }

        
int  X  =  ( this .Coordinate.X  +  coordinate.X)  /   2 ;
        
int  Y  =  ( this .Coordinate.Y  +  coordinate.Y)  /   2 ;

        
if  (boardstatus[X, Y]  !=   ' 0 ' ) {  return   false ; }

        
return   true ;
    }
}

 

马的走法实现:  

ExpandedBlockStart.gif Knight
class  Knight : PieceBase
{
    
public   override   bool  CanMoveTo(Point coordinate,  char [,] boardstatus)
    {
        
if  (Math.Pow( this .Coordinate.X  -  coordinate.X,  2 +  Math.Pow( this .Coordinate.Y  -  coordinate.Y,  2 !=   5 )
            
return   false ;

        
int  X  =   this .Coordinate.X  +  (Math.Abs(coordinate.X  -   this .Coordinate.X)  -   1 *  (coordinate.X  -   this .Coordinate.X)  /  Math.Abs(coordinate.X  -   this .Coordinate.X);
        
int  Y  =   this .Coordinate.Y  +  (Math.Abs(coordinate.Y  -   this .Coordinate.Y)  -   1 *  (coordinate.Y  -   this .Coordinate.Y)  /  Math.Abs(coordinate.Y  -   this .Coordinate.Y);

        
if  (boardstatus[X, Y]  !=   ' 0 ' ) {  return   false ; }
        
        
return   true ;
    }
}

 

車的走法实现:  

ExpandedBlockStart.gif Rook
class  Rook : PieceBase
{
    
public   override   bool  CanMoveTo(Point coordinate,  char [,] boardstatus)
    {
        
if  (( this .Coordinate.X  !=  coordinate.X)  &  ( this .Coordinate.Y  !=  coordinate.Y))
            
return   false ;

        
int  d  =  Math.Abs( this .Coordinate.X  -  coordinate.X)  +  Math.Abs( this .Coordinate.Y  -  coordinate.Y);

        
for  ( int  i  =   1 ; i  <  d; i ++ )
        {
            
int  X  =   this .Coordinate.X  +  (coordinate.X  -   this .Coordinate.X)  /  d  *  i;
            
int  Y  =   this .Coordinate.Y  +  (coordinate.Y  -   this .Coordinate.Y)  /  d  *  i;

            
if  (boardstatus[X, Y]  !=   ' 0 ' ) {  return   false ; }
        }

        
return   true ;
    }
}

 

炮的走法实现: 

ExpandedBlockStart.gif Cannon
class  Cannon : PieceBase
{
    
public   override   bool  CanMoveTo(Point coordinate,  char [,] boardstatus)
    {
        
if  (( this .Coordinate.X  !=  coordinate.X)  &  ( this .Coordinate.Y  !=  coordinate.Y))
            
return   false ;

        
int  d  =  Math.Abs( this .Coordinate.X  -  coordinate.X)  +  Math.Abs( this .Coordinate.Y  -  coordinate.Y);
        
bool  flag  =   false ;

        
for  ( int  i  =   1 ; i  <  d; i ++ )
        {
            
int  X  =   this .Coordinate.X  +  (coordinate.X  -   this .Coordinate.X)  /  d  *  i;
            
int  Y  =   this .Coordinate.Y  +  (coordinate.Y  -   this .Coordinate.Y)  /  d  *  i;

            
if  (boardstatus[X, Y]  !=   ' 0 ' )
            {
                
if  (flag  ==   true ) {  return   false ; }  else  { flag  =   true ; }
            }
        }
        
if  (boardstatus[coordinate.X, coordinate.Y]  ==   ' 0 '   &  flag  ==   true ) {  return   false ; }
        
else   if  (boardstatus[coordinate.X, coordinate.Y]  !=   ' 0 '   &  flag  ==   false ) {  return   false ; }
     
        
return   true ;
    }
}

 

卒和兵的走法实现: 

ExpandedBlockStart.gif Pawn
class  Pawn : PieceBase
{
    
public   override   bool  CanMoveTo(Point coordinate,  char [,] boardstatus)
    {
        
if  (Math.Pow( this .Coordinate.X  -  coordinate.X,  2 +  Math.Pow( this .Coordinate.Y  -  coordinate.Y,  2 !=   1 )
            
return   false ;

        
if  ( this .Side  ==  PieceSide.Red)
        {
            
if  ( this .Coordinate.Y  >   4   &   this .Coordinate.X  !=  coordinate.X) {  return   false ; }
            
if  ( this .Coordinate.Y  <  coordinate.Y) {  return   false ; }
        }
        
else
        {
            
if  ( this .Coordinate.Y  <   5   &   this .Coordinate.X  !=  coordinate.X) {  return   false ; }
            
if  ( this .Coordinate.Y  >  coordinate.Y) {  return   false ; }
        }
 
        
return   true ;
    }
}

 

 

转载于:https://www.cnblogs.com/yang_sq/archive/2009/12/29/1634830.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值