class test_flip_current_player(unittest.TestCase): """Testing the flip_current_player() function""" def setUp(self): self.model = Connect_Four_Model() print( "Running: ", str(self._testMethodName) + "\n " + str(self.shortDescription()) + "\n") def tearDown(self): del self.model def test_flip_player(self): """Testing that flip_current_player() returns a player""" #create a variable to hold the previous player previous_player = self.model.player #compare the current player to the previous player after calling the #flip_current_player() function self.model.flip_current_player() self.assertNotEqual(self.model.player, previous_player) def test_flip_returns(self): """Test that the flip_current_player() flips the player """ #Player 1 and Player 2 are represented by 1 and -1 #Multiplying current_player by -1 will flip them current_player = self.model.player * -1 #after running flip_current_player function, test current player self.assertEqual(self.model.flip_current_player(), current_player)
class test_flip_current_player(unittest.TestCase): """Testing the flip_current_player() function""" def setUp(self): self.model = Connect_Four_Model() print("Running: ", str(self._testMethodName) + "\n " + str(self.shortDescription()) + "\n") def tearDown(self): del self.model def test_flip_player(self): """Testing that flip_current_player() returns a player""" #create a variable to hold the previous player previous_player = self.model.player #compare the current player to the previous player after calling the #flip_current_player() function self.model.flip_current_player() self.assertNotEqual(self.model.player, previous_player) def test_flip_returns(self): """Test that the flip_current_player() flips the player """ #Player 1 and Player 2 are represented by 1 and -1 #Multiplying current_player by -1 will flip them current_player = self.model.player * -1 #after running flip_current_player function, test current player self.assertEqual(self.model.flip_current_player(), current_player)
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()
class test_reset_game_state(unittest.TestCase): """Testing the board after a player turn updates it""" def setUp(self): self.model = Connect_Four_Model() print( "Running: ", str(self._testMethodName) + "\n " + str(self.shortDescription()) + "\n") def tearDown(self): del self.model def test_reset_board_state(self): """Testing the reset game function returns a clean board""" #create a valid board good_col = [-1, 1, 1, -1, 1, 1] good_board = [ good_col, good_col, good_col, good_col, good_col, good_col, good_col ] empty_board = [[], [], [], [], [], [], []] #testing that valid board == a valid board after calling the update #board function self.model.board = good_board self.model.reset_state() self.assertEqual(empty_board, self.model.board) def test_reset_player(self): """""" self.model.player = 1 self.model.reset_state() self.assertEqual(-1, self.model.player)
class test_model_board(unittest.TestCase): """Testing the get board function""" def setUp(self): self.model = Connect_Four_Model() print("Running: ", str(self._testMethodName) + "\n " + str(self.shortDescription()) + "\n") def tearDown(self): del self.model def test_return_board(self): """Testing that get_board() returns the connect 4 board""" self.assertEqual(self.model.get_board(), self.model.board)
class test_get_current_player(unittest.TestCase): """Testing the get_current_player() function""" def setUp(self): self.model = Connect_Four_Model() print("Running: ", str(self._testMethodName) + "\n " + str(self.shortDescription()) + "\n") def tearDown(self): del self.model def test_get_player(self): """Testing that get_current_player() returns a player""" self.assertEqual(self.model.get_current_player(), self.model.player)
class test_reset_game_state(unittest.TestCase): """Testing the board after a player turn updates it""" def setUp(self): self.model = Connect_Four_Model() print("Running: ", str(self._testMethodName) + "\n " + str(self.shortDescription()) + "\n") def tearDown(self): del self.model def test_reset_board_state(self): """Testing the reset game function returns a clean board""" #create a valid board good_col = [-1,1,1,-1,1,1] good_board = [good_col, good_col, good_col, good_col, good_col, good_col, good_col] empty_board = [[],[],[],[],[],[],[]] #testing that valid board == a valid board after calling the update #board function self.model.board = good_board self.model.reset_state() self.assertEqual(empty_board, self.model.board) def test_reset_player(self): """""" self.model.player = 1 self.model.reset_state() self.assertEqual(-1, self.model.player)
class test_get_current_player(unittest.TestCase): """Testing the get_current_player() function""" def setUp(self): self.model = Connect_Four_Model() print( "Running: ", str(self._testMethodName) + "\n " + str(self.shortDescription()) + "\n") def tearDown(self): del self.model def test_get_player(self): """Testing that get_current_player() returns a player""" self.assertEqual(self.model.get_current_player(), self.model.player)
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
def setUp(self): self.model = Connect_Four_Model() print( "Running: ", str(self._testMethodName) + "\n " + str(self.shortDescription()) + "\n")
def setUp(self): self.model = Connect_Four_Model() print("Running: ", str(self._testMethodName) + "\n " + str(self.shortDescription()) + "\n")
class test_model_update_board(unittest.TestCase): """Testing the board after a player turn updates it""" def setUp(self): self.model = Connect_Four_Model() print("Running: ", str(self._testMethodName) + "\n " + str(self.shortDescription()) + "\n") def tearDown(self): del self.model def test_update_good_data_board(self): """Testing that update_board() function updates the board when given good data""" #create a valid board good_col = [-1,1,1,-1,1,1] good_board = [good_col, good_col, good_col, good_col, good_col, good_col, good_col] #testing that valid board == a valid board after calling the update #board function self.model.update_board(good_board) self.assertEqual(good_board, self.model.board) def test_update_bad_data_board(self): """Testing the update_board() function when given bad column data""" #create a valid board good_col = [-1,1,1,-1,1,1] good_board = [good_col, good_col, good_col, good_col, good_col, good_col, good_col] self.model.board = good_board #create an invalid board bad_col = [-1,1,1,-1,1,1] bad_col_board = [bad_col, bad_col, bad_col, bad_col, bad_col, bad_col, bad_col, bad_col] #test that the update board function is not setting an invalid board #with too many columns self.model.update_board(bad_col_board) self.assertEqual(good_board, self.model.board) def test_update_bad_height_board(self): """Testing update_board() function does not return a board with an extra row""" #create a valid board good_col = [-1,1,1,-1,1,1] good_board = [good_col, good_col, good_col, good_col, good_col, good_col, good_col] self.model.board = good_board #create an invalid board bad_col = [-1,1,1,-1,1,1,1] bad_col_board = [bad_col, bad_col, bad_col, bad_col, bad_col, bad_col, bad_col] #test that the update board function is not setting an invalid board #with an incorrect height self.model.update_board(bad_col_board) self.assertEqual(good_board, self.model.board) def test_update_bad_value_board(self): """Test update_board() does not create a board with bad player values which are currently 1 and -1""" #create a valid board good_col = [-1,1,1,-1,1,1] good_board = [good_col, good_col, good_col, good_col, good_col, good_col, good_col] self.model.board = good_board #create an invalid board bad_value = [2,2,2,2,2,2] bad_val_board = [bad_value, bad_value, bad_value, bad_value, bad_value, bad_value, bad_value] #test that the update board function is not setting an invalid board #with incorrect values self.model.update_board(bad_val_board) self.assertEqual(good_board, self.model.board) def test_update_empty_board(self): """Test update_board() does not create a board with empty columns/ rows""" #create a valid board good_col = [-1,1,1,-1,1,1] good_board = [good_col, good_col, good_col, good_col, good_col, good_col, good_col] self.model.board = good_board #create an empty board empty_board = [] #test that the update board function is not setting an invalid board #that is empty self.model.update_board(empty_board) self.assertEqual(good_board, self.model.board)
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