![]() ![]() Int toIdx = GetIndex(ridx + 2, cidx + 2) We know that the column to the right always exists because it's a triangle! int fromIdx = GetIndex(ridx, cidx) Protected void AddDiagonalRightHop(Row r, int ridx, int cidx) Note that the column index stay constant. Protected void AddDiagonalLeftHop(Row r, int ridx, int cidx) ![]() Protected void AddHorizontalHop(Row r, int ridx, int cidx) Check for valid diagonal hop, which is any hop downward where the target cell exists. The idea here is to allow for diagonal down-left and down-right hops and left-to-right hops. Initializing all the possible hops populates the List allHops structure: It took me quite a while to realize that the Hop instance was being re-used for multiple iterations, thus overwriting the colors of a previous iteration! The issue of preserving color was actually the hardest bug to solve. The ToString() method gives us an easy way to display the solution in a ListBox of hops.However, because a hop preserves color state information, we need a cloned instance for the reason that the comment states. In the board's model, there is only one instance of each possible hop. We'll see later that these colors are assigned when a hop is made based on the current game board state. A hop preserves the state of the peg colors involved in the hop.A hop knows its from, to, and cell hopped over indices. ![]() Ret = FromCellIndex.ToString() + " to " + ToCellIndex.ToString() + " over " + HoppedCellIndex.ToString() Hop hop = new Hop(FromCellIndex, ToCellIndex, HoppedCellIndex) / Hop instance's color state will be overwritten by the new hop. / Otherwise, the Hop instance might be re-used in a later iteration and the previous / /// We clone the Hop so that the color state is preserved for this *specific* hop. Public Hop( int from, int to, int hopped) More semantically friendly: public bool HasPeg I decided to borrow from Ruby to create an extension method that iterates on an integer with an optional starting offset: The algorithm only needs to determine which of these "hops" is legal for the current board configuration.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |