Exemple #1
0
def test4_horizontal_match_found():
    b = Game_Board()
    p1 = Player('X', 3, 1)
    b.assign_player_move_on_board(p1)
    p2 = Player('O', 3, 2)
    b.assign_player_move_on_board(p2)
    p3 = Player('X', 3, 3)
    b.assign_player_move_on_board(p3)
    assert b.is_horizontal_match_found('3, 3', 'X') == False
Exemple #2
0
def test2_diagonal2_match_found():
    b = Game_Board()
    p1 = Player('O', 3, 1)
    b.assign_player_move_on_board(p1)
    p2 = Player('X', 2, 2)
    b.assign_player_move_on_board(p2)
    p3 = Player('X', 1, 3)
    b.assign_player_move_on_board(p3)
    assert b.is_diagonal2_match_found('X') == False
Exemple #3
0
def test1_horizontal_match_found():
    b = Game_Board()
    p1 = Player('X', 2, 1)
    b.assign_player_move_on_board(p1)
    p2 = Player('X', 2, 2)
    b.assign_player_move_on_board(p2)
    p3 = Player('X', 2, 3)
    b.assign_player_move_on_board(p3)
    assert b.is_horizontal_match_found('2, 3', 'X') == True
Exemple #4
0
def test4_vertical_match_found():
    b = Game_Board()
    p1 = Player('X', 1, 3)
    b.assign_player_move_on_board(p1)
    p2 = Player('O', 2, 3)
    b.assign_player_move_on_board(p2)
    p3 = Player('X', 3, 3)
    b.assign_player_move_on_board(p3)
    assert b.is_vertical_match_found('3, 3', 'X') == False
Exemple #5
0
def test1_vertical_match_found():
    b = Game_Board()
    p1 = Player('X', 1, 1)
    b.assign_player_move_on_board(p1)
    p2 = Player('X', 2, 1)
    b.assign_player_move_on_board(p2)
    p3 = Player('X', 3, 1)
    b.assign_player_move_on_board(p3)
    assert b.is_vertical_match_found('2, 1', 'X') == True
Exemple #6
0
def test_any_move_left():
    b = Game_Board()
    b._moves_left=1
    p1 = Player('X', 2, 3)
    b.assign_player_move_on_board(p1)
    b.display_board()
    assert b._is_any_move_left()==0
 def __init__(self):
     self._game_board = Game_Board()
     self._first_player_turn = False
class Tic_Tac_Toe:
    def __init__(self):
        self._game_board = Game_Board()
        self._first_player_turn = False

    def _get_player_turn(self):
        self._first_player_turn = not self._first_player_turn
        if self._first_player_turn:
            return ('Player 1', 'X')
        else:
            return ('Player 2', 'O')

    def _is_input_pattern_matched(self, coordinates):
        return re.match('^[0-9]+\s*,\s*[0-9]+$|q', coordinates)

    def _get_user_input(self):
        player_number, player_symbol = self._get_player_turn()
        while True:
            coordinates = input(
                f'{player_number} enter a coord x,y to place your {player_symbol} or enter '
                'q'
                ' to give up: ').strip()
            if not self._is_input_pattern_matched(coordinates):
                print('Oh no, Invalid input format. Try again...')
                continue
            if (coordinates.lower()).strip() == 'q':
                print(
                    f'{player_number} gave up, try again and you may win next time'
                )
                sys.exit()
            if not self._game_board.are_board_coordnates_valid(coordinates):
                print(
                    f'Oh no, input coordinates are outside the board! Try again...'
                )
                continue
            if not self._game_board.are_board_coordinates_vacant(coordinates):
                print(f'Oh no, a piece is already at this place! Try again...')
                continue
            break
        return (coordinates, player_number, player_symbol)

    def is_game_drawn(self):
        if not self._game_board._is_any_move_left():
            print(f'Good Try, Game drawn this time, you may win next time')
            return True
        return False

    def is_game_won(self, coordinates_to_check, player_symbol):
        game_won = False
        if self._game_board.is_horizontal_match_found(coordinates_to_check,
                                                      player_symbol):
            game_won = True
        if self._game_board.is_vertical_match_found(coordinates_to_check,
                                                    player_symbol):
            game_won = True
        if self._game_board.is_diagonal1_match_found(player_symbol):
            game_won = True
        if self._game_board.is_diagonal2_match_found(player_symbol):
            game_won = True
        if game_won:
            print("well done you've won the game!")
            return True
        return False

    def _is_game_over(self, coordinates, player_symbol):
        return any(
            (self.is_game_drawn(), self.is_game_won(coordinates,
                                                    player_symbol)))

    def start_game(self):
        while True:
            self._game_board.display_board_message()
            self._game_board.display_board()
            coordinates, player_number, player_symbol = self._get_user_input()
            print('Move accepted, ', end=' ')
            x_coordinate, y_coordinate = self._game_board._extract_x_and_y_coordinates(
                coordinates)
            self._game_board.assign_player_move_on_board(
                Player(player_symbol, x_coordinate + 1, y_coordinate + 1))
            if self._is_game_over(coordinates, player_symbol):
                self._game_board.display_board()
                break
Exemple #9
0
def test_board_for_out_of_board_coordinates(input, expected):
    b = Game_Board()
    assert b.are_board_coordnates_valid(input) == expected
Exemple #10
0
def test_board_for_vacant_positions(input, expected):
    b = Game_Board()
    p1 = Player('X', 2, 3)
    b.assign_player_move_on_board(p1)
    assert b.are_board_coordinates_vacant(input) == expected