private void RebindState()
        {
            Queen  = new ChessPiece(ChessPieceType.Queen, CurrentPlayer);
            Bishop = new ChessPiece(ChessPieceType.Bishop, CurrentPlayer);
            Knight = new ChessPiece(ChessPieceType.Knight, CurrentPlayer);
            Rook   = new ChessPiece(ChessPieceType.Rook, CurrentPlayer);
            // Rebind the possible moves, now that the board has changed.
            PossibleMoves = new HashSet <ChessMove>(
                from ChessMove m in mBoard.GetPossibleMoves()
                select m
                );

            // Update the collection of squares by examining the new board state.
            var newSquares = BoardPosition.GetRectangularPositions(8, 8);
            int i          = 0;

            foreach (var pos in newSquares)
            {
                mSquares[i].Chess_Piece = mBoard.GetPieceAtPosition(pos);
                i++;
            }
            foreach (var square in mSquares)
            {
                ChessPiece square_piece = square.Chess_Piece;
                square.IsChecked = false;
                if (square_piece.PieceType.Equals(ChessPieceType.King) && square_piece.Player == mBoard.CurrentPlayer && mBoard.IsCheck)
                {
                    square.IsChecked = true;
                }
            }
            OnPropertyChanged(nameof(BoardAdvantage));
            OnPropertyChanged(nameof(CurrentPlayer));
            OnPropertyChanged(nameof(CanUndo));
        }
        public ChessViewModel()
        {
            //SelectedPiece = false;
            mBoard = new ChessBoard();

            // Initialize the squares objects based on the board's initial state.
            mSquares = new ObservableCollection <ChessSquare>(
                BoardPosition.GetRectangularPositions(8, 8)
                .Select(pos => new ChessSquare()
            {
                Position = pos,
                Player   = mBoard.GetPieceAtPosition(pos)
            })
                );

            // Initialize the squares objects based on the board's initial state.
            mPawnPromotionPieces = new ObservableCollection <ChessSquare>();

            mPawnPromotionPieces.Add(new ChessSquare(new ChessPiece(ChessPieceType.Knight, 1)));
            mPawnPromotionPieces.Add(new ChessSquare(new ChessPiece(ChessPieceType.Bishop, 1)));
            mPawnPromotionPieces.Add(new ChessSquare(new ChessPiece(ChessPieceType.Rook, 1)));
            mPawnPromotionPieces.Add(new ChessSquare(new ChessPiece(ChessPieceType.Queen, 1)));

            if (SelectedPiece == false)
            {
                PossibleMoves = new HashSet <BoardPosition>(
                    from ChessMove m in mBoard.GetPossibleMoves()
                    select m.StartPosition
                    );
            }
        }
Пример #3
0
        private void RebindState()
        {
            // Rebind the possible moves, now that the board has changed.
            PossibleMoves = new HashSet <ChessMove>(
                from ChessMove m in mBoard.GetPossibleMoves()
                select m
                );

            // Update the collection of squares by examining the new board state.
            var newSquares = BoardPosition.GetRectangularPositions(8, 8);
            int i          = 0;

            foreach (var pos in newSquares)
            {
                var piece = mBoard.GetPieceAtPosition(pos);
                mSquares[i].ChessPiece = piece;
                if (piece.PieceType == ChessPieceType.King && mBoard.IsCheck && piece.Player == mBoard.CurrentPlayer)
                {
                    mSquares[i].IsInCheck = true;
                }
                else
                {
                    mSquares[i].IsInCheck = false;
                }
                mSquares[i].IsSelected = false;
                i++;
            }
            mCurrentlySelected = null;
            OnPropertyChanged(nameof(BoardAdvantage));
            OnPropertyChanged(nameof(CurrentPlayer));
            OnPropertyChanged(nameof(CanUndo));
        }
Пример #4
0
        public ChessViewModel()
        {
            mBoard = new ChessBoard();
            // Initialize the squares objects based on the board's initial state.
            mSquares = new ObservableCollection <ChessSquare>(
                BoardPosition.GetRectangularPositions(8, 8)
                .Select(pos => new ChessSquare()
            {
                Position   = pos,
                Player     = mBoard.GetPieceAtPosition(pos),
                PlayerTurn = mBoard.GetPlayerAtPosition(pos)
            })
                );

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

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

            PossibleMoves = mBoard.GetPossibleMoves();
        }
