예제 #1
0
    def test_appropriate_code(self):
        """Test user input guess"""

        game = mastermind.HumanPlayer()
        game2 = mastermind.ComputerPlayer()

        'too long'
        myinput = '12345'

        with self.assertRaises(
                mastermind.InappropriateCodeException) as context:
            game.appropriate_code(myinput)
            game2.appropriate_code(myinput)

        self.assertTrue('Kod musi mieć długość = 4.' in str(context.exception))

        'too short'
        myinput = '123'

        with self.assertRaises(
                mastermind.InappropriateCodeException) as context:
            game.appropriate_code(myinput)
            game2.appropriate_code(myinput)

        self.assertTrue('Kod musi mieć długość = 4.' in str(context.exception))

        'not digits'
        myinput = 'asdas'

        with self.assertRaises(
                mastermind.InappropriateCodeException) as context:
            game.appropriate_code(myinput)
            game2.appropriate_code(myinput)

        self.assertTrue(
            'Kod musi składać się z samych cyfr.' in str(context.exception))

        'special chars'
        myinput = ' $@_+'

        with self.assertRaises(
                mastermind.InappropriateCodeException) as context:
            game.appropriate_code(myinput)
            game2.appropriate_code(myinput)

        self.assertTrue(
            'Kod musi składać się z samych cyfr.' in str(context.exception))

        'digit out of range'
        myinput = '1228'

        with self.assertRaises(
                mastermind.InappropriateCodeException) as context:
            game.appropriate_code(myinput)
            game2.appropriate_code(myinput)

        self.assertTrue(
            'Dozwolone tylko cyfry od 1 do 6.' in str(context.exception))
예제 #2
0
    def test_completely_wrong_code_human(self):
        """
        Test if method gives correct answers (human)
            completely wrong code
        """

        game = mastermind.HumanPlayer()
        game.set_code([1, 1, 1, 1])
        black, white = game.check([2, 2, 2, 2], game.get_code())
        self.assertEqual(black, 0)
        self.assertEqual(white, 0)

        black, white = game.check([4, 5, 4, 5], game.get_code())
        self.assertEqual(black, 0)
        self.assertEqual(white, 0)
예제 #3
0
    def test_correct_digits_and_wrong_positions_computer(self):
        """
        Test if method gives correct answers (computer)
            digits on correct positions and on wrong positions
        """

        game = mastermind.HumanPlayer()
        game.set_code([6, 4, 2, 5])
        black, white = game.check([2, 4, 3, 4], game.get_code())
        self.assertEqual(black, 1)
        self.assertEqual(white, 1)

        black, white = game.check([2, 4, 6, 5], game.get_code())
        self.assertEqual(black, 2)
        self.assertEqual(white, 2)

        black, white = game.check([5, 4, 2, 5], game.get_code())
        self.assertEqual(black, 3)
        self.assertEqual(white, 0)
예제 #4
0
    def test_correct_digits_and_wrong_positions_human(self):
        """
        Test if method gives correct answers (human)
            digits on correct positions and on wrong positions
        """

        game = mastermind.HumanPlayer()
        game.set_code([1, 2, 3, 4])
        black, white = game.check([2, 1, 3, 4], game.get_code())
        self.assertEqual(black, 2)
        self.assertEqual(white, 2)

        black, white = game.check([4, 2, 3, 1], game.get_code())
        self.assertEqual(black, 2)
        self.assertEqual(white, 2)

        black, white = game.check([4, 4, 4, 4], game.get_code())
        self.assertEqual(black, 1)
        self.assertEqual(white, 0)
예제 #5
0
    def test_wrong_positions_human(self):
        """
        Test if method gives correct answers (human)
            digits on wrong positions
        """

        game = mastermind.HumanPlayer()
        game.set_code([6, 3, 6, 1])
        black, white = game.check([3, 6, 1, 6], game.get_code())
        self.assertEqual(black, 0)
        self.assertEqual(white, 4)

        black, white = game.check([1, 6, 3, 6], game.get_code())
        self.assertEqual(black, 0)
        self.assertEqual(white, 4)

        black, white = game.check([1, 1, 1, 3], game.get_code())
        self.assertEqual(black, 0)
        self.assertEqual(white, 2)
예제 #6
0
    def play_as_human(self):
        """
        Here you can try to break the code by yourself.

            remaining_attempts - label that displays how many attempts left
            guess_frame - frame that contains text box where you enter your guess
            check_button - check if the guess is valid(get_guess method)
            restart_button - restart the game(restart method)
            back_button - back to choose_player window
        """

        self.clear_current_window()
        window_background_label = tkinter.Label(self.__main_window)
        window_background_label.config(image=assets.Assets.WINDOW_BACKGROUND)
        window_background_label.place(relwidth=1, relheight=1)

        self.__current_game = mastermind.HumanPlayer()
        self.__current_game.set_random_code()
        remaining = mastermind.MAX_GUESSES - self.__current_game.get_count_guesses(
        )
        self.__remaining_attempts = tkinter.Label(self.__main_window,
                                                  font='Calibri 13 bold',
                                                  text=str(remaining),
                                                  bg='grey')
        self.__remaining_attempts.config(width=2,
                                         borderwidth=2,
                                         relief="ridge")
        self.__remaining_attempts.place(relx=0.05, rely=0.05)
        guesses_label = tkinter.Label(self.__main_window,
                                      image=assets.Assets.YOUR_GUESS)
        guesses_label.place(relx=0.3, rely=0.15, anchor=tkinter.CENTER)
        answers_label = tkinter.Label(self.__main_window,
                                      image=assets.Assets.ANSWERS)
        answers_label.place(relx=0.7, rely=0.15, anchor=tkinter.CENTER)
        guess_frame = tkinter.Frame(self.__main_window,
                                    height=10,
                                    width=15,
                                    bg='#707271')
        self.__entry = tkinter.Entry(guess_frame,
                                     textvariable=self.__entry_var)
        self.__entry.config(width=7, font="Calibri 13 bold", justify='center')
        self.__entry.grid(row=0, column=1)
        here_label = tkinter.Label(guess_frame,
                                   image=assets.Assets.GUESS,
                                   bg='#707271')
        here_label.grid(row=0, column=0)
        guess_frame.place(relx=0.5, rely=0.98, anchor=tkinter.S)
        self.__check_button = tkinter.Button(self.__main_window,
                                             command=self.get_guess,
                                             image=assets.Assets.CHECK_BUTTON)
        self.__check_button.place(relx=0.85, rely=0.98, anchor=tkinter.S)
        self.__main_window.bind('<Return>',
                                lambda event=None: self.get_guess())
        restart_button_window = tkinter.Button(
            self.__main_window,
            command=lambda: self.restart(0),
            image=assets.Assets.RESTART_BUTTON_WINDOW)
        restart_button_window.place(relx=0.15, rely=0.98, anchor=tkinter.S)
        self.__back_button = tkinter.Button(self.__main_window,
                                            command=lambda: self.back(0),
                                            image=assets.Assets.BACK_BUTTON)
        self.__back_button.place(relx=0.98, rely=0.02, anchor=tkinter.NE)