Module 5 Lab - Recursive Board Game
Many of the problems used to teach recursion are not efficiently solved with recursion - summing numbers, finding Fibonacci numbers, and calculating factorials all have elegant but inefficient recursive solutions.
Here, we look at a problem type where recursive solutions tend to be genuinely useful: exploring branching paths until we hit a dead end or find a solution. We'll see this a lot in graphs later this semester, and it often comes up when modeling games.
The basic problem abstraction is this:
- We have multiple options of what to do at each step.
- We want to see if any series of steps exists that connect a starting point to a valid solution.
Stepping Game
We will model a circular board game. The board consists of a series of tiles with numbers on them.
The number on a tile represents the number of tiles you can move from there, forwards clockwise or backwards (counter-clockwise).
It's okay to circle around - moves that go before the first tile or after the last are valid.
The goal is to reach the final tile (the tile 1 counter-clockwise from the start). This is the only valid solution - finding a tile with 0 is not necessarily a valid solution, since non-final tiles may contain 0.
Not all boards will be solvable. See below for a solvable and an unsolvable example.
Deliverables
TestSolvePuzzle.py
First, write tests. This helps solidify the rules of the game in your mind and ensures you will know when you get a working algorithm.
Tests will not directly impact your grade, but you will not be able to debug your code without them.
Test at least 4 puzzles:
- Solvable using only clockwise moves. Make sure counter-clockwise moves do not produce a valid solution here.
- Solvable using only counter-clockwise moves. Make sure clockwise moves do not produce a valid solution here.
- Requires a mixture of clockwise and counter-clockwise moves to solve. Make sure a purely clockwise or purely counter-clockwise step-sequence does not produce a valid solution here.
- Unsolvable.
Use the Unittest package.
Keep the boards for these tests relatively small (<=5 spaces) to make debugging easier.
solve_puzzle.py
Contains a function solve_puzzle.