Пример #5
0
        private void RebindState()
        {
            // Rebind the possible moves, now that the board has changed.
            PossibleMoves = mBoard.GetPossibleMoves();

            // Update the collection of squares by examining the new board state.
            var newSquares = BoardPosition.GetRectangularPositions(8, 8);
            int i          = 0;

            foreach (var pos in newSquares)
            {
                mSquares[i].Player   = mBoard.GetPlayerAtPosition(pos);
                mSquares[i].Position = pos;
                mSquares[i].Piece    = mBoard.GetPieceAtPosition(pos);
                if (pos == mBoard.GetKingPosition(CurrentPlayer) && mBoard.IsCheck)
                {
                    mSquares[i].IsInCheck = true;
                }
                else
                {
                    mSquares[i].IsInCheck = false;
                }
                i++;
            }
            OnPropertyChanged(nameof(BoardAdvantage));
            OnPropertyChanged(nameof(CurrentPlayer));
            OnPropertyChanged(nameof(CanUndo));
            OnPropertyChanged(nameof(CanEnable));
        }
Пример #6
0
        /// <summary>
        /// Rebind possible moves and update all squares
        /// </summary>
        private void RebindState()
        {
            PossMoves = mBoard.GetPossibleMoves();
            EndMoves = new HashSet<BoardPosition>();
            StartMoves = new HashSet<BoardPosition>(
                from ChessMove m in mBoard.GetPossibleMoves()
                select m.StartPosition
            );

            var newSquares = BoardPosition.GetRectangularPositions(8, 8);
            int i = 0;
            foreach (var pos in newSquares)
            {
                ChessSquare cs = mSquares[i];
                cs.Player = mBoard.GetPlayerAtPosition(pos);
                cs.Piece = mBoard.GetPieceAtPosition(pos);
                cs.IsCheck = (Check && cs.Piece.PieceType == ChessPieceType.King && cs.Player == CurrentPlayer) ? true : false;
                i++;
            }
            
            OnPropertyChanged(nameof(BoardAdvantage));
            OnPropertyChanged(nameof(CurrentPlayer));
            OnPropertyChanged(nameof(CanUndo));
            OnPropertyChanged(nameof(Check));
        }
Пример #7
0
        public ChessViewModel()
        {
            mBoard = new ChessBoard();

            SelectedSquare = null;

            Promote = null;


            // Initialize the squares objects based on the board's initial state.
            mSquares = new ObservableCollection <ChessSquare>(
                BoardPosition.GetRectangularPositions(8, 8)
                .Select(pos => new ChessSquare()
            {
                Position = pos,
                Player   = mBoard.GetPieceAtPosition(pos)
            })
                );

            PossibleMoves = new HashSet <BoardPosition>(
                from ChessMove m in mBoard.GetPossibleMoves()
                select m.EndPosition                 //most likely end position as thats where we are trying to go?
                );

            PossibleStartMoves = new HashSet <BoardPosition>(
                from ChessMove m in mBoard.GetPossibleMoves()
                select m.StartPosition
                );
        }
Пример #8
0
        public ChessBoard(IEnumerable <Tuple <BoardPosition, ChessPiece> > startingPositions)
            : this()
        {
            var king1 = startingPositions.Where(t => t.Item2.Player == 1 && t.Item2.PieceType == ChessPieceType.King);
            var king2 = startingPositions.Where(t => t.Item2.Player == 2 && t.Item2.PieceType == ChessPieceType.King);

            if (king1.Count() != 1 || king2.Count() != 1)
            {
                throw new ArgumentException("A chess board must have a single king for each player");
            }

            foreach (var position in BoardPosition.GetRectangularPositions(8, 8))
            {
                SetPieceAtPosition(position, ChessPiece.Empty);
            }

            int[] values = { 0, 0 };
            foreach (var pos in startingPositions)
            {
                SetPieceAtPosition(pos.Item1, pos.Item2);
                // TODO: you must calculate the overall advantage for this board, in terms of the pieces
                // that the board has started with. "pos.Item2" will give you the chess piece being placed
                // on this particular position.
            }
        }
