def main() -> None:
    # Connect to server
    while True:                 
        connection = net_utils.connect_to_server()
        if connection != None:
            user = net_utils.get_username()
            if net_utils.start_game(connection, user):                
                break
            else:
                return

    # Test code
    #connection = net_utils._open_connection("woodhouse.ics.uci.edu", 4444)
    #user = "******"
    #net_utils.start_game(connection, user)
    #random.seed(calendar.timegm(time.gmtime()))
    
    # Variable Initialization
    winner = game.NONE
    players = ("RED", "YELLOW")
    player_names = {"RED": user, "YELLOW": "Server AI"}
    server_turn = False
    player_move = None

    input_format = "[{{}}] {}:".format(user)
    utils.print_instructions()
    game_state = game.new_game()

    while winner == game.NONE:
        utils.print_board(game_state.board)

        # Receive player moves and execute server side
        if not server_turn:
            # Test bot code
            #player_move = get_random_move()
            player_move = utils.get_input(game_state,players[server_turn], input_format)
        else:
            player_move = net_utils.sync_move(connection, player_move.action, player_move.col)
            print('[{}] {}: {} {}'.format(players[server_turn], \
                                          player_names[players[server_turn]], \
                                          player_move.action.title(), \
                                          (player_move.col + 1)))

        # Execute player moves
        try:
            game_state = utils.execute_move(game_state,player_move)
            winner = game.winner(game_state)
        except game.InvalidMoveError:
            print("[Connect Four] Invalid move")
            continue

        server_turn = not server_turn

    # Sync final moves to server and validate
    #  the winner remotely and locally
    player_move = net_utils.sync_move(connection, player_move.action, player_move.col)
    utils.print_board(game_state.board)
    _validate_winner(players[winner-1],player_move.winner,player_names)

    net_utils.end_game(connection)
Example #2
0
def run_game() -> None:
    ''' Starts a local game '''
    utils.print_instructions()
    game_state = game.new_game()

    winner = game.NONE
    players = ("RED", "YELLOW")
    yellow_turn = False

    while winner == game.NONE:
        utils.print_board(game_state.board)
        player_move = utils.get_input(game_state, players[yellow_turn])

        # Select a player action
        try:
            game_state = utils.execute_move(game_state, player_move)
        except game.InvalidMoveError:
            print("[Connect Four] Invalid move")
            continue

        # Swap players
        yellow_turn = not yellow_turn

        winner = game.winner(game_state)

    utils.print_board(game_state.board)

    winner_name = 'NONE'
    if winner == game.RED:
        winner_name = 'RED'
    elif winner == game.YELLOW:
        winner_name = 'YELLOW'

    print('[Connect Four] Winner: {}'.format(winner_name))
def run_game() -> None:
    ''' Starts a local game '''
    utils.print_instructions()
    game_state = game.new_game()
    
    winner = game.NONE
    players = ("RED", "YELLOW")
    yellow_turn =  False
    
    while winner == game.NONE:
        utils.print_board(game_state.board)
        player_move = utils.get_input(game_state,players[yellow_turn])
        
        # Select a player action 
        try:
            game_state = utils.execute_move(game_state,player_move)
        except game.InvalidMoveError:
            print("[Connect Four] Invalid move")
            continue

        # Swap players
        yellow_turn = not yellow_turn

        winner = game.winner(game_state)

    utils.print_board(game_state.board)

    winner_name = 'NONE'
    if winner == game.RED:
        winner_name = 'RED'
    elif winner == game.YELLOW:
        winner_name = 'YELLOW'

    print('[Connect Four] Winner: {}'.format(winner_name))