// File: /workspace/github_cpp_makefile_star2_O3/NathanWine[P]2048-AI-Solvers/src/Minimax.cpp
// Function: minimaxSearch(int, int, Game)
std::pair<int, int> minimaxSearch(int depth, int display_level, Game game) {
    std::cout << "Attempting to solve a new game with Minimax... " << 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
            int len = (int) entry.second.size();                // entry.second is a vector of (prob, game)
            if (len > 0) {
                scores[move] = 0.0;
                for (int j = 0; j < len; ++j) {
                    scores[move] += minimaxScore(depth, entry.second[j].second) * entry.second[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) {     // score.first is the move direction, score.second is the 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 $0x88, %rsp
movq %rdx, %r12
movl %esi, %r13d
movl %edi, %r15d
movq 0x9797(%rip), %r14      # 0xcfd8
leaq 0x58d8(%rip), %rsi      # 0x9120
movl $0x2f, %edx
movq %r14, %rdi
callq 0x21e0
movq %r14, %rdi
callq 0x2110
movq %r12, %rdi
callq 0x61ca
testb %al, %al
je 0x3b7d
leaq 0x8(%r12), %rax
movq %rax, 0x48(%rsp)
leaq 0x18(%rsp), %rbp
leaq 0x14(%rsp), %rbx
movq %r12, 0x50(%rsp)
movl %r13d, 0xc(%rsp)
cmpl $0x2, %r13d
jl 0x38c3
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x2180
movsbl %al, %esi
movq %r14, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2110
movq %rax, %rdi
movq %r12, %rsi
callq 0x70ca
leaq 0x58(%rsp), %rdi
movq %r12, %rsi
callq 0x664a
movl $0x0, 0x20(%rsp)
xorl %ecx, %ecx
movq %rcx, 0x28(%rsp)
leaq 0x20(%rsp), %rax
movq %rax, 0x30(%rsp)
movq %rax, 0x38(%rsp)
movq %rcx, 0x40(%rsp)
movq 0x70(%rsp), %r12
leaq 0x60(%rsp), %rax
cmpq %rax, %r12
je 0x39ae
movl 0x20(%r12), %eax
movl %eax, 0x14(%rsp)
movq 0x30(%r12), %r13
subq 0x28(%r12), %r13
sarq $0x3, %r13
movabsq $-0x3333333333333333, %rax # imm = 0xCCCCCCCCCCCCCCCD
imulq %rax, %r13
testl %r13d, %r13d
jle 0x399e
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x3da4
movl $0x0, (%rax)
andl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
movl $0x8, %r14d
movq 0x28(%r12), %rsi
addq %r14, %rsi
movl %r15d, %edi
callq 0x3680
movss %xmm0, 0x8(%rsp)
movq 0x28(%r12), %rax
movss -0x8(%rax,%r14), %xmm0
movss %xmm0, 0x10(%rsp)
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x3da4
movss 0x8(%rsp), %xmm0
mulss 0x10(%rsp), %xmm0
addss (%rax), %xmm0
movss %xmm0, (%rax)
addq $0x28, %r14
decq %r13
jne 0x394e
movq %r12, %rdi
callq 0x2120
movq %rax, %r12
jmp 0x38f8
movl 0xc(%rsp), %r13d
cmpl $0x2, %r13d
movq 0x961a(%rip), %r14      # 0xcfd8
jle 0x3abe
movl $0x11, %edx
movq %r14, %rdi
leaq 0x577d(%rip), %rsi      # 0x9150
callq 0x21e0
movq 0x48(%rsp), %rdi
callq 0x8ea9
cvtss2sd %xmm0, %xmm0
movq %r14, %rdi
callq 0x2260
movq %rax, %r12
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x2180
movsbl %al, %esi
movq %r12, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2110
movl $0xd, %edx
movq %rax, %rdi
leaq 0x573b(%rip), %rsi      # 0x9162
callq 0x21e0
movq 0x30(%rsp), %r12
leaq 0x20(%rsp), %rax
cmpq %rax, %r12
je 0x3a92
movl 0x20(%r12), %esi
movq %r14, %rdi
callq 0x22a0
movq %rax, %r13
movl $0x2, %edx
movq %rax, %rdi
leaq 0x5a4d(%rip), %rsi      # 0x94a7
callq 0x21e0
xorps %xmm0, %xmm0
cvtss2sd 0x24(%r12), %xmm0
movq %r13, %rdi
callq 0x2260
movl $0x2, %edx
movq %rax, %rdi
leaq 0x56f0(%rip), %rsi      # 0x9170
callq 0x21e0
movq %r12, %rdi
callq 0x2120
movq %rax, %r12
jmp 0x3a31
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x2180
movl 0xc(%rsp), %r13d
movsbl %al, %esi
movq %r14, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2110
movq 0x30(%rsp), %rax
leaq 0x20(%rsp), %rcx
cmpq %rcx, %rax
movq 0x50(%rsp), %r12
je 0x3b26
xorl %r14d, %r14d
movss 0x562b(%rip), %xmm0     # 0x9108
movss 0x24(%rax), %xmm1
ucomiss %xmm0, %xmm1
jbe 0x3aeb
movl 0x20(%rax), %r14d
maxss %xmm0, %xmm1
movss %xmm1, 0x8(%rsp)
movq %rax, %rdi
callq 0x2120
movss 0x8(%rsp), %xmm0
leaq 0x20(%rsp), %rcx
cmpq %rcx, %rax
jne 0x3add
cmpl $0x3, %r14d
ja 0x3b54
movl %r14d, %eax
leaq 0x55f3(%rip), %rcx      # 0x9110
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %r12, %rdi
xorl %esi, %esi
callq 0x6542
jmp 0x3b54
movq %r12, %rdi
xorl %esi, %esi
callq 0x65ea
jmp 0x3b54
movq %r12, %rdi
xorl %esi, %esi
callq 0x6612
jmp 0x3b54
movq %r12, %rdi
xorl %esi, %esi
callq 0x65c2
movq %rbp, %rdi
callq 0x3e0a
leaq 0x58(%rsp), %rdi
callq 0x3e5a
movq %r12, %rdi
callq 0x61ca
testb %al, %al
movq 0x9461(%rip), %r14      # 0xcfd8
jne 0x388b
cmpl $0x1, %r13d
jg 0x3bc6
movq 0x944e(%rip), %rbx      # 0xcfd8
leaq 0x5523(%rip), %rsi      # 0x90b4
movl $0x5, %edx
movq %rbx, %rdi
callq 0x21e0
xorl %edx, %edx
testl %r13d, %r13d
sete %dl
leaq 0x550d(%rip), %rax      # 0x90ba
leaq 0x5507(%rip), %rsi      # 0x90bb
cmoveq %rax, %rsi
movq %rbx, %rdi
callq 0x21e0
cmpl $0x1, %r13d
jne 0x3c22
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x2180
movsbl %al, %esi
movq %r14, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2110
movq %rax, %rdi
movq %r12, %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 %r12, %rdi
callq 0x7018
movl (%r12), %ecx
shlq $0x20, %rcx
movl %eax, %eax
orq %rcx, %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3c51
jmp 0x3c51
jmp 0x3c51
jmp 0x3c51
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x3e0a
leaq 0x58(%rsp), %rdi
callq 0x3e5a
movq %rbx, %rdi
callq 0x22b0