// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %rdx, %rbx
movl %esi, 0x8(%rsp)
movl %edi, %r15d
movq 0x4877(%rip), %r14      # 0xcfd8
leaq 0xf04(%rip), %rsi       # 0x966c
movl $0x32, %edx
movq %r14, %rdi
callq 0x21e0
movq %r14, %rdi
callq 0x2110
movq %rbx, (%rsp)
movq %rbx, %rdi
callq 0x61ca
testb %al, %al
je 0x8afb
leaq 0xa0(%rsp), %r12
movq (%rsp), %rax
addq $0x8, %rax
movq %rax, 0x60(%rsp)
leaq 0x18(%rsp), %rbx
leaq 0x14(%rsp), %rbp
cmpl $0x2, 0x8(%rsp)
jl 0x87f6
movq 0x481a(%rip), %rbx      # 0xcfd8
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2180
movsbl %al, %esi
movq %rbx, %rdi
leaq 0x18(%rsp), %rbx
callq 0x2030
movq %rax, %rdi
callq 0x2110
movq %rax, %rdi
movq (%rsp), %rsi
callq 0x70ca
leaq 0x68(%rsp), %rdi
movq (%rsp), %rsi
callq 0x664a
movl $0x0, 0x38(%rsp)
xorl %ecx, %ecx
movq %rcx, 0x40(%rsp)
leaq 0x38(%rsp), %rax
movq %rax, 0x48(%rsp)
movq %rax, 0x50(%rsp)
movq %rcx, 0x58(%rsp)
movq 0x80(%rsp), %r13
leaq 0x70(%rsp), %rax
cmpq %rax, %r13
je 0x892b
movl 0x20(%r13), %eax
movl %eax, 0x14(%rsp)
leaq 0x28(%r13), %rsi
movq %rbx, %rdi
callq 0x8d00
movq 0x20(%rsp), %r14
subq 0x18(%rsp), %r14
sarq $0x3, %r14
movabsq $-0x3333333333333333, %rax # imm = 0xCCCCCCCCCCCCCCCD
imulq %rax, %r14
testl %r14d, %r14d
jle 0x890e
leaq 0x30(%rsp), %rdi
movq %rbp, %rsi
callq 0x3da4
movl $0x0, (%rax)
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
movl $0x10, %ebx
movq 0x18(%rsp), %rsi
movl -0x8(%rsi,%rbx), %eax
addq %rbx, %rsi
movl %eax, 0x98(%rsp)
movq %r12, %rdi
callq 0x2cc2
movl %r15d, %edi
leaq 0x98(%rsp), %rsi
movl $0x1, %edx
callq 0x82d0
movss %xmm0, 0xc(%rsp)
movq 0x18(%rsp), %rax
movss -0x10(%rax,%rbx), %xmm0
movss %xmm0, 0x10(%rsp)
leaq 0x30(%rsp), %rdi
movq %rbp, %rsi
callq 0x3da4
movss 0xc(%rsp), %xmm0
mulss 0x10(%rsp), %xmm0
addss (%rax), %xmm0
movss %xmm0, (%rax)
movq %r12, %rdi
callq 0x2da2
addq $0x28, %rbx
decq %r14
jne 0x8895
leaq 0x18(%rsp), %rbx
movq %rbx, %rdi
callq 0x3eb2
movq %r13, %rdi
callq 0x2120
movq %rax, %r13
jmp 0x882f
cmpl $0x2, 0x8(%rsp)
jle 0x8a40
movl $0x11, %edx
movq 0x4696(%rip), %rdi      # 0xcfd8
leaq 0x807(%rip), %rsi       # 0x9150
callq 0x21e0
movq 0x60(%rsp), %rdi
callq 0x8ea9
cvtss2sd %xmm0, %xmm0
movq 0x4675(%rip), %rdi      # 0xcfd8
callq 0x2260
movq %rax, %r13
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x2180
movsbl %al, %esi
movq %r13, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2110
movl $0xd, %edx
movq %rax, %rdi
leaq 0x7c1(%rip), %rsi       # 0x9162
callq 0x21e0
movq 0x48(%rsp), %r13
leaq 0x38(%rsp), %rax
cmpq %rax, %r13
je 0x8a0e
movl 0x20(%r13), %esi
movq 0x4618(%rip), %rdi      # 0xcfd8
callq 0x22a0
movq %rax, %r14
movl $0x2, %edx
movq %rax, %rdi
leaq 0xad0(%rip), %rsi       # 0x94a7
callq 0x21e0
xorps %xmm0, %xmm0
cvtss2sd 0x24(%r13), %xmm0
movq %r14, %rdi
callq 0x2260
movl $0x2, %edx
movq %rax, %rdi
leaq 0x774(%rip), %rsi       # 0x9170
callq 0x21e0
movq %r13, %rdi
callq 0x2120
movq %rax, %r13
jmp 0x89ab
movq 0x45c3(%rip), %rcx      # 0xcfd8
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x2180
movsbl %al, %esi
movq 0x45a5(%rip), %rdi      # 0xcfd8
callq 0x2030
movq %rax, %rdi
callq 0x2110
movq 0x48(%rsp), %rax
leaq 0x38(%rsp), %rcx
cmpq %rcx, %rax
je 0x8a9f
xorl %ebx, %ebx
movss 0x6af(%rip), %xmm0      # 0x9108
movss 0x24(%rax), %xmm1
ucomiss %xmm0, %xmm1
jbe 0x8a66
movl 0x20(%rax), %ebx
maxss %xmm0, %xmm1
movss %xmm1, 0xc(%rsp)
movq %rax, %rdi
callq 0x2120
movss 0xc(%rsp), %xmm0
leaq 0x38(%rsp), %rcx
cmpq %rcx, %rax
jne 0x8a59
cmpl $0x3, %ebx
ja 0x8ad1
movl %ebx, %eax
leaq 0xbc6(%rip), %rcx       # 0x965c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq (%rsp), %rdi
xorl %esi, %esi
callq 0x6542
jmp 0x8ad1
movq (%rsp), %rdi
xorl %esi, %esi
callq 0x65ea
jmp 0x8ad1
movq (%rsp), %rdi
xorl %esi, %esi
callq 0x6612
jmp 0x8ad1
movq (%rsp), %rdi
xorl %esi, %esi
callq 0x65c2
leaq 0x30(%rsp), %rdi
callq 0x3e0a
leaq 0x68(%rsp), %rdi
callq 0x3e5a
movq (%rsp), %rdi
callq 0x61ca
testb %al, %al
leaq 0x18(%rsp), %rbx
jne 0x87b0
movl 0x8(%rsp), %ebp
cmpl $0x1, %ebp
jg 0x8b45
movq 0x44cd(%rip), %rbx      # 0xcfd8
leaq 0x5a2(%rip), %rsi       # 0x90b4
movl $0x5, %edx
movq %rbx, %rdi
callq 0x21e0
xorl %edx, %edx
testl %ebp, %ebp
sete %dl
leaq 0x58d(%rip), %rax       # 0x90ba
leaq 0x587(%rip), %rsi       # 0x90bb
cmoveq %rax, %rsi
movq %rbx, %rdi
callq 0x21e0
cmpl $0x1, %ebp
jne 0x8ba9
movq 0x448c(%rip), %rbx      # 0xcfd8
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2180
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2110
movq %rax, %rdi
movq (%rsp), %rsi
callq 0x70ca
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2180
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2110
movq (%rsp), %rbx
movq %rbx, %rdi
callq 0x7018
movl (%rbx), %ecx
shlq $0x20, %rcx
movl %eax, %eax
orq %rcx, %rax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x8bda
jmp 0x8bda
jmp 0x8bdf
jmp 0x8bda
movq %rax, %rbx
jmp 0x8bf9
movq %rax, %rbx
jmp 0x8bef
movq %rax, %rbx
movq %r12, %rdi
callq 0x2da2
leaq 0x18(%rsp), %rdi
callq 0x3eb2
leaq 0x30(%rsp), %rdi
callq 0x3e0a
leaq 0x68(%rsp), %rdi
callq 0x3e5a
movq %rbx, %rdi
callq 0x22b0