Beispiel #1
0
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            BoardPosition      boardPosition      = (BoardPosition)values[1];
            ChessPiecePosition chessPiecePosition = (ChessPiecePosition)values[0];
            ChessViewModel     chessViewModel     = (ChessViewModel)values[2];
            bool isSelected = (bool)values[3];
            bool isHovered  = (bool)values[4];

            if (isSelected)
            {
                return(Red);
            }
            if (isHovered)
            {
                return(Red);
            }
            if (chessViewModel.IsCheck && chessPiecePosition.PieceType == ChessPieceType.King && chessPiecePosition.Player == chessViewModel.CurrentPlayer)
            {
                return((object)ChessSquareColorConverter.Yellow);
            }
            if ((boardPosition.Row + boardPosition.Col) % 2 != 0)
            {
                return(White);
            }
            return(Black);
        }
Beispiel #2
0
        private void CheckPromotionMoves()
        {
            var       possMoves     = mBoard.GetPossibleMoves() as IEnumerable <ChessMove>;
            ChessMove promotionMove = (ChessMove)mBoard.MoveHistory.Last();

            if (promotionMove.Piece.PieceType == ChessPieceType.Pawn && (promotionMove.EndPosition.Row == 0 || promotionMove.EndPosition.Row == 7))
            {
                IsPawnPromotion = true;
                possMoves       = mBoard.GetPossibleMoves() as IEnumerable <ChessMove>;

                List <ChessPiecePosition> pawnMoves = new List <ChessPiecePosition>();
                var queen    = new ChessPiecePosition(ChessPieceType.Queen, CurrentPlayer);
                var knight   = new ChessPiecePosition(ChessPieceType.Knight, CurrentPlayer);
                var rookPawn = new ChessPiecePosition(ChessPieceType.RookPawn, CurrentPlayer);
                var bioshop  = new ChessPiecePosition(ChessPieceType.Bishop, CurrentPlayer);
                pawnMoves.Add(queen);
                pawnMoves.Add(knight);
                pawnMoves.Add(rookPawn);
                pawnMoves.Add(bioshop);

                mPromotionMoves = new ObservableCollection <ChessSquare>();
                for (int pMoves = 0; pMoves < 4; pMoves++)
                {
                    mPromotionMoves.Add(new ChessSquare());
                    mPromotionMoves[pMoves].CurrentPlayer = CurrentPlayer;
                    mPromotionMoves[pMoves].Piece         = pawnMoves[pMoves];
                    mPromotionMoves[pMoves].IsSelected    = false;
                }

                PossibleMoves = new HashSet <ChessMove>(
                    from ChessMove m in mBoard.GetPossibleMoves()
                    select m
                    );
            }
        }
Beispiel #3
0
        private void RebindState(ChessPiecePosition piece)
        {
            PossibleMoves = new HashSet <ChessMove>(
                from ChessMove m in mBoard.GetPossibleMoves()
                select m
                );

            var newSquares =
                from r in Enumerable.Range(0, 8)
                from c in Enumerable.Range(0, 8)
                select new BoardPosition(r, c);
            int i = 0;

            foreach (var pos in newSquares)
            {
                mSquares[i].CurrentPlayer = mBoard.GetPieceAtPosition(pos).Player;
                mSquares[i].Piece         = piece;
                mSquares[i].IsSelected    = false;
                i++;
            }

            OnPropertyChanged(nameof(BoardValue));
            OnPropertyChanged(nameof(CurrentPlayer));
            OnPropertyChanged(nameof(CanUndo));
            OnPropertyChanged(nameof(IsPawnPromotion));
        }
Beispiel #4
0
        public ChessViewModel()
        {
            mBoard   = new ChessBoard();
            mSquares = new ObservableCollection <ChessSquare>(
                from pos in (
                    from r in Enumerable.Range(0, 8)
                    from c in Enumerable.Range(0, 8)
                    select new BoardPosition(r, c)
                    )
                select new ChessSquare()
            {
                Position      = pos,
                CurrentPlayer = mBoard.GetPieceAtPosition(pos).Player,
                Piece         = mBoard.GetPieceAtPosition(pos),
                ViewModel     = this
            }
                );



            PossibleMoves = new HashSet <ChessMove>(
                from ChessMove m in mBoard.GetPossibleMoves()
                select m
                );

            var possMoves = mBoard.GetPossibleMoves() as IEnumerable <ChessMove>;

            List <ChessPiecePosition> pawnMoves = new List <ChessPiecePosition>();
            var queen    = new ChessPiecePosition(ChessPieceType.Queen, CurrentPlayer);
            var knight   = new ChessPiecePosition(ChessPieceType.Knight, CurrentPlayer);
            var rookPawn = new ChessPiecePosition(ChessPieceType.RookPawn, CurrentPlayer);
            var bioshop  = new ChessPiecePosition(ChessPieceType.Bishop, CurrentPlayer);

            pawnMoves.Add(queen);
            pawnMoves.Add(knight);
            pawnMoves.Add(rookPawn);
            pawnMoves.Add(bioshop);

            mPromotionMoves = new ObservableCollection <ChessSquare>();
            for (int pMoves = 0; pMoves < 4; pMoves++)
            {
                mPromotionMoves.Add(new ChessSquare());
                mPromotionMoves[pMoves].CurrentPlayer = CurrentPlayer;
                mPromotionMoves[pMoves].Piece         = pawnMoves[pMoves];
                mPromotionMoves[pMoves].IsSelected    = false;
            }
        }
