/// <summary> /// Check for check mate. /// </summary> /// <param name="initiatorColor">Initiator color.</param> /// <param name="field">Game field.</param> /// <param name="masterFactory">Master factory implementation.</param> /// <param name="isCheck">Is check on table.</param> /// <returns>Checkmate flag.</returns> public static bool IsCheckMate( PlayerColor initiatorColor, VirtualField field, IPieceMasterFactory masterFactory, out bool isCheck) { isCheck = false; // Check for check now if (IsCheck(initiatorColor, field, masterFactory)) { isCheck = true; var opponentColor = initiatorColor.Invert(); // Otherwise do all possible movements, in the case of no possibility to prevent being attacked = initiator wins foreach (var opponmentPiecePoint in FindPieces(p => p.GetPlayerColor() == opponentColor, field)) { PieceMasterBase master; if (masterFactory.TryGetMaster(field, opponmentPiecePoint, out master)) { foreach (var movement in master.GetMovements()) { var fieldCopy = new VirtualField(field.CloneMatrix()); if (fieldCopy[movement].GetPlayerColor() == fieldCopy[opponmentPiecePoint].GetPlayerColor()) { // Step on free space var temp = fieldCopy[opponmentPiecePoint]; fieldCopy[opponmentPiecePoint] = fieldCopy[movement]; fieldCopy[movement] = temp; } else { // Eat opponent piece fieldCopy[movement] = fieldCopy[opponmentPiecePoint]; fieldCopy[opponmentPiecePoint] = ChessPiece.Empty; } if (!IsCheck(initiatorColor, fieldCopy, masterFactory)) { return(false); } } } } // Passed all steps and no possibilities to prevent a check return(true); } return(false); }
/// <summary> /// Get all points under attack by initiator. /// </summary> /// <param name="initiatorColor">Initiator color.</param> /// <param name="field">Game field.</param> /// <param name="masterFactory">Masters factory.</param> /// <returns></returns> public static IEnumerable <ChessPoint> UnderAttackPoints( PlayerColor initiatorColor, VirtualField field, IPieceMasterFactory masterFactory) { var movements = new HashSet <ChessPoint>(); foreach (var point in FindPieces(piece => piece.GetPlayerColor() == initiatorColor, field)) { PieceMasterBase master; if (masterFactory.TryGetMaster(field, point, out master)) { foreach (var movement in master.GetRealMovements()) { movements.Add(movement); } } } return(movements); }
private void CheaterCheck(Player initiator, ChessPoint from, ChessPoint to) { var color = initiator.PlayerColor; var piece = _virtualField[from.X, from.Y]; var pieceColor = piece.GetPlayerColor(); if (!pieceColor.HasValue) { throw new FaultException <InvalidMovementException>( new InvalidMovementException(from.X, from.Y, "You're trying move empty field")); } if (color != pieceColor.Value) { throw new FaultException <CheaterException>( new CheaterException("You're trying to move your opponent piece")); } if (color != _turnColor) { throw new FaultException <CheaterException>( new CheaterException("You're trying to move your piece in opponent turn")); } PieceMasterBase master; if (!_masterFactory.TryGetMaster(_virtualField, from, out master)) { throw new FaultException <InvalidMovementException>( new InvalidMovementException(from.X, from.Y, "You're trying move unknown piece without master")); } if (master.GetMovements().All(step => !Equals(step, to))) { throw new FaultException <CheaterException>( new CheaterException("You're trying to move your piece to incorrect position")); } var fieldCopy = new VirtualField(_virtualField.CloneMatrix()) {