예제 #1
0
 def board_size_setting(input_text, key):
     val = MenuParser.settings_standard_int(input_text, key, set=False)
     if val is not None:
         if val not in [4, 5]:
             print("The board size should be 4 or 5")
         else:
             SettingsParser.set_setting(key=key, value=val)
예제 #2
0
 def test_run_one_player(self):
     standard_setup()
     SettingsParser.set_setting('number_players', 1)
     # Return code if the amount of players are incorrect
     self.assertEqual(GameLogic.run_game(StandardBoggle, mockup=True),
                      -1,
                      msg="The game was started with only one player")
예제 #3
0
def find_words_in_board():
    SettingsParser.load_settings()
    DictionaryParser.load_dictionary(
        language=SettingsParser.get_setting('language'))
    boogle_instance = StandardBoggle(1, 4)
    boogle_instance.game_paralell_process()
    print(boogle_instance.get_game_end_info())
예제 #4
0
    def test_settings_correct(self):
        try:
            SettingsParser.load_settings(silent=True)
            success = True
        except SystemExit:
            success = False

        self.assertTrue(success)
예제 #5
0
    def test_dictionary_correct(self):
        SettingsParser.load_settings(silent=True)
        try:
            DictionaryParser.load_dictionary(language=SettingsParser.get_setting('language'), silent=True)
            success = True
        except SystemExit:
            success = False

        self.assertTrue(success)
예제 #6
0
 def settings_standard_bool(input_text, key):
     val = HostIO.get_input(
         f"Set {input_text} (should be 1, true, True, 0, false or False): ")
     if val in ['1', 'true', 'True']:
         SettingsParser.set_setting(key=key, value=1)
     elif val in ['0', 'false', 'False']:
         SettingsParser.set_setting(key=key, value=0)
     else:
         HostIO.print("Input is not a bool")
예제 #7
0
def check_winner(boggle_instance, words, time=1, further_words=[]):
    SettingsParser.set_setting('game_time', time)

    moves = [[words] if words is not None else [], further_words]

    return GameLogic.run_game(boggle_class=StandardBoggle,
                              mockup=True,
                              moves=moves,
                              predefined_boggle_instance=boggle_instance)
예제 #8
0
 def settings_standard_int(input_text, key, set=True):
     try:
         val = int(HostIO.get_input(f"Set {input_text}: "))
     except ValueError:
         print("Input is not an int")
         return
     if set:
         SettingsParser.set_setting(key=key, value=val)
     else:
         return val
예제 #9
0
 def check_grid_size(self, size):
     standard_setup()
     SettingsParser.set_setting('board_size', size)
     if size not in [4, 5]:
         with self.assertRaises(IOError):
             StandardBoggle(number_of_boards=2, board_size=size)
     else:
         try:
             StandardBoggle(number_of_boards=2, board_size=size)
         except IOError:
             self.fail()
예제 #10
0
    def run_game(boggle_class,
                 mockup=False,
                 moves=None,
                 predefined_boggle_instance=None):
        if SettingsParser.get_setting('number_players') < 2:
            if not mockup:
                HostIO.print(
                    "To few player for a game. The minimal number of players are 2.",
                    clear_screen=True)
                HostIO.get_input("Press any key to end\n")
            return -1

        PlayerIO.mockup = mockup
        if not mockup:
            HostIO.print(
                f"The game is {boggle_class.get_name()}!\nWaiting for players",
                direct_continue=True,
                clear_screen=True)
            HostNetworking.connect_to_clients(
                number_of_connections=SettingsParser.get_setting(
                    'number_players'),
                on_connect=GameLogic.on_client_connect)
            HostIO.print("All players registered. Begin game.")

        HostIO.clear()

        boggle_instance, \
        base_manager, \
        players, \
        processes = GameLogic.generate_game(boggle_class, moves, predefined_boggle_instance)

        try:
            for i in range(SettingsParser.get_setting('game_time'), 0, -1):
                HostIO.print(f'Time left: {i}',
                             clear_screen=True,
                             direct_continue=True)
                sleep(1)
        except KeyboardInterrupt:
            HostIO.print("\nTerminated")

        return GameLogic.terminate_game(boggle_instance, base_manager, players,
                                        processes, mockup)