Beispiel #5
0
        public object Convert(object values, Type targetType, object parameter, CultureInfo culture)
        {
            ChessPiecePosition piece = (ChessPiecePosition)values;

            if (piece.Player == 1)
            {
                string src = piece.PieceType.ToString();
                return(new Image()
                {
                    Source = new BitmapImage(new Uri("/Cecs475.BoardGames.Chess.View;component/Resources/White" + src + ".png", UriKind.Relative))
                });
            }
            else
            {
                string src = piece.PieceType.ToString();
                return(new Image()
                {
                    Source = new BitmapImage(new Uri("/Cecs475.BoardGames.Chess.View;component/Resources/Black" + src + ".png", UriKind.Relative))
                });
            }
        }
Beispiel #6
0
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            ChessPiecePosition chessPiecePosition = (ChessPiecePosition)value;

            switch (chessPiecePosition.PieceType)
            {
            case ChessPieceType.Empty:
                return(null);

            case ChessPieceType.RookQueen:
                return(new BitmapImage(new Uri(string.Format("/Cecs475.BoardGames.Chess.View;component/Resources/{0}Rook.png", Player(chessPiecePosition.Player)), UriKind.Relative)));

            case ChessPieceType.RookKing:
                return(new BitmapImage(new Uri(string.Format("/Cecs475.BoardGames.Chess.View;component/Resources/{0}Rook.png", Player(chessPiecePosition.Player)), UriKind.Relative)));

            case ChessPieceType.RookPawn:
                return(new BitmapImage(new Uri(string.Format("/Cecs475.BoardGames.Chess.View;component/Resources/{0}Rook.png", Player(chessPiecePosition.Player)), UriKind.Relative)));

            case ChessPieceType.Bishop:
                return(new BitmapImage(new Uri(string.Format("/Cecs475.BoardGames.Chess.View;component/Resources/{0}Bishop.png", Player(chessPiecePosition.Player)), UriKind.Relative)));

            case ChessPieceType.Knight:
                return(new BitmapImage(new Uri(string.Format("/Cecs475.BoardGames.Chess.View;component/Resources/{0}Knight.png", Player(chessPiecePosition.Player)), UriKind.Relative)));

            case ChessPieceType.Queen:
                return(new BitmapImage(new Uri(string.Format("/Cecs475.BoardGames.Chess.View;component/Resources/{0}Queen.png", Player(chessPiecePosition.Player)), UriKind.Relative)));

            case ChessPieceType.King:
                return(new BitmapImage(new Uri(string.Format("/Cecs475.BoardGames.Chess.View;component/Resources/{0}King.png", Player(chessPiecePosition.Player)), UriKind.Relative)));

            case ChessPieceType.Pawn:
                return(new BitmapImage(new Uri(string.Format("/Cecs475.BoardGames.Chess.View;component/Resources/{0}Pawn.png", Player(chessPiecePosition.Player)), UriKind.Relative)));

            default:
                //return new BitmapImage(new Uri("/Cecs475.BoardGames.Chess.View;component/Resources/BlackRook.png", UriKind.Relative));
                return(null);
            }
        }
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            BoardPosition      pos    = (BoardPosition)values[0];
            ChessPiecePosition piece  = (ChessPiecePosition)values[1];
            bool           isSelected = (bool)values[2];
            bool           isHovered  = (bool)values[3];
            ChessViewModel viewModel  = (ChessViewModel)values[4];

            if (viewModel.SelectedSquare != null && isHovered)
            {
                return(RED_BRUSH);
            }

            if (isSelected)
            {
                return(RED_BRUSH);
            }

            if (isHovered)
            {
                return(GREEN_BRUSH);
            }

            if (viewModel.IsCheck && piece.Player == viewModel.CurrentPlayer && piece.PieceType == ChessPieceType.King)
            {
                return(YELLOW_BRUSH);
            }

            if ((pos.Row + pos.Col) % 2 == 0)
            {
                return(PATTERN_ONE);
            }
            else
            {
                return(PATTERN_TWO);
            }
        }
Beispiel #8
0
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            try {
                ChessPiecePosition piece = (ChessPiecePosition)value;

                if (piece.PieceType == ChessPieceType.Empty)
                {
                    return(null);
                }

                string player;
                string pieceType;

                if (piece.Player == 1)
                {
                    player = "white";
                }
                else
                {
                    player = "black";
                }

                switch (piece.PieceType)
                {
                case ChessPieceType.Pawn:
                    pieceType = "pawn";
                    break;

                case ChessPieceType.RookKing:
                    pieceType = "rook";
                    break;

                case ChessPieceType.RookQueen:
                    pieceType = "rook";
                    break;

                case ChessPieceType.RookPawn:
                    pieceType = "rook";
                    break;

                case ChessPieceType.Knight:
                    pieceType = "knight";
                    break;

                case ChessPieceType.Bishop:
                    pieceType = "bishop";
                    break;

                case ChessPieceType.Queen:
                    pieceType = "queen";
                    break;

                case ChessPieceType.King:
                    pieceType = "king";
                    break;

                default:
                    pieceType = "";
                    break;
                }

                string src = player + "_" + pieceType;

                return(new BitmapImage(new Uri("/Cecs475.BoardGames.Chess.View;component/Resources/" + src + ".png", UriKind.Relative)));
            }
            catch (Exception e) {
                return(null);
            }
        }