Beispiel #1
0
        public static bool IsKingInCheck(Board board, PieceColor color)
        {
            // Is there a king on the board for this color? (There might not be
            // when this is used for constructions or unit tests)
            var pieces     = color == PieceColor.Black ? board.BlackPieces : board.WhitePieces;
            var kingPieces = (from p in pieces where p.Type == PieceType.King select p).ToList();

            if (kingPieces.Count == 0)
            {
                return(false);
            }
            var king = kingPieces[0];

            var moves       = board.GetPossibleMoves(color.Opposite(), true);
            var takingMoves = (from m in moves
                               where m.PieceCaptured &&
                               m.Type == MoveType.NormalPiece
                               select m.NormalPieceMove).ToList();

            var takeKingMoves = (from m in takingMoves
                                 where m.NewPosition.Equals(king.Position)
                                 select m).ToList();

            return(takeKingMoves.Count > 0);
        }
        private List <Vector2> GetThreatPositionsForColor(PieceColor color, Board.Board board)
        {
            var threatPositionsForColor = new List <Vector2>();

            foreach (var data in board.ChessPieceData.Where(d => d.Color == color.Opposite()))
            {
                threatPositionsForColor.AddRange(GetThreatMoves(data.Position, board).Select(m => m.To));
            }

            return(threatPositionsForColor);
        }
Beispiel #3
0
        // check — это шах
        private static bool IsCheckFor(PieceColor pieceColor)
        {
            foreach (var destination in GetAllMoves(pieceColor.Opposite()))
            {
                if (Piece.Is(board.GetPiece(destination),
                             pieceColor, PieceType.King))
                {
                    return(true);
                }
            }

            return(false);
        }
Beispiel #4
0
        protected List <Move> GetPossibleMovesInDirection(Vector2 startingPosition, PieceColor color, Board board, Vector2 direction)
        {
            var moves         = new List <Move>();
            var checkPosition = startingPosition + direction;

            while (board.IsPositionAvailableFor(color, checkPosition))
            {
                if (board.IsPositionOccupiedByColor(checkPosition, color.Opposite()))
                {
                    moves.Add(new KillingMove(startingPosition, checkPosition, board.GetPieceData(startingPosition).Type, color, checkPosition));
                    break;
                }
                else
                {
                    moves.Add(new Move(startingPosition, checkPosition, board.GetPieceData(startingPosition).Type, color));
                }


                checkPosition += direction;
            }

            return(moves);
        }
 public void Concede()
 {
     _signalBus.Fire(new EndGameSignal(_color.Opposite()));
 }
Beispiel #6
0
 private void ChangePlayer()
 {
     _activePlayerColor = _activePlayerColor.Opposite();
 }
Beispiel #7
0
        /// <summary>
        /// Start playground in a regular fashion when a match is known
        /// </summary>
        private async void StartRegular(MatchEntity match)
        {
            // which player are we?
            playerColor = match.WhitePlayer == Auth.Player
                ? PieceColor.White : PieceColor.Black;

            // who is the opponent
            UnisavePlayer opponentPlayer = playerColor.IsWhite()
                ? match.BlackPlayer : match.WhitePlayer;

            // connect to the opponent
            if (opponentPlayer == null)
            {
                // AI
                Debug.Log("Starting computer opponent...");
                opponent = new ComputerOpponent(
                    playerColor.Opposite(),
                    board,
                    match.Duration
                    );
            }
            else
            {
                // Real person
                Debug.Log("Connecting to photon opponent...");
                opponent = PhotonOpponent.CreateInstance(match.EntityId);
            }

            // create and setup the board
            board.CreateBoard(
                playerColor.IsWhite(),
                match.WhitePlayerSet,
                match.BlackPlayerSet
                );

            // create the clock
            clock = new Clock(match.Duration);

            // register opponent events
            opponent.OnMoveFinish += OpponentsMoveWasFinished;
            opponent.OnGiveUp     += OnOpponentGaveUp;
            opponent.OnOutOfTime  += OnOpponentTimeOver;

            // wait for the opponent
            Debug.Log("Waiting for the opponent...");
            await opponent.WaitForReady();

            // === start the game ===

            // if we are white, we are the one to start
            Debug.Log("Game has started.");
            if (playerColor.IsWhite())
            {
                PerformOurMove();
            }
            else
            {
                // it's the opponents turn so just wait
                clock.StartOpponent();
            }
        }