// File: /workspace/github_cpp_makefile_star2_O2/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 $0x58, %rsp
movq %rsi, 0x28(%rsp)
xorl %ebx, %ebx
testl %edi, %edi
cmovlel %ebx, %edi
movl %edi, 0x8(%rsp)
xorps %xmm0, %xmm0
movss %xmm0, 0x4(%rsp)
leaq 0x10(%rsp), %r14
leaq 0x40(%rsp), %r15
cmpl 0x8(%rsp), %ebx
je 0x3574
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
andq $0x0, 0x20(%rsp)
movq 0x28(%rsp), %rdi
movq %r14, %rsi
callq 0x56fe
movq 0x10(%rsp), %r13
movq 0x18(%rsp), %rbp
cmpq %r13, %rbp
je 0x355c
movq %rbx, 0x38(%rsp)
movq %r13, 0x30(%rsp)
xorl %r12d, %r12d
movss 0x4c62(%rip), %xmm0     # 0x80d8
cmpq %rbp, %r13
je 0x34a8
movss %xmm0, 0x4(%rsp)
addq $0x10, %r13
movq %r13, %rdi
callq 0x7ce2
movss 0x4(%rsp), %xmm1
ucomiss %xmm1, %xmm0
leaq -0x8(%r13), %rax
cmovaq %rax, %r12
maxss %xmm1, %xmm0
addq $0x18, %r13
jmp 0x3476
addq $0x8, %r12
xorl %r13d, %r13d
movss 0x4c25(%rip), %xmm0     # 0x80dc
movss %xmm0, 0x4(%rsp)
cmpq $0x4, %r13
je 0x354d
imulq $0x18, %r13, %r14
xorl %ebx, %ebx
cmpq $0x4, %rbx
je 0x3545
movq (%r12), %rax
movq (%rax,%r14), %rax
cmpl $0x0, (%rax,%rbx,4)
jne 0x3540
movq %r15, %rdi
movq %r12, %rsi
callq 0x29fa
movq 0x40(%rsp), %rax
movq (%rax,%r14), %rax
movl $0x2, (%rax,%rbx,4)
movq %r15, %rdi
callq 0x7ce2
movss %xmm0, 0xc(%rsp)
movq 0x40(%rsp), %rax
movq (%rax,%r14), %rax
movl $0x4, (%rax,%rbx,4)
movq %r15, %rdi
callq 0x7ce2
movss 0xc(%rsp), %xmm1
minss 0x4(%rsp), %xmm1
minss %xmm1, %xmm0
movss %xmm0, 0x4(%rsp)
movq %r15, %rdi
callq 0x2abc
incq %rbx
jmp 0x34cd
incq %r13
jmp 0x34bd
movq 0x38(%rsp), %rbx
leaq 0x10(%rsp), %r14
movq 0x30(%rsp), %r13
movq %r14, %rdi
callq 0x39ca
incl %ebx
cmpq %r13, %rbp
jne 0x3429
xorps %xmm0, %xmm0
jmp 0x357a
movss 0x4(%rsp), %xmm0
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x358d
jmp 0x358d
movq %rax, %rbx
jmp 0x359f
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x2abc
leaq 0x10(%rsp), %rdi
callq 0x39ca
movq %rbx, %rdi
callq 0x22c0