示例#1
0
def main():
    for iteration in range(ITERATION):
        print("Iteration: " + str(iteration + 1))
        start = time.time()
        step = 0
        matrix = np.zeros((4, 4), dtype=np.int)
        matrix = logic.add_two(matrix)
        while True:
            matrix = logic.add_two(matrix)
            if logic.game_state(matrix) == 'lose':
                break
            if logic.game_state(matrix) == 'lose':
                break
            matrix = montecarlo.getMove(matrix, NUM_BACKGROUND_RUNS)
            step += 1
            # move=montecarlo.getMove(matrix, NUM_BACKGROUND_RUNS)
            # # print("got a move " + str(move))
            # matrix=montecarlo.moveGrid(matrix,move)
            # step+=1
        print(matrix)
        print("Step= " + str(step))
        print("Max= " + str(2**np.max(matrix)))
        print("Score= " + str(logic.getScore(matrix)))
        print('Depth= ' + str(NUM_BACKGROUND_RUNS))
        print('Time= ' + str(time.time() - start))
        print('')
        stat.append((step, 2**np.max(matrix), logic.getScore(matrix)))
示例#2
0
def main():
    for iteration in range(ITERATION):
        print("Iternaton: " + str(iteration + 1))
        step = 0
        matrix = np.zeros((4, 4))
        matrix = logic.add_two(matrix)
        while True:
            matrix = logic.add_two(matrix)
            if logic.game_state(matrix) == 'lose':
                break
            while True:
                move = ExpectiMax.getMove(matrix, DEPTH)
                if move == "'w'":
                    newMatrix, _ = logic.up(matrix)
                if move == "'a'":
                    newMatrix, _ = logic.left(matrix)
                if move == "'s'":
                    newMatrix, _ = logic.down(matrix)
                if move == "'d'":
                    newMatrix, _ = logic.right(matrix)
                if ExpectiMax.isSame(newMatrix, matrix) == False:
                    matrix = newMatrix
                    break
            step += 1
        print("Step= " + str(step))
        print("Max= " + str(np.max(matrix)))
        print("Score= " + str(logic.getScore(matrix)))
        print('')
        stat.append((step, np.max(matrix), logic.getScore(matrix)))
    np.savetxt("stat.txt", stat)
示例#3
0
def expectiMax(grid, agent, depth):
    score = 0

    if (logic.game_state(grid) == 'lose'):
        return FAIL_SCORE
    if depth == 0:
        return logic.getScore(grid) * getUtility(grid)

    # Player's turn
    if agent == 0:
        for i in range(4):
            newGrid = moveGrid(grid, i)
            score = max(score, expectiMax(newGrid, 1, depth))
        return score
    # Computer's turn
    elif agent == 1:
        count = 0
        score = 0
        for i in range(4):
            for j in range(4):
                if grid[i][j] == 0:
                    grid[i][j] = 2
                    count += 1
                    score += expectiMax(grid, 0, depth - 1)
                    grid[i][j] = 0
        if count == 0:
            return FAIL_SCORE
        else:
            return score / count
示例#4
0
def main():
    for iteration in range(ITERATION):
        print("Iteration: " + str(iteration + 1))
        start = time.time()
        step = 0
        matrix = logic.new_game()
        matrix = logic.add_two(matrix)
        while True:
            matrix = logic.add_two(matrix)
            if logic.gameOver(matrix):
                break
            # print("given this board")
            # logic.printBoard(matrix)
            move = ExpectiMax.getMove(matrix, DEPTH)
            matrix = ExpectiMax.moveGrid(matrix, move)
            # print("expectimax recommends this move " + str(move))
            # print("resulting in this board")
            # logic.printBoard(matrix)
            step += 1

        print("Step= " + str(step))
        print("Max= " + str(2**logic.getMax(matrix)))
        print("Score= " + str(logic.getScore(matrix)))
        print('Depth= ' + str(DEPTH))
        print('Time= ' + str(time.time() - start))
        print('')
示例#5
0
def randomPolicyUntilGameOver(grid, numBackgroundRuns):
    score = 0
    for i in range(numBackgroundRuns):
        curGrid = grid.copy()
        while True:
            curGrid = logic.add_two(curGrid)
            board_list = randomMove(curGrid)
            if not board_list:
                break
            curGrid = random.choice(board_list)
        score += logic.getScore(curGrid)
    return score
