// File: /workspace/github_cpp_makefile_star2_O1/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 0xa6f2(%rip), %r14      # 0xdfd8
leaq 0x6803(%rip), %rsi      # 0xa0f0
movl $0x2f, %edx
movq %r14, %rdi
callq 0x21e0
movq %r14, %rdi
callq 0x2110
movq %r12, %rdi
callq 0x63a6
testb %al, %al
je 0x3c29
leaq 0x8(%r12), %rax
movq %rax, 0x50(%rsp)
leaq 0x20(%rsp), %rbp
leaq 0x14(%rsp), %rbx
movq %r12, 0x18(%rsp)
movl %r13d, 0xc(%rsp)
cmpl $0x2, %r13d
jl 0x3968
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 0x7287
leaq 0x58(%rsp), %rdi
movq %r12, %rsi
callq 0x6844
movl $0x0, 0x28(%rsp)
xorl %ecx, %ecx
movq %rcx, 0x30(%rsp)
leaq 0x28(%rsp), %rax
movq %rax, 0x38(%rsp)
movq %rax, 0x40(%rsp)
movq %rcx, 0x48(%rsp)
movq 0x70(%rsp), %r12
leaq 0x60(%rsp), %rax
cmpq %rax, %r12
je 0x3a53
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 0x3a43
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x3e48
movl $0x0, (%rax)
andl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
movl $0x8, %r14d
movq 0x28(%r12), %rsi
addq %r14, %rsi
movl %r15d, %edi
callq 0x3704
movss %xmm0, 0x8(%rsp)
movq 0x28(%r12), %rax
movss -0x8(%rax,%r14), %xmm0
movss %xmm0, 0x10(%rsp)
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x3e48
movss 0x8(%rsp), %xmm0
mulss 0x10(%rsp), %xmm0
addss (%rax), %xmm0
movss %xmm0, (%rax)
addq $0x28, %r14
decq %r13
jne 0x39f3
movq %r12, %rdi
callq 0x2120
movq %rax, %r12
jmp 0x399d
movl 0xc(%rsp), %r13d
cmpl $0x2, %r13d
movq 0xa575(%rip), %r14      # 0xdfd8
jle 0x3b63
movl $0x11, %edx
movq %r14, %rdi
leaq 0x66a8(%rip), %rsi      # 0xa120
callq 0x21e0
movq 0x50(%rsp), %rdi
callq 0x9011
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 0x6666(%rip), %rsi      # 0xa132
callq 0x21e0
movq 0x38(%rsp), %r12
leaq 0x28(%rsp), %rax
cmpq %rax, %r12
je 0x3b37
movl 0x20(%r12), %esi
movq %r14, %rdi
callq 0x22a0
movq %rax, %r13
movl $0x2, %edx
movq %rax, %rdi
leaq 0x6978(%rip), %rsi      # 0xa477
callq 0x21e0
xorps %xmm0, %xmm0
cvtss2sd 0x24(%r12), %xmm0
movq %r13, %rdi
callq 0x2260
movl $0x2, %edx
movq %rax, %rdi
leaq 0x661b(%rip), %rsi      # 0xa140
callq 0x21e0
movq %r12, %rdi
callq 0x2120
movq %rax, %r12
jmp 0x3ad6
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 0x38(%rsp), %rax
leaq 0x28(%rsp), %rcx
xorl %r14d, %r14d
cmpq %rcx, %rax
je 0x3bb4
movss 0x655b(%rip), %xmm0     # 0xa0d8
movq 0x18(%rsp), %r12
movss 0x24(%rax), %xmm1
ucomiss %xmm0, %xmm1
jbe 0x3b90
movl 0x20(%rax), %r14d
maxss %xmm0, %xmm1
movss %xmm1, 0x8(%rsp)
movq %rax, %rdi
callq 0x2120
movss 0x8(%rsp), %xmm0
leaq 0x28(%rsp), %rcx
cmpq %rcx, %rax
jne 0x3b82
jmp 0x3bb9
movq 0x18(%rsp), %r12
cmpl $0x3, %r14d
ja 0x3c00
movl %r14d, %eax
leaq 0x6517(%rip), %rcx      # 0xa0e0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %r12, %rdi
xorl %esi, %esi
callq 0x673c
jmp 0x3c00
movq %r12, %rdi
xorl %esi, %esi
callq 0x67e4
jmp 0x3c00
movq %r12, %rdi
xorl %esi, %esi
callq 0x680c
jmp 0x3c00
movq %r12, %rdi
xorl %esi, %esi
callq 0x67bc
movq %rbp, %rdi
callq 0x3eac
leaq 0x58(%rsp), %rdi
callq 0x3efc
movq %r12, %rdi
callq 0x63a6
testb %al, %al
movq 0xa3b5(%rip), %r14      # 0xdfd8
jne 0x3930
cmpl $0x1, %r13d
jg 0x3c6c
movq 0xa3a2(%rip), %rbx      # 0xdfd8
leaq 0x6447(%rip), %rsi      # 0xa084
movl $0x5, %edx
movq %rbx, %rdi
callq 0x21e0
xorl %edx, %edx
testl %r13d, %r13d
sete %dl
leaq 0x6431(%rip), %rax      # 0xa08a
leaq 0x642b(%rip), %rsi      # 0xa08b
cmoveq %rax, %rsi
movq %rbx, %rdi
callq 0x21e0
testl %r13d, %r13d
jle 0x3ccd
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 0x7287
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 0x7228
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 0x3cfc
jmp 0x3cfc
jmp 0x3cfc
jmp 0x3cfc
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x3eac
leaq 0x58(%rsp), %rdi
callq 0x3efc
movq %rbx, %rdi
callq 0x22b0