def test_get_real_world(): input_code = [4, 1, 3, 2] expected_world_assignment = { '1:green': True, '2:yellow': True, '3:red': True, '4:violet': True } km = KnowledgeManager() km.get_real_world(input_code) assert km.real_world.assignment == expected_world_assignment assert km.real_world.name == "w13"
class Game: """ Provides an interface for the code-maker and the code-breaker to play the game. Passes on the code and feedback from code-maker to the code breaker. Passes on the moves from the code-breaker to the code-maker. Keeps track of the common knowledge in the game. Keeps track of the state of the game and decides the winner. """ def __init__(self): self.codemaker = CodeMaker() self.codebreaker = MathematicianCodeBreaker() self.knowledge_manager = KnowledgeManager() self.knowledge_manager.get_real_world(self.codemaker.code) self.agent_knowledge = AgentKnowledge() self.agent_knowledge.update_code_maker_knowledge(self.codemaker.code) self.moves = [] self.feedback = [] self.log = "" def play(self): feedback = self.__handle_first_move() if self.__codebreaker_won(feedback): return for i in range(2, NUMBER_OF_CHANCES+1): next_move = self.codebreaker.get_next_move(feedback) feedback = self.codemaker.analyze_move(next_move) self.__update_knwoledge(next_move, feedback) self.moves.append(next_move) self.feedback.append(feedback) print_game_state(i, next_move, feedback, self) if self.__codebreaker_won(feedback): return self.winner = CODE_MAKER return def __handle_first_move(self): first_move = self.codebreaker.get_first_move() feedback = self.codemaker.analyze_move(first_move) self.__update_knwoledge(first_move, feedback) self.moves.append(first_move) self.feedback.append(feedback) print_game_state(1, first_move, feedback, self) return feedback def __update_knwoledge(self, move, feedback): self.knowledge_manager.handle_move(move, feedback) self.agent_knowledge.update_move_knowledge(move, feedback) return def __codebreaker_won(self, feedback): for val in feedback: if val == 0 or val == -1: return False self.winner = CODE_BREAKER return True
class Game: """ Provides an interface for the code-maker and the code-breaker to play the game. Passes on the code and feedback from code-maker to the code breaker. Passes on the moves from the code-breaker to the code-maker. Keeps track of the common knowledge in the game. Keeps track of the state of the game and decides the winner. """ def __init__(self): self.codemaker = CodeMaker() self.codebreaker = MathematicianCodeBreaker() self.knowledge_manager = KnowledgeManager() self.knowledge_manager.get_real_world(self.codemaker.code) self.agent_knowledge = AgentKnowledge() self.agent_knowledge.update_code_maker_knowledge(self.codemaker.code) def play(self): """ Provides interface to conduct the game. Provides opportunities to the code-breaker to make moves, passes them to code-maker, gets the feedback and passes it to the code-breaker, to get the next move. Also, keeps track of the winner for the game. """ feedback = self.__handle_first_move() if self.__codebreaker_won(feedback): return for i in range(2, NUMBER_OF_CHANCES + 1): next_move = self.codebreaker.get_next_move(feedback) feedback = self.codemaker.analyze_move(next_move) self.__update_knwoledge(next_move, feedback) print_game_state(i, next_move, feedback, self) if self.__codebreaker_won(feedback): return self.winner = CODE_MAKER return def __handle_first_move(self): """ Handles the first move for the game. Gets the move from code-breaker, passes it onto the code-maker, and gets feedback from it. """ first_move = self.codebreaker.get_first_move() feedback = self.codemaker.analyze_move(first_move) self.__update_knwoledge(first_move, feedback) print_game_state(1, first_move, feedback, self) return feedback def __update_knwoledge(self, move, feedback): """ Updates the knowledge model and the agent knowledge of the game, with the knowledge acquired from this move and its feedback. """ self.knowledge_manager.handle_move(move, feedback) self.agent_knowledge.update_move_knowledge(move, feedback) return def __codebreaker_won(self, feedback): """ Checks if the codebreaker has correctly guessed the secret code, and won the game. """ for val in feedback: if val == 0 or val == -1: return False self.winner = CODE_BREAKER return True