![peg solitaire solver u shape peg solitaire solver u shape](http://recmath.org/pegsolitaire/diagonal/Beasley_EnglishDiagonal_d4Tod4in16Moves.gif)
- Peg solitaire solver u shape cracker#
- Peg solitaire solver u shape code#
- Peg solitaire solver u shape free#
Did you do your algorithm by hand for some simple end game situations to check your algorithm? I'm not a fan of looping in a recursive function, though, but it seemed like a simple way to iterate.Īnd you should have one stack to keep saved states not new each time or I do not understand what you are doing. The idea behind this implementation is that the loop iterator, i, is what is changing and controlling how long the program stays in the function. I was able to get it to run through once but it wouldn't loop right so the sim ended after the first game was over.
![peg solitaire solver u shape peg solitaire solver u shape](https://chezsoi.org/lucas/blog/images/2021/08/european_board_shifted.png)
I had a version where I was popping from the temp stack and undoing board updates but that wasn't working right, either. ()Īs I see, you are not getting any use out of your recursion, no modified self variables or using a return value from recursion (and actually no returned value in any case). Here's the move function: def move(self, board, temp_stack): Some have actually explicitly created tree data structures to house each board state but I feel that is not necessary if you use the 'imaginary' recursion tree. I know other people have written peg solitaire solvers.
Peg solitaire solver u shape free#
In fact, it winds up freezing up the computer.Īny thoughts as to what I'm doing wrong? Also, feel free to offer up ideas on how you might do this. After all the loops expire, the program should back fully out of recursion and resume. I know Python uses pass by reference so I was a bit unsure as to whether I'd be able to use a recursion tree effectively so this is why I used deep copy, so that there is a previous version of the board and temp_stack to revert to when the back tracking occurs. The idea is that when the game ends, the program backs out of the recursion tree and picks up where it left off in the main loop.
![peg solitaire solver u shape peg solitaire solver u shape](https://chezsoi.org/lucas/blog/images/2021/08/peg_solitaire_alt_european_solution.gif)
Recursion happens as move is called again with the copies as arguments. If a move can be made, it deep copies a new board and stack (so there can be fresh, new objects being passed into the recursion tree), adjusts the board copy, and updates the temp_stack copy that a new move has been made. If the game is not over, it cycles through the list of all possible moves and checks to see which ones that apply to the state of the board that has been passed through as a parameter. It appends to the simulation.moves list a snapshot of the game and returns. So, when move is called, it checks to see if there are any moves left.
Peg solitaire solver u shape code#
Turns out C++ probably wouldn't have been too bad a choice but I'm enjoying the experience of learning Python.īelow, you'll find the code for the main recursive algorithm for moving along the board and solving it.
Peg solitaire solver u shape cracker#
It will allow the user to select the shape of the board in the future but for now I'm testing it with a standard 5 row triangle game, like the ones you find in Cracker Barrel. I am in the process of writing a peg solitaire solver in Python.