def main():
    '''function that runs connectfour consol'''
    
    print('Welcome to ConnectFour! \n')
    print('Player 1, you are the R player. Player 2, you are the Y player')
    the_winner = connectfour.NONE
    player_one = connectfour.RED
    player_two = connectfour.YELLOW
    connectfour_function.display_board(connectfour.new_game_state().board)
    game_state = connectfour.new_game_state()

    #play the game until it ends
    while the_winner == connectfour.NONE:
        if game_state.turn == player_one:
            print('Player 1 this is your turn. please make a move')
        else:
            print('Player 2 this is your turn. please make a move')

        try:
            move = int(input('where will you drop your piece? type 1~7'))-1
            game_state = connectfour.drop_piece(game_state, move)
            connectfour_function.display_board(game_state.board)
            the_winner = connectfour.winning_player(game_state)
        except ValueError:
            print('Invalid input. Please type integer between 1 and 7')
        except connectfour.InvalidConnectFourMoveError:
            print('The column is full. Please select other column')

    #print the winner
    if the_winner == player_one:
        print('player 1, you won!')
    else:
        print('player 2, you won!')
def who_r_you():
    state = connectfour.new_game_state()
    mode=input('Please enter what mode you want to play as (s: server, c: client, con: console): ')
    if mode == 's':
        return server(state)
    elif mode == 'c':
        return client(state)
    
    elif mode == 'con':
        return console()


    state = connectfour.new_game_state()
    
    winner = ' '
    print_board(state.board)



    while winner == ' ':
        drop_or_pop = input('It\'s ' + state.turn +' turn. Do you want to drop or pop? ')
        while drop_or_pop == 'drop':
            
            column = input('Which column (0-6) do you want to drop? ')
            newstate = _drop_piece(state,column)
            if state != newstate:
                state = newstate
                winner = connectfour.winning_player(state)
                break

        while drop_or_pop == 'pop':
            try:
                column = input('Which column (0-6) do you want to pop? ')
                newstate = _pop_piece(state,column)
                if state != newstate:
                    state = newstate
                    winner = connectfour.winning_player(state)
                    break
                if state.board == connectfour.new_game_state().board:
                    break
            except:
                print('Error. There is no piece you can pop out.\n'
                      + 'Please look for another column to pop out or you can drop another piece.')
                break
            
        while drop_or_pop != 'drop' and drop_or_pop != 'pop':
            print('Missspell. Please try again, drop or pop?')
            break
    else:
        if winner == 'R':
            print('Red won.')
        elif winner == 'Y':
            print ('Yellow won.')
def init_game():
    '''
    Creates and instance of the game for 2 players
    and runs until there is a winner
    '''
    game_state=connectfour.new_game_state()
    connectfourfunctions.print_board_game(game_state.board)


    winner = ' '
    
    while True:
        try:
            player=(input(game_state.turn + ":Drop and a col number or Pop and a col number: "))
            command,col=player.split()
            col=int(col)
            game_state=connectfourfunctions.give_turn(game_state,command,col)            
        except:
            pass
        winner=connectfour.winning_player(game_state)
        connectfourfunctions.print_board_game(game_state.board)
        
        if (winner != ' '):
            print(winner+" Won")
            break
Example #4
0
def user_interface()->None:
    print("Welcome to Connect Four!")
    last_state = connectfour.new_game_state()
    linkui._rotate_board(last_state)
    while True:
        option = linkui.ask_for_option()
        if option == 'D':
            try:
                
                col = linkui.ask_for_col_drop()
            except:
                print('Invalid value.')
            else:
                last_state = linkui.option_d(last_state,col)
        elif option == 'P':
            try:
                
                col = linkui.ask_for_col_pop()
            except:
                print('Invalid value.')
            else:
                last_state = linkui.option_p(last_state,col)
        else:
            print('Invalid Command.')
            
        

        if connectfour.winning_player(last_state) != ' ':
            print("Player " + connectfour.winning_player(last_state) + " wins!")
            break
    rematch()