Пример #9
0
        public void ApplyMove(BoardPosition position)
        {
            var possMoves = mBoard.GetPossibleMoves() as IEnumerable <OthelloMove>;

            foreach (var move in possMoves)
            {
                if (move.Position.Equals(position))
                {
                    mBoard.ApplyMove(move);
                    break;
                }
            }

            PossibleMoves = new HashSet <BoardPosition>(mBoard.GetPossibleMoves().Select(m => m.Position));
            var newSquares = BoardPosition.GetRectangularPositions(8, 8);
            int i          = 0;

            foreach (var pos in newSquares)
            {
                mSquares[i].Player = mBoard.GetPlayerAtPosition(pos);
                i++;
            }
            OnPropertyChanged(nameof(CurrentAdvantage));
            OnPropertyChanged(nameof(CurrentPlayer));
        }
Пример #10
0
 public IEnumerable <TicTacToeMove> GetPossibleMoves()
 {
     if (IsFinished)
     {
         return(new TicTacToeMove[0]);
     }
     return
         (BoardPosition.GetRectangularPositions(3, 3)
          .Where(pos => GetPieceAtPosition(pos) == 0)
          .Select(pos => new TicTacToeMove(pos)));
 }
        public void UpdateSquares()
        {
            PossibleMoves = new HashSet <BoardPosition>(mBoard.GetPossibleMoves().Select(m => m.Position));
            var newSquares = BoardPosition.GetRectangularPositions(8, 8);
            int i          = 0;

            foreach (var pos in newSquares)
            {
                mSquares[i].Player = mBoard.GetPlayerAtPosition(pos);
                i++;
            }
        }
Пример #12
0
        /// <summary>
        /// Set board, square objects, and possible moves
        /// </summary>
        public ChessViewModel()
        {
            // Pawn Promotion Board Testing
            List<Tuple<BoardPosition, ChessPiece>> PawnPromoTest = new List<Tuple<BoardPosition, ChessPiece>>();
            PawnPromoTest.Add(new Tuple<BoardPosition, ChessPiece>(new BoardPosition(7, 4), new ChessPiece(ChessPieceType.King, 1)));
            PawnPromoTest.Add(new Tuple<BoardPosition, ChessPiece>(new BoardPosition(0, 4), new ChessPiece(ChessPieceType.King, 2)));
            PawnPromoTest.Add(new Tuple<BoardPosition, ChessPiece>(new BoardPosition(7, 0), new ChessPiece(ChessPieceType.Rook, 1)));
            PawnPromoTest.Add(new Tuple<BoardPosition, ChessPiece>(new BoardPosition(0, 0), new ChessPiece(ChessPieceType.Rook, 2)));
            PawnPromoTest.Add(new Tuple<BoardPosition, ChessPiece>(new BoardPosition(7, 7), new ChessPiece(ChessPieceType.Rook, 1)));
            PawnPromoTest.Add(new Tuple<BoardPosition, ChessPiece>(new BoardPosition(0, 7), new ChessPiece(ChessPieceType.Rook, 2)));
            PawnPromoTest.Add(new Tuple<BoardPosition, ChessPiece>(new BoardPosition(5, 6), new ChessPiece(ChessPieceType.Bishop, 2)));

            // King Check Test - Kings Only, can test Stalemate
            List<Tuple<BoardPosition, ChessPiece>> KingOnlyCheckTest = new List<Tuple<BoardPosition, ChessPiece>>();
            KingOnlyCheckTest.Add(new Tuple<BoardPosition, ChessPiece>(new BoardPosition(7, 4), new ChessPiece(ChessPieceType.King, 1)));
            KingOnlyCheckTest.Add(new Tuple<BoardPosition, ChessPiece>(new BoardPosition(0, 4), new ChessPiece(ChessPieceType.King, 2)));

            // King Check Test - Kings + Other Pieces
            List<Tuple<BoardPosition, ChessPiece>> CheckTest = new List<Tuple<BoardPosition, ChessPiece>>();
            CheckTest.Add(new Tuple<BoardPosition, ChessPiece>(new BoardPosition(7, 4), new ChessPiece(ChessPieceType.King, 1)));
            CheckTest.Add(new Tuple<BoardPosition, ChessPiece>(new BoardPosition(0, 4), new ChessPiece(ChessPieceType.King, 2)));
            CheckTest.Add(new Tuple<BoardPosition, ChessPiece>(new BoardPosition(7, 0), new ChessPiece(ChessPieceType.Rook, 1)));
            CheckTest.Add(new Tuple<BoardPosition, ChessPiece>(new BoardPosition(0, 0), new ChessPiece(ChessPieceType.Rook, 2)));

            List<Tuple<BoardPosition, ChessPiece>> VisTest = new List<Tuple<BoardPosition, ChessPiece>>();
            VisTest.Add(new Tuple<BoardPosition, ChessPiece>(new BoardPosition(3, 4), new ChessPiece(ChessPieceType.King, 1)));
            VisTest.Add(new Tuple<BoardPosition, ChessPiece>(new BoardPosition(1, 4), new ChessPiece(ChessPieceType.King, 2)));
            VisTest.Add(new Tuple<BoardPosition, ChessPiece>(new BoardPosition(3, 3), new ChessPiece(ChessPieceType.Rook, 1)));
            VisTest.Add(new Tuple<BoardPosition, ChessPiece>(new BoardPosition(1, 3), new ChessPiece(ChessPieceType.Rook, 2)));

            //mBoard = new ChessBoard(VisTest);
            //mBoard = new ChessBoard(PawnPromoTest);
            //mBoard = new ChessBoard(KingOnlyCheckTest);
            //mBoard = new ChessBoard(CheckTest);
             mBoard = new ChessBoard();

            mSquares = new ObservableCollection<ChessSquare>(
                BoardPosition.GetRectangularPositions(8, 8)
                .Select(pos => new ChessSquare()
                {
                    Position = pos,
                    Player = mBoard.GetPlayerAtPosition(pos),
                    Piece = mBoard.GetPieceAtPosition(pos)
                })
            );

            PossMoves = mBoard.GetPossibleMoves();
            EndMoves = new HashSet<BoardPosition>();
            StartMoves = new HashSet<BoardPosition>(
                from ChessMove m in mBoard.GetPossibleMoves()
                select m.StartPosition
            );
        }
Пример #13
0
        public OthelloViewModel()
        {
            mBoard   = new OthelloBoard();
            mSquares = new ObservableCollection <OthelloSquare>(
                BoardPosition.GetRectangularPositions(8, 8)
                .Select(p => new OthelloSquare()
            {
                Position = p,
                Player   = mBoard.GetPlayerAtPosition(p)
            })
                );

            PossibleMoves = new HashSet <BoardPosition>(mBoard.GetPossibleMoves().Select(m => m.Position));
        }
Пример #14
0
        private void RebindState()
        {
            //Rebind the possible moves, now that the board has changed.
            PossibleStartPositions = new HashSet <BoardPosition>(
                from ChessMove m in mBoard.GetPossibleMoves()
                select m.StartPosition
                );

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

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

            //Update the collection of square by examining the new board state
            var newSquares = BoardPosition.GetRectangularPositions(8, 8);
            int i          = 0;

            foreach (var pos in newSquares)
            {
                mSquares[i].Player     = mBoard.GetPlayerAtPosition(pos);
                mSquares[i].ChessPiece = mBoard.GetPieceAtPosition(pos);
                if ((mSquares[i].ChessPiece.PieceType == ChessPieceType.King) && (mBoard.IsCheck) && (mSquares[i].ChessPiece.Player == CurrentPlayer))
                {
                    mSquares[i].IsKingInCheck = true;
                }
                else
                {
                    mSquares[i].IsKingInCheck = false;
                }
                i++;
            }
            //Update promotionSquares to the Current Player
            for (int j = 0; j < promotionSquares.Count; j++)
            {
                promotionSquares[j].Player     = CurrentPlayer;
                promotionSquares[j].ChessPiece = new ChessPiece(promotionSquares[j].ChessPiece.PieceType, CurrentPlayer);
            }

            PromotedPiece = ChessPieceType.Empty;
            OnPropertyChanged(nameof(BoardAdvantage));
            OnPropertyChanged(nameof(CurrentPlayer));
            OnPropertyChanged(nameof(CanUndo));
        }
