// File: 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
movq %rsp, %rbp
subq $0x170, %rsp            # imm = 0x170
movq %rdx, -0x108(%rbp)
movl %edi, -0xc(%rbp)
movl %esi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq 0x1b845(%rip), %rdi     # 0x22fc8
leaq 0x10936(%rip), %rsi     # 0x180c0
callq 0x3260
movq %rax, %rdi
movq 0x1b837(%rip), %rsi     # 0x22fd0
callq 0x32a0
movq -0x108(%rbp), %rdi
callq 0xf4f0
testb $0x1, %al
jne 0x77b3
jmp 0x7c8b
cmpl $0x2, -0x10(%rbp)
jl 0x77db
movq 0x1b808(%rip), %rdi     # 0x22fc8
movq 0x1b7e9(%rip), %rsi     # 0x22fb0
callq 0x32a0
movq -0x108(%rbp), %rsi
movq %rax, %rdi
callq 0x10f60
movq -0x108(%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 0x7838
jmp 0x79b1
leaq -0x88(%rbp), %rdi
callq 0x80e0
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x9c(%rbp)
movq -0x98(%rbp), %rdi
addq $0x8, %rdi
callq 0x8100
movl %eax, -0xa0(%rbp)
cmpl $0x0, -0xa0(%rbp)
jle 0x799e
leaq -0x78(%rbp), %rdi
leaq -0x9c(%rbp), %rsi
callq 0x8130
movq %rax, -0x110(%rbp)
jmp 0x7896
movq -0x110(%rbp), %rax
xorps %xmm0, %xmm0
movss %xmm0, (%rax)
movl $0x0, -0xb0(%rbp)
movl -0xb0(%rbp), %eax
cmpl -0xa0(%rbp), %eax
jge 0x799c
movl -0xc(%rbp), %eax
movl %eax, -0x118(%rbp)
movq -0x98(%rbp), %rdi
addq $0x8, %rdi
movslq -0xb0(%rbp), %rsi
callq 0x8210
movl -0x118(%rbp), %edi
movq %rax, %rsi
addq $0x8, %rsi
callq 0x7430
movss %xmm0, -0x114(%rbp)
jmp 0x78fc
movq -0x98(%rbp), %rdi
addq $0x8, %rdi
movslq -0xb0(%rbp), %rsi
callq 0x8210
movss (%rax), %xmm0
movss %xmm0, -0x124(%rbp)
leaq -0x78(%rbp), %rdi
leaq -0x9c(%rbp), %rsi
callq 0x8130
movq %rax, -0x120(%rbp)
jmp 0x7938
movq -0x120(%rbp), %rax
movss -0x124(%rbp), %xmm2
movss -0x114(%rbp), %xmm0
movss (%rax), %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, (%rax)
movl -0xb0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb0(%rbp)
jmp 0x78ae
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa8(%rbp)
movl %eax, -0xac(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x8340
leaq -0x48(%rbp), %rdi
callq 0x8360
jmp 0x7d31
jmp 0x799e
jmp 0x79a0
leaq -0x88(%rbp), %rdi
callq 0x8230
jmp 0x781c
cmpl $0x3, -0x10(%rbp)
jl 0x7b39
movq 0x1b606(%rip), %rdi     # 0x22fc8
leaq 0x10727(%rip), %rsi     # 0x180f0
callq 0x3260
movq %rax, -0x130(%rbp)
jmp 0x79d7
movq -0x108(%rbp), %rdi
addq $0x8, %rdi
callq 0x16f90
movss %xmm0, -0x134(%rbp)
jmp 0x79f1
movss -0x134(%rbp), %xmm0
movq -0x130(%rbp), %rdi
callq 0x31b0
movq %rax, -0x140(%rbp)
jmp 0x7a0e
movq -0x140(%rbp), %rdi
movq 0x1b594(%rip), %rsi     # 0x22fb0
callq 0x32a0
movq %rax, -0x148(%rbp)
jmp 0x7a2a
movq -0x148(%rbp), %rdi
leaq 0x106ca(%rip), %rsi     # 0x18102
callq 0x3260
jmp 0x7a3f
leaq -0x78(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0xb8(%rbp), %rdi
callq 0x8260
movq %rax, -0xc0(%rbp)
movq -0xb8(%rbp), %rdi
callq 0x8290
movq %rax, -0xc8(%rbp)
leaq -0xc0(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
callq 0x82c0
testb $0x1, %al
jne 0x7a8c
jmp 0x7b22
leaq -0xc0(%rbp), %rdi
callq 0x82f0
movq %rax, -0xd0(%rbp)
movq -0xd0(%rbp), %rax
movl (%rax), %esi
movq 0x1b519(%rip), %rdi     # 0x22fc8
callq 0x33b0
movq %rax, -0x150(%rbp)
jmp 0x7abd
movq -0x150(%rbp), %rdi
leaq 0x10988(%rip), %rsi     # 0x18453
callq 0x3260
movq %rax, -0x158(%rbp)
jmp 0x7ad9
movq -0x158(%rbp), %rdi
movq -0xd0(%rbp), %rax
movss 0x4(%rax), %xmm0
callq 0x31b0
movq %rax, -0x160(%rbp)
jmp 0x7afa
movq -0x160(%rbp), %rdi
leaq 0x10608(%rip), %rsi     # 0x18110
callq 0x3260
jmp 0x7b0f
jmp 0x7b11
leaq -0xc0(%rbp), %rdi
callq 0x8310
jmp 0x7a70
movq 0x1b49f(%rip), %rdi     # 0x22fc8
movq 0x1b480(%rip), %rsi     # 0x22fb0
callq 0x32a0
jmp 0x7b37
jmp 0x7b39
callq 0x7e80
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
movss %xmm0, -0xd4(%rbp)
movl $0x0, -0xd8(%rbp)
leaq -0x78(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq -0xe0(%rbp), %rdi
callq 0x8260
movq %rax, -0xe8(%rbp)
movq -0xe0(%rbp), %rdi
callq 0x8290
movq %rax, -0xf0(%rbp)
leaq -0xe8(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
callq 0x82c0
testb $0x1, %al
jne 0x7ba7
jmp 0x7c02
leaq -0xe8(%rbp), %rdi
callq 0x82f0
movq %rax, -0xf8(%rbp)
movq -0xf8(%rbp), %rax
movss 0x4(%rax), %xmm0
ucomiss -0xd4(%rbp), %xmm0
jbe 0x7bf2
movq -0xf8(%rbp), %rax
movss 0x4(%rax), %xmm0
movss %xmm0, -0xd4(%rbp)
movq -0xf8(%rbp), %rax
movl (%rax), %eax
movl %eax, -0xd8(%rbp)
jmp 0x7bf4
leaq -0xe8(%rbp), %rdi
callq 0x8310
jmp 0x7b8e
movl -0xd8(%rbp), %eax
movq %rax, -0x168(%rbp)
subq $0x3, %rax
ja 0x7c74
movq -0x168(%rbp), %rax
leaq 0x1048d(%rip), %rcx     # 0x180b0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x108(%rbp), %rdi
xorl %esi, %esi
callq 0xfa10
jmp 0x7c3c
jmp 0x7c74
movq -0x108(%rbp), %rdi
xorl %esi, %esi
callq 0xfac0
jmp 0x7c4e
jmp 0x7c74
movq -0x108(%rbp), %rdi
xorl %esi, %esi
callq 0xfb10
jmp 0x7c60
jmp 0x7c74
movq -0x108(%rbp), %rdi
xorl %esi, %esi
callq 0xfb60
jmp 0x7c72
jmp 0x7c74
leaq -0x78(%rbp), %rdi
callq 0x8340
leaq -0x48(%rbp), %rdi
callq 0x8360
jmp 0x779e
cmpl $0x1, -0x10(%rbp)
jg 0x7cc4
movq 0x1b330(%rip), %rdi     # 0x22fc8
leaq 0x103b9(%rip), %rsi     # 0x18058
callq 0x3260
movq %rax, %rdi
movl -0x10(%rbp), %ecx
leaq 0x103ae(%rip), %rsi     # 0x1805f
leaq 0x103a6(%rip), %rax     # 0x1805e
cmpl $0x0, %ecx
cmoveq %rax, %rsi
callq 0x3260
cmpl $0x1, -0x10(%rbp)
jl 0x7cfb
movq 0x1b2f7(%rip), %rdi     # 0x22fc8
movq 0x1b2d8(%rip), %rsi     # 0x22fb0
callq 0x32a0
movq -0x108(%rbp), %rsi
movq %rax, %rdi
callq 0x10f60
movq %rax, %rdi
movq 0x1b2ba(%rip), %rsi     # 0x22fb0
callq 0x32a0
movq -0x108(%rbp), %rdi
callq 0x10e40
movq -0x108(%rbp), %rdx
movl %eax, -0xfc(%rbp)
leaq -0x8(%rbp), %rdi
leaq -0xfc(%rbp), %rsi
callq 0x4480
movq -0x8(%rbp), %rax
addq $0x170, %rsp            # imm = 0x170
popq %rbp
retq
movq -0xa8(%rbp), %rdi
callq 0x33d0
nopl (%rax)