示例#6
0
def iteration(iter):
    start = time.time()
    step = 0
    matrix = np.zeros((4, 4), dtype=np.int)
    matrix = logic.add_two(matrix)
    while True:
        matrix = logic.add_two(matrix)
        if logic.game_state(matrix) == 'lose':
            break

        move = ExpectiMax.getMove(matrix, DEPTH)
        matrix = ExpectiMax.moveGrid(matrix, move)
        step += 1

    print("Iteration: " + str(iter + 1))
    print("Step= " + str(step))
    print("Max= " + str(2**np.max(matrix)))
    print("Score= " + str(logic.getScore(matrix)))
    print('Depth= ' + str(DEPTH))
    print('Time= ' + str(time.time() - start))
    print('')

    return (step, 2**np.max(matrix), logic.getScore(matrix))
示例#7
0
def displayCorrection(questionsNode, answer):
    # Check if answer is correct:
    category = logic.getCategory(questionsNode)
    question = logic.getQuestion(questionsNode)

    if logic.checkAnswer(category, question, answer) == True:
        print("Correct!")
    # Makes correction if incorrect
    else:
        correctAnswer = logic.getCorrectAnswer(questionsNode)
        reference = logic.getReference(questionsNode)
        print("Incorrect (-$4).  The correct answer was " + correctAnswer) # + answer from json
        print("You can learn about this topic here: " + reference)

    score = logic.getScore()
    print("Your total sum is now $" + str(score["balance"]))
示例#8
0
def displayResult():
    #todo 3: display the final game result"
    score = logic.getScore()
    balance = score["balance"]
    budget = str(round(score["budget"] * 100.0, 1))
    bill = str(round(score["bill"] * 100.0, 1))
    loans = str(round(score["loans"] * 100.0, 1))
    credit = str(round(score["credit"] * 100.0,1))
    invest = str(round(score["invest"] * 100.0,1))
    print("Congratulations, you have completed the exam!  You have $" + str(balance) + " left")
    print("... you lost $" + str((100 - balance)))
    # or instead say how much money you lost (or both?)?
    print("Here is your breakdown percent correct by category: ")
    print("    Budgeting and setting financial goals: " + budget + "%")
    print("    Paying bills, taxes, and saving money: " + bill + "%") 
    print("    Loans................................: " + loans + "%")
    print("    Credit...............................: " + credit + "%")
    print("    Investing, 401(k)s, and stocks.......: " + invest + "%") 
示例#9
0
    def alphaBetaPruning(grid, agent, depth, alpha, beta):
        # base case
        if (logic.game_state(grid) == 'lose'):
            return FAIL_SCORE
        if depth == 0:
            return logic.getScore(grid)

        # Player's turn
        if agent == 0:
            score = 0
            for i in range(4):
                newGrid = moveGrid(grid, i)
                futureScore = alphaBetaPruning(newGrid, 1, depth, alpha, beta)
                # new score should be in range [alpha, beta]
                if futureScore > beta:
                    return futureScore
                alpha = max(alpha, futureScore)  # update alpha/lower bound
                score = max(score, futureScore)
            return score
        # Computer's turn
        elif agent == 1:
            count = 0
            score = float('inf')
            for i in range(4):
                for j in range(4):
                    if (grid[i][j] == 0):  # for each cell with a 0
                        grid[i][j] = 2  # create a 2 tile
                        count += 1
                        futureScore = alphaBetaPruning(grid, 0, depth - 1,
                                                       alpha,
                                                       beta)  # agent goes next
                        # new score should be in range [alpha, beta]
                        if futureScore < alpha:
                            return futureScore
                        beta = min(beta,
                                   futureScore)  # update beta/upper bound
                        score = min(score, futureScore)
                        grid[i][j] = 0  # reset the 2 tile back to 0
            if count == 0:  # if no cell with 0 val, fail
                return FAIL_SCORE
            else:
                return score
示例#10
0
def main():
    f = open("expectimax2.txt", "w+")
    for iteration in range(ITERATION):
        print("Iteration: " + str(iteration + 1))
        start = time.time()
        step = 0
        matrix = np.zeros((4, 4), dtype=np.int)
        matrix = logic.add_two(matrix)
        while True:
            matrix = logic.add_two(matrix)
            if logic.game_state(matrix) == 'lose':
                break

            move = ExpectiMax.getMove(matrix, DEPTH)
            matrix = ExpectiMax.moveGrid(matrix, move)
            step += 1

        f.write("Step %d, max %d, Score %d, \r\n" %
                (step, 2**np.max(matrix), logic.getScore(matrix)))
        f.flush()

    f.close()