示例#1
0
def main():

    # Load the dictionary.
    dictionary = Dictionary.load(DICTIONARY_FILENAME)
    board = Board()

    # Keep track of the winning solution at each round.
    winners = []

    # List of letters we can still pick from.
    bag = get_full_bag()

    # Rack starts out empty. Keep track of current and last rack state.
    rack = ""
    old_rack = ""
    count = 0

    # Keep track of current and last board state,
    update_board = None
    old_board = None

    # Baxter's score
    my_score = 0

    #Create classifier
    classify = CNN_Model()

    # Create video feeds
    # cam = cv2.VideoCapture(1)
    # print cam.isOpened()

    # Keep playing until we're out of tiles or solutions.
    while count < 8:
        count+=1
        # Fill up our rack.
        print "Bag: %s" % "".join(bag)
        old_rack = rack

        # Updates rack with current rack from video feed.
        # cam1 = cv2.VideoCapture(1)
        # print cam1.isOpened()
        cam1 = 1
        rack = get_rack(classify,cam1)
        # cam1.release()
        cv2.destroyAllWindows()

        # Get a list of possible solutions. These aren't all necessarily legal.
        solutions = board.generate_solutions(rack, dictionary)

        solution = board.find_best_solution(solutions, dictionary)

        if solution:
            print "Winner: %s" % solution

            # Play the winning solution.
            board.create_board()
            print("I suggest you play the word:"+solution.word)
            #speech.speak(solution)
        else:
            # Should put letters back in bag.
            break
        print board
        
        # Wait for "Enter" press, signifying the player has completed his/her turn.
        #wait = raw_input("Press enter when finished with move")

        # Get word that was just played on the board by fetching the new board state.
        # cam1.release()
        # del cam1
        # cam1 = cv2.VideoCapture(0)
        # while not cam1.isOpened():
        #     cam1.release()
        #     del cam1
        #     cam1 = cv2.VideoCapture(0)

        update_board = get_board(classify,cam1)
        # cam1.release()
        
        move,letter_placed_on_board = board.get_played_word(update_board,old_board,dictionary)
        print ("The word"+move+"was just played")

        if (move == solution.word):
            print("Player listened to Baxter")
        else:
            print("defied Baxter")




        print "Baxter's Score: %d" % my_score

        generate_rack(rack,old_rack,bag)

        for char in letter_placed_on_board:
            rack = rack.replace(char,"")

    print "Baxter's Score: %d" % my_score
    print "Baxter's Words:"
    for rack, winner in winners:
        print "    %s: %s" % (rack, winner)
示例#2
0
def main():
    # Load the dictionary.
    dictionary = Dictionary.load(DICTIONARY_FILENAME)
    board = Board()

    # Keep track of the winning solution at each round.
    winners = []

    # List of letters we can still pick from.
    bag = get_full_bag()


    # Rack starts out empty.
    rack = ""
    old_rack = ""
    count = 0
    # Baxter's score
    my_score = 0
    # Opponent's score
    opp_score = 0
    # Parameter setting the minimum point defiticit of Baxter's opponent\
    # before Baxter attempts to manipulate his teammate to play low-scoring words
    manip_threshold = 50
    # Keep playing until we're out of tiles or solutions.
    while count < 8:
        count = count +1 
        # Fill up our rack.
        print "Bag: %s" % "".join(bag)
        
        update_board = None
        old_board = None
        # rack = get_rack()
        #if not rack:
        #    break
        old_rack = rack
        rack = scrabblerack.boardtrainer('')
        print rack
        

        # start_time = 0
        # elapsed_time = 0
        # wait_time = 8*60
        # while (elapsed_time < wait_time)
        #old_board = updated_board
        #updated_board = board.create_board()
        
        #rack = get_rack()
        #     if rack:
        #        generate_rack(rack,old_rack,bag)
        #         break
        #opp_score = opp_score + board.opponent_word_score(update_board,old_board,dictionary)
        # opp_score = opp_score + board.opponent_word_score(dictionary)
        #         elapsed_time = time.time() - start_time
        #generate_rack(rack,old_rack,bag)

        # Get a list of possible solutions. These aren't all necessarily legal.
        solutions = board.generate_solutions(rack, dictionary)
        #rack = generate_rack(rack,old_rack,bag)

        # Weed out the illegal solutions and score the rest, returning the
        # highest-scoring solution.
        
        # sets the percentile of the word's score
        low_score_param = 1/2
        if (my_score > (opp_score + manip_threshold) or my_score > 500):
            solution  = board.find_suboptimal_solution(solutions, dictionary, low_score_param)
        else: solution = board.find_best_solution(solutions, dictionary)

        if solution:
            print "Winner: %s" % solution

            # Play the winning solution.
            board.add_solution(solution)
            winners.append((rack, solution))
            my_score = my_score + solution.score

            # Deplete the rack of the used tiles.
            rack = solution.get_new_rack(rack)
        else:
            # Should put letters back in bag.
            break
        print board
        board.create_board()
        speech.speak(solution)

        word = raw_input("What word would you like to play? ")
        row = raw_input("What row does it start at? ")
        col = raw_input("What column does it start at? ")
        direc = raw_input("Is it oriented vertically or horizontally? ")
        #indices = raw_input("indices? ")
        # if (dir == 'horizontally'): dir = direction.Direction(0,1)
        # else: direction.Direction(1,0)
        #opp_score = opp_score + board.opponent_word_score(row,col,dir,word,dictionary,indices)
        row = int(row)
        col = int(col)
        opp_score = opp_score + board.opponent_word_score(row,col,word,direc,dictionary)


        print "Baxter's Score: %d" % my_score
        print "Opponent's Score: %d" % opp_score

    print "Baxter's Score: %d" % my_score
    print "Opponent's Score: %d" % opp_score
    print "Baxter's Words:"
    for rack, winner in winners:
        print "    %s: %s" % (rack, winner)