예제 #11
0
    def generate_game(boogle_class,
                      moves=None,
                      predefined_boggle_instance=None):
        if predefined_boggle_instance is None:
            BaseManager.register('boogle_class', boogle_class)
        else:
            BaseManager.register('boogle_class',
                                 type(predefined_boggle_instance))
        BaseManager.register('Player', Player)
        base_manager = BaseManager()
        base_manager.start()
        if predefined_boggle_instance is None:
            boggle_instance = base_manager.boogle_class(
                number_of_boards=int(
                    SettingsParser.get_setting('number_players')),
                board_size=int(SettingsParser.get_setting('board_size')))
        else:
            boggle_instance = predefined_boggle_instance

        players = [
            base_manager.Player(i)
            for i in range(int(SettingsParser.get_setting("number_players")))
        ]
        parse_lock = Lock()

        processes = [
            Process(target=GameLogic.player_parallel_process,
                    args=(player, boggle_instance, parse_lock,
                          SettingsParser.get_setting('generous_boggle'),
                          boggle_instance.get_board_size(), moves))
            for player in players
        ]
        processes.append(Process(target=boggle_instance.game_paralell_process))

        for process in processes:
            process.start()

        return boggle_instance, base_manager, players, processes
예제 #12
0
    def game_paralell_process(self):
        board = self.get_board(0)
        letters = []
        for row in board:
            for char in row:
                letters.append(char.value)
        letters = list(dict.fromkeys(letters))

        def check_word(word):
            for character in list(word):
                if character not in letters:
                    return False
            return self._check_move(word,
                                    0,
                                    generous_boggle,
                                    register_used_word=False)

        generous_boggle = SettingsParser.get_setting('generous_boggle')
        self.words_in_board = list(
            filter(check_word,
                   [word.lower() for word in DictionaryParser.wordlist]))
예제 #13
0
 def settings_main_menu(exit_command):
     exit_command[0] = True
     SettingsParser.save_settings()
     DictionaryParser.load_dictionary(
         language=SettingsParser.get_setting('language'))
예제 #14
0
 def test_settings_wrong_filepath(self):
     path = '/this_dosnt_exist'
     try:
         SettingsParser.load_settings(path, silent=True)
     except SystemExit as cm:
         self.assertEqual(cm.code, 1)
예제 #15
0
 def test_settings_incorrect_format(self):
     path = get_root_dir() / 'tests' / 'test_settings_incorrect_format.json'
     try:
         SettingsParser.load_settings(path, silent=True)
     except SystemExit as cm:
         self.assertEqual(cm.code, 3)
예제 #16
0
 def get_game_end_info(self):
     if SettingsParser.get_setting('show_solution'):
         return "The board contained the following words:\n" + ', '.join(
             self.words_in_board)
     return super(WordBoggleBase, self).get_game_end_info()
예제 #17
0
 def settings_standard(input_text, key):
     SettingsParser.set_setting(
         key=key, value=HostIO.get_input(f"Set {input_text}: "))
예제 #18
0
def main():
    SettingsParser.load_settings()
    DictionaryParser.load_dictionary(
        language=SettingsParser.get_setting('language'))
    MenuParser.generate_menu()
예제 #19
0
def generate_board():
    SettingsParser.load_settings()
    DictionaryParser.load_dictionary(
        language=SettingsParser.get_setting('language'))
    boogle_instance = FoggleBoggle(2, 4)
    print(boogle_instance.get_board_string(0, colored=False))
예제 #20
0
def mock_game():
    SettingsParser.load_settings()
    DictionaryParser.load_dictionary(
        language=SettingsParser.get_setting('language'))
    GameLogic.run_game(boggle_class=StandardBoggle, mockup=True)
예제 #21
0
def standard_setup():
    SettingsParser.load_settings()
    DictionaryParser.load_dictionary(language=SettingsParser.get_setting('language'))
    SettingsParser.set_setting('generous_boggle', False)
    HostIO.set_terminal_output(False)