Example #1
0
class test_print_rules(unittest.TestCase):
    """Check that it prints correctly when"""
    def setUp(self):
        self.view = Connect_Four_View()
        print(
            "Running: ",
            str(self._testMethodName) + "\n   " +
            str(self.shortDescription()) + "\n")

    def tearDown(self):
        del self.view

    @patch("sys.stdout", new_callable=StringIO)
    def test_print_rules(self, mock_stdout):
        """Test if the rules print correctly"""

        self.view.print_rules()
        self.assertEqual(
            "Connect Four is a two-player connection game in which "
            "the players take turns dropping pieces from the top "
            "into a seven-column, six-row vertically suspended "
            "grid. The pieces fall straight down, occupying the "
            "next available space within the column. The objective "
            "of the game is to connect four of one's own pieces of "
            "the same color next to each other vertically, "
            "horizontally, or diagonally before your opponent.\n"
            "\n", mock_stdout.getvalue())
class test_print_rules(unittest.TestCase):
    """Check that it prints correctly when"""

    def setUp(self):
        self.view = Connect_Four_View()
        print("Running: ", str(self._testMethodName) + "\n   " +
              str(self.shortDescription()) + "\n")

    def tearDown(self):
        del self.view


    @patch("sys.stdout",new_callable=StringIO)
    def test_print_rules(self,mock_stdout):
        """Test if the rules print correctly"""

        self.view.print_rules()
        self.assertEqual("Connect Four is a two-player connection game in which "
                         "the players take turns dropping pieces from the top "
                         "into a seven-column, six-row vertically suspended "
                         "grid. The pieces fall straight down, occupying the "
                         "next available space within the column. The objective "
                         "of the game is to connect four of one's own pieces of "
                         "the same color next to each other vertically, "
                         "horizontally, or diagonally before your opponent.\n"
                         "\n", mock_stdout.getvalue())
Example #3
0
    def __init__(self):
        """Initializes a new game of Connect 4"""

        #This holds the game's model
        self.game_state = Connect_Four_Model()

        #This is the current display
        self.game_display = Connect_Four_View()
    def __init__(self):
        """Initializes a new game of Connect 4"""

        # This holds the game's model
        self.game_state = Connect_Four_Model()

        # This is the current display
        self.game_display = Connect_Four_View()
Example #5
0
class test_message_for_end_of_game(unittest.TestCase):
    """Test for the message that concludes the game"""

    def setUp(self):
        self.view = Connect_Four_View()
        print("Running: ", str(self._testMethodName) + "\n   " + str(self.shortDescription()) + "\n")

    def tearDown(self):
        del self.view


    @patch("sys.stdout",new_callable=StringIO)
    def test_print_goodbye(self,mock_stdout):
        """Test the correct message prints at the end of the game"""

        self.view.print_goodbye()
        self.assertEqual("Thanks for playing! Come back soon!\n"
                         "\n", mock_stdout.getvalue())
class test_prompt_play_again(unittest.TestCase):
    """Check that the game asks the players if they want to play again """

    def setUp(self):
        self.view = Connect_Four_View()
        print("Running: ", str(self._testMethodName) + "\n   " + str(self.shortDescription()) + "\n")

    def tearDown(self):
        del self.view


    @patch("sys.stdout",new_callable=StringIO)
    def test_prompt_for_new_game(self,mock_stdout):
        """Test if the play again prompt prints correctly"""

        self.view.prompt_play_again()
        self.assertEqual("Would you care to play again? Yes or No (Y / N) \n"
                         "\n", mock_stdout.getvalue())
class test_print_stage(unittest.TestCase):
    """Check that it prints correctly when"""

    def setUp(self):
        self.view = Connect_Four_View()
        print("Running: ", str(self._testMethodName) + "\n   " + str(self.shortDescription()) + "\n")

    def tearDown(self):
        del self.view


    @patch("sys.stdout",new_callable=StringIO)
    def test_print_greeting(self,mock_stdout):
        """Test if the greeting prints correctly"""

        self.view.print_greeting()
        self.assertEqual("Greetings! Welcome to Connect Four!\n"
                         "\n", mock_stdout.getvalue())
