Dominoes is a game played with rectangular domino 'tiles'. Today the tiles are often made of plastic or wood, but in the past, they were made of real stone or ivory. They have a rectangle shape and are divided in 2 square fields, each marked with zero to six pips. In a standard set, all 28 combinations of stones with 0 to 6 pips occur exactly once.
The genesis of the game is unclear. Possibly, dominoes originates from China and the stones were brought here by Marco Polo, but this is uncertain.
Define a class Domino to represent domino tiles in Python. We choose a design where the tiles are immutable objects. This way, not a single method may change the internal state of an object after is was initialized. The objects of this class should have at least the following methods:
An initializing method to which two numbers must be given. We represent a domino tile as two square halves that are next to each other, and the given numbers respectively are the amount of pips on the left and right half.
The initializing method must print an AssertionError with the message invalid number of pips, if the number of pips on both halves is not between 0 and 6 (boundaries included).
A method __repr__ that prints a string representation of the domino tile. This string representation reads as a Python-expression that makes a new object of the class Domino that has the same state as the domino tiles on which the method was called.
A method __str__ that prints a string representation of the domino tile. This string representation uses the templates below to compose both halves of the tile based on their number of pips.
+---+ +---+ +---+ +---+ +---+ +---+ +---+
| | | | |o | |o | |o o| |o o| |ooo|
| | | o | | | | o | | | | o | | |
| | | | | o| | o| |o o| |o o| |ooo|
+---+ +---+ +---+ +---+ +---+ +---+ +---+
0 1 2 3 4 5 6
A method rotate that prints a new tile (object of the class Domino), of which the number of pips on the left and right half were switched with regard to the tile on which the method was called.
A method that allows to add two domino tiles based on the + operator. Two tiles can be added if the number of pips on the right of the first stone is equal to the amount of pips on the left half of the second stone. If this isn't the case, the method must print an AssertionError with the message domino tiles do not match.
>>> tile1 = Domino(3, 4)
>>> Domino(-1, 7)
Traceback (most recent call last):
AssertionError: invalid number of pips
>>> tile1
Domino(3, 4)
>>> print(tile1)
+---+---+
|o |o o|
| o | |
| o|o o|
+---+---+
>>> print(tile1.rotate())
+---+---+
|o o|o |
| | o |
|o o| o|
+---+---+
>>> print(tile1)
+---+---+
|o |o o|
| o | |
| o|o o|
+---+---+
>>> tile2 = Domino(1, 3)
>>> tile1 + tile2
Traceback (most recent call last):
AssertionError: domino tiles do not match
>>> print(tile2 + tile1)
+---+---+
| |o o|
| o | |
| |o o|
+---+---+
>>> tile3 = tile1.rotate() + tile2.rotate()
>>> tile3
Domino(4, 1)
>>> print(tile3)
+---+---+
|o o| |
| | o |
|o o| |
+---+---+