![]() newboard = false # Remove the jumped peg. newboard = copy ( board ) # Remove the jumping peg from its current location. As long as the applicable neighbor positions have a peg and the neighbors' neighbors are empty, these moves are valid.įunction mdist ( a, b ) return abs ( a - b ) + abs ( a - b ) end function getnextstates ( board, indices ) nextstates = Vector () for ind in indices # (1) there must be a peg to use to jump if ! board continue end neighbors = ( n for n in indices if ind != n & mdist ( ind, n ) <= 2 ) for n in neighbors # (2) the neighbor location must contain a peg if ! board continue end n2 = ( n - ( ind - n ), n - ( ind - n )) # (3) the neighbor's neighbor in the same direction # must be a valid location and empty if n2 in indices & ! board # If (1), (2), and (3) are satisfied, # make a copy of the current board. There are two potential moves: (1) jump from 3,5 to 1,3 and remove the peg at 2,4, and (2) jump from 3,5 to 1,7 and remove the peg at 2,6. Then, locations marked n are its neighbors, and n2 are the neighbors' neighbors at a valid index. Using the following figure, suppose i is the location of the peg that will jump. In addition, the neighbor's neighbor in the same direction must be empty, so we have a hole to jump to. The neighbor position must contain a peg, so we have something to jump. ![]() In other words, given the current state of the board, what are the potential states of the board after one jump? For each peg, consider its neighbor positions (northwest, northeast, east, southeast, southwest, or west). Next, we need to determine the potential moves. All holes are filled with pegs except the top left one. ![]() This board is a potential starting position. Enter fullscreen mode Exit fullscreen mode ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |