// File: 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
movq %rsp, %rbp
subq $0xd0, %rsp
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
xorps %xmm0, %xmm0
movss %xmm0, -0x14(%rbp)
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl -0x8(%rbp), %eax
jge 0x7735
leaq -0x30(%rbp), %rdi
movq %rdi, -0xa8(%rbp)
callq 0x7e30
movq -0xa8(%rbp), %rsi
movq -0x10(%rbp), %rdi
callq 0xf630
jmp 0x747f
leaq -0x30(%rbp), %rdi
callq 0x7e50
cmpq $0x0, %rax
jne 0x74b3
xorps %xmm0, %xmm0
movss %xmm0, -0x4(%rbp)
movl $0x1, -0x40(%rbp)
jmp 0x7706
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x772a
movq $0x0, -0x48(%rbp)
callq 0x7e80
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
movss %xmm0, -0x4c(%rbp)
leaq -0x30(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rdi
callq 0x7e90
movq %rax, -0x60(%rbp)
movq -0x58(%rbp), %rdi
callq 0x7ec0
movq %rax, -0x68(%rbp)
leaq -0x60(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x7ef0
testb $0x1, %al
jne 0x7507
jmp 0x7566
leaq -0x60(%rbp), %rdi
callq 0x7f30
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rdi
addq $0x10, %rdi
callq 0x16f90
movss %xmm0, -0xac(%rbp)
jmp 0x752b
movss -0xac(%rbp), %xmm0
movss %xmm0, -0x74(%rbp)
movss -0x74(%rbp), %xmm0
ucomiss -0x4c(%rbp), %xmm0
jbe 0x7559
movq -0x70(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x48(%rbp)
movss -0x74(%rbp), %xmm0
movss %xmm0, -0x4c(%rbp)
jmp 0x755b
leaq -0x60(%rbp), %rdi
callq 0x7f50
jmp 0x74f4
callq 0x7e80
movss %xmm0, -0x78(%rbp)
movl $0x0, -0x7c(%rbp)
cmpl $0x4, -0x7c(%rbp)
jge 0x76f5
movl $0x0, -0x80(%rbp)
cmpl $0x4, -0x80(%rbp)
jge 0x76e5
movq -0x48(%rbp), %rdi
addq $0x8, %rdi
movslq -0x7c(%rbp), %rsi
callq 0x7f70
movq %rax, %rdi
movslq -0x80(%rbp), %rsi
callq 0x7f90
cmpl $0x0, (%rax)
jne 0x76d5
movq -0x48(%rbp), %rsi
addq $0x8, %rsi
leaq -0x98(%rbp), %rdi
callq 0x3fb0
jmp 0x75ce
movslq -0x7c(%rbp), %rsi
leaq -0x98(%rbp), %rdi
movq %rdi, -0xb8(%rbp)
callq 0x7fb0
movq %rax, %rdi
movslq -0x80(%rbp), %rsi
callq 0x4230
movq -0xb8(%rbp), %rdi
movl $0x2, (%rax)
callq 0x16f90
movss %xmm0, -0xb0(%rbp)
jmp 0x760d
movss -0xb0(%rbp), %xmm0
movss %xmm0, -0x9c(%rbp)
movss -0x9c(%rbp), %xmm1
movss -0x78(%rbp), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x765b
movss -0x9c(%rbp), %xmm0
movss %xmm0, -0x78(%rbp)
jmp 0x765b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x43d0
jmp 0x772a
movslq -0x7c(%rbp), %rsi
leaq -0x98(%rbp), %rdi
movq %rdi, -0xc8(%rbp)
callq 0x7fb0
movq %rax, %rdi
movslq -0x80(%rbp), %rsi
callq 0x4230
movq -0xc8(%rbp), %rdi
movl $0x4, (%rax)
callq 0x16f90
movss %xmm0, -0xbc(%rbp)
jmp 0x769a
movss -0xbc(%rbp), %xmm0
movss %xmm0, -0x9c(%rbp)
movss -0x9c(%rbp), %xmm1
movss -0x78(%rbp), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x76c9
movss -0x9c(%rbp), %xmm0
movss %xmm0, -0x78(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x43d0
jmp 0x76d7
movl -0x80(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x80(%rbp)
jmp 0x7588
jmp 0x76e7
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0x7577
movss -0x78(%rbp), %xmm0
movss %xmm0, -0x14(%rbp)
movl $0x0, -0x40(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x7fd0
movl -0x40(%rbp), %eax
testl %eax, %eax
je 0x771a
jmp 0x7718
jmp 0x773f
jmp 0x771c
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x7451
leaq -0x30(%rbp), %rdi
callq 0x7fd0
jmp 0x774d
movss -0x14(%rbp), %xmm0
movss %xmm0, -0x4(%rbp)
movss -0x4(%rbp), %xmm0
addq $0xd0, %rsp
popq %rbp
retq
movq -0x38(%rbp), %rdi
callq 0x33d0
nopw %cs:(%rax,%rax)