Esempio n. 1
0
        private void NotifyAboutCheckMate(Player initiator, Player opponent, ChessPoint from, ChessPoint to)
        {
            var field = _virtualField.CloneMatrix();

            _turnColor = _turnColor.Invert();
            var winner = initiator.PlayerColor;

            var winnerGameInfo = new WcfGameInfo(
                initiator.PlayerColor,
                opponent.PlayerName,
                field.ToJaggedArray(),
                _turnColor,
                winner);

            var loserGameInfo = new WcfGameInfo(
                initiator.PlayerColor,
                opponent.PlayerName,
                field.ToJaggedArray(),
                _turnColor,
                winner);

            initiator.Callback.GameHasEnded(winnerGameInfo, from.FromBusiness(), to.FromBusiness());
            opponent.Callback.GameHasEnded(loserGameInfo, from.FromBusiness(), to.FromBusiness());

            GameEnded?.Invoke(this, EventArgs.Empty);
        }
Esempio n. 2
0
        /// <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);
        }