Пример #1
0
 /// <summary>
 /// Check if any other teammates can block the checking path to protect the king
 /// </summary>
 /// <returns><c>true</c>, if for any cells in the checking path, there is a
 /// teammate which can move to that cell to protect the king,
 /// <c>false</c> otherwise.
 /// </returns>
 /// <param name="opponent">opponent player.</param>
 /// <param name="b">game board.</param>
 public bool CanBlockMate(Player opponent, Board b)
 {
     //if checking piece is a Pawn, or King, or Knight, return false
     if (_pieceChecking.GetType() == typeof(Knight) || _pieceChecking.GetType() == typeof(Pawn) || _pieceChecking.GetType() == typeof(King))
     {
         return(false);
     }
     foreach (Cell c in CheckingPath(opponent))
     {
         Piece tempPiece = c.Piece;
         foreach (Piece p in opponent.Opponent.Pieces)
         {
             //if the piece mentioned is not the King and the cell in the checking path is
             //a possible move of that piece
             if (p.GetType() != typeof(King) && c.isPossibleMoveOf(p, b))
             {
                 Cell temp = p.Cell;
                 //if the cell in the checking path is not holding any piece
                 if (c.Piece == null)
                 {
                     p.Cell.RemovePiece(opponent.Opponent);
                     c.Piece = p;
                     if (!isChecked(opponent, b))
                     {
                         c.RemovePiece(opponent.Opponent);
                         temp.Piece = p;
                         return(true);
                     }
                     c.RemovePiece(opponent.Opponent);
                     temp.Piece = p;
                 }
                 else
                 {
                     tempPiece.Cell.RemovePiece(opponent);
                     p.Cell.RemovePiece(opponent.Opponent);
                     c.Piece = p;
                     if (!isChecked(opponent, b, tempPiece))
                     {
                         c.RemovePiece(opponent.Opponent);
                         c.Piece    = tempPiece;
                         temp.Piece = p;
                         return(true);
                     }
                     c.RemovePiece(opponent.Opponent);
                     c.Piece    = tempPiece;
                     temp.Piece = p;
                 }
             }
         }
     }
     return(false);
 }
Пример #2
0
 /// <summary>
 /// Key of the dictionary
 /// </summary>
 /// <returns>The string representation of a piece</returns>
 /// <param name="p">the Piece</param>
 public string Key(Piece p)
 {
     foreach (string s in _pieceRegistry.Keys)
     {
         if (_pieceRegistry [s] == p.GetType())
         {
             return(s);
         }
     }
     return(null);
 }
Пример #3
0
        /// <summary>
        /// Converts the piece to string representation.
        /// </summary>
        /// <returns>string representation of a piece</returns>
        /// <param name="p">The Piece</param>
        public string ConvertPieceToString(Piece p)
        {
            Dictionary <Type, string> pieceEquivalent = new Dictionary <Type, string> ();

            pieceEquivalent.Add(typeof(Pawn), "Pawn");
            pieceEquivalent.Add(typeof(Rook), "Rook");
            pieceEquivalent.Add(typeof(Knight), "Knight");
            pieceEquivalent.Add(typeof(Bishop), "Bishop");
            pieceEquivalent.Add(typeof(Queen), "Queen");
            pieceEquivalent.Add(typeof(King), "King");
            return(pieceEquivalent [p.GetType()]);
        }