def test_C4GameEngine_check_valid_move_invalid_column(): eng = C4GameEngine() is_move_valid = eng.is_move_valid(COLUMNS) expected_validity = False assert is_move_valid == expected_validity
def test_C4GameEngine_raise_error_invalid_move(column): eng = C4GameEngine() for _ in range(C4GameEngine.ROWS): eng.play_move(column) with pytest.raises(InvalidMoveError): eng.play_move(column)
def main(): #p1 = HumanPlayer() p1 = RandomPlayer() #p2 = HumanPlayer() #p2 = RandomPlayer() p2 = MinimaxPlayer(depth=3) env = C4GameEngine() rounds = 10000 outcomes = [] winners = [] round_lengths = [] for _ in range(rounds): game = TwoPlayerGame(env, [p1, p2], verbose=False) game.play() outcomes.append(game.env) winners.append(game.winner) round_lengths.append(game.rounds) #for outcome in outcomes: # pass # print(outcome) c = Counter(winners) print(c.most_common()) r = Counter(round_lengths) print(sorted(r.items())) plt.hist(round_lengths) plt.savefig('hist.png')
def test_C4GameEngine_change_player_one_move(): eng = C4GameEngine() eng.play_move(0) current_player = eng.player expected_player = C4GameEngine.PLAYER_TWO assert current_player == expected_player
def test_C4GameEngine_initial_state(): eng = C4GameEngine() eng_state = eng.get_state() empty_space = C4GameEngine.EMPTY initial_board = [[empty_space for _ in range(COLUMNS)] for _ in range(ROWS)] assert eng_state == initial_board
def test_C4GameEngine_check_valid_move_column_not_full(row, column): eng = C4GameEngine() for _ in range(row): eng.play_move(column) is_move_valid = eng.is_move_valid(column) expected_validity = True assert is_move_valid == expected_validity
def test_C4GameEngine_change_player_two_moves(): eng = C4GameEngine() eng.play_move(0) eng.play_move(1) current_player = eng.player expected_player = C4GameEngine.PLAYER_ONE assert current_player == expected_player
def test_C4GameEngine_reset_column_hieghts(column): eng = C4GameEngine() eng.play_move(column) eng.reset() column_heights = eng.column_heights expected_column_heights = {n: ROWS - 1 for n in range(COLUMNS)} assert column_heights == expected_column_heights
def test_C4GameEngine_reset_board(): eng = C4GameEngine() eng.play_move(0) eng.reset() board_state = eng.get_state() expected_state = [[C4GameEngine.EMPTY for _ in range(COLUMNS)] for _ in range(ROWS)] assert board_state == expected_state
def main(): p1 = HumanPlayer() #p1 = RandomPlayer() #p2 = HumanPlayer() #p2 = RandomPlayer() p2 = MinimaxPlayer(depth=3) env = C4GameEngine() game = TwoPlayerGame(env, [p1, p2], verbose=True) game.play()
def test_C4GameEngine_check_valid_move_column_full(column): eng = C4GameEngine() for _ in range(C4GameEngine.ROWS): eng.play_move(column) is_move_valid = eng.is_move_valid(column) expected_validity = False assert is_move_valid == expected_validity
def test_C4GameEngine_reset_player(): eng = C4GameEngine() eng.play_move(0) eng.reset() player = eng.player expected_player = C4GameEngine.PLAYER_ONE assert player == expected_player
def test_C4GameEngine_first_move(first_move): eng = C4GameEngine() eng.play_move(first_move) bottom_row = [C4GameEngine.EMPTY for _ in range(COLUMNS)] bottom_row[first_move] = C4GameEngine.PLAYER_ONE rest_of_board = [[C4GameEngine.EMPTY for _ in range(COLUMNS)] for _ in range(ROWS - 1)] expected_state = rest_of_board + [bottom_row] assert eng.get_state() == expected_state
def test_C4GameEngine_winning_vertical_states(player, column, offset): eng = C4GameEngine() in_a_row = C4GameEngine.IN_A_ROW empty_space = C4GameEngine.EMPTY winning_board = [[empty_space for _ in range(COLUMNS)] for _ in range(ROWS)] for row in range(offset, offset + in_a_row): winning_board[row][column] = player eng.board_state = winning_board assert eng.winning_state(player) == True
def test_C4GameEngine_winning_negative_diagonal_states(player, col_offset, row_offset): eng = C4GameEngine() in_a_row = C4GameEngine.IN_A_ROW empty_space = C4GameEngine.EMPTY winning_board = [[empty_space for _ in range(COLUMNS)] for _ in range(ROWS)] for val in range(in_a_row): winning_board[row_offset + val][col_offset + val] = player eng.board_state = winning_board assert eng.winning_state(player) == True
def test_C4GameEngine_winning_horizontal_states(player, row, offset): eng = C4GameEngine() in_a_row = C4GameEngine.IN_A_ROW winning_row = [C4GameEngine.EMPTY for _ in range(COLUMNS)] winning_row[offset: offset + in_a_row] = [player for _ in range(in_a_row)] winning_board = [[C4GameEngine.EMPTY for _ in range(COLUMNS)] for _ in range(ROWS)] winning_board[row] = winning_row eng.board_state = winning_board assert eng.winning_state(player)
def test_C4GameEngine_play_winning_move(player): eng = C4GameEngine() in_a_row = eng.IN_A_ROW empty_space = eng.EMPTY initial_board = [[empty_space for _ in range(COLUMNS)] for _ in range(ROWS)] initial_board[ROWS - 1][:in_a_row - 1] = [player] * (in_a_row - 1) eng.board_state = initial_board eng.player = player eng.play_move(in_a_row - 1) assert eng.done == True
def test_C4GameEngine_two_moves_same_position(column): eng = C4GameEngine() eng.play_move(column) eng.play_move(column) board_state = eng.get_state() bottom_row = [C4GameEngine.EMPTY for _ in range(COLUMNS)] bottom_row[column] = C4GameEngine.PLAYER_ONE next_up_row = [C4GameEngine.EMPTY for _ in range(COLUMNS)] next_up_row[column] = C4GameEngine.PLAYER_TWO rest_of_board = [[C4GameEngine.EMPTY for _ in range(COLUMNS)] for _ in range(ROWS - 2)] expected_state = rest_of_board + [next_up_row] + [bottom_row] assert board_state == expected_state
def test_C4GameEngine_winning_state(player): eng = C4GameEngine() eng.board_state = [] assert eng.winning_state(player) == True
from connect_four.game_engine import C4GameEngine board = [['_', '_', '_', '_', '_', '_', '_'], ['_', '_', '_', '_', '_', '_', '_'], ['_', '_', '_', '_', '_', '_', '_'], ['X', '_', '_', '_', '_', '_', '_'], ['X', '_', '_', '_', '_', '_', '_'], ['O', 'O', 'O', '_', '_', '_', '_']] eng = C4GameEngine() eng.board_state = board print(eng.score()) eng.change_player() print(eng.score())
def test_C4GameEngine_empty_board_winning_state(player): eng = C4GameEngine() assert eng.winning_state(player) == False