Example #8
0
class test_message_if_game_tied(unittest.TestCase):
    """Check that the correct message prints for a tie"""

    def setUp(self):
        self.view = Connect_Four_View()
        print("Running: ", str(self._testMethodName) + "\n   " + str(self.shortDescription()) + "\n")

    def tearDown(self):
        del self.view


    @patch("sys.stdout",new_callable=StringIO)
    def test_print_tie(self,mock_stdout):
        """Test the correct message in the event of a tie"""

        self.view.print_tie()
        self.assertEqual("You tied - and playing is half the battle! "
                         "(The other is strategy.)\n"
                         "\n", mock_stdout.getvalue())
Example #9
0
class test_print_stage(unittest.TestCase):
    """Check that it prints correctly when"""
    def setUp(self):
        self.view = Connect_Four_View()
        print(
            "Running: ",
            str(self._testMethodName) + "\n   " +
            str(self.shortDescription()) + "\n")

    def tearDown(self):
        del self.view

    @patch("sys.stdout", new_callable=StringIO)
    def test_print_greeting(self, mock_stdout):
        """Test if the greeting prints correctly"""

        self.view.print_greeting()
        self.assertEqual("Greetings! Welcome to Connect Four!\n"
                         "\n", mock_stdout.getvalue())
class test_print_win(unittest.TestCase):
    """Check that it prints correctly when"""

    def setUp(self):
        self.view = Connect_Four_View()
        print("Running: ", str(self._testMethodName) + "\n   " + str(self.shortDescription()) + "\n")

    def tearDown(self):
        del self.view

    @patch("sys.stdout",new_callable=StringIO)
    def test_print_congratulations(self,mock_stdout):
        """Test for congratulatory message for the correct player"""
        fake_player = "Red"
        prompt_string = "Congratulations {}! A Winner is You ".\
            format(fake_player)
        self.view.print_win(fake_player)
        self.assertEqual("Congratulations Red! A Winner is You "
                         "\n", mock_stdout.getvalue())
Example #11
0
class test_prompt_play_again(unittest.TestCase):
    """Check that the game asks the players if they want to play again """
    def setUp(self):
        self.view = Connect_Four_View()
        print(
            "Running: ",
            str(self._testMethodName) + "\n   " +
            str(self.shortDescription()) + "\n")

    def tearDown(self):
        del self.view

    @patch("sys.stdout", new_callable=StringIO)
    def test_prompt_for_new_game(self, mock_stdout):
        """Test if the play again prompt prints correctly"""

        self.view.prompt_play_again()
        self.assertEqual(
            "Would you care to play again? Yes or No (Y / N) \n"
            "\n", mock_stdout.getvalue())
Example #12
0
class test_print_win(unittest.TestCase):
    """Check that it prints correctly when"""
    def setUp(self):
        self.view = Connect_Four_View()
        print(
            "Running: ",
            str(self._testMethodName) + "\n   " +
            str(self.shortDescription()) + "\n")

    def tearDown(self):
        del self.view

    @patch("sys.stdout", new_callable=StringIO)
    def test_print_congratulations(self, mock_stdout):
        """Test for congratulatory message for the correct player"""
        fake_player = "Red"
        prompt_string = "Congratulations {}! A Winner is You ".\
            format(fake_player)
        self.view.print_win(fake_player)
        self.assertEqual("Congratulations Red! A Winner is You "
                         "\n", mock_stdout.getvalue())
class test_prompt_turn(unittest.TestCase):
    """Check that the game correctly prompts the player for a turn"""

    def setUp(self):
        self.view = Connect_Four_View()
        print("Running: ", str(self._testMethodName) + "\n   " +
              str(self.shortDescription()) + "\n")

    def tearDown(self):
        del self.view


    @patch("sys.stdout",new_callable=StringIO)
    def test_prompt_turn(self,mock_stdout):
        """Test if the player is prompted for their turn"""
        fake_player = "Red"
        prompt_string = "Red Player: Please select the column for this turn ".\
            format(fake_player)
        self.view.prompt_turn(fake_player)
        self.assertEqual("Red Player: Please select the column for this turn "
                         "\n", mock_stdout.getvalue())
