// File: NathanWine[P]2048-AI-Solvers/src/Expectimax.cpp
// Function: expectimaxScore(int, Game, bool)
float expectimaxScore(int depth, Game game, bool is_max) {
    std::map<int, weightedmoves> possibilities = game.computePossibilities();    
    if (possibilities.size() == 0) {
        return 0.0;
    }
    else if (is_max) {
        if (depth == 0) {
            return Heuristics::get_h_score(game.state);
        }
        else {
            std::map<int, float> scores;
            for (auto const& entry : possibilities) {
                int move = entry.first;                             // Direction of move
                weightedmoves weighted_subset = entry.second;       // Vector of (prob, game)
                int len = (int) weighted_subset.size();
                if (len > 0) {
                    scores[move] = 0.0;
                    for (int j = 0; j < len; ++j) {
                        scores[move] += Heuristics::get_h_score(weighted_subset[j].second.state) * weighted_subset[j].first;
                    }
                }
            }

            float max_score = -std::numeric_limits<float>::max();
            int max_choice = UP;
            for (auto const& score : scores) {      // Determine best move choice based on tabulated scores
                if (score.second > max_score) {
                    max_score = score.second;
                    max_choice = score.first;
                }
            }

            switch (max_choice) {
                case UP:
                    game.up(false);
                    break;
                case DOWN:
                    game.down(false);
                    break;
                case LEFT: 
                    game.left(false);
                    break;
                case RIGHT:
                    game.right(false);
            }

            return expectimaxScore(depth - 1, game, false);
        }
    }
    else {
        std::map<int, float> scores;
        for (auto const& entry : possibilities) {
            int move = entry.first;                             // Direction of move
            weightedmoves weighted_subset = entry.second;       // Vector of (prob, game)
            int len = (int) weighted_subset.size();
            if (len > 0) {
                scores[move] = 0.0;
                for (int j = 0; j < len; ++j) {
                    scores[move] += expectimaxScore(depth, weighted_subset[j].second, true) * weighted_subset[j].first;
                }
            }
        }
        float sum = 0.0;
        for (auto const& score : scores) {
            sum += score.second;
        }
        return sum / scores.size();
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x250, %rsp            # imm = 0x250
movq %rsi, -0x1e8(%rbp)
movb %dl, %al
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
andb $0x1, %al
movb %al, -0x11(%rbp)
leaq -0x48(%rbp), %rdi
callq 0xfbb0
leaq -0x48(%rbp), %rdi
callq 0x16940
cmpq $0x0, %rax
jne 0x15a3c
xorps %xmm0, %xmm0
movss %xmm0, -0x4(%rbp)
movl $0x1, -0x4c(%rbp)
jmp 0x161af
testb $0x1, -0x11(%rbp)
je 0x15e4b
cmpl $0x0, -0x8(%rbp)
jne 0x15a90
movq -0x1e8(%rbp), %rdi
addq $0x8, %rdi
callq 0x16f90
movss %xmm0, -0x1ec(%rbp)
jmp 0x15a66
movss -0x1ec(%rbp), %xmm0
movss %xmm0, -0x4(%rbp)
movl $0x1, -0x4c(%rbp)
jmp 0x161af
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x161c6
leaq -0x90(%rbp), %rdi
callq 0x8030
leaq -0x48(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rdi
callq 0x8050
movq %rax, -0xa0(%rbp)
movq -0x98(%rbp), %rdi
callq 0x8080
movq %rax, -0xa8(%rbp)
leaq -0xa0(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x80b0
testb $0x1, %al
jne 0x15ae9
jmp 0x15c75
leaq -0xa0(%rbp), %rdi
callq 0x80e0
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
movl (%rax), %eax
movl %eax, -0xb4(%rbp)
movq -0xb0(%rbp), %rsi
addq $0x8, %rsi
leaq -0xd0(%rbp), %rdi
callq 0x16960
jmp 0x15b24
leaq -0xd0(%rbp), %rdi
callq 0x8100
movl %eax, -0xd4(%rbp)
cmpl $0x0, -0xd4(%rbp)
jle 0x15c58
leaq -0x90(%rbp), %rdi
leaq -0xb4(%rbp), %rsi
callq 0x8130
movq %rax, -0x1f8(%rbp)
jmp 0x15b5f
movq -0x1f8(%rbp), %rax
xorps %xmm0, %xmm0
movss %xmm0, (%rax)
movl $0x0, -0xd8(%rbp)
movl -0xd8(%rbp), %eax
cmpl -0xd4(%rbp), %eax
jge 0x15c56
movslq -0xd8(%rbp), %rsi
leaq -0xd0(%rbp), %rdi
callq 0x11650
movq %rax, %rdi
addq $0x10, %rdi
callq 0x16f90
movss %xmm0, -0x1fc(%rbp)
jmp 0x15bb2
movslq -0xd8(%rbp), %rsi
leaq -0xd0(%rbp), %rdi
callq 0x11650
movss (%rax), %xmm0
movss %xmm0, -0x20c(%rbp)
leaq -0x90(%rbp), %rdi
leaq -0xb4(%rbp), %rsi
callq 0x8130
movq %rax, -0x208(%rbp)
jmp 0x15bed
movq -0x208(%rbp), %rax
movss -0x20c(%rbp), %xmm2
movss -0x1fc(%rbp), %xmm0
movss (%rax), %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, (%rax)
movl -0xd8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xd8(%rbp)
jmp 0x15b77
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x15e3a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0xd0(%rbp), %rdi
callq 0x8b00
jmp 0x15e3a
jmp 0x15c58
leaq -0xd0(%rbp), %rdi
callq 0x8b00
leaq -0xa0(%rbp), %rdi
callq 0x8230
jmp 0x15acd
callq 0x7e80
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
movss %xmm0, -0xdc(%rbp)
movl $0x0, -0xe0(%rbp)
leaq -0x90(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0xe8(%rbp), %rdi
callq 0x8260
movq %rax, -0xf0(%rbp)
movq -0xe8(%rbp), %rdi
callq 0x8290
movq %rax, -0xf8(%rbp)
leaq -0xf0(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
callq 0x82c0
testb $0x1, %al
jne 0x15ce6
jmp 0x15d41
leaq -0xf0(%rbp), %rdi
callq 0x82f0
movq %rax, -0x100(%rbp)
movq -0x100(%rbp), %rax
movss 0x4(%rax), %xmm0
ucomiss -0xdc(%rbp), %xmm0
jbe 0x15d31
movq -0x100(%rbp), %rax
movss 0x4(%rax), %xmm0
movss %xmm0, -0xdc(%rbp)
movq -0x100(%rbp), %rax
movl (%rax), %eax
movl %eax, -0xe0(%rbp)
jmp 0x15d33
leaq -0xf0(%rbp), %rdi
callq 0x8310
jmp 0x15ccd
movl -0xe0(%rbp), %eax
movq %rax, -0x218(%rbp)
subq $0x3, %rax
ja 0x15db3
movq -0x218(%rbp), %rax
leaq 0x280a(%rip), %rcx      # 0x1856c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x1e8(%rbp), %rdi
xorl %esi, %esi
callq 0xfa10
jmp 0x15d7b
jmp 0x15db3
movq -0x1e8(%rbp), %rdi
xorl %esi, %esi
callq 0xfac0
jmp 0x15d8d
jmp 0x15db3
movq -0x1e8(%rbp), %rdi
xorl %esi, %esi
callq 0xfb10
jmp 0x15d9f
jmp 0x15db3
movq -0x1e8(%rbp), %rdi
xorl %esi, %esi
callq 0xfb60
jmp 0x15db1
jmp 0x15db3
movq -0x1e8(%rbp), %rsi
movl -0x8(%rbp), %eax
decl %eax
movl %eax, -0x21c(%rbp)
leaq -0x120(%rbp), %rdi
callq 0x3f70
jmp 0x15dd3
movl -0x21c(%rbp), %edi
leaq -0x120(%rbp), %rsi
xorl %edx, %edx
callq 0x159f0
movss %xmm0, -0x220(%rbp)
jmp 0x15df1
movss -0x220(%rbp), %xmm0
movss %xmm0, -0x4(%rbp)
leaq -0x120(%rbp), %rdi
callq 0x4460
movl $0x1, -0x4c(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x8340
jmp 0x161af
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x120(%rbp), %rdi
callq 0x4460
leaq -0x90(%rbp), %rdi
callq 0x8340
jmp 0x161c6
leaq -0x150(%rbp), %rdi
callq 0x8030
leaq -0x48(%rbp), %rax
movq %rax, -0x158(%rbp)
movq -0x158(%rbp), %rdi
callq 0x8050
movq %rax, -0x160(%rbp)
movq -0x158(%rbp), %rdi
callq 0x8080
movq %rax, -0x168(%rbp)
leaq -0x160(%rbp), %rdi
leaq -0x168(%rbp), %rsi
callq 0x80b0
testb $0x1, %al
jne 0x15ea4
jmp 0x16081
leaq -0x160(%rbp), %rdi
callq 0x80e0
movq %rax, -0x170(%rbp)
movq -0x170(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x174(%rbp)
movq -0x170(%rbp), %rsi
addq $0x8, %rsi
leaq -0x190(%rbp), %rdi
callq 0x16960
jmp 0x15edf
leaq -0x190(%rbp), %rdi
callq 0x8100
movl %eax, -0x194(%rbp)
cmpl $0x0, -0x194(%rbp)
jle 0x16053
leaq -0x150(%rbp), %rdi
leaq -0x174(%rbp), %rsi
callq 0x8130
movq %rax, -0x228(%rbp)
jmp 0x15f1a
movq -0x228(%rbp), %rax
xorps %xmm0, %xmm0
movss %xmm0, (%rax)
movl $0x0, -0x198(%rbp)
movl -0x198(%rbp), %eax
cmpl -0x194(%rbp), %eax
jge 0x16051
movl -0x8(%rbp), %eax
movl %eax, -0x22c(%rbp)
movslq -0x198(%rbp), %rsi
leaq -0x190(%rbp), %rdi
callq 0x11650
movq %rax, %rsi
addq $0x8, %rsi
leaq -0x1b8(%rbp), %rdi
callq 0x3f70
jmp 0x15f75
movl -0x22c(%rbp), %edi
leaq -0x1b8(%rbp), %rsi
movl $0x1, %edx
callq 0x159f0
movss %xmm0, -0x230(%rbp)
jmp 0x15f96
movslq -0x198(%rbp), %rsi
leaq -0x190(%rbp), %rdi
callq 0x11650
movss (%rax), %xmm0
movss %xmm0, -0x23c(%rbp)
leaq -0x150(%rbp), %rdi
leaq -0x174(%rbp), %rsi
callq 0x8130
movq %rax, -0x238(%rbp)
jmp 0x15fd1
movq -0x238(%rbp), %rax
movss -0x23c(%rbp), %xmm2
movss -0x230(%rbp), %xmm0
movss (%rax), %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, (%rax)
leaq -0x1b8(%rbp), %rdi
callq 0x4460
movl -0x198(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x198(%rbp)
jmp 0x15f32
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x161a1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x16070
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x1b8(%rbp), %rdi
callq 0x4460
jmp 0x16070
jmp 0x16053
leaq -0x190(%rbp), %rdi
callq 0x8b00
leaq -0x160(%rbp), %rdi
callq 0x8230
jmp 0x15e88
leaq -0x190(%rbp), %rdi
callq 0x8b00
jmp 0x161a1
xorps %xmm0, %xmm0
movss %xmm0, -0x1bc(%rbp)
leaq -0x150(%rbp), %rax
movq %rax, -0x1c8(%rbp)
movq -0x1c8(%rbp), %rdi
callq 0x8260
movq %rax, -0x1d0(%rbp)
movq -0x1c8(%rbp), %rdi
callq 0x8290
movq %rax, -0x1d8(%rbp)
leaq -0x1d0(%rbp), %rdi
leaq -0x1d8(%rbp), %rsi
callq 0x82c0
testb $0x1, %al
jne 0x160d9
jmp 0x16116
leaq -0x1d0(%rbp), %rdi
callq 0x82f0
movq %rax, -0x1e0(%rbp)
movq -0x1e0(%rbp), %rax
movss 0x4(%rax), %xmm0
addss -0x1bc(%rbp), %xmm0
movss %xmm0, -0x1bc(%rbp)
leaq -0x1d0(%rbp), %rdi
callq 0x8310
jmp 0x160c0
movss -0x1bc(%rbp), %xmm0
movss %xmm0, -0x248(%rbp)
leaq -0x150(%rbp), %rdi
callq 0x16a50
movq %rax, %rdx
shrq %rdx
movl %eax, %ecx
andl $0x1, %ecx
orq %rdx, %rcx
cvtsi2ss %rcx, %xmm0
addss %xmm0, %xmm0
cvtsi2ss %rax, %xmm1
movss %xmm1, -0x244(%rbp)
testq %rax, %rax
movss %xmm0, -0x240(%rbp)
js 0x16173
movss -0x244(%rbp), %xmm0
movss %xmm0, -0x240(%rbp)
movss -0x248(%rbp), %xmm0
movss -0x240(%rbp), %xmm1
divss %xmm1, %xmm0
movss %xmm0, -0x4(%rbp)
movl $0x1, -0x4c(%rbp)
leaq -0x150(%rbp), %rdi
callq 0x8340
jmp 0x161af
leaq -0x150(%rbp), %rdi
callq 0x8340
jmp 0x161c6
leaq -0x48(%rbp), %rdi
callq 0x8360
movss -0x4(%rbp), %xmm0
addq $0x250, %rsp            # imm = 0x250
popq %rbp
retq
leaq -0x48(%rbp), %rdi
callq 0x8360
movq -0x58(%rbp), %rdi
callq 0x33d0
nopl (%rax,%rax)