def main_program() -> None:
##    while True:
    State = C.new_game_state()
    while True:
        winner = C.winning_player(State)
        F.new_board(State.board)
        if winner != C.NONE:
            break
        try:
            print("It's " + F.full_name(State.turn) + " player's turn. ", end = '')
            command = input(F.Menu).upper()
            if command == 'A':
                column_number = int(input('please enter the column number: '))
                State = C.drop_piece(State, column_number - 1)
                print()
            elif command == 'B':
                column_number = int(input('please enter the column number: '))
                State = C.pop_piece(State, column_number - 1)
                print()
            else:
                F.invalid_command(command)
        except C.InvalidConnectFourMoveError:
            print('\nInvaild command. Please try again.\n')
        except ValueError:
            print('\nError. Column number must be between 1 and 7.\n') 
        except C.ConnectFourGameOverError:
            print('\nError. Game is over.\n')
    if winner == C.RED:
        print('Red player wins!')
    elif winner == C.YELLOW:
        print('Yellow player wins!')
    print()
def menu() -> None:
    'This function is the main menu of the connect four console game'
    game = connectfour.new_game_state()
    winner = connectfour.NONE
    while True:
        while True:
            username = input('Please enter a username (no spaces!):  ').strip()
            if username.find(' ') < 0:
                break
            else:
                print('Invalid input.  Please try a username without spaces.\n')

        host = input('Please enter the host name:  ')
        

        while True:
            try:
                port = int(input('Please enter the port:  '))
                break
            except:
                print('Invalid input, please enter a number.\n')

        print('Trying to connect...')
        try:
            connection = connectfour_protocol.connect(host, port)
            print('Connected Successfully!')        
            if connectfour_protocol.login(connection, username) == True:
                print('Login Successful!')
                if connectfour_protocol.startGame(connection) == True:
                    print('Game Started!')
                    break
                else:
                    print('Could not Start Game!  Try again.\n')
                    connectfour_protocol.close(connection)
            else:
                print('Could not log in!  Try again.\n')
                connectfour_protocol.close(connection)
        except:
            print('Connection Failed!  Try again.\n')

    try:
        while winner == connectfour.NONE:
            while game.turn == connectfour.RED:
                connectfour_game_functions.printBoard(game)
                game = connectfour_game_functions.directions_network(game, username, connection)
                winner = connectfour.winning_player(game)
            if winner == connectfour.RED:
                    break
            connectfour_game_functions.printBoard(game)
            game = connectfour_game_functions.move_network_ai(game, connection)
            winner = connectfour.winning_player(game)
        connectfour_game_functions.printBoard(game)
        if winner == connectfour.RED:
            print('The winner is ' + username + '!')
        else:
            print('The winner is AI!')
    finally:
        connectfour_protocol.close(connection)
def who_r_you():
    state = connectfour.new_game_state()
    mode=input('Please enter what mode you want to play (s: server, c: client, con: console): ')
    if mode == 's':
        return server(state)
    elif mode == 'c':
        return client(state)
    elif mode == 'con':
        return console()
    
    else:
        print('Error, please try again.')
        who_r_you()
def menu() -> None:
    'This function is the main menu of the connect four console game'
    game = connectfour.new_game_state()
    winner = connectfour.NONE
    while winner == connectfour.NONE:  
        connectfour_game_functions.printBoard(game)
        game = connectfour_game_functions.directions_console(game)
        winner = connectfour.winning_player(game)
    connectfour_game_functions.printBoard(game)
    if winner == connectfour.RED:
        print('The winner is Red!')
    else:
        print('The winner is Yellow!')
