def test_whenNewMoveIsInFinishedBoardThenExceptionRaised(): finished_board = SubBoard().add_my_move(Move(0, 0))\ .add_my_move(Move(0, 1))\ .add_my_move(Move(0, 2)) with pytest.raises(MoveInFinishedBoardError): finished_board.add_my_move(Move(1, 1))
def test_whenBoardIsFinishedThenGetValidMovesIsEmpty(): board = SubBoard().add_my_move(Move(0, 0))\ .add_opponent_move(Move(0, 1))\ .add_my_move(Move(1, 1))\ .add_opponent_move(Move(0, 2))\ .add_my_move(Move(2, 2)) assert len(board.get_valid_moves()) is 0
def test_whenRowIsBlockedThenBoardIsNotFinished(): blocked = SubBoard().add_my_move(Move(0, 0))\ .add_my_move(Move(1, 1))\ .add_opponent_move(Move(2, 2)) assert blocked.is_finished == False with pytest.raises(BoardNotFinishedError): blocked.winner
def test_row_col_checks_work(): main_board = MainBoard() for game_event in get_game_events('tests/logs/diag_fail.gamelog'): main_board = main_board._add_move(game_event[0], game_event[1]) assert is_diagonal_won(main_board._board[0][1]._board, PlayerMove(Player.OPPONENT, Move(1,1))) is True assert is_col_won(main_board._board[0][2]._board, PlayerMove(Player.ME, Move(0, 1))) is True assert is_row_won(main_board._board[2][1]._board, PlayerMove(Player.ME, Move(1, 1))) is True
def test_whenNewMoveIsInAlreadyPlayedCellThenExceptionRaised(): board = SubBoard().add_my_move(Move(1, 2)) string_board = str(board) move_count_before = board._moves_so_far with pytest.raises(MoveInPlayedCellError): board = board.add_opponent_move(Move(1, 2)) #Ensure board state has not changed assert string_board == str(board) assert board._moves_so_far == move_count_before
def test_whenBoardIsPlayedThenGetValidMovesReturnsCorrectly(): board = SubBoard().add_my_move(Move(0,0))\ .add_opponent_move(Move(1,1))\ .add_my_move(Move(2,1)) assert board.get_valid_moves() == \ [Move(0, 1), Move(0, 2),\ Move(1, 0), Move(1, 2),\ Move(2, 0), Move(2, 2)]
def get_game_events(filename): log_file = open(filename, 'r') lines = log_file.readlines() log_file.close() game_events = [] for line in lines: line = line.strip() player = Player.NONE if 'opponent' in line: player = Player.OPPONENT elif 'player' in line: player = Player.ME else: continue move_str = line[-7:] board_coords = BoardCoords(int(move_str[0]),int(move_str[2])) move_coords = Move(int(move_str[4]),int(move_str[6])) game_events.append((board_coords, PlayerMove(player, move_coords))) return game_events
def test_whenNextBoardIsFinishedThenAnyBoardCanBePlayed(): main_board = MainBoard() #Force some sub_board plays to finish a board finished_sub_board = main_board._board[2][2]\ .add_my_move(Move(0, 0))\ .add_my_move(Move(1, 1))\ .add_my_move(Move(2, 2)) #Set that sub-board where the next_board_coords will be main_board._board[2][2] = finished_sub_board #Play a move that will make the finished board the next board (Move 2, 2) main_board = main_board.add_my_move(BoardCoords(0, 0), Move(2, 2)) #Playing anywhere is now allowed assert main_board.next_board_coords == None assert main_board.is_valid_board_for_next_move(BoardCoords(1, 1)) == True main_board.add_opponent_move(BoardCoords(0, 0), Move(1, 1))
def test_whenBoardIsPrettyPrintedThenItIsRenderedCorrectly(): string_board = str(MainBoard(3).add_my_move(BoardCoords(0, 0), Move(1, 1))\ .add_opponent_move(BoardCoords(1, 1), Move(2, 2))\ .add_opponent_move(BoardCoords(2, 2), Move(0, 0))) assert string_board == "0 0 0 | 0 0 0 | 0 0 0 \n"+\ "0 1 0 | 0 0 0 | 0 0 0 \n"+\ "0 0 0 | 0 0 0 | 0 0 0 \n"+\ "- - - | - - - | - - - \n"+\ "0 0 0 | 0 0 0 | 0 0 0 \n"+\ "0 0 0 | 0 0 0 | 0 0 0 \n"+\ "0 0 0 | 0 0 2 | 0 0 0 \n"+\ "- - - | - - - | - - - \n"+\ "0 0 0 | 0 0 0 | 2 0 0 \n"+\ "0 0 0 | 0 0 0 | 0 0 0 \n"+\ "0 0 0 | 0 0 0 | 0 0 0 \n"
def test_whenBoardReachesMaxMovesThenBoardIsFinishedAndTied(): tied_board = SubBoard().add_my_move(Move(0, 0))\ .add_opponent_move(Move(2, 2))\ .add_my_move(Move(2, 0))\ .add_opponent_move(Move(1, 0))\ .add_my_move(Move(0, 2))\ .add_opponent_move(Move(0, 1))\ .add_my_move(Move(1, 2))\ .add_opponent_move(Move(1, 1))\ .add_my_move(Move(2, 1)) assert tied_board.is_finished == True assert tied_board.winner == Player.NONE
def test_whenNextBoardIsFinishedThenGetValidBoardsReturnsAllAvailableBoards(): main_board = MainBoard() #Force some sub_board plays to finish a board finished_sub_board = main_board._board[2][2]\ .add_my_move(Move(0, 0))\ .add_my_move(Move(1, 1))\ .add_my_move(Move(2, 2)) #Set that sub-board where the next_board_coords will be main_board._board[2][2] = finished_sub_board #Play a move that will make the finished board the next board (Move 2, 2) main_board = main_board.add_my_move(BoardCoords(0, 0), Move(2, 2)) #Playing anywhere is now allowed valid_boards = main_board.get_valid_boards() assert len(valid_boards) == 8 assert valid_boards == [BoardCoords(0, 0), BoardCoords(0, 1), BoardCoords(0, 2),\ BoardCoords(1, 0), BoardCoords(1, 1), BoardCoords(1, 2),\ BoardCoords(2, 0), BoardCoords(2, 1)]
def test_whenNewMoveIsOutsideBoardBoundsThenExceptionRaised(): with pytest.raises(MoveOutsideSubBoardError): SubBoard().add_my_move(Move(1, 3)) with pytest.raises(MoveOutsideSubBoardError): SubBoard().add_opponent_move(Move(-1, 1))
def test_whenNextBoardIsAvailableThenGetValidBoardsReturnsOnlyThatBoard(): board = MainBoard().add_my_move(BoardCoords(0, 0), Move(2, 2)) #Only valid board now should be 2, 2 assert len(board.get_valid_boards()) == 1 assert board.get_valid_boards()[0] == BoardCoords(2, 2)
def test_whenBoardIsPlayedThenStringRepresentationIsCorrect(): board = SubBoard().add_my_move(Move(0,0))\ .add_opponent_move(Move(1,1))\ .add_my_move(Move(2,2)) assert str(board) == "1 0 0 \n0 2 0 \n0 0 1 \n"
def test_whenNewMoveIsOutsideValidSubBoardBoundsThenExceptionRaised(): with pytest.raises(MoveOutsideSubBoardError): MainBoard().add_my_move(BoardCoords(1, 1), Move(1, 3))
def test_whenNewMoveIsNotOnGameNextBoardThenExceptionRaised(): board = MainBoard().add_my_move(BoardCoords(0, 0), Move(1, 1)) #Move must now be on board at 1, 1 with pytest.raises(MoveNotOnNextBoardError): board.add_opponent_move(BoardCoords(1, 0), Move(1, 1))
def force_sub_board_tie(main_board, board_row, board_col): return main_board._copy_applying_move(BoardCoords(board_row, board_col), PlayerMove(Player.ME, Move(0, 0)))\ ._copy_applying_move(BoardCoords(board_row, board_col), PlayerMove(Player.OPPONENT, Move(1, 1)))\ ._copy_applying_move(BoardCoords(board_row, board_col), PlayerMove(Player.ME, Move(2, 2)))\ ._copy_applying_move(BoardCoords(board_row, board_col), PlayerMove(Player.OPPONENT, Move(0, 2)))\ ._copy_applying_move(BoardCoords(board_row, board_col), PlayerMove(Player.ME, Move(2, 0)))\ ._copy_applying_move(BoardCoords(board_row, board_col), PlayerMove(Player.OPPONENT, Move(1, 0)))\ ._copy_applying_move(BoardCoords(board_row, board_col), PlayerMove(Player.ME, Move(1, 2)))\ ._copy_applying_move(BoardCoords(board_row, board_col), PlayerMove(Player.OPPONENT, Move(2, 1)))\ ._copy_applying_move(BoardCoords(board_row, board_col), PlayerMove(Player.ME, Move(0, 1)))
def test_whenNewMoveBoardCoordsAreOutOfBoundsThenExceptionRaised(): with pytest.raises(MoveOutsideMainBoardError): MainBoard().add_my_move(BoardCoords(1, 3), Move(0, 0)) with pytest.raises(MoveOutsideMainBoardError): MainBoard().add_opponent_move(BoardCoords(3, 1), Move(0, 0))
def test_whenNewMoveIsInValidCellThenReturnedBoardHasMove(): assert SubBoard().add_my_move(Move(0, 0))\ ._board[0][0].played_by == Player.ME assert SubBoard().add_opponent_move(Move(0, 0))\ ._board[0][0].played_by == Player.OPPONENT
def force_sub_board_win(main_board, board_row, board_col, player): return main_board._copy_applying_move(BoardCoords(board_row, board_col), PlayerMove(player, Move(0, 0)))\ ._copy_applying_move(BoardCoords(board_row, board_col), PlayerMove(player, Move(1, 1)))\ ._copy_applying_move(BoardCoords(board_row, board_col), PlayerMove(player, Move(2, 2)))
def test_whenMainBoardIsFinishedThenNewMoveRaisesException(): main_board = MainBoard() main_board._is_finished = True with pytest.raises(MoveInFinishedBoardError): main_board.add_my_move(BoardCoords(1, 1), Move(1, 1))
def whenPlayerMoveIsPrintedThenItIsPretty(): assert print(PlayerMove(Player.ME, Move(1,2)) is "(1,2) by Player.ME")
def whenMoveIsPrintedThenItIsPretty(): assert print(Move(1,2)) is "(1,2)"
def test_whenBoardIsInProgressThenBoardIsNotFinished(): assert SubBoard().add_my_move(Move(0, 0))\ .add_my_move(Move(0, 1))\ .add_opponent_move(Move(0, 2))\ .is_finished == False
def test_whenRowIsWonThenBoardIsFinishedAndWon(): #LTR Diag check i_win = SubBoard().add_my_move(Move(0, 0))\ .add_my_move(Move(1, 1))\ .add_my_move(Move(2, 2)) assert i_win.is_finished == True assert i_win.winner == Player.ME #RTL Diag check i_win = SubBoard().add_my_move(Move(0, 2))\ .add_my_move(Move(1, 1))\ .add_my_move(Move(2, 0)) assert i_win.is_finished == True assert i_win.winner == Player.ME #Row check opponent_wins = SubBoard().add_opponent_move(Move(1, 0))\ .add_opponent_move(Move(1, 1))\ .add_opponent_move(Move(1, 2)) assert opponent_wins.is_finished == True assert opponent_wins.winner == Player.OPPONENT #Col check opponent_wins = SubBoard().add_opponent_move(Move(0, 1))\ .add_opponent_move(Move(1, 1))\ .add_opponent_move(Move(2, 1)) assert opponent_wins.is_finished == True assert opponent_wins.winner == Player.OPPONENT