def test_vertical_victory_bottom_right(self): '''Tests a board which contains a vertical win at the bottom right returns a win''' board1 = self.empty_board board2 = self.empty_board for i in range(4): board1[6][i] = 1 board2[6][i] = 2 self.assertEqual(check_victory(board1), 1) self.assertEqual(check_victory(board2), 2)
def test_negative_diagonal_middle(self): '''Tests a board which contains a horizontal win at the top left returns a win''' board1 = self.empty_board board2 = self.empty_board for i in range(4): board1[1 + 1][1 + i] = 1 board2[1 + 1][1 + i] = 2 self.assertEqual(check_victory(board1), 1) self.assertEqual(check_victory(board2), 2)
def test_negative_diagonal_bottom_right(self): '''Tests a board which contains a horizontal win at the bottom right returns a win''' board1 = self.empty_board board2 = self.empty_board for i in range(4): board1[3 + i][3 - i] = 1 board2[3 + i][3 - i] = 2 self.assertEqual(check_victory(board1), 1) self.assertEqual(check_victory(board2), 2)
def test_positive_diagonal_top_right(self): '''Tests a board which contains a horizontal win at the top left returns a win''' board1 = self.empty_board board2 = self.empty_board for i in range(4): board1[3 + 1][2 + i] = 1 board2[3 + 1][2 + i] = 2 self.assertEqual(check_victory(board1), 1) self.assertEqual(check_victory(board2), 2)
def test_positive_diagonal_victory_bottom_left(self): '''Tests a board which contains a horizontal win at the bottom left returns a win''' board1 = self.empty_board board2 = self.empty_board for i in range(4): board1[i][i] = 1 board2[i][i] = 2 self.assertEqual(check_victory(board1), 1) self.assertEqual(check_victory(board2), 2)
def test_horizontal_victory_middle(self): '''Tests a board which contains a horizontal win at the top left returns a win''' board1 = self.empty_board board2 = self.empty_board for i in range(1, 5): board1[i][3] = 1 board2[i][3] = 2 self.assertEqual(check_victory(board1), 1) self.assertEqual(check_victory(board2), 2)
def test_vertical_victory_top_left(self): '''Tests a board which contains a vertical win at the top left returns a win''' board1 = self.empty_board board2 = self.empty_board for i in range(2): board1[0][i] = 2 board2[0][i] = 1 for i in range(2, 6): board1[0][i] = 1 board2[0][i] = 2 self.assertEqual(check_victory(board1), 1) self.assertEqual(check_victory(board2), 2)
def process_turn(board, strategy, player_number, player_name, print_output): '''Processes a turn for the specified player (param) board ([[int]*6]*7): A board (param) strategy (function) The function which returns which column the token is to be added to (param) player_number The number of the player''' # Copy the board so the original can't be modified board_copy = copy.deepcopy(board) try: # Get the index of the column selected by the move logic i_column_add = strategy(board_copy, player_number) except TimeoutError: raise except Exception: raise MoveExceptionError( "Player {} raised an exception when asked for a move.") if print_output: print("Player {} selects column {}".format(player_name, i_column_add)) # Check the value provided is valid if type(i_column_add) != int: raise InvalidMoveException( "The provided column index was {} when it should be an integer". format(i_column_add)) if i_column_add < 0 or i_column_add > 6: raise InvalidMoveException( "The provided column index was {} when it should be between 0 and 6, inclusive" .format(i_column_add)) add_token(board, i_column_add, player_number) return check_victory(board)
def test_full_board_draw(self): '''Tests no victory is returned for a full board with no victory''' board = self.empty_board for i in range(0, 7, 2): board[i] = [1, 1, 2, 2, 1, 1] for i in range(1, 7, 2): board[i] = [2, 2, 1, 1, 2, 2] self.assertEqual(check_victory(board), -1)
def test_no_victory_empty(self): '''Tests no victory is returned for an empty board''' self.assertEqual(check_victory(self.empty_board), 0)