天天看点

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

使用以下代码请遵循

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

协议.

将和帅的走法实现:

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 ;

    }

}

士和仕的走法实现:

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 ;

    }

}

象和相的走法实现:  

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 ;

    }

}

马的走法实现:  

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 ;

    }

}

車的走法实现:  

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 ;

    }

}

炮的走法实现: 

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 ;

    }

}

卒和兵的走法实现: 

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