コード例 #1
0
        //gets: index of current piece location and final location. does: moes piece to said location.
        //Will not accept occupied spot - removePiece must be used beforhand in order to simulate eating
        public void MovePiece(int firstXIndex, int firstYIndex, int secondXIndex, int secondYIndex)
        {
            if (!ChessUtilities.isIndexInBounds(firstXIndex, firstYIndex, secondXIndex, secondYIndex))
            {
                throw new ArgumentOutOfRangeException();
            }

            PictureBox pieceAtLocation;
            Point      locationNow   = ConvertIndicesToLocation(firstXIndex, firstYIndex);
            Point      finalLocation = ConvertIndicesToLocation(secondXIndex, secondYIndex);

            PieceUIMap.TryGetValue(locationNow, out pieceAtLocation);

            if (pieceAtLocation == null)
            {
                throw new PieceDoesntExistAtThatLocationException();
            }
            if (PieceUIMap.ContainsKey(finalLocation))
            {
                throw new PieceAlreadyAtLocationConflictException();
            }

            pieceAtLocation.Visible = false;
            //physically animate piece to new location
            pieceAtLocation.Location = finalLocation;

            //remove old location pair from UIMap
            PieceUIMap.Remove(locationNow);

            //add new location pair to UIMap
            PieceUIMap[finalLocation] = pieceAtLocation;
            pieceAtLocation.Visible   = true;

            resetCurrentPieceClicked();
        }