def console():
    state = connectfour.new_game_state()
    winner = ' '
    print_board(state.board)
    while winner == ' ':
        
        drop_or_pop = input('It\'s ' + state.turn +' turn. Please enter either drop or pop: ')

        while drop_or_pop == 'drop':
            column = input('Please enter one number from 0 to 6 for the column you want to drop: ')
            newstate = _drop_piece(state,column)
            if state != newstate:
                state = newstate
                winner = connectfour.winning_player(state)
                break

        while drop_or_pop == 'pop':
            try:
                column = input('Please enter one number from 0 to 6 for the column you want to pop: ')
                newstate = _pop_piece(state,column)
                if state != newstate:
                    state = newstate
                    winner = connectfour.winning_player(state)
                    break
                if state.board == connectfour.new_game_state().board:
                    break
            except:
                print('Error. There is no piece you can pop out.\n'
                      + 'Please look for another column to pop out or you can drop another piece.')
                break
            
        while drop_or_pop != 'drop' and drop_or_pop != 'pop':
            print('Error. Please enter either drop or pop.')
            break
    else:
        if winner == 'R':
            print('Red won.')
        elif winner == 'Y':
            print ('Yellow won.')
def conduct_connectfour_battle(user_host: str, user_port: int, user_id: str) -> None:
    the_winner = connectfour.NONE
    the_user = connectfour.RED
    try:
        s = connect_socket(user_host, user_port, user_id)
        setup_game(s)
        print('Connected! Let the Battle Begin!!!!!')
        game_state = connectfour.new_game_state()
        connectfour_consol.display_board(game_state.board)
        while the_winner == connectfour.NONE:
            if game_state.turn == the_user:
                print('%s this is your turn. Please make a move.' % user_id)
                move = int(input('where will you drop your piece? type 1~7.'))-1
            else:
                
        game_state = connectfour.drop_piece(game_state, move)
        display_board(game_state.board)
        the_winner = connectfour.winning_player(game_state)                
        
    except:
        print('Connection failed. Closing the game.')
    finally:
        s.close()
Example #11
0
# Jonathan Law and Tim Tandiyo 
# ICS 32 Project 2 

import connectfour
import C4S


def move_menu(game):
    while True:
        C4S.print_board(game)
        print('Current turn: ' + game.turn)
        move = C4S.pop_or_drop()
        game = C4S.make_move(game, move)
        if connectfour.winning_player(game) == connectfour.RED or connectfour.winning_player(game) == connectfour.YELLOW:
            C4S.print_board(game)
            print('Winning player: ' + connectfour.winning_player(game))
            break
                
               
game = connectfour.new_game_state() 
move_menu(game)
def main_program(host, port) -> None:
    username = _P_.confirm_username()
    game_socket = socket.socket()
    try:
        print('\nconnecting to server...\n')
        game_socket.connect((host, port))
        print('Connected successfully.\n')
        _P_.send_message(game_socket, str('I32CFSP_HELLO ' + username))
        reply1 = _P_.receive_message(game_socket)
        print(reply1, end = '\n\n')
        _P_.send_message(game_socket, 'AI_GAME')
        reply2 = _P_.receive_message(game_socket)
        print(reply2, end = '\n\n')
    except ConnectionRefusedError:
        print('Invalid port')
        return 'Invalid'
##    while True:
    State = _X_.new_game_state()
    while True:
        winner = _X_.winning_player(State)
        _F_.new_board(State.board)
        if winner != _X_.NONE:
            break
        try:
            _F_.turn(State.turn)
            command = input(_F_.Menu).upper()
            if command == 'A':
                column_number = int(input('please enter the column number: '))
                State = _X_.drop_piece(State, column_number - 1)
                print()
                AI = _P_.AI(game_socket, 'A', column_number).split()
##                print(AI)
##                _C_.new_board(State.board)
##                print()
##                _N_.AI_turn(State.turn)
                if AI[0] == 'DROP':
                    State = _X_.drop_piece(State, int(AI[1]) - 1)
                elif AI[0] == 'POP':
                    State = _X_.pop_piece(State, int(AI[1]) - 1)
            elif command == 'B':
                column_number = int(input('please enter the column number: '))
                State = _X_.pop_piece(State, column_number - 1)
                print()
                AI = _P_.AI(game_socket, 'B', column_number).split()
