// File: /workspace/github_cpp_makefile_star2_O1/NathanWine[P]2048-AI-Solvers/src/MonteCarlo.cpp
// Function: monteCarloSimulateGame(int, int, Game)
std::pair<int, int> monteCarloSimulateGame(int runs, int display_level, Game game) {
    std::cout << "Attempting to solve a new game with Monte Carlo... " << std::flush;
    while (game.canContinue()) {
        if (display_level >= 2) {
            std::cout << std::endl << game;
        }
        int scores[4] = {0, 0, 0, 0};
        int counter[4] = {0, 0, 0, 0};

        for (int i = 0; i < runs; ++i) {
            std::pair<int, int> res = simulateOneRun(game);
            scores[res.first] += res.second;
            counter[res.first] += 1;
        }

        int best_avg_score = 0;
        int best_move = UP;
        for (int i = 0; i < 4; ++i) {
            int move = i;
            if (counter[move] != 0 && ((float) scores[move] / counter[move]) > best_avg_score) {
                best_avg_score = (float) scores[move] / counter[move];
                best_move = move;
            }
        }

        switch(best_move) {
            case UP:
                game.up(false);
                break;
            case DOWN:
                game.down(false);
                break;
            case LEFT:
                game.left(false);
                break;
            case RIGHT:
                game.right(false);
                break;
        }
    }
    if (display_level <= 1) {
        std::cout << "Done!" << (display_level == 0 ? "\n" : "");
    }
    if (display_level >= 1) {
        std::cout << std::endl << game << std::endl;
    }
    std::pair<int, int> ret(game.getHighestTile(), game.score);
    return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdx, %rbx
movl %esi, %ebp
movl %edi, 0xc(%rsp)
movq 0xb5cb(%rip), %r14      # 0xdfd8
leaq 0x763c(%rip), %rsi      # 0xa050
movl $0x33, %edx
movq %r14, %rdi
callq 0x21e0
movq %r14, %rdi
callq 0x2110
movq %rbx, %rdi
callq 0x63a6
testb %al, %al
je 0x2b70
leaq 0x40(%rsp), %r12
leaq 0x8(%rbx), %r13
movl %ebp, 0x8(%rsp)
cmpl $0x2, %ebp
jl 0x2a84
movq 0xb586(%rip), %r14      # 0xdfd8
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x2180
movsbl %al, %esi
movq %r14, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2110
movq %rax, %rdi
movq %rbx, %rsi
callq 0x7287
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movaps %xmm0, 0x10(%rsp)
movl 0xc(%rsp), %eax
movl %eax, %r15d
testl %eax, %eax
jle 0x2ada
movl (%rbx), %eax
movl %eax, 0x38(%rsp)
movq %r12, %rdi
movq %r13, %rsi
callq 0x2d38
leaq 0x38(%rsp), %rdi
callq 0x27fc
movq %rax, %rbp
movq %rax, %r14
shrq $0x20, %r14
movq %r12, %rdi
callq 0x2e18
movslq %ebp, %rax
addl %r14d, 0x20(%rsp,%rax,4)
incl 0x10(%rsp,%rax,4)
decl %r15d
jne 0x2a9c
xorl %eax, %eax
xorl %ecx, %ecx
xorl %edx, %edx
movl 0x10(%rsp,%rax,4), %esi
testl %esi, %esi
je 0x2b0e
xorps %xmm0, %xmm0
cvtsi2ssl 0x20(%rsp,%rax,4), %xmm0
xorps %xmm1, %xmm1
cvtsi2ss %esi, %xmm1
xorps %xmm2, %xmm2
cvtsi2ss %edx, %xmm2
divss %xmm1, %xmm0
ucomiss %xmm2, %xmm0
jbe 0x2b0e
cvttss2si %xmm0, %edx
movl %eax, %ecx
incq %rax
cmpq $0x4, %rax
jne 0x2ae0
cmpl $0x3, %ecx
movl 0x8(%rsp), %ebp
ja 0x2b60
movl %ecx, %eax
leaq 0x7517(%rip), %rcx      # 0xa040
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %rbx, %rdi
xorl %esi, %esi
callq 0x673c
jmp 0x2b60
movq %rbx, %rdi
xorl %esi, %esi
callq 0x67e4
jmp 0x2b60
movq %rbx, %rdi
xorl %esi, %esi
callq 0x680c
jmp 0x2b60
movq %rbx, %rdi
xorl %esi, %esi
callq 0x67bc
movq %rbx, %rdi
callq 0x63a6
testb %al, %al
jne 0x2a46
cmpl $0x1, %ebp
jg 0x2bb1
movq 0xb45c(%rip), %r14      # 0xdfd8
leaq 0x7501(%rip), %rsi      # 0xa084
movl $0x5, %edx
movq %r14, %rdi
callq 0x21e0
xorl %edx, %edx
testl %ebp, %ebp
sete %dl
leaq 0x74ec(%rip), %rax      # 0xa08a
leaq 0x74e6(%rip), %rsi      # 0xa08b
cmoveq %rax, %rsi
movq %r14, %rdi
callq 0x21e0
testl %ebp, %ebp
jle 0x2c18
movq 0xb41c(%rip), %r14      # 0xdfd8
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x2180
movsbl %al, %esi
movq %r14, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2110
movq %rax, %rdi
movq %rbx, %rsi
callq 0x7287
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x2180
movsbl %al, %esi
movq %r14, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2110
movq %rbx, %rdi
callq 0x7228
movl (%rbx), %ecx
shlq $0x20, %rcx
movl %eax, %eax
orq %rcx, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq %r12, %rdi
callq 0x2e18
movq %rbx, %rdi
callq 0x22b0