コード例 #1
0
ファイル: Board.cs プロジェクト: JonHaywood/PegBoard
        /// <summary>
        /// Executes the provided jump on the board. Returns a new version of the board
        /// that has the executed jump.
        /// </summary>
        /// <param name="jump">The jump to execute.</param>
        /// <returns>New board with the moved pegs.</returns>
        public Board ExecuteJump(Jump jump)
        {
            // find index of vertices
            int currentIndex = Array.FindIndex(spaces, s => s.Coords == jump.Current);
            int jumpedIndex = Array.FindIndex(spaces, s => s.Coords == jump.Jumped);
            int targetIndex = Array.FindIndex(spaces, s => s.Coords == jump.Target);

            // make sure the move is valid
            Check.Require(spaces[currentIndex].HasPeg == true, "current must be space with a peg.");
            Check.Require(spaces[jumpedIndex].HasPeg == true, "jumped must be space with a peg.");
            Check.Require(spaces[targetIndex].HasPeg == false, "target must be an empty space.");

            // make a clone of all spaces
            var newSpaces = spaces.Select(s => s.Clone()).ToArray();

            // perform the jump and change associated values
            newSpaces[currentIndex] = newSpaces[currentIndex].SetPeg(false);
            newSpaces[jumpedIndex] = newSpaces[jumpedIndex].SetPeg(false);
            newSpaces[targetIndex] = newSpaces[targetIndex].SetPeg(true);

            // create a new board
            return new Board(newSpaces);
        }