Пример #15
0
        public ChessViewModel()
        {
            mBoard = new ChessBoard();

            //Initialize the square objects based on the board's initial state
            mSquares = new ObservableCollection <ChessSquare>(
                BoardPosition.GetRectangularPositions(8, 8)
                .Select(pos => new ChessSquare()
            {
                Position   = pos,
                Player     = mBoard.GetPlayerAtPosition(pos),
                ChessPiece = mBoard.GetPieceAtPosition(pos)
            })
                );

            //Initialize squares to use for PromotionWindow
            promotionSquares = new ObservableCollection <ChessSquare>();
            var pieceType = new ObservableCollection <ChessPieceType>();

            pieceType.Add(ChessPieceType.Rook);
            pieceType.Add(ChessPieceType.Knight);
            pieceType.Add(ChessPieceType.Bishop);
            pieceType.Add(ChessPieceType.Queen);
            for (int i = 0; i < 4; i++)
            {
                promotionSquares.Add(new ChessSquare()
                {
                    Position   = new BoardPosition(0, i),
                    Player     = CurrentPlayer,
                    ChessPiece = new ChessPiece(pieceType[i], CurrentPlayer),
                });
            }

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

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

            PossibleMoves = new HashSet <ChessMove>(
                from ChessMove m in mBoard.GetPossibleMoves()
                select m
                );
        }
Пример #16
0
        public TicTacToeViewModel()
        {
            mBoard   = new TicTacToeBoard();
            mSquares = new ObservableCollection <TicTacToeSquare>(
                BoardPosition.GetRectangularPositions(3, 3)
                .Select(pos => new TicTacToeSquare()
            {
                Position = pos,
                Player   = mBoard.GetPieceAtPosition(pos)
            })
                );

            PossibleMoves = new HashSet <BoardPosition>(
                mBoard.GetPossibleMoves().Select(m => m.Position)
                );
        }
Пример #17
0
        public ChessViewModel()
        {
            mChessBoard = new ChessBoard();
            mSquares    = new ObservableCollection <ChessSquare>(
                BoardPosition.GetRectangularPositions(8, 8)
                .Select(pos => new ChessSquare()
            {
                Position   = pos,
                chessPiece = mChessBoard.GetPieceAtPosition(pos)
            })
                );


            //might  not be select from m.startposition
            PossibleMoves = new HashSet <ChessMove>(
                from ChessMove m in mChessBoard.GetPossibleMoves()
                select m
                );
        }
        public OthelloViewModel()
        {
            mBoard = new OthelloBoard();

            // Initialize the squares objects based on the board's initial state.
            mSquares = new ObservableCollection <OthelloSquare>(
                BoardPosition.GetRectangularPositions(8, 8)
                .Select(pos => new OthelloSquare()
            {
                Position = pos,
                Player   = mBoard.GetPlayerAtPosition(pos)
            })
                );

            PossibleMoves = new HashSet <BoardPosition>(
                from OthelloMove m in mBoard.GetPossibleMoves()
                select m.Position
                );
        }
Пример #19
0
        /// <summary>
        /// Returns an enumeration of moves that would be valid to apply to the current board state.
        /// </summary>
        /// <returns></returns>
        public IEnumerable <OthelloMove> GetPossibleMoves()
        {
            var moves = new List <OthelloMove>();

            foreach (BoardPosition position in BoardPosition.GetRectangularPositions(BOARD_SIZE, BOARD_SIZE))
            {
                if (!PositionIsEmpty(position))
                {
                    continue;
                }

                // Iterate through all 8 cardinal directions from the current position.
                foreach (BoardDirection dir in BoardDirection.CardinalDirections)
                {
                    // Repeatedly move in the selected direction, as long as we find "enemy" squares.
                    BoardPosition newPos = position;
                    int           steps  = 0;
                    do
                    {
                        newPos = newPos.Translate(dir);
                        steps++;
                    } while (PositionIsEnemy(newPos, CurrentPlayer));

                    // This is a valid direction of flips if we moved at least 2 squares, and ended in bounds and on a
                    // "friendly" square.
                    if (steps > 1 && GetPlayerAtPosition(newPos) == CurrentPlayer)
                    {
                        moves.Add(new OthelloMove(CurrentPlayer, position));
                        break;
                    }
                }
                // If the current position is valid, yield a move at the position.
            }

            // If no positions were valid, return a "pass" move.
            if (moves.Count == 0)
            {
                moves.Add(new OthelloMove(CurrentPlayer, new BoardPosition(-1, -1)));
            }

            return(moves);
        }
        public ChessViewModel()
        {
            mBoard = new ChessBoard();

            // Initialize the squares objects based on the board's initial state.
            mSquares = new ObservableCollection <ChessSquare>(
                BoardPosition.GetRectangularPositions(8, 8)
                .Select(pos => new ChessSquare()
            {
                Position    = pos,
                Chess_Piece = mBoard.GetPieceAtPosition(pos),
                IsChecked   = false
            })
                );

            PossibleMoves = new HashSet <ChessMove>(
                from ChessMove m in mBoard.GetPossibleMoves()
                select m
                );
        }
        private void RebindState()
        {
            // Rebind the possible moves, now that the board has changed.
            PossibleMoves = new HashSet <BoardPosition>(
                from OthelloMove m in mBoard.GetPossibleMoves()
                select m.Position
                );

            // Update the collection of squares by examining the new board state.
            var newSquares = BoardPosition.GetRectangularPositions(8, 8);
            int i          = 0;

            foreach (var pos in newSquares)
            {
                mSquares[i].Player = mBoard.GetPlayerAtPosition(pos);
                i++;
            }
            OnPropertyChanged(nameof(BoardAdvantage));
            OnPropertyChanged(nameof(CurrentPlayer));
            OnPropertyChanged(nameof(CanUndo));
        }
