コード例 #1
0
 def test_CodeMakerProvidedFeedbackOfAllWrongPlacedColors(self):
     """Check if CodeMaker provides feedback of all pegs in wrong place."""
     ai = CodeMaker()
     ai.code = [0, 1, 2, 3]
     guess = [3, 2, 1, 0]
     feedback = ai.provide_feedback(guess)
     self.assertEqual([1, 1, 1, 1], feedback)
コード例 #2
0
 def test_CodeMakerProvidedFeedbackOfTwoMatchingButWrongPlacedColors(self):
     """Check if CodeMaker provides feedback of 2 correct pegs in wrong place."""
     ai = CodeMaker()
     ai.code = [0, 1, 2, 3]
     guess = [1, 0, 4, 4]
     feedback = ai.provide_feedback(guess)
     self.assertEqual([1, 1, 0, 0], feedback)
コード例 #3
0
 def test_CodeMakerProvidedFeedbackOfAllCorrectlyPlacedPegs(self):
     """Check if CodeMaker provides feedback of all pegs in the correct place."""
     ai = CodeMaker()
     ai.code = [0, 1, 2, 3]
     guess = [0, 1, 2, 3]
     feedback = ai.provide_feedback(guess)
     self.assertEqual([2, 2, 2, 2], feedback)
コード例 #4
0
 def test_CodeMakerProvidedFeedbackOfTwoCorrectIdealPlacements(self):
     """Check if CodeMaker provides feedback of 2 correct pegs."""
     ai = CodeMaker()
     ai.code = [0, 1, 2, 3]
     guess = [0, 1, 4, 4]
     feedback = ai.provide_feedback(guess)
     self.assertEqual([2, 2, 0, 0], feedback)
コード例 #5
0
 def test_CodeMakerProvidedFeedbackOfAllWrongPegs(self):
     """Check if CodeMaker provides feedback of whole wrong guess."""
     ai = CodeMaker()
     ai.code = [0, 1, 2, 3]
     guess = [4, 4, 4, 4]
     feedback = ai.provide_feedback(guess)
     self.assertEqual([0, 0, 0, 0], feedback)
コード例 #6
0
ファイル: game.py プロジェクト: deepshig/mastermind
    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)
コード例 #7
0
 def test_CodeMakerProvidedFeedbackOfTwoCorrectAndOneWrongPlacedColors(
         self):
     """Check if CodeMaker provides feedback of various combination of correct pegs."""
     ai = CodeMaker()
     ai.code = [0, 1, 2, 3]
     guess = [1, 4, 2, 3]
     feedback = ai.provide_feedback(guess)
     self.assertEqual([2, 2, 1, 0], feedback)
     self.assertEqual(3, ai.key_peg_amount)
コード例 #8
0
 def test_FeedbackResets(self):
     """"Check if feedback can be reset."""
     ai = CodeMaker()
     ai.code = [0, 1, 2, 3]
     guess = [1, 4, 2, 3]
     feedback = ai.provide_feedback(guess)
     self.assertEqual([2, 2, 1, 0], feedback)
     ai.reset_feedback_pegs()
     self.assertEqual(ai.key_pegs, [0] * 4)
     self.assertEqual(ai.key_peg_amount, 0)
コード例 #9
0
    def run_simulation(self):
        for i in range(0, self.number_of_games):
            self.knowledge_manager = KnowledgeManager()
            self.codemaker = CodeMaker()

            # print("Running Game : ", i+1, " : Code : ", self.codemaker.code)

            self.codes.append(self.codemaker.code)

            self.mathematician_codebreaker = MathematicianCodeBreaker()
            self.logician_codebreaker = LogicianCodeBreaker(
                self.knowledge_manager)
            self.random_codebreaker = RandomCodeBreaker()

            self.__play(self.mathematician_codebreaker)
            self.__play(self.logician_codebreaker)
            self.__play(self.random_codebreaker)
        return
コード例 #10
0
    def run_simulation(self):
        """
        Runs the strategy analyser simulation.
        Allows 'n' games to be played, with new instances
        of all the players and the knowledge model
        for each game.
        """
        for i in range(0, self.number_of_games):
            self.knowledge_manager = KnowledgeManager()
            self.codemaker = CodeMaker()

            print("Running Game : ", i + 1, " : Code : ", self.codemaker.code)

            self.mathematician_codebreaker = MathematicianCodeBreaker()
            self.logician_codebreaker = LogicianCodeBreaker(
                self.knowledge_manager)
            self.random_codebreaker = RandomCodeBreaker()

            self.__play(self.mathematician_codebreaker)
            self.__play(self.logician_codebreaker)
            self.__play(self.random_codebreaker)
        return
コード例 #11
0
def codemaker_player():
    return CodeMaker()
コード例 #12
0
ファイル: game.py プロジェクト: tskr1681/MastermindGame_rug
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
コード例 #13
0
ファイル: game.py プロジェクト: deepshig/mastermind
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
コード例 #14
0
 def test_CodeMakerInit(self):
     """Check if CodeMaker initialize with blank code."""
     ai = CodeMaker()
     self.assertEqual(ai.code, [None] * 4)
コード例 #15
0
 def test_CodeMakerMadeAPatternOfFourCodePegs(self):
     """Check if CodeMaker makes code."""
     ai = CodeMaker()
     self.assertEqual(ai.code, [None] * 4)
     ai.draw_code()
     self.assertNotEqual(ai.code, [None] * 4)
コード例 #16
0
 def run_game(self):
     codebreaker = CodeBreaker()
     codemaker = CodeMaker()
     codemaker.draw_code()
     while self.finished or codemaker.codebreaker_wins_condition():
         code = self.ask_for_code(codebreaker)