##                print(AI)
##                _C_.new_board(State.board)
##                print()
##                _N_.AI_turn(State.turn)
                if AI[0] == 'DROP':
                    State = _X_.drop_piece(State, int(AI[1]) - 1)
                elif AI[0] == 'POP':
                    State = _X_.pop_piece(State, int(AI[1]) - 1)
            else:
                _F_.invalid_command(command)
        except _X_.InvalidConnectFourMoveError:
            print('\nInvaild command. Please try again.\n')
        except ValueError:
            print('\nError. Column number must be between 1 and 7.\n') 
        except _X_.ConnectFourGameOverError:
            print('\nError. Game is over.\n')
    if winner == _X_.RED:
        print('Red player wins!')
    elif winner == _X_.YELLOW:
        print('Yellow player wins!')
##        restart = input('\nPlay again?  (Y/N)').upper()
##        if restart == 'Y':
##            print('game restart!\n\n')
##            State = _X_.new_game_state()
##        elif restart == 'N':
##            print('\nThanks for playing, Bye!')
##            break
##        else:
##            _C_.invalid_command(restart)
    print('\nGame is over.\nThanks for playing, Bye!')
def connect_four():
    print('Welcom to Connect Four!')
    Start_Game = connectfour.new_game_state()
    select_game_mode(Start_Game)
Example #14
0
def play_game(connection:i32cfsp.i32cfconnection, username: str) -> None:
    '''plays game'''
    last_state = connectfour.new_game_state()
    linkui._rotate_board(last_state)
    while True:
        print(username)
        option = linkui._ask_for_option()
        if option == 'D':
            try:
                col = linkui._ask_for_col_drop()
            except:
                print("invalid value.")
            else:
                last_state = linkui._option_d(last_state, col)
                if connectfour.winning_player(last_state) != ' ':
                    print("Player " + username[1:] + " (Red) wins!")
                    break
                print()
                if i32cfsp.login(connection, username):
                    move = send_read_server(option, col, connection)                    
                    if move[0] == 'D':
                        col_server = int(move[-1])
                        last_state = linkui._option_d(last_state, col_server)
                        i32cfsp._read_line(connection)
                    elif move[0] == 'P':
                        col_server = int(move[-1])
                        last_state = linkui._option_p(last_state, col_server)
                        i32cfsp._read_line(connection)

                    if connectfour.winning_player(last_state) != ' ':
                        print("Computer (Yellow) wins!")
                        break
                else:
                    print("Connection lost")


        elif option == 'P':
            try:
                col = linkui._ask_for_col_pop()
            except:
                print("invalid value.")
            else:
                last_state = linkui._option_p(last_state, col)
                if connectfour.winning_player(last_state) != ' ':
                    print("Player " + username[1:]+ " (Red) wins!")
                    break
                print()
                if i32cfsp.login(connection, username):
                    move = send_read_server(option, col, connection)

                    
                    if move[0] == 'D':
                        col_server = int(move[-1])
                        last_state = linkui._option_d(last_state, col_server)
                        i32cfsp._read_line(connection)
                    elif move[0] == 'P':
                        col_server = int(move[-1])
                        last_state = linkui._option_p(last_state, col_server)
                        i32cfsp._read_line(connection)


                    if connectfour.winning_player(last_state) != ' ':
                        print("Computer (Yellow) wins!")
                        break
                else:
                    print("Connection lost")
        else:
            print('Invalid command')
def init_the_game() -> connectfour.ConnectFourGameState:
    """initialize the game and print the board"""
    display_board(connectfour.new_game_state().board)
    game_state = connectfour.new_game_state()
    return game_state