Пример #1
0
def _main():
    '''function that runs connectfour consol'''
    _greet_to_players(connectfour.RED, connectfour.YELLOW)
    the_winner = connectfour.NONE
    player_one = connectfour.RED
    player_two = connectfour.YELLOW
    column_number = connectfour.BOARD_COLUMNS
    game_state = connectfour_tools.init_the_game()

    while the_winner == connectfour.NONE:
        
        _please_make_move(game_state, player_one, player_two)    

        try:
            action = connectfour_tools.ask_action()
            move = connectfour_tools.ask_move()
            game_state = connectfour_tools.pop_or_drop(game_state, action, move)
            connectfour_tools.display_board(game_state.board)
            the_winner = connectfour.winning_player(game_state)
        except ValueError:
            print('This is invalid move. Please type in 1~{}\n'.format(column_number))
        except connectfour.InvalidConnectFourMoveError:
            print('This is invalid move. Please try again.\n')

    connectfour_tools.print_the_winning_player(the_winner, player_one, player_two)
Пример #2
0
def _conduct_connectfour_battle():
    ''' conduct the battle with AI'''
    _welcome_banner()
    
    user_host = input('Please specify your IP address or a host.').strip()
    user_port = int(input('Please enter the port.'))
    user_id = input('Please enter your User ID.').strip()
    the_winner = connectfour.NONE
    user_player = connectfour.RED
    ai_player = connectfour.YELLOW
    column_number = connectfour.BOARD_COLUMNS

    connect_four_connection = connectfour_I32CFSP_final.connect(user_host, user_port)    

    try:
        if connectfour_I32CFSP_final.login(connect_four_connection, user_id):
            print('Welcome, {}!'.format(user_id))
        else:
            print('Login Failed.')
        #consdering to erase the "else" what do you think?
        if connectfour_I32CFSP_final.declare_match(connect_four_connection):
            print('Initializing the game.')
        else:
            print('Battle request Failed.')
        #consdering to erase the "else" what do you think?    
        game_state = connectfour_tools.init_the_game()

        while the_winner == connectfour.NONE:
            _make_move_please(game_state, user_player, ai_player, user_id)
            
            try:
                action = connectfour_tools.ask_action()
                move = connectfour_tools.ask_move()
                if not connectfour_I32CFSP_final.drop_or_pop_request(action, move, connect_four_connection):
                    raise connectfour.InvalidConnectFourMoveError()
                game_state = connectfour_tools.pop_or_drop(game_state, action, move)
                connectfour_tools.display_board(game_state.board)
                the_winner = connectfour.winning_player(game_state)

                # break if the user player wins on his/her move
                if the_winner != connectfour.NONE:
                    break
                
                _make_move_please(game_state, user_player, ai_player, user_id)
                ai_message = connectfour_I32CFSP_final.classify_ai_move(connect_four_connection)
                game_state = connectfour_tools.pop_or_drop(game_state, ai_message.action, ai_message.move)
                connectfour_tools.display_board(game_state.board)
                the_winner = connectfour.winning_player(game_state)
                
            except ValueError:
                print('This is invalid move. Please type in 1~{}\n'.format(column_number))

            except connectfour.InvalidConnectFourMoveError:
                print('This is invalid move. Please try again.\n')

        _print_the_winning_player(the_winner, user_player, ai_player, user_id)

    except:
        print('Disconnected! Goodbye!')
Пример #3
0
def _conduct_connectfour_battle():
    '''conduct the battle with AI'''
    _welcome_banner()

    user_host = input('Please specify your IP address or a host.').strip()
    user_port = int(input('Please enter the port.'))
    user_id = input('Please enter your User ID.').strip()
    the_winner = connectfour.NONE
    user_player = connectfour.RED
    ai_player = connectfour.YELLOW
    column_number = connectfour.BOARD_COLUMNS

    try:
        connect_four_connection = connectfour_I32CFSP_final.connect(
            user_host, user_port)
        if connectfour_I32CFSP_final.login(connect_four_connection, user_id):
            print('Welcome, {}!'.format(user_id))
        if connectfour_I32CFSP_final.declare_match(connect_four_connection):
            print('Initializing the game.')
        game_state = connectfour_tools.init_the_game()

        while the_winner == connectfour.NONE:
            _make_move_please(game_state, user_player, ai_player, user_id)

            try:
                action = connectfour_tools.ask_action()
                move = connectfour_tools.ask_move()
                if connectfour_I32CFSP_final.drop_or_pop_request(
                        action, move, connect_four_connection) == False:
                    raise connectfour.InvalidConnectFourMoveError()
                game_state = connectfour_tools.pop_or_drop(
                    game_state, action, move)
                connectfour_tools.display_board(game_state.board)
                the_winner = connectfour.winning_player(game_state)

                if the_winner != connectfour.NONE:
                    break

                _make_move_please(game_state, user_player, ai_player, user_id)
                ai_message = connectfour_I32CFSP_final.classify_ai_move(
                    connect_four_connection)
                game_state = connectfour_tools.pop_or_drop(
                    game_state, ai_message.action, ai_message.move)
                connectfour_tools.display_board(game_state.board)
                the_winner = connectfour.winning_player(game_state)

            except ValueError:
                print('This is invalid move. Please type in 1~{}\n'.format(
                    column_number))

            except connectfour.InvalidConnectFourMoveError:
                print('This is invalid move. Please try again.\n')

        _print_the_winning_player(the_winner, user_player, ai_player, user_id)

    except:
        print('Disconnected! Goodbye!')