Пример #22
0
        public void UndoLastMove()
        {
            var moveHistory = mBoard.MoveHistory as IReadOnlyList <OthelloMove>;

            if (moveHistory.Count() > 0)
            {
                mBoard.UndoLastMove();

                PossibleMoves = new HashSet <BoardPosition>(mBoard.GetPossibleMoves().Select(m => m.Position));
                var newSquares = BoardPosition.GetRectangularPositions(8, 8);
                int i          = 0;
                foreach (var pos in newSquares)
                {
                    mSquares[i].Player = mBoard.GetPlayerAtPosition(pos);
                    i++;
                }

                OnPropertyChanged(nameof(CurrentAdvantage));
                OnPropertyChanged(nameof(CurrentPlayer));
            }
        }
Пример #23
0
        private void RebindState()
        {
            // Rebind the possible moves, now that the board has changed.
            PossibleStartMoves = new HashSet <BoardPosition>(
                from ChessMove m in mBoard.GetPossibleMoves()
                select m.StartPosition
                );

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

            PossibleMoves = mBoard.GetPossibleMoves();

            // Update the collection of squares by examining the new board state.
            var newSquares = BoardPosition.GetRectangularPositions(8, 8);
            int i          = 0;

            foreach (var pos in newSquares)
            {
                mSquares[i].Player     = mBoard.GetPieceAtPosition(pos);
                mSquares[i].PlayerTurn = mBoard.GetPlayerAtPosition(pos);
                if (mSquares[i].Player.PieceType == ChessPieceType.King && mBoard.IsCheck && mSquares[i].PlayerTurn == CurrentPlayer)
                {
                    mSquares[i].IsCheck = true;
                }
                else
                {
                    mSquares[i].IsCheck = false;
                }
                i++;
            }
            OnPropertyChanged(nameof(PossibleStartMoves));
            OnPropertyChanged(nameof(PossibleEndMoves));
            OnPropertyChanged(nameof(PossibleMoves));
            OnPropertyChanged(nameof(BoardAdvantage));
            OnPropertyChanged(nameof(CurrentPlayer));
            OnPropertyChanged(nameof(CanUndo));
        }
Пример #24
0
        public void RebindState()
        {
            //might  not be select from m.startposition
            PossibleMoves = new HashSet <ChessMove>(
                from ChessMove m in mChessBoard.GetPossibleMoves()
                select m
                );
            // Update the collection of squares by examining the new board state.
            var newSquares = BoardPosition.GetRectangularPositions(8, 8);
            int i          = 0;

            foreach (var pos in newSquares)
            {
                mSquares[i].chessPiece = mChessBoard.GetPieceAtPosition(pos);

                //if piece at position is not king, set ischeck to false
                if (mSquares[i].chessPiece.PieceType != ChessPieceType.King)
                {
                    mSquares[i].IsCheck = false;
                }
                //if peice is in check and it's their turn, check if he is in check
                if (mSquares[i].chessPiece.PieceType == ChessPieceType.King && CurrentPlayer == mSquares[i].chessPiece.Player)
                {
                    mSquares[i].IsCheck = mChessBoard.IsCheck || mChessBoard.IsCheckmate;
                }
                //if not then check if the enemy is in check
                if (mSquares[i].chessPiece.PieceType == ChessPieceType.King && CurrentPlayer != mSquares[i].chessPiece.Player)
                {
                    mSquares[i].IsCheck = mChessBoard.isEnemyCheck();
                }

                i++;
            }

            OnPropertyChanged(nameof(BoardAdvantage));
            OnPropertyChanged(nameof(CurrentPlayer));
            OnPropertyChanged(nameof(CanUndo));
        }
