Example #1
0
        /// <summary>Moves a Checkers piece on the board.</summary>
        /// <param name="piece">The checkers piece to move.</param>
        /// <param name="path">The the location for the piece to be moved to, and the path taken to get there.</param>
        /// <returns>True if the piece was moved successfully.</returns>
        public bool MovePiece(CheckersPiece piece, Point[] path)
        {
            if (isReadOnly)
            {
                throw new InvalidOperationException("Game is read only.");
            }
            if (!isPlaying)
            {
                throw new InvalidOperationException("Operation requires game to be playing.");
            }
            // Check for valid move
            CheckersMove move = IsMoveValidCore(piece, path);

            // Remove jumped pieces
            foreach (CheckersPiece jumped in move.Jumped)
            {
                if (board[jumped.Location.X, jumped.Location.Y] == jumped)
                {
                    board[jumped.Location.X, jumped.Location.Y] = null;
                }
                pieces.Remove(jumped);
                jumped.RemovedFromPlay();
            }
            // Move the piece on board
            board[piece.Location.X, piece.Location.Y]             = null;
            board[move.CurrentLocation.X, move.CurrentLocation.Y] = piece;
            piece.Moved(move.CurrentLocation);
            // King a pawn if reached other end of board
            if (move.Kinged)
            {
                piece.Promoted();
            }
            // Remember last move
            lastMove = move;
            // Update player's turn
            int prevTurn = turn;

            if (++turn > PlayerCount)
            {
                turn = 1;
            }
            // Check for win by removal of opponent's pieces or by no turns available this turn
            if ((EnumPlayerPieces(prevTurn).Length == 0) || (EnumMovablePieces().Length == 0))
            {
                DeclareWinner(prevTurn);
            }
            else
            if (TurnChanged != null)
            {
                TurnChanged(this, EventArgs.Empty);
            }
            return(true);
        }