The printed board looks like this: - | X X X | | X X X | | X X X X X X X | -1 => " " | X X X o X X X | 0 => "o" | X X X X X X X | 1 => "X" | X X X | | X X X |. The board is encoded as matrix (2D array) including the following values: Let’s start with the board representation. Before we can implement the algorithm, we have to write some game logic first. You’re not allowed to jump over multiple pegs or multiple empty cells.įor the implementation we’ll use Kotlin, but of course you can use any other language. The game is won when there’s 1 peg left in the center of the board and it’s lost when you cannot jump anymore or the last peg is not in the center. The rules are simple: each turn you can pick one peg, jump over a neighboring one (horizontal or vertical) and remove the one you just jumped over. What is Peg Solitaire? Peg Solitaire is a single player board game where the player has to move pegs (pins) and remove them on a board in order to win. There’s also an example repository on GitHub.īut first things first. So in this post we’ll solve Peg Solitaire with the help of backtracking. I really liked the idea and was curious to solve this puzzle. If you read the comments, you’ll notice that backtracking is the most common answer given. A few days ago I saw a post on Reddit where someone asked what an algorithm for solving Peg Solitaire might look like.
0 Comments
Leave a Reply. |