Example #14
0
class test_prompt_turn(unittest.TestCase):
    """Check that the game correctly prompts the player for a turn"""
    def setUp(self):
        self.view = Connect_Four_View()
        print(
            "Running: ",
            str(self._testMethodName) + "\n   " +
            str(self.shortDescription()) + "\n")

    def tearDown(self):
        del self.view

    @patch("sys.stdout", new_callable=StringIO)
    def test_prompt_turn(self, mock_stdout):
        """Test if the player is prompted for their turn"""
        fake_player = "Red"
        prompt_string = "Red Player: Please select the column for this turn ".\
            format(fake_player)
        self.view.prompt_turn(fake_player)
        self.assertEqual(
            "Red Player: Please select the column for this turn "
            "\n", mock_stdout.getvalue())
 def setUp(self):
     self.view = Connect_Four_View()
     print("Running: ", str(self._testMethodName) + "\n   " +
           str(self.shortDescription()) + "\n")
Example #16
0
class Connect_Four_Controller:
    """This object controls game play for the Connect 4 game"""
    def __init__(self):
        """Initializes a new game of Connect 4"""

        #This holds the game's model
        self.game_state = Connect_Four_Model()

        #This is the current display
        self.game_display = Connect_Four_View()

    def handoff_board(self):
        """Hands off the board to the current player """

        return self.game_state.get_board()

    def play_turn(self):
        """This is where the player plays a turn"""

        current_board = self.handoff_board()

        now_playing = self.switch_player()

        self.game_display.print_board(current_board)

        self.game_display.prompt_turn(self.mask_player(now_playing))

        try:
            move = int(input()) - 1
        except ValueError:
            move = 10

        while not self.check_move_validity(current_board, move):
            try:
                move = int(input()) - 1
            except ValueError:
                move = 10

        current_board[move].append(now_playing)
        self.game_state.update_board(current_board)

    def mask_player(self, player_value):

        if player_value == -1:
            return "Black"
        else:
            return "Red"

    def check_game_status(self, board):
        """Verifies if a player has won, if the game is tied, or if the
        game play switches turns
        :param board: A list of lists that represents the board
        :return: An int, 42 means win, anything else means no winner
        """

        #declare column height variables
        column_height = [
            len(board[0][:]),
            len(board[1][:]),
            len(board[2][:]),
            len(board[3][:]),
            len(board[4][:]),
            len(board[5][:]),
            len(board[6][:])
        ]

        #check for vertical win by comparing values in each column
        for count, column in enumerate(board):
            if (column_height[count] >= 4 and column[0] == column[1]
                    and column[1] == column[2] and column[2] == column[3]):
                return 42
            elif (column_height[count] >= 5 and column[1] == column[2]
                  and column[2] == column[3] and column[3] == column[4]):
                return 42
            elif (column_height[count] >= 6 and column[2] == column[3]
                  and column[3] == column[4] and column[4] == column[5]):
                return 42

        #check for the correct minimum number of pieces in each column
        #compare values in each adjacent column for 4 pieces in a diagonal up
        for column in range(4):
            if (column_height[column] >= 1 and column_height[column + 1] >= 2
                    and column_height[column + 2] >= 3
                    and column_height[column + 3] >= 4
                    and board[column][0] == board[column + 1][1]
                    and board[column + 1][1] == board[column + 2][2]
                    and board[column + 2][2] == board[column + 3][3]):
                return 42

            elif (column_height[column] >= 2 and column_height[column + 1] >= 3
                  and column_height[column + 2] >= 4
                  and column_height[column + 3] >= 5
                  and board[column][1] == board[column + 1][2]
                  and board[column + 1][2] == board[column + 2][3]
                  and board[column + 2][3] == board[column + 3][4]):
                return 42

            elif (column_height[column] >= 3 and column_height[column + 1] >= 4
                  and column_height[column + 2] >= 5
                  and column_height[column + 3] >= 6
                  and board[column][2] == board[column + 1][3]
                  and board[column + 1][3] == board[column + 2][4]
                  and board[column + 2][4] == board[column + 3][5]):
                return 42

        #check for the correct minimum number of pieces in each column
        #compare values in each adjacent column for 4 pieces in a diagonal down
        for column in range(4):
            if (column_height[column] >= 4 and column_height[column + 1] >= 3
                    and column_height[column + 2] >= 2
                    and column_height[column + 3] >= 1
                    and board[column][3] == board[column + 1][2]
                    and board[column + 1][2] == board[column + 2][1]
                    and board[column + 2][1] == board[column + 3][0]):
                return 42

            elif (column_height[column] >= 5 and column_height[column + 1] >= 4
                  and column_height[column + 2] >= 3
                  and column_height[column + 3] >= 2
                  and board[column][4] == board[column + 1][3]
                  and board[column + 1][3] == board[column + 2][2]
                  and board[column + 2][2] == board[column + 3][1]):
                return 42

            elif (column_height[column] >= 6 and column_height[column + 1] >= 5
                  and column_height[column + 2] >= 4
                  and column_height[column + 3] >= 3
                  and board[column][5] == board[column + 1][4]
                  and board[column + 1][4] == board[column + 2][3]
                  and board[column + 2][3] == board[column + 3][2]):
                return 42

        for row in range(6):
            for column in range(4):
                if (column_height[column - 1] >= row
                        and column_height[column] >= row
                        and column_height[column + 1] >= row
                        and column_height[column + 2] >= row):
                    try:
                        if (board[column][row] == board[column + 1][row]
                                and board[column + 1][row]
                                == board[column + 2][row]
                                and board[column + 2][row]
                                == board[column + 3][row]):
                            return 42
                    except IndexError:
                        break
        # if the sum of the values of the board == 42, the game ends in a tie
        if (len(board[0][:]) + len(board[1][:]) + len(board[2][:]) +
                len(board[3][:]) + len(board[4][:]) + len(board[5][:]) +
                len(board[6][:]) == 42):
            return 1

        #if none of these conditions are true: no winner, no tie - return 0
        return 0

    def switch_player(self):
        """Tells the model to switch the current player"""

        return self.game_state.flip_current_player()

    def close_game(self):
        """Exits the game with a fond farewell """

        self.game_display.print_goodbye()
        exit()

    def check_move_validity(self, board, move):
        """Verifies if a move is valid or invalid"""
        try:
            if len(board[move][:]) < 6:
                return True
            else:
                return False
        except IndexError:
            return False

    def check_play_again(self):
        """Asks the player if they would like to play again"""

        self.game_display.prompt_play_again()

        play_again_prompt = input()

        if play_again_prompt == 'y':
            self.game_state.reset_state()
        else:
            self.close_game()

    def main(self):

        self.game_display.print_greeting()

        self.game_display.print_rules()

        flag = 0

        while True:
            while flag == 0:
                self.play_turn()
                flag = self.check_game_status(self.handoff_board())
            self.game_display.print_board(self.handoff_board())
            if flag == 1:
                self.game_display.print_tie()
            elif flag == 42:
                self.game_display.print_win(
                    self.mask_player(self.game_state.get_current_player()))
            self.check_play_again()
            flag = 0
