def play(self, domino: Domino) -> bool: """ Add a domino to the current train, updating internal state as necessary. :param domino: the Domino to add :return: a boolean indicating if the domino was added successfully """ if not domino.contains(self.requires): return False self.cars.append(domino) self.requires = domino.get_other_number(self.requires) self.demands_satisfaction = domino.is_double return True
def test_matches(self): d1 = Domino(2, 3) d2 = Domino(5, 6) d3 = Domino(3, 5) self.assertFalse(d1.matches(d2)) self.assertFalse(d2.matches(d1)) self.assertTrue(d1.matches(d3)) self.assertTrue(d3.matches(d1)) self.assertTrue(d2.matches(d3)) self.assertTrue(d3.matches(d2))
def is_valid_play(self, domino: Domino, player: Player) -> bool: """ Assert if the player is allowed to place the specified domino on this train. :param domino: the Domino to be placed. :param player: the Player attempting to place it. :return: True if play is allowed, False otherwise. """ return self.can_player_add(player) and domino.contains(self.requires)
def test_draw(self): d = Domino(2, 3) self.assertEqual("[2|3]", d.draw(d.left)) self.assertEqual("[3|2]", d.draw(d.right)) dd = Domino(6, 6) self.assertEqual("[ 6 ]", dd.draw(d.right))
def test_other(self): d = Domino(2, 3) self.assertEqual(3, d.get_other_number(2)) self.assertEqual(2, d.get_other_number(3)) self.assertIsNone(d.get_other_number(42))