// File: NathanWine[P]2048-AI-Solvers/src/Expectimax.cpp
// Function: expectimaxSearch(int, int, Game)
std::pair<int, int> expectimaxSearch(int depth, int display_level, Game game) {
    std::cout << "Attempting to solve a new game with Expectimax... " << std::flush;
    while (game.canContinue()) {
        if (display_level >= 2) {
            std::cout << std::endl << game;
        }

        std::map<int, weightedmoves> possibilities = game.computePossibilities();
        std::map<int, float> scores;

        for (auto const& entry : possibilities) {
            int move = entry.first;                             // Direction of move
            weightedmoves weighted_subset = entry.second;       // Vector of (prob, game)
            int len = (int) weighted_subset.size();
            if (len > 0) {
                scores[move] = 0.0;
                for (int j = 0; j < len; ++j) {
                    scores[move] += expectimaxScore(depth, weighted_subset[j].second, true) * weighted_subset[j].first;
                }
            }
        }
        if (display_level >= 3) {
            std::cout << "Heuristic score: " << Heuristics::get_h_score(game.state) << std::endl << "Move scores: ";
            for (auto const& score : scores) {
                std::cout << score.first << ": " << score.second << ", ";
            }
            std::cout << std::endl;
        }

        float max_score = -std::numeric_limits<float>::max();
        int max_choice = UP;
        for (auto const& score : scores) {      // Determine best move choice based on tabulated scores
            if (score.second > max_score) {
                max_score = score.second;
                max_choice = score.first;
            }
        }

        switch (max_choice) {
            case UP:
                game.up(false);
                break;
            case DOWN:
                game.down(false);
                break;
            case LEFT: 
                game.left(false);
                break;
            case RIGHT:
                game.right(false);
        }
    }
    if (display_level <= 1) {
        std::cout << "Done!" << (display_level == 0 ? "\n" : "");
    }
    if (display_level >= 1) {
        std::cout << std::endl << game << std::endl;
    }
    return std::pair<int, int>(game.getHighestTile(), game.score);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1b0, %rsp            # imm = 0x1B0
movq %rdx, -0x148(%rbp)
movl %edi, -0xc(%rbp)
movl %esi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq 0xcdc5(%rip), %rdi      # 0x22fc8
leaq 0x2382(%rip), %rsi      # 0x1858c
callq 0x3260
movq %rax, %rdi
movq 0xcdb7(%rip), %rsi      # 0x22fd0
callq 0x32a0
movq -0x148(%rbp), %rdi
callq 0xf4f0
testb $0x1, %al
jne 0x16233
jmp 0x16792
cmpl $0x2, -0x10(%rbp)
jl 0x1625b
movq 0xcd88(%rip), %rdi      # 0x22fc8
movq 0xcd69(%rip), %rsi      # 0x22fb0
callq 0x32a0
movq -0x148(%rbp), %rsi
movq %rax, %rdi
callq 0x10f60
movq -0x148(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0xfbb0
leaq -0x78(%rbp), %rdi
callq 0x8030
leaq -0x48(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rdi
callq 0x8050
movq %rax, -0x88(%rbp)
movq -0x80(%rbp), %rdi
callq 0x8080
movq %rax, -0x90(%rbp)
leaq -0x88(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x80b0
testb $0x1, %al
jne 0x162b8
jmp 0x164a1
leaq -0x88(%rbp), %rdi
callq 0x80e0
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x9c(%rbp)
movq -0x98(%rbp), %rsi
addq $0x8, %rsi
leaq -0xb8(%rbp), %rdi
callq 0x16960
jmp 0x162f3
leaq -0xb8(%rbp), %rdi
callq 0x8100
movl %eax, -0xc8(%rbp)
cmpl $0x0, -0xc8(%rbp)
jle 0x16473
leaq -0x78(%rbp), %rdi
leaq -0x9c(%rbp), %rsi
callq 0x8130
movq %rax, -0x150(%rbp)
jmp 0x1632b
movq -0x150(%rbp), %rax
xorps %xmm0, %xmm0
movss %xmm0, (%rax)
movl $0x0, -0xcc(%rbp)
movl -0xcc(%rbp), %eax
cmpl -0xc8(%rbp), %eax
jge 0x16471
movl -0xc(%rbp), %eax
movl %eax, -0x154(%rbp)
movslq -0xcc(%rbp), %rsi
leaq -0xb8(%rbp), %rdi
callq 0x11650
movq %rax, %rsi
addq $0x8, %rsi
leaq -0xf0(%rbp), %rdi
callq 0x3f70
jmp 0x16386
movl -0x154(%rbp), %edi
leaq -0xf0(%rbp), %rsi
movl $0x1, %edx
callq 0x159f0
movss %xmm0, -0x158(%rbp)
jmp 0x163a7
movslq -0xcc(%rbp), %rsi
leaq -0xb8(%rbp), %rdi
callq 0x11650
movss (%rax), %xmm0
movss %xmm0, -0x164(%rbp)
leaq -0x78(%rbp), %rdi
leaq -0x9c(%rbp), %rsi
callq 0x8130
movq %rax, -0x160(%rbp)
jmp 0x163df
movq -0x160(%rbp), %rax
movss -0x164(%rbp), %xmm2
movss -0x158(%rbp), %xmm0
movss (%rax), %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, (%rax)
leaq -0xf0(%rbp), %rdi
callq 0x4460
movl -0xcc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xcc(%rbp)
jmp 0x16343
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc0(%rbp)
movl %eax, -0xc4(%rbp)
jmp 0x1677b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc0(%rbp)
movl %eax, -0xc4(%rbp)
jmp 0x16490
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc0(%rbp)
movl %eax, -0xc4(%rbp)
leaq -0xf0(%rbp), %rdi
callq 0x4460
jmp 0x16490
jmp 0x16473
leaq -0xb8(%rbp), %rdi
callq 0x8b00
leaq -0x88(%rbp), %rdi
callq 0x8230
jmp 0x1629c
leaq -0xb8(%rbp), %rdi
callq 0x8b00
jmp 0x1677b
cmpl $0x3, -0x10(%rbp)
jl 0x16629
movq 0xcb16(%rip), %rdi      # 0x22fc8
leaq 0x1c37(%rip), %rsi      # 0x180f0
callq 0x3260
movq %rax, -0x170(%rbp)
jmp 0x164c7
movq -0x148(%rbp), %rdi
addq $0x8, %rdi
callq 0x16f90
movss %xmm0, -0x174(%rbp)
jmp 0x164e1
movss -0x174(%rbp), %xmm0
movq -0x170(%rbp), %rdi
callq 0x31b0
movq %rax, -0x180(%rbp)
jmp 0x164fe
movq -0x180(%rbp), %rdi
movq 0xcaa4(%rip), %rsi      # 0x22fb0
callq 0x32a0
movq %rax, -0x188(%rbp)
jmp 0x1651a
movq -0x188(%rbp), %rdi
leaq 0x1bda(%rip), %rsi      # 0x18102
callq 0x3260
jmp 0x1652f
leaq -0x78(%rbp), %rax
movq %rax, -0xf8(%rbp)
movq -0xf8(%rbp), %rdi
callq 0x8260
movq %rax, -0x100(%rbp)
movq -0xf8(%rbp), %rdi
callq 0x8290
movq %rax, -0x108(%rbp)
leaq -0x100(%rbp), %rdi
leaq -0x108(%rbp), %rsi
callq 0x82c0
testb $0x1, %al
jne 0x1657c
jmp 0x16612
leaq -0x100(%rbp), %rdi
callq 0x82f0
movq %rax, -0x110(%rbp)
movq -0x110(%rbp), %rax
movl (%rax), %esi
movq 0xca29(%rip), %rdi      # 0x22fc8
callq 0x33b0
movq %rax, -0x190(%rbp)
jmp 0x165ad
movq -0x190(%rbp), %rdi
leaq 0x1e98(%rip), %rsi      # 0x18453
callq 0x3260
movq %rax, -0x198(%rbp)
jmp 0x165c9
movq -0x198(%rbp), %rdi
movq -0x110(%rbp), %rax
movss 0x4(%rax), %xmm0
callq 0x31b0
movq %rax, -0x1a0(%rbp)
jmp 0x165ea
movq -0x1a0(%rbp), %rdi
leaq 0x1b18(%rip), %rsi      # 0x18110
callq 0x3260
jmp 0x165ff
jmp 0x16601
leaq -0x100(%rbp), %rdi
callq 0x8310
jmp 0x16560
movq 0xc9af(%rip), %rdi      # 0x22fc8
movq 0xc990(%rip), %rsi      # 0x22fb0
callq 0x32a0
jmp 0x16627
jmp 0x16629
callq 0x7e80
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
movss %xmm0, -0x114(%rbp)
movl $0x0, -0x118(%rbp)
leaq -0x78(%rbp), %rax
movq %rax, -0x120(%rbp)
movq -0x120(%rbp), %rdi
callq 0x8260
movq %rax, -0x128(%rbp)
movq -0x120(%rbp), %rdi
callq 0x8290
movq %rax, -0x130(%rbp)
leaq -0x128(%rbp), %rdi
leaq -0x130(%rbp), %rsi
callq 0x82c0
testb $0x1, %al
jne 0x16697
jmp 0x166f2
leaq -0x128(%rbp), %rdi
callq 0x82f0
movq %rax, -0x138(%rbp)
movq -0x138(%rbp), %rax
movss 0x4(%rax), %xmm0
ucomiss -0x114(%rbp), %xmm0
jbe 0x166e2
movq -0x138(%rbp), %rax
movss 0x4(%rax), %xmm0
movss %xmm0, -0x114(%rbp)
movq -0x138(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x118(%rbp)
jmp 0x166e4
leaq -0x128(%rbp), %rdi
callq 0x8310
jmp 0x1667e
movl -0x118(%rbp), %eax
movq %rax, -0x1a8(%rbp)
subq $0x3, %rax
ja 0x16764
movq -0x1a8(%rbp), %rax
leaq 0x1e69(%rip), %rcx      # 0x1857c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x148(%rbp), %rdi
xorl %esi, %esi
callq 0xfa10
jmp 0x1672c
jmp 0x16764
movq -0x148(%rbp), %rdi
xorl %esi, %esi
callq 0xfac0
jmp 0x1673e
jmp 0x16764
movq -0x148(%rbp), %rdi
xorl %esi, %esi
callq 0xfb10
jmp 0x16750
jmp 0x16764
movq -0x148(%rbp), %rdi
xorl %esi, %esi
callq 0xfb60
jmp 0x16762
jmp 0x16764
leaq -0x78(%rbp), %rdi
callq 0x8340
leaq -0x48(%rbp), %rdi
callq 0x8360
jmp 0x1621e
leaq -0x78(%rbp), %rdi
callq 0x8340
leaq -0x48(%rbp), %rdi
callq 0x8360
jmp 0x16838
cmpl $0x1, -0x10(%rbp)
jg 0x167cb
movq 0xc829(%rip), %rdi      # 0x22fc8
leaq 0x18b2(%rip), %rsi      # 0x18058
callq 0x3260
movq %rax, %rdi
movl -0x10(%rbp), %ecx
leaq 0x18a7(%rip), %rsi      # 0x1805f
leaq 0x189f(%rip), %rax      # 0x1805e
cmpl $0x0, %ecx
cmoveq %rax, %rsi
callq 0x3260
cmpl $0x1, -0x10(%rbp)
jl 0x16802
movq 0xc7f0(%rip), %rdi      # 0x22fc8
movq 0xc7d1(%rip), %rsi      # 0x22fb0
callq 0x32a0
movq -0x148(%rbp), %rsi
movq %rax, %rdi
callq 0x10f60
movq %rax, %rdi
movq 0xc7b3(%rip), %rsi      # 0x22fb0
callq 0x32a0
movq -0x148(%rbp), %rdi
callq 0x10e40
movq -0x148(%rbp), %rdx
movl %eax, -0x13c(%rbp)
leaq -0x8(%rbp), %rdi
leaq -0x13c(%rbp), %rsi
callq 0x4480
movq -0x8(%rbp), %rax
addq $0x1b0, %rsp            # imm = 0x1B0
popq %rbp
retq
movq -0xc0(%rbp), %rdi
callq 0x33d0
nopw %cs:(%rax,%rax)