Project: Builders game (final project)

Write a class called BuildersGame for a two-player game on a 5x5 grid. Each player has two builders. The builders move around the board and add levels to towers. The first player to move a builder on top of a 3-story tower wins.

The rules. Player A is 'x' and player B is 'o'. Player A places two builders on the board, then player B places two. No two builders can ever share a square. After that, player A moves one builder to an adjacent square (one step in any direction, including diagonals). A builder can move down any number of levels but can move up at most 1 level. It can also stay at the same level. A builder always ends up on top of the tower on its new square.

After moving, the builder must add a level to an adjacent square. A level cannot be added to a square with a builder on it. Once a tower has a 4th level, it is complete: no more levels can be added and no builder can move onto it. Players then alternate moving and building. A player wins by moving a builder on top of a 3-story tower, or when the opponent has no legal move.

The class. It has these private data members: a representation of the board; the current state, which is "playerA_WON", "playerB_WON" or "UNFINISHED"; and something that tracks whose turn it is.

It is not required, but a method that prints the board helps a lot when testing and debugging. Whether you index the board as [row][column] or [column][row] does not matter as long as you are consistent.

Here is a simple example of how the class could be used:

board = BuildersGame()
board.initial_placement(2,2,1,2,'x')
board.initial_placement(0,1,4,2,'o')
board.make_move(2,2,1,1,1,0)
board.make_move(0,1,1,0,2,0)
board.get_current_state()

Save the file as BuildersGame.py.

Show a solution

Try the project on your own first. This is one working solution, followed by its tests. Press Run to run the tests. Read them to see what thorough testing looks like, then change the solution and run again to watch a test fail. The testing tools used here (classes and unittest) are not part of the course yet, so do not worry about how they work.

Projects run on your own computer, so you tell us when you are done.