Пример #25
0
        public ChessViewModel()
        {
            mBoard = new ChessBoard();

            mSquares = new ObservableCollection <ChessSquare>(
                BoardPosition.GetRectangularPositions(8, 8)
                .Select(pos => new ChessSquare()
            {
                Position = pos,
                Player   = mBoard.GetPlayerAtPosition(pos),
                Piece    = mBoard.GetPieceAtPosition(pos)
            })
                );

            CanEnable = true;

            PossibleMoves    = mBoard.GetPossibleMoves();
            mPromotionPieces = new ObservableCollection <PromotionPiece>();
            mPromotionPieces.Add(new PromotionPiece(ChessPieceType.Bishop, 3));
            mPromotionPieces.Add(new PromotionPiece(ChessPieceType.Knight, 3));
            mPromotionPieces.Add(new PromotionPiece(ChessPieceType.Rook, 5));
            mPromotionPieces.Add(new PromotionPiece(ChessPieceType.Queen, 9));
        }
Пример #26
0
        public ChessViewModel()
        {
            mBoard = new ChessBoard();

            if (Players == NumberOfPlayers.One && !mBoard.IsFinished)
            {
                var bestMove = mGameAi.FindBestMove(mBoard);
                if (bestMove != null)
                {
                    mBoard.ApplyMove(bestMove as ChessMove);
                }
            }


            // Initialize the squares objects based on the board's initial state.
            mSquares = new ObservableCollection <ChessSquare>(
                BoardPosition.GetRectangularPositions(8, 8)
                .Select(pos => new ChessSquare()
            {
                Position = pos,
                Player   = mBoard.GetPlayerAtPosition(pos),
                Piece    = mBoard.GetPieceAtPosition(pos)
            })
                );

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

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

            PossibleMoves = new HashSet <ChessMove>(mBoard.GetPossibleMoves());
        }
Пример #27
0
 /// <summary>
 /// Returns all chess piece positions controlled by the given player
 /// </summary>
 protected IEnumerable <ChessPiece> GetAllPiecesForPlayer(ChessBoard b, int player) =>
 BoardPosition.GetRectangularPositions(8, 8)
 .Select(b.GetPieceAtPosition)
 .Where(piece => piece.Player == player);
        private void RebindState()
        {
            if (isPieceSelected() == true)
            {
                BoardPosition startpos = new BoardPosition();
                foreach (ChessSquare x in mSquares)
                {
                    if (x.IsSelected)
                    {
                        startpos = x.Position;
                    }
                }

                // Rebind the possible moves, now that the board has changed.
                PossibleMoves = new HashSet <BoardPosition>(
                    from ChessMove m in mBoard.GetPossibleMoves() where m.StartPosition == startpos
                    select m.EndPosition
                    );
            }
            else
            {
                PossibleMoves = new HashSet <BoardPosition>(
                    from ChessMove m in mBoard.GetPossibleMoves()
                    select m.StartPosition
                    );
            }


            // Update the collection of squares by examining the new board state.
            var newSquares = BoardPosition.GetRectangularPositions(8, 8);
            int i          = 0;

            foreach (var pos in newSquares)
            {
                mSquares[i].Player = mBoard.GetPieceAtPosition(pos);
                i++;
            }
            OnPropertyChanged(nameof(BoardAdvantage));
            OnPropertyChanged(nameof(CurrentPlayer));
            OnPropertyChanged(nameof(CanUndo));

            if (mBoard.IsCheck || mBoard.IsCheckmate)
            {
                foreach (var x in mSquares)
                {
                    if (x.Player.PieceType == ChessPieceType.King && x.Player.Player == mBoard.CurrentPlayer)
                    {
                        x.IsCheck = true;
                    }
                }
            }
            else
            {
                {
                    foreach (var x in mSquares)
                    {
                        x.IsCheck = false;
                    }
                }
            }
        }