Ejemplo n.º 1
0
def program(game):
    """
    program that handles the connect four game
    """
    while connectfour.winner(game) == 0:  # while there is no winner
        if game.turn == 1:  # if it is red player's turn
            print("Player RED make your move")  # prints this message
            print()
        elif game.turn == 2:  # if it is yellow players turn
            print("Player YELLOW make your move")  # prints this message
            print()
        sharedfunctions.print_board(game.board)  # print a new game
        current_move = sharedfunctions.get_move()  # gets the players move and stores it in a variable

        while True:

            try:

                if current_move[0].upper() == "DROP":  # if players says they want to drop
                    game = connectfour.drop(
                        game, int(current_move[-1]) - 1
                    )  # drops the players move in appropriate column and changes the game state
                elif current_move[0].upper() == "POP":  # if player says they want to pop
                    game = connectfour.pop(
                        game, int(current_move[-1]) - 1
                    )  # pops players move as long as players piece is in specified column

            except:
                print("Invalid Move")  # if playes move is invalid prints this message
                print()
                current_move = sharedfunctions.get_move()  # recursively ask for players move until input is acceptable
            else:
                break  # leave the function
        print("\n\n")
    sharedfunctions.print_board(game.board)  # prints new game state
    print("Game Over")  # when game is over prints this message
    if game.turn == 1:  # if it is red playes turn
        print("Player YELLOW is the Winner")  # print this message
    elif game.turn == 2:  # if is is yellow players turn
        print("Player RED is the Winner")  # print this message
Ejemplo n.º 2
0
def main_program(game):
    '''
    program that handles the connect four game
    '''
    try:
        
        x = random() #(prompts user for a username, host and port. this is the first function in this module
        c = connecting.connection(x.host, x.port) #connects to given host and port
        connecting.send_move(c, 'I32CFSP_HELLO ' + x.username) #send a message and includes the username provided by user
        connecting.read_line(c) #reads a line from the server
        connecting.send_move(c, 'AI_GAME') #sends this message to the server
        connecting.read_line(c) #reads a line from the server
        
        while connectfour.winner(game) == 0: #while there is no winner

            if game.turn == 1: #if it is player 
                print('Player RED make your move') #prints this message
                print()


                sharedfunctions.print_board(game.board) #print a new game


                while True:

                    try:
                        current_move = sharedfunctions.get_move() #gets the players move and stores it in a variable
                        print('\n')
                        
                        if current_move[0].upper().startswith('DROP'): #takes players input at first index, makes it uppercase and checks if it equals a string
                            current_move = int(current_move[-1]) #converts players move at last index to an integer
                            current_move = current_move - 1 #subtracts 1 from players move to account for indexing
                            game = connectfour.drop(game, current_move) #calls drop function from connectfour module that handles dropping a piece onto connect four board
                            sharedfunctions.print_board(game.board) #prints updated game board
                            connecting.send_move(c, 'DROP ' + str(current_move+1)) #sends string and adds one to players move to account for subtractoin earlier then converts players move back to a string to send to the server
                            break #leaves the function
              


                        elif current_move[0].upper().startswith('POP'): #takes players input at first index, makes it uppercase and checks if it equals a string
                            current_move = int(current_move[-1]) #converts players move at last index to an integer
                            current_move = current_move - 1 #subtracts 1 from players move to account for indexing
                            game = connectfour.pop(game, current_move) #calls pop function from connectfour module that handles popping a piece onto connect four board
                            sharedfunctions.print_board(game.board) #prints updated game board
                            connecting.send_move(c, 'POP ' + str(current_move+1)) #sends string and adds one to players move to account for subtractoin earlier then converts players move back to a string to send to the server
                            break #leaves the function

                    except:
                        print('Invalid Move') #prints this message if try statement fails
                        print()
            
                    
                
            elif game.turn == 2: #if it is the servers move

                connecting.read_line(c) #read input from server
                servers_move = connecting.read_line(c) #reads another line from server. this is servers move

                if servers_move.startswith('POP'): #if servers move starts with POP
                    servers_move = int(servers_move.split()[-1]) #split servers input and grab last index and convert to an integer
                    servers_move = servers_move - 1 #subtracts one from servers move to account for 0 indexing
                    connecting.read_line(c) #read line of input from server
                    game = connectfour.pop(game, servers_move) #calls pop function from connectfour module that handles popping a piece onto connect four board
                    sharedfunctions.print_board(game.board) #prints updated game board

                else:
                    servers_move = int(servers_move.split()[-1]) #split servers input and grab last index and convert to an integer
                    servers_move = servers_move - 1 #subtracts one from servers move to account for 0 indexing
                    connecting.read_line(c) #read line of input from server
                    game = connectfour.drop(game, servers_move) #calls drop function from connectfour module that handles dropping a piece onto connect four board
                    sharedfunctions.print_board(game.board) #prints updated game board


            print('\n\n')
        sharedfunctions.print_board(game.board) #prints new game state
        print('Game Over') #when the game is over prints this message
        if game.turn == 1: #if it is red players turn (player red has lost)
            print('Sorry, you have lost') #prints this message
        elif game.turn == 2: #if it yellow players move (player yellow has lost)
            print('Congratulations! You have won') #prints this message
    except:
        print('The connection was unsuccessful')

    finally:

        try:
            connecting.close(c) #closes the connection

        except:
            print('Goodbye')