Example #17
0
 def setUp(self):
     self.view = Connect_Four_View()
     print(
         "Running: ",
         str(self._testMethodName) + "\n   " +
         str(self.shortDescription()) + "\n")
class Connect_Four_Controller:
    """This object controls game play for the Connect 4 game"""

    def __init__(self):
        """Initializes a new game of Connect 4"""

        # This holds the game's model
        self.game_state = Connect_Four_Model()

        # This is the current display
        self.game_display = Connect_Four_View()

    def handoff_board(self):
        """Hands off the board to the current player """

        return self.game_state.get_board()

    def play_turn(self):
        """This is where the player plays a turn"""

        current_board = self.handoff_board()

        now_playing = self.switch_player()

        self.game_display.print_board(current_board)

        self.game_display.prompt_turn(self.mask_player(now_playing))

        try:
            move = int(input()) - 1
        except ValueError:
            move = 10

        while not self.check_move_validity(current_board, move):
            try:
                move = int(input()) - 1
            except ValueError:
                move = 10

        current_board[move].append(now_playing)
        self.game_state.update_board(current_board)

    def mask_player(self, player_value):

        if player_value == -1:
            return "Black"
        else:
            return "Red"

    def check_game_status(self, board):
        """Verifies if a player has won, if the game is tied, or if the
        game play switches turns
        :param board: A list of lists that represents the board
        :return: An int, 42 means win, anything else means no winner
        """

        # declare column height variables
        column_height = [
            len(board[0][:]),
            len(board[1][:]),
            len(board[2][:]),
            len(board[3][:]),
            len(board[4][:]),
            len(board[5][:]),
            len(board[6][:]),
        ]

        # check for vertical win by comparing values in each column
        for count, column in enumerate(board):
            if (
                column_height[count] >= 4
                and column[0] == column[1]
                and column[1] == column[2]
                and column[2] == column[3]
            ):
                return 42
            elif (
                column_height[count] >= 5
                and column[1] == column[2]
                and column[2] == column[3]
                and column[3] == column[4]
            ):
                return 42
            elif (
                column_height[count] >= 6
                and column[2] == column[3]
                and column[3] == column[4]
                and column[4] == column[5]
            ):
                return 42

        # check for the correct minimum number of pieces in each column
        # compare values in each adjacent column for 4 pieces in a diagonal up
        for column in range(4):
            if (
                column_height[column] >= 1
                and column_height[column + 1] >= 2
                and column_height[column + 2] >= 3
                and column_height[column + 3] >= 4
                and board[column][0] == board[column + 1][1]
                and board[column + 1][1] == board[column + 2][2]
                and board[column + 2][2] == board[column + 3][3]
            ):
                return 42

            elif (
                column_height[column] >= 2
                and column_height[column + 1] >= 3
                and column_height[column + 2] >= 4
                and column_height[column + 3] >= 5
                and board[column][1] == board[column + 1][2]
                and board[column + 1][2] == board[column + 2][3]
                and board[column + 2][3] == board[column + 3][4]
            ):
                return 42

            elif (
                column_height[column] >= 3
                and column_height[column + 1] >= 4
                and column_height[column + 2] >= 5
                and column_height[column + 3] >= 6
                and board[column][2] == board[column + 1][3]
                and board[column + 1][3] == board[column + 2][4]
                and board[column + 2][4] == board[column + 3][5]
            ):
                return 42

        # check for the correct minimum number of pieces in each column
        # compare values in each adjacent column for 4 pieces in a diagonal down
        for column in range(4):
            if (
                column_height[column] >= 4
                and column_height[column + 1] >= 3
                and column_height[column + 2] >= 2
                and column_height[column + 3] >= 1
                and board[column][3] == board[column + 1][2]
                and board[column + 1][2] == board[column + 2][1]
                and board[column + 2][1] == board[column + 3][0]
            ):
                return 42

            elif (
                column_height[column] >= 5
                and column_height[column + 1] >= 4
                and column_height[column + 2] >= 3
                and column_height[column + 3] >= 2
                and board[column][4] == board[column + 1][3]
                and board[column + 1][3] == board[column + 2][2]
                and board[column + 2][2] == board[column + 3][1]
            ):
                return 42

            elif (
                column_height[column] >= 6
                and column_height[column + 1] >= 5
                and column_height[column + 2] >= 4
                and column_height[column + 3] >= 3
                and board[column][5] == board[column + 1][4]
                and board[column + 1][4] == board[column + 2][3]
                and board[column + 2][3] == board[column + 3][2]
            ):
                return 42

        for row in range(6):
            for column in range(4):
                if (
                    column_height[column - 1] >= row
                    and column_height[column] >= row
                    and column_height[column + 1] >= row
                    and column_height[column + 2] >= row
                ):
                    try:
                        if (
                            board[column][row] == board[column + 1][row]
                            and board[column + 1][row] == board[column + 2][row]
                            and board[column + 2][row] == board[column + 3][row]
                        ):
                            return 42
                    except IndexError:
                        break
        # if the sum of the values of the board == 42, the game ends in a tie
        if (
            len(board[0][:])
            + len(board[1][:])
            + len(board[2][:])
            + len(board[3][:])
            + len(board[4][:])
            + len(board[5][:])
            + len(board[6][:])
            == 42
        ):
            return 1

        # if none of these conditions are true: no winner, no tie - return 0
        return 0

    def switch_player(self):
        """Tells the model to switch the current player"""

        return self.game_state.flip_current_player()

    def close_game(self):
        """Exits the game with a fond farewell """

        self.game_display.print_goodbye()
        exit()

    def check_move_validity(self, board, move):
        """Verifies if a move is valid or invalid"""
        try:
            if len(board[move][:]) < 6:
                return True
            else:
                return False
        except IndexError:
            return False

    def check_play_again(self):
        """Asks the player if they would like to play again"""

        self.game_display.prompt_play_again()

        play_again_prompt = input()

        if play_again_prompt == "y":
            self.game_state.reset_state()
        else:
            self.close_game()

    def main(self):

        self.game_display.print_greeting()

        self.game_display.print_rules()

        flag = 0

        while True:
            while flag == 0:
                self.play_turn()
                flag = self.check_game_status(self.handoff_board())
            self.game_display.print_board(self.handoff_board())
            if flag == 1:
                self.game_display.print_tie()
            elif flag == 42:
                self.game_display.print_win(self.mask_player(self.game_state.get_current_player()))
            self.check_play_again()
            flag = 0
class test_board_instantiation(unittest.TestCase):
    """Check that the print board function returns a board"""

    def setUp(self):
        self.view = Connect_Four_View()
        print("Running: ", str(self._testMethodName) + "\n   " + str(self.shortDescription()) + "\n")

    def tearDown(self):
        del self.view

    @patch("sys.stdout", new_callable=StringIO)
    def test_print_board(self, mock_stdout):
        """Test if the print board function prints a connect 4 board"""

        # create a dummy board from scratch to compare to the board with list
        # values
        dummyboard = (
            "|"
            + " "
            + "|"
            + " "
            + "|"
            + " "
            + "|"
            + " "
            + "|"
            + " "
            + "|"
            + u"\u25EF"
            + "|"
            + " "
            + "|"
            + "\n"
            + "|"
            + " "
            + "|"
            + " "
            + "|"
            + " "
            + "|"
            + " "
            + "|"
            + " "
            + "|"
            + u"\u25EF"
            + "|"
            + " "
            + "|"
            + "\n"
            + "|"
            + " "
            + "|"
            + " "
            + "|"
            + " "
            + "|"
            + " "
            + "|"
            + " "
            + "|"
            + u"\u25EF"
            + "|"
            + " "
            + "|"
            + "\n"
            + "|"
            + " "
            + "|"
            + " "
            + "|"
            + " "
            + "|"
            + " "
            + "|"
            + " "
            + "|"
            + u"\u25EF"
            + "|"
            + " "
            + "|"
            + "\n"
            + "|"
            + " "
            + "|"
            + " "
            + "|"
            + " "
            + "|"
            + " "
            + "|"
            + " "
            + "|"
            + u"\u25CF"
            + "|"
            + " "
            + "|"
            + "\n"
            + "|"
            + u"\u25EF"
            + "|"
            + u"\u25CF"
            + "|"
            + u"\u25EF"
            + "|"
            + u"\u25EF"
            + "|"
            + u"\u25CF"
            + "|"
            + u"\u25EF"
            + "|"
            + u"\u25CF"
            + "|"
            + "\n"
            + "===============\n"
            + "[]           []\n\n"
        )

        # create good columns using list values
        good_col_1 = [1]
        good_col_2 = [-1]
        good_col_3 = [1]
        good_col_4 = [1]
        good_col_5 = [-1]
        good_col_6 = [1, -1, 1, 1, 1, 1]
        good_col_7 = [-1]

        # create a good board using good columns
        good_board = [good_col_1, good_col_2, good_col_3, good_col_4, good_col_5, good_col_6, good_col_7]

        # compare the dummy board to the good board that's the been run through
        # the print board function
        self.view.print_board(good_board)
        self.assertEqual(mock_stdout.getvalue(), dummyboard)