Beispiel #1
0
def main():
    print('Starting a new game...!')
    newgamestate = connectfour.new_game_state()
    ##    connectfour.BOARD_ROWS = int(input('How many columns would you like to play with?: '))
    ##    connectfour.BOARD_COLUMNS = int(input('How many rows would you like to play with?: '))
    while True:
        if newgamestate.turn == 'R':
            red_turn = int(
                input(
                    '''Red's turn!\n Please enter the corresponding numbers for desired action\n 1.) Drop piece, 2.)Pop Piece: '''
                ))
            if red_turn == 1:
                red_drop = int(
                    input(
                        "Enter number of column to drop red piece from 0 to {}: "
                        .format(connectfour.BOARD_COLUMNS - 1)))
                if red_drop > connectfour.BOARD_COLUMNS or red_drop < 0:
                    raise connectfour.InvalidConnectFourMoveError()
                else:
                    newgamestate = connectfour.drop_piece(
                        newgamestate, red_drop)
                    print_board(newgamestate)
            else:
                red_pop = int(
                    input(
                        'Enter column number to pop red piece from 0 to {}: '.
                        format(connectfour.BOARD_COLUMNS - 1)))
                if red_pop > connectfour.BOARD_COLUMNS or red_pop < 0:
                    raise connectfour.InvalidConnectFourMoveError()
                else:
                    newgamestate = connectfour.pop_piece(newgamestate, red_pop)
                    print_board(newgamestate)
        else:
            yellow_turn = int(
                input(
                    '''Yellow's turn!\n Please enter the corresponding numbers for desired action\n 1.) Drop piece, 2.)Pop Piece: '''
                ))
            if yellow_turn == 1:
                yellow_drop = int(
                    input(
                        "Enter column number to drop yellow piece from 0 to {}: "
                        .format(connectfour.BOARD_COLUMNS - 1)))
                newgamestate = connectfour.drop_piece(newgamestate,
                                                      yellow_drop)
                print_board(newgamestate)
            else:
                yellow_pop = int(
                    input(
                        'Enter column number to pop yellow piece from 0 to {}: '
                        .format(connectfour.BOARD_COLUMNS - 1)))
                newgamestate = connectfour.pop_piece(newgamestate, yellow_pop)
                print_board(newgamestate)
        if connectfour.winning_player(newgamestate) == 'R':
            print('RED player has won! Congradulations!')
            break
        elif connectfour.winning_player(newgamestate) == 'Y':
            print('YELLOW player has won! Congradulations!')
            break
Beispiel #2
0
    def __init__(self: object):
        """Init new game"""

        self.state = connectfour.new_game_state()

        while True:
            self.print_state()

            winner = connectfour.winning_player(self.state)
            if winner != connectfour.NONE:
                if winner == connectfour.RED:
                    print("Red is the winner!")
                elif winner == connectfour.YELLOW:
                    print("Yellow is the winner!")
                break

            input_is_vaild = False
            while not input_is_vaild:
                action = self._get_input()
                try:
                    if action["action"] == "d":
                        self.state = connectfour.drop_piece(
                            self.state, action["column_number"] - 1)
                    elif action["action"] == "p":
                        self.state = connectfour.pop_piece(
                            self.state, action["column_number"] - 1)
                    else:
                        raise connectfour.InvalidConnectFourMoveError()
                    input_is_vaild = True
                except:
                    print("Sorry, invalid move.")
Beispiel #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!')
Beispiel #4
0
def handle_game_menu(current_game: connectfour.ConnectFourGameState) -> str:
    """Displays Startup Menu, accepts and processes commands
    """
    while True:
        response = input(GAME_MENU).strip().lower()
        if response == 'd':
            while True:
                try:
                    column_number = input(
                        'Enter a Column to drop piece or enter nothing to return to previous menu: '
                    ).strip()
                    if column_number == '':
                        break
                    else:
                        column_number = int(column_number) - 1
                    if column_number < 0:
                        raise connectfour.InvalidConnectFourMoveError()
                    else:
                        try:
                            if connectfour._find_bottom_empty_row_in_column(
                                    current_game.board, column_number) != -1:
                                column_number += 1
                                return 'DROP {}'.format(column_number)
                            else:
                                raise connectfour.InvalidConnectFourMoveError()
                        except IndexError:
                            raise connectfour.InvalidConnectFourMoveError()
                except connectfour.InvalidConnectFourMoveError:
                    print(
                        "Cannot make this move. Please input a different move."
                    )
                    break
                except ValueError:
                    print("Invalid Command. Please enter a different command.")
        elif response == 'p':
            while True:
                try:
                    column_number = input(
                        'Enter a Column to pop piece out or enter nothing to return to previous menu: '
                    ).strip()
                    if column_number == '':
                        break
                    else:
                        column_number = int(column_number) - 1
                    if column_number < 0:
                        raise connectfour.InvalidConnectFourMoveError()
                    else:
                        try:
                            if current_game.turn == current_game.board[int(
                                    column_number)][connectfour.BOARD_ROWS -
                                                    1]:
                                column_number += 1
                                return 'POP {}'.format(column_number)
                            else:
                                raise connectfour.InvalidConnectFourMoveError()
                        except IndexError:
                            raise connectfour.InvalidConnectFourMoveError()
                except connectfour.InvalidConnectFourMoveError:
                    print(
                        "Cannot make this move. Please input a different move."
                    )
                    break
                except ValueError:
                    print("Invalid Command. Please enter a different command.")
        else:
            invalid_command(response)