Project: Add Three game (final project)
Write a class named AddThreeGame for a two-player game. The players take turns
choosing numbers from 1 to 9. A number cannot be chosen if either player has
already chosen it. If any three of a player's numbers add up to 15, that
player wins. If all nine numbers are chosen and nobody has won, the game is a
draw.
The class needs private data members for:
- which numbers have been chosen, and by whom
- the current state, which is one of "FIRST_WON", "SECOND_WON", "DRAW" or "UNFINISHED"
- whose turn it is
The class must have these methods:
- An
__init__method that takes no parameters other thanself. get_current_state, which returns the current state of the game.make_move, which takes two parameters in this order: a string saying which player is moving, either "first" or "second", and the integer they chose. ReturnFalseif it is not that player's turn, if the integer is out of range, if it was already chosen, or if the game is already won or drawn. Otherwise record the move, update the state if the move won or drew the game, update whose turn it is, and returnTrue.
Here is how the class could be used:
game = AddThreeGame()
game.make_move("first", 2)
game.make_move("second", 5)
result = game.make_move("first", 7)
state = game.get_current_state()
Save the file as AddThreeGame.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.