def test_board_is_path(self): board = Board([], 0) assert board.is_path(I(1, 1), I(0, 1)) is True assert board.is_path(I(1, 1), I(0, 1), I(0, 0)) is True assert board.is_path(I(1, 1), I(0, 1), I(0, 0), I(2, 1)) is False assert board.is_path(I(1, 1), I(0, 0)) is False assert board.is_path(I(2, 2), I(0, 0)) is False
class Game(object): @logged def __init__(self, molecules_file, board_size): self.storage = Storage.load_molecules(molecules_file) atoms = self.storage.get_atoms() self.board = Board([], 0) self.board.generate(board_size, atoms) @logged def main(self): """ Main game function. """ score = 0 partial_indeces = list() # Main game loop while not self.board.all_marked(): print_board(self.board) try: s_user_input = input("Create molecule:") logging.debug("user input `%s`", s_user_input) except KeyboardInterrupt: break try: indeces = self.parse_user_input(s_user_input) try: if not self.board.is_path(partial_indeces[-1], *indeces): raise ValueError except IndexError: pass partial_indeces.extend(indeces) user_molecule = self.board.\ find_molecule_in_board(partial_indeces) except (ValueError, IndexError): print("Bad coords inserted!") logging.warn("bad coords input `%s`", s_user_input) continue try: self.storage.find(user_molecule) self.board.mark_molecules_in_board(partial_indeces, BoardItemStatus.CHECKED) except MissingError: possible_molecules = self.storage.\ get_super_molecules(user_molecule) if possible_molecules: print("You are on the right way") logging.debug("possible molecules %s", possible_molecules) self.board.mark_molecules_in_board(partial_indeces, BoardItemStatus.MARKED) else: self.board.mark_molecules_in_board(partial_indeces, BoardItemStatus.EMPTY) partial_indeces = list() score = score - 1 print("Try it again") except IndexError: self.board.mark_molecules_in_board(partial_indeces, BoardItemStatus.EMPTY) partial_indeces = list() score = score - 1 print("Try it again!") else: partial_indeces = list() score = score + 1 print("Found it!") logging.debug("score %s", score) else: print("You are finished!") logging.debug("Empty molecules") return score @staticmethod @logged def parse_user_input(user_input): return tuple(map(lambda x: I(int(x[0]), int(x[1])), map(lambda x: x.split(":"), user_input.strip().split(" "))))