Exemplo n.º 1
0
 def test_board_generate(self):
     with mock.patch('random.choice',
                     side_effect=[O, H2, O, Na, H2, H2, O, Na, H2]):
         board = Board([], 0)
         board.generate(3, (H2, O, Na))
         assert board.iterable == \
             [BoardItem(O, I(0, 0)), BoardItem(H2, I(0, 1)),
              BoardItem(O, I(0, 2)),
              BoardItem(Na, I(1, 0)), BoardItem(H2, I(1, 1)),
              BoardItem(H2, I(1, 2)),
              BoardItem(O, I(2, 0)), BoardItem(Na, I(2, 1)),
              BoardItem(H2, I(2, 2))]
Exemplo n.º 2
0
    def test_board_neigbours(self):
        with mock.patch('random.choice',
                        side_effect=[O2, H2, O, Na, Cl, H2, O, Na, H2]):
            board = Board([], 0)
            board.generate(3, (H2, O, Na))
            assert board.neighbours(I(1, 1), I(0, 1)) is True
            assert board.neighbours(I(1, 0), I(1, 1)) is True
            assert board.neighbours(I(1, 1), I(2, 1)) is True
            assert board.neighbours(I(1, 2), I(1, 1)) is True

            assert board.neighbours(I(1, 0), I(0, 1)) is False
            assert board.neighbours(I(0, 1), I(0, 1)) is False
Exemplo n.º 3
0
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(" "))))