예제 #1
0
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
예제 #2
0
def _handle_ai_command(connection: connectfourserver.ConnectFourConnection) -> None:
    '''
    Handles the command sent by the AI from the server and processes it with the game state
    '''
    global game_state
    command,col = connectfourserver._read_line(connection).split()
    col = int(col)
    game_state=connectfourfunctions.give_turn(game_state,command,col)
예제 #3
0
def _handle_command(connection: connectfourserver.ConnectFourConnection) -> bool:
    '''
    Handles a single command from the user, by asking the user what command
    they'd like to execute and then handling it.  Returns True if the move is valid
    and False if the move is invalid or a Winner is declared
    '''
    global game_state
    while True:
        try:
            player = input("Please Enter (Drop and a col number) or (Pop and a col number): ")
            command,col=player.split()
            col = int(col)
            connectfourserver.send(connection,command.upper() + ' ' + str(col))
            game_state = connectfourfunctions.give_turn(game_state,command,col)
        except:
            print("Invalid Move, Please Go Again")
            connectfourserver._expect_line(connection,'INVALID')
            _handle_command(connection)
        

        if connectfourserver._expect_line(connection, 'OKAY') == False:
            return False
       
        return True