public void CanSelectLocation_WithoutCurrentSelection_WrongPieceColour()
        {
            var location = new BoardLocation(ChessColour.Black, new Pawn(ChessColour.Black));
            Assert.IsFalse(CanSelect(location));

            model.CurrentPlayerColour = ChessColour.Black;
            location.Piece = new Pawn(ChessColour.White);
            Assert.IsFalse(CanSelect(location));
        }
        public void CanSelectLocation_WithCurrentSelection_WithPiece_WrongColour()
        {
            var location = new BoardLocation(ChessColour.Black, new Pawn(ChessColour.Black)) { IsTargeted = false };
            model.SelectedBoardLocation = new BoardLocation(ChessColour.Black);
            Assert.IsFalse(CanSelect(location));

            location.IsTargeted = true;
            Assert.IsTrue(CanSelect(location));
        }
 public void CanSelectLocation_WithoutCurrentSelection_NoPiece()
 {
     var location = new BoardLocation(ChessColour.Black);
     Assert.IsFalse(CanSelect(location));
 }
 private void Select(BoardLocation location)
 {
     model.ExecuteCommand(ChessboardViewModel.SelectLocationCommand, location, out handled);
 }
 public void CanSelectLocation_WithCurrentSelection_WithPiece_RightColour()
 {
     var location = new BoardLocation(ChessColour.Black, new Pawn(ChessColour.White));
     model.SelectedBoardLocation = new BoardLocation(ChessColour.White);
     Assert.IsTrue(CanSelect(location));
 }
Exemple #6
0
        private void MakeMove(BoardLocation from, int fromIndex, BoardLocation to, int toIndex)
        {
            if (fromIndex < 0 || fromIndex >= Chessboard.NumLocations)
            {
                throw new ArgumentOutOfRangeException("fromIndex");
            }
            if (toIndex < 0 || toIndex >= Chessboard.NumLocations)
            {
                throw new ArgumentOutOfRangeException("toIndex");
            }

            var oldBlackLocations = this.BlackLocations;
            var oldWhiteLocations = this.WhiteLocations;

            if (from.PieceColour == ChessColour.Black)
            {
                oldBlackLocations[fromIndex] = false;
                oldBlackLocations[toIndex] = true;

                // if a piece of the other player is being taken
                if (to.HasPiece)
                {
                    oldWhiteLocations[toIndex] = false;
                }
            }
            else
            {
                oldWhiteLocations[fromIndex] = false;
                oldWhiteLocations[toIndex] = true;

                if (to.HasPiece)
                {
                    oldBlackLocations[toIndex] = false;
                }
            }

            BlackLocations = oldBlackLocations;
            WhiteLocations = oldWhiteLocations;

            to.Piece = from.Piece;
            from.Piece = null;
        }
 private bool CanSelect(BoardLocation location)
 {
     return model.CanExecuteCommand(ChessboardViewModel.SelectLocationCommand, location, out handled);
 }
Exemple #8
0
        public void MakeMove(BoardLocation from, BoardLocation to)
        {
            if (from == null)
            {
                throw new ArgumentNullException("from");
            }
            if (to == null)
            {
                throw new ArgumentNullException("to");
            }

            this.MakeMove(from, Locations.IndexOf(from), to, Locations.IndexOf(to));
        }
Exemple #9
0
        public BitArray GetRay(BoardLocation location)
        {
            if (location == null)
            {
                throw new ArgumentNullException("location");
            }

            var piece = location.Piece;
            if (piece == null)
            {
                throw new ArgumentException("Expected location to have a piece but it doesn't");
            }

            var index = this.Locations.IndexOf(location);
            var ray = piece.GetCorrectedRay(index, this.WhiteLocations, this.BlackLocations);

            return this.GetCheckPreventionRay(ray, index, piece.Colour);
        }
Exemple #10
0
        private void TargetLocations(BoardLocation location)
        {
            if (location == null)
            {
                return;
            }

            ChessPiece piece = location.Piece;
            var ray = board.GetRay(location);

            for (int i = 0; i < ray.Count; i++)
            {
                if (ray[i])
                {
                    this.Locations[i].IsTargeted = true;
                }
            }
        }
Exemple #11
0
        private void SelectLocation(BoardLocation location)
        {
            UntargetAllLocations();
            if (this.SelectedBoardLocation == null)
            {
                //nothing is currently selected
                this.SelectedBoardLocation = location;
            }
            else
            {
                if (location == this.SelectedBoardLocation)
                {
                    //deselect the current selection
                    this.SelectedBoardLocation = null;
                }
                else if (location.HasPiece && !location.IsTargeted && location.PieceColour == this.CurrentPlayerColour)
                {
                    // select another location of the player
                    this.SelectedBoardLocation.IsSelected = false;
                    this.SelectedBoardLocation = location;
                }
                else
                {
                    // move piece. Can only be moved to a location that is targeted.
                    board.MakeMove(this.SelectedBoardLocation, location);
                    this.ToggleCurrentPlayerColour();
                    this.SelectedBoardLocation.IsSelected = false;
                    location.IsSelected = false;
                    this.SelectedBoardLocation = null;
                }
            }

            TargetLocations(this.SelectedBoardLocation);
            DetectCheck();
            DetectCheckmate();
            DetectStalemate();

            if(CurrentPlayerColour == ChessColour.Black)
            {
                var move = engine.FindBestAlphaBetaMove(board, CurrentPlayerColour);
                board.MakeMove(move.FromIndex, move.ToIndex);
                this.ToggleCurrentPlayerColour();
                DetectCheck();
                DetectCheckmate();
                DetectStalemate();
                this.BranchCount = engine.recursiveCount;
            }
        }
Exemple #12
0
 private bool CanSelectLocation(BoardLocation location)
 {
     //any piece of the current player can be selected
     if (location.HasPiece && location.PieceColour == this.CurrentPlayerColour)
     {
         return true;
     }
     //if a piece is already selected, then any location without a piece can be selected
     else if (this.SelectedBoardLocation != null && location.IsTargeted)
     {
         return true;
     }
     else
     {
         return false;
     }
 }