def test_create_fork(player_mark): other_mark = other_player(player_mark) ai = AI(player_mark) board = Board() board._rows = [[player_mark, ' ', ' '], [' ', other_mark, ' '], [' ', ' ', player_mark]] assert ai.turn(board.representation()) in ((2, 0), (0, 2)) board._rows = [[player_mark, ' ', ' '], [player_mark, ' ', ' '], [' ', ' ', ' ']] assert ai._fork(board.representation()) == (1, 1)
def test_block_fork(player_mark): other_mark = other_player(player_mark) ai = AI(player_mark) board = Board() board._rows = [[other_mark, ' ', ' '], [' ', player_mark, ' '], [' ', ' ', other_mark]] assert ai.turn(board.representation()) in ((0, 2), (2, 0)) board._rows = [[player_mark, ' ', ' '], [' ', other_mark, ' '], [' ', ' ', other_mark]] assert ai.turn(board.representation()) in ((0, 2), (2, 0))
def test_center(player_mark): other_mark = other_player(player_mark) ai = AI(player_mark) board = Board() board._rows = [[other_mark, ' ', ' '], [' ', player_mark, ' '], [' ', ' ', other_mark]] assert ai._center(board.representation()) is None board._rows = [[player_mark, ' ', ' '], [' ', ' ', ' '], [other_mark, ' ', other_mark]] assert ai._center(board.representation()) == (1, 1)
def test_second_turn_corner_opening(player_mark): other_mark = Game.FIRST_PLAYER_MARK ai = AI(player_mark) board = Board() board._rows = [[other_mark, ' ', ' '], [' ', ' ', ' '], [' ', ' ', ' ']] assert ai._second_turn(board.representation()) == (1, 1) board._rows = [[' ', ' ', other_mark], [' ', ' ', ' '], [' ', ' ', ' ']] assert ai._second_turn(board.representation()) == (1, 1) board._rows = [[' ', ' ', ' '], [' ', ' ', ' '], [' ', ' ', other_mark]] assert ai._second_turn(board.representation()) == (1, 1) board._rows = [[' ', ' ', ' '], [' ', ' ', ' '], [other_mark, ' ', ' ']] assert ai._second_turn(board.representation()) == (1, 1)
def test_empty_corner(player_mark): other_mark = other_player(player_mark) ai = AI(player_mark) board = Board() board._rows = [[other_mark, ' ', ' '], [' ', player_mark, ' '], [' ', ' ', ' ']] assert ai._empty_corner(board.representation()) in ((0, 2), (2, 2), (2, 0)) board._rows = [[player_mark, ' ', ' '], [' ', ' ', ' '], [other_mark, ' ', other_mark]] assert ai._empty_corner(board.representation()) == (0, 2) board._rows = [[player_mark, ' ', player_mark], [' ', ' ', ' '], [other_mark, ' ', other_mark]] assert ai._empty_corner(board.representation()) is None
def test_empty_edge(player_mark): other_mark = other_player(player_mark) ai = AI(player_mark) board = Board() board._rows = [[other_mark, ' ', ' '], [' ', player_mark, ' '], [' ', player_mark, ' ']] assert ai._empty_edge(board.representation()) in ((0, 1), (1, 2), (1, 0)) board._rows = [[player_mark, other_mark, ' '], [' ', ' ', ' '], [other_mark, player_mark, other_mark]] assert ai._empty_edge(board.representation()) in ((1, 2), (1, 0)) board._rows = [[player_mark, player_mark, player_mark], [other_mark, ' ', player_mark], [other_mark, other_mark, other_mark]] assert ai._empty_edge(board.representation()) is None
def test_row(mark): board = Board() board._rows = [[' ', ' ', ' '], [' ', mark, mark], [' ', mark, ' ']] for _ in range(100): player = RandomComputer(mark) row, column = player.turn(board) assert board.is_cell_empty(row, column)
def test_second_turn_center_opening(player_mark): other_mark = Game.FIRST_PLAYER_MARK ai = AI(player_mark) board = Board() board._rows = [[' ', ' ', ' '], [' ', other_mark, ' '], [' ', ' ', ' ']] for _ in range(100): assert ai._second_turn(board.representation()) in product((0, 2), (0, 2))
def test_win(player_mark): ai = AI(player_mark) board = Board() board._rows = [[player_mark, ' ', player_mark], [' ', ' ', ' '], [' ', ' ', ' ']] assert ai.turn(board.representation()) == (0, 1) board._rows = [[' ', player_mark, ' '], [' ', player_mark, ' '], [' ', ' ', ' ']] assert ai.turn(board.representation()) == (2, 1) board._rows = [[' ', ' ', ' '], [' ', player_mark, ' '], [' ', ' ', player_mark]] assert ai.turn(board.representation()) == (0, 0) board._rows = [[' ', ' ', player_mark], [' ', ' ', ' '], [player_mark, ' ', ' ']] assert ai.turn(board.representation()) == (1, 1)
def test_row(mark): board = Board() board._rows = [[' ', ' ', ' '], [mark, mark, mark], [' ', ' ', ' ']] for column in range(3): assert board._has_win_occurred(1, column) assert not board._has_win_occurred(2, 0) assert not board._has_win_occurred(2, 1) assert not board._has_win_occurred(2, 2)
def test_secondary_diagonal(mark): board = Board() board._rows = [[' ', ' ', mark], [' ', mark, ' '], [mark, ' ', ' ']] for row in range(3): assert board._has_win_occurred(row, 2 - row) assert not board._has_win_occurred(0, 0) assert not board._has_win_occurred(2, 2) assert not board._has_win_occurred(1, 0)
def test_main_diagonal(mark): board = Board() board._rows = [[mark, ' ', ' '], [' ', mark, ' '], [' ', ' ', mark]] for row in range(3): assert board._has_win_occurred(row, row) assert not board._has_win_occurred(0, 2) assert not board._has_win_occurred(2, 0) assert not board._has_win_occurred(2, 1)
def test_column(mark): board = Board() board._rows = [[' ', mark, ' '], [' ', mark, ' '], [' ', mark, ' ']] for row in range(3): assert board._has_win_occurred(row, 1) assert not board._has_win_occurred(0, 2) assert not board._has_win_occurred(2, 0) assert not board._has_win_occurred(2, 2)
def test_block(player_mark): other_mark = other_player(player_mark) ai = AI(player_mark) board = Board() board._rows = [[other_mark, ' ', other_mark], [' ', ' ', ' '], [' ', ' ', ' ']] assert ai.turn(board.representation()) == (0, 1) board._rows = [[' ', other_mark, ' '], [' ', other_mark, ' '], [' ', ' ', ' ']] assert ai.turn(board.representation()) == (2, 1) board._rows = [[' ', ' ', ' '], [' ', other_mark, ' '], [' ', ' ', other_mark]] assert ai.turn(board.representation()) == (0, 0) board._rows = [[' ', ' ', other_mark], [' ', ' ', ' '], [other_mark, ' ', ' ']] assert ai.turn(board.representation()) == (1, 1) board._rows = [[player_mark, ' ', ' '], [other_mark, other_mark, ' '], [player_mark, ' ', other_mark]] assert ai.turn(board.representation()) == (1, 2)
def test_is_board_full(): board = Board() board._rows = [['X', 'O', 'X'], ['O', 'X', 'O'], ['X', 'O', 'X']] assert board.is_full()