// File: /workspace/github_cpp_makefile_star2_O3/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 $0x48, %rsp
movq %rsi, 0x28(%rsp)
movl %edi, 0x8(%rsp)
testl %edi, %edi
jle 0x37da
xorl %r13d, %r13d
leaq 0x10(%rsp), %rbx
leaq 0x30(%rsp), %r15
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
movq 0x28(%rsp), %rdi
movq %rbx, %rsi
callq 0x6238
movq 0x10(%rsp), %rbx
movq 0x18(%rsp), %r14
cmpq %rbx, %r14
je 0x37d0
movss 0x5a23(%rip), %xmm0     # 0x9108
xorl %r12d, %r12d
movss %xmm0, 0x4(%rsp)
leaq 0x10(%rbx), %rdi
callq 0x8ea9
movss 0x4(%rsp), %xmm1
ucomiss %xmm1, %xmm0
leaq 0x8(%rbx), %rax
cmovaq %rax, %r12
maxss %xmm1, %xmm0
addq $0x28, %rbx
cmpq %r14, %rbx
jne 0x36e8
addq $0x8, %r12
xorl %r14d, %r14d
movss 0x59e8(%rip), %xmm0     # 0x910c
movss %xmm0, 0x4(%rsp)
leaq (%r14,%r14,2), %rbx
xorl %ebp, %ebp
movq (%r12), %rax
movq (%rax,%rbx,8), %rax
cmpl $0x0, (%rax,%rbp,4)
jne 0x379d
movq %r15, %rdi
movq %r12, %rsi
callq 0x2cc2
movq 0x30(%rsp), %rax
movq (%rax,%rbx,8), %rax
movl $0x2, (%rax,%rbp,4)
movq %r15, %rdi
callq 0x8ea9
movss %xmm0, 0xc(%rsp)
movq 0x30(%rsp), %rax
movq (%rax,%rbx,8), %rax
movl $0x4, (%rax,%rbp,4)
movq %r15, %rdi
callq 0x8ea9
movss 0xc(%rsp), %xmm1
minss 0x4(%rsp), %xmm1
minss %xmm1, %xmm0
movss %xmm0, 0x4(%rsp)
movq %r15, %rdi
callq 0x2da2
incq %rbp
cmpq $0x4, %rbp
jne 0x3730
incq %r14
cmpq $0x4, %r14
jne 0x372a
leaq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x3d5c
incl %r13d
cmpl 0x8(%rsp), %r13d
jne 0x36ac
jmp 0x37e3
leaq 0x10(%rsp), %rdi
callq 0x3d5c
xorps %xmm0, %xmm0
movss %xmm0, 0x4(%rsp)
movss 0x4(%rsp), %xmm0
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x37fc
jmp 0x37fc
movq %rax, %rbx
jmp 0x380e
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x2da2
leaq 0x10(%rsp), %rdi
callq 0x3d5c
movq %rbx, %rdi
callq 0x22b0