// File: tyhsu[P]Ewn/randomAi.cpp
// Function: RandomAi::AI_move(Game&, int)
Movement RandomAi::AI_move(Game& cur_game, int dice)
{
    //cerr << "calculating..." ;
    //cerr.flush();
    // cout << "read to random" << endl;
    clock_t init_time = clock();
    this->ai_side = cur_game.get_is_switch();
    this->ai_symbol = this->ai_side? 'A': '1';

    Movement available_mvmt_list[6]; // random
    int available_mvmt_cnt = 0;

    int best_val = -1e9, child_val;
    int win = this->ai_side? 2: 1;
    int next_move_cnt = cur_game.count_movable_chs(dice);
    for (int i=0; i<next_move_cnt; i++) {
        int chs_index = cur_game.get_movable_chs(i).symbol - this->ai_symbol;
        for (int direct = 0; direct < 3; direct++) {
            Movement mvmt(chs_index, direct);
            if (cur_game.check_in_board(mvmt)) {
                available_mvmt_list[available_mvmt_cnt] = mvmt;
                available_mvmt_cnt ++;
            }
        }
    }

    // randomly pick a move.
    Movement next_mvmt = available_mvmt_list[rand() % available_mvmt_cnt];

    double timeCost = (double)(clock() - init_time) / CLOCKS_PER_SEC;

    return next_mvmt;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xc0(%rbp)
callq 0x20b0
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rdi
callq 0xb910
movb %al, %cl
movq -0xc0(%rbp), %rax
andb $0x1, %cl
movb %cl, (%rax)
movb (%rax), %cl
movb $0x41, %al
movb $0x31, %dl
movb %dl, -0xb2(%rbp)
testb $0x1, %cl
movb %al, -0xb1(%rbp)
jne 0x6874
movb -0xb2(%rbp), %al
movb %al, -0xb1(%rbp)
movq -0xc0(%rbp), %rax
movb -0xb1(%rbp), %cl
movb %cl, 0x1(%rax)
leaq -0x60(%rbp), %rax
movq %rax, %rcx
addq $0x30, %rcx
movq %rcx, -0xd0(%rbp)
movq %rax, -0xc8(%rbp)
movq -0xc8(%rbp), %rdi
movq %rdi, -0xd8(%rbp)
callq 0x4c60
movq -0xd8(%rbp), %rax
movq -0xd0(%rbp), %rcx
addq $0x8, %rax
cmpq %rcx, %rax
movq %rax, -0xc8(%rbp)
jne 0x689d
movq -0xc0(%rbp), %rax
movl $0x0, -0x64(%rbp)
movl $0xc4653600, -0x68(%rbp) # imm = 0xC4653600
movb (%rax), %dl
movl $0x1, %eax
movl $0x2, %ecx
testb $0x1, %dl
cmovnel %ecx, %eax
movl %eax, -0x70(%rbp)
movq -0x18(%rbp), %rdi
leaq -0x1c(%rbp), %rsi
callq 0xada0
movl %eax, -0x74(%rbp)
movl $0x0, -0x78(%rbp)
movl -0x78(%rbp), %eax
cmpl -0x74(%rbp), %eax
jge 0x69ea
movq -0x18(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0xb7c0
movq -0xc0(%rbp), %rcx
movl %edx, -0x90(%rbp)
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
movq %rax, -0x88(%rbp)
movl -0x90(%rbp), %eax
movl %eax, -0x80(%rbp)
movsbl -0x88(%rbp), %eax
movsbl 0x1(%rcx), %ecx
subl %ecx, %eax
movl %eax, -0x7c(%rbp)
movl $0x0, -0x9c(%rbp)
cmpl $0x3, -0x9c(%rbp)
jge 0x69da
leaq -0xa4(%rbp), %rdi
leaq -0x7c(%rbp), %rsi
leaq -0x9c(%rbp), %rdx
callq 0x5c40
movq -0x18(%rbp), %rdi
leaq -0xa4(%rbp), %rsi
callq 0xafe0
testb $0x1, %al
jne 0x69a3
jmp 0x69c7
movslq -0x64(%rbp), %rax
leaq -0x60(%rbp), %rdi
shlq $0x3, %rax
addq %rax, %rdi
leaq -0xa4(%rbp), %rsi
callq 0x6a30
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x69c9
movl -0x9c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x9c(%rbp)
jmp 0x696d
jmp 0x69dc
movl -0x78(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x78(%rbp)
jmp 0x690f
callq 0x2050
cltd
idivl -0x64(%rbp)
movslq %edx, %rax
movq -0x60(%rbp,%rax,8), %rax
movq %rax, -0x8(%rbp)
callq 0x20b0
subq -0x28(%rbp), %rax
cvtsi2sd %rax, %xmm0
movsd 0x669b(%rip), %xmm1     # 0xd0b0
divsd %xmm1, %xmm0
movsd %xmm0, -0xb0(%rbp)
movq -0x8(%rbp), %rax
addq $0xe0, %rsp
popq %rbp
retq
nop