Nim is a two-player strategy game. The player that goes first
chooses a number (
The two players take turns removing matches from the heaps. On each turn, a player must remove at least one match, and may remove any number of matches provided they all come from the same heap. The player that removes the last match wins the game.
Define a class Nim that can be used to simulate games of Nim. Two or more natural numbers (int) must be passed when creating a new game (Nim). The numbers describe the initial configuration of the game: the number of arguments corresponds to the number of heaps and each argument indicates how many matches are in the heap at the start of the game. In case no two or more natural numbers are passed, an AssertionError must be raised with the message invalid heaps.
If a game
If a game
Make sure the following methods can be called on a game
A method remove that takes two numbers
A method isfinished that takes no arguments. The method
must return a Boolean value (bool) that indicates whether
game
For two games
>>> nim = Nim(3, 4, 2, 0, 7)
>>> nim
Nim(3, 4, 2, 0, 7)
>>> print(nim)
1: |||
2: ||||
3: ||
4:
5: |||||||
>>> nim.take_away(2, 3)
Nim(3, 1, 2, 0, 7)
>>> print(nim)
1: |||
2: |
3: ||
4:
5: |||||||
>>> nim.take_away(5, 2).take_away(5, 1)
Nim(3, 1, 2, 0, 4)
>>> nim.take_away(1, 4) # not enough matches in heap
Traceback (most recent call last):
AssertionError: invalid move
>>> nim.take_away(7, 1) # heap does not exist
Traceback (most recent call last):
AssertionError: invalid move
>>> nim.take_away(3, 0) # at least one match must be taken away
Traceback (most recent call last):
AssertionError: invalid move
>>> print(nim)
1: |||
2: |
3: ||
4:
5: ||||
>>> nim.isfinished()
False
>>> nim.take_away(1, 3).take_away(2, 1).take_away(3, 2).take_away(5, 4)
Nim(0, 0, 0, 0, 0)
>>> nim.isfinished()
True
>>> Nim(4, 3, 0, 5) + Nim(3, 0, 2, 1, 6, 2)
Nim(7, 3, 2, 6, 6, 2)
>>> Nim(1.0, 2.0, 3.0, 4.0, 5.0) # all arguments must be integers
Traceback (most recent call last):
AssertionError: invalid heaps
>>> Nim('three', 'seven', 'two') # all arguments must be integers
Traceback (most recent call last):
AssertionError: invalid heaps
>>> Nim(7) # there must be at least one argument
Traceback (most recent call last):
AssertionError: invalid heaps