Пример #1
0
        public Jump Clone()
        {
            var clone = new Jump();

            for (var i = 0; i < LocationsJumped.Count; i++)
            {
                var jr = LocationsJumped[i];
                clone.LocationsJumped.Add(new JumpResult(jr.JumpedLocation, jr.FinalLocation));
            }

            return clone;
        }
        //returns a list of all availables jumps from a target location, given a piece color, and makes sure it doesn't jump over a jumped location
        public List<Jump> GetAvailableJumps(int pieceX, int pieceY, PieceColor pieceColor, Location? jumpedLoc = null)
        {
            var possibleEndValues = new List<Jump>();

            //check 4 corners
            var potentialJumpedLocs = new List<Location>
                                        {
                                            new Location(pieceX - 1, pieceY - 1),
                                            new Location(pieceX + 1, pieceY - 1),
                                            new Location(pieceX - 1, pieceY + 1),
                                            new Location(pieceX + 1, pieceY + 1)
                                        };

            //remove locations that are invalid
            for (var i = potentialJumpedLocs.Count - 1; i >= 0; i--)
            {
                var j = potentialJumpedLocs[i];

                if (jumpedLoc != null)
                    if (j == jumpedLoc)
                    {
                        potentialJumpedLocs.RemoveAt(i);
                        continue;
                    }

                //if the jumped location is invalid, if the jump would not jump over a piece, or if the jump would jump over a friendly piece, remove it
                if(!TileBoard.IsValidLocation(j) || Pieces.GetPieceAtPosition(j) == null || Pieces.GetPieceAtPosition(j).Color == pieceColor)
                    potentialJumpedLocs.RemoveAt(i);
            }

            //now all the locations in possibleEndValues are pieces that we can jump over - advance them all forward
            //check if the final location is now valid (within game board)
            for (var i = potentialJumpedLocs.Count-1; i >= 0; i--)
            {
                var jump = new Jump();
                var pos = potentialJumpedLocs[i];
                Location recentlyJumpedLoc;

                var dirX = pos.X - pieceX;
                var dirY = pos.Y - pieceY;

                var newFinalLoc = recentlyJumpedLoc = pos;
                newFinalLoc.X += dirX;
                newFinalLoc.Y += dirY;

                if (TileBoard.IsValidLocation(newFinalLoc) && Pieces.GetPieceAtPosition(newFinalLoc) == null)
                {
                    jump.AddJumpedLocation(new JumpResult(recentlyJumpedLoc, newFinalLoc));

                    //see if we can make even more jumps
                    var jumpsAvailableAgain =
                        GetAvailableJumps(newFinalLoc.X, newFinalLoc.Y, pieceColor, recentlyJumpedLoc);

                    if (jumpsAvailableAgain.Count > 0)
                    {
                        //for each jump, make a new permutation of that jump and added it to the list
                        for (var index = 0; index < jumpsAvailableAgain.Count; index++)
                        {
                            var newJump = jumpsAvailableAgain[index];
                            var branchOfJump = jump.Clone();

                            //skip any further jumps that jump back to where we started

                            var anyJumpBackwards = false;
                            for (var j = 0; j < newJump.LocationsJumped.Count; j++)
                            {
                                var locJumped = newJump.LocationsJumped[j];
                                if (locJumped.JumpedLocation == new Location(pieceX, pieceY))
                                {
                                    anyJumpBackwards = true;
                                    break;
                                }
                            }

                            if (anyJumpBackwards)
                                break;

                            branchOfJump.LocationsJumped.AddRange(newJump.LocationsJumped);

                            possibleEndValues.Add(branchOfJump);
                        }
                    }
                    else
                    {
                        possibleEndValues.Add(jump);
                    }
                }
            }

            return possibleEndValues;
        }