// File: /workspace/github_cpp_makefile_star2_O1/NathanWine[P]2048-AI-Solvers/src/Minimax.cpp
// Function: minimaxScore(int, Game const&)
float minimaxScore(int depth, const Game &game) {
    // Not sure if depth is working properly. Following the code from python codebase
    float score = 0.0;
    for (int stage = 0; stage < depth; ++stage) {
        movelist possible_moves;
        game.generatePossibleMoves(possible_moves);
        if (possible_moves.size() == 0) {
            return 0.0;
        }
        
        const Game *best_game = NULL;
        float best_score = -std::numeric_limits<float>::max();
        for (auto const& move : possible_moves) {
            float copy_h_score = Heuristics::get_h_score(move.second.state);
            if (copy_h_score > best_score) {
                best_game = &(move.second);
                best_score = copy_h_score;
            }
        }

        float worst_score = std::numeric_limits<float>::max();
        for (int i = 0; i < DIM; ++i) {
            for (int j = 0; j < DIM; ++j) {
                if (best_game -> state[i][j] == 0) {
                    board state_copy = best_game -> state;
                    state_copy[i][j] = 2;
                    float copy_h_score = Heuristics::get_h_score(state_copy);
                    if (copy_h_score < worst_score) {
                        worst_score = copy_h_score;
                    }

                    state_copy[i][j] = 4;
                    copy_h_score = Heuristics::get_h_score(state_copy);
                    if (copy_h_score < worst_score) {
                        worst_score = copy_h_score;
                    }
                }
            }
        }
        score = worst_score;
    }
    return score;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rsi, 0x40(%rsp)
movl %edi, 0x10(%rsp)
testl %edi, %edi
jle 0x387f
xorl %eax, %eax
xorps %xmm0, %xmm0
movss %xmm0, 0xc(%rsp)
leaq 0x20(%rsp), %r14
leaq 0x50(%rsp), %r15
movq %rax, 0x48(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movq $0x0, 0x30(%rsp)
movq 0x40(%rsp), %rdi
movq %r14, %rsi
callq 0x6432
movq 0x20(%rsp), %rax
movq 0x28(%rsp), %rbx
movq %rax, 0x18(%rsp)
cmpq %rax, %rbx
je 0x3858
movss 0x695d(%rip), %xmm0     # 0xa0d8
xorl %r12d, %r12d
movq 0x18(%rsp), %r14
movss %xmm0, 0xc(%rsp)
leaq 0x10(%r14), %rdi
callq 0x9011
movss 0xc(%rsp), %xmm1
ucomiss %xmm1, %xmm0
leaq 0x8(%r14), %rax
cmovaq %rax, %r12
maxss %xmm1, %xmm0
addq $0x28, %r14
cmpq %rbx, %r14
jne 0x3783
addq $0x8, %r12
xorl %ebp, %ebp
movss 0x691e(%rip), %xmm0     # 0xa0dc
movss %xmm0, 0xc(%rsp)
leaq (,%rbp,2), %r14
addq %rbp, %r14
xorl %r13d, %r13d
movq (%r12), %rax
movq (%rax,%r14,8), %rax
cmpl $0x0, (%rax,%r13,4)
jne 0x3842
movq %r15, %rdi
movq %r12, %rsi
callq 0x2d38
movq 0x50(%rsp), %rax
movq (%rax,%r14,8), %rax
movl $0x2, (%rax,%r13,4)
movq %r15, %rdi
callq 0x9011
movss %xmm0, 0x14(%rsp)
movq 0x50(%rsp), %rax
movq (%rax,%r14,8), %rax
movl $0x4, (%rax,%r13,4)
movq %r15, %rdi
callq 0x9011
movss 0x14(%rsp), %xmm1
minss 0xc(%rsp), %xmm1
minss %xmm1, %xmm0
movss %xmm0, 0xc(%rsp)
movq %r15, %rdi
callq 0x2e18
incq %r13
cmpq $0x4, %r13
jne 0x37d2
incq %rbp
cmpq $0x4, %rbp
jne 0x37c4
leaq 0x20(%rsp), %r14
movq %r14, %rdi
callq 0x3e06
cmpq 0x18(%rsp), %rbx
movq 0x48(%rsp), %rax
je 0x387f
incl %eax
cmpl 0x10(%rsp), %eax
jne 0x3738
jmp 0x3888
xorps %xmm0, %xmm0
movss %xmm0, 0xc(%rsp)
movss 0xc(%rsp), %xmm0
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x38a1
jmp 0x38a1
movq %rax, %rbx
jmp 0x38b3
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x2e18
leaq 0x20(%rsp), %rdi
callq 0x3e06
movq %rbx, %rdi
callq 0x22b0