// File: /workspace/github_cpp_makefile_star2_O1/syky27[P]TicTacToe/src/DNSmarterBotPlayer.cpp
// Function: DNSmarterBotPlayer::triggerTurn(int, int)
bool DNSmarterBotPlayer::triggerTurn(int y, int x)
{

    for (size_t i = 1; i < game_map->getMapSize() - 1; i++) {
        for (size_t j = 1; j < game_map->getMapSize() - 1; j++) {
            if (game_map->my_map[i][j]->getState() != EMPTY) {
                if (game_map->my_map[i + 1][j]->getState() == EMPTY) {
                    availableMoves.insert(make_pair(i + 1, j));
                }
                if (game_map->my_map[i][j + 1]->getState() == EMPTY) {
                    availableMoves.insert(make_pair(i, j + 1));
                }
                //                if (game_map->my_map[i+1][j+1]->getState() == EMPTY) {
                //                    availableMoves.insert(make_pair(i+1,j+1));
                //                }
                if (game_map->my_map[i - 1][j]->getState() == EMPTY) {
                    availableMoves.insert(make_pair(i - 1, j));
                }
                if (game_map->my_map[i][j - 1]->getState() == EMPTY) {
                    availableMoves.insert(make_pair(i, j - 1));
                }
                //                if (game_map->my_map[i-1][j-1]->getState() == EMPTY) {
                //                    availableMoves.insert(make_pair(i-1,j-1));
                //                }
                //                if (game_map->my_map[i+1][j-1]->getState() == EMPTY) {
                //                    availableMoves.insert(make_pair(i+1,j-1));
                //                }
                //                if (game_map->my_map[i-1][j+1]->getState() == EMPTY) {
                //                    availableMoves.insert(make_pair(i-1,j+1));
                //                }


            }
        }
    }


    int x_p;
    int y_p;
   // int y_plus;
    int x_plus;

    if (availableMoves.size() > 0) {
        srand(time(0));
        size_t ran = rand() % availableMoves.size();
        set <pair<int, int> >::iterator it = availableMoves.begin();
        for (; ran != 0; ran--) *it++;

        x_p = it->first;
        y_p = it->second;
     //   y_plus = y_p + 3;
        x_plus = x_p + 3;

        it = availableMoves.begin();
        for (; it != availableMoves.end(); it++) {
            DNLogService::sharedObject() << it->first << "-" << it->second << "\n";
        }
    }
    else {
        srand(time(0));
        x_p = (rand() % 10) + 1;
        y_p = (rand() % 10) + 1;
     //   y_plus = y_p + 3;
        x_plus = x_p + 3;
    }

    DNCellState state;
    if (this->game_map->recordMove(this->getSymbol(), y_p, x_p, state)) {
        usleep(100000);
        DNWindow::getInstance().drawSymbolBetter(this->getSymbol(), y_p, x_plus);
        this->game_map->debugToFile();
        if (state != EMPTY) {
            if (state == this->getSymbol()) {
                string win = " is Winner!!!";
                win = this->getNick() + win;
                sleep(3);
                DNWindow::getInstance().terminateWithErrorMessage(win);
            }

        }
        else {
            availableMoves.clear();
            return true;
        }
    }


    availableMoves.clear();

    return false;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rdi, %rbx
movq 0x48(%rdi), %rdi
callq 0x4f8a
addq $-0x3, %rax
cmpq $-0x3, %rax
ja 0x708b
leaq 0x50(%rbx), %r14
movl $0x1, %r12d
xorl %r13d, %r13d
leaq 0x8(%rsp), %r15
movq 0x48(%rbx), %rdi
callq 0x4f8a
addq $-0x3, %rax
cmpq $-0x3, %rax
ja 0x706c
leal 0x1(%r12), %eax
movl %eax, 0x30(%rsp)
leal -0x1(%r12), %eax
movl %eax, 0x2c(%rsp)
xorl %ebp, %ebp
movq 0x48(%rbx), %rax
addq %r13, %rax
movq 0x340(%rax,%rbp,8), %rdi
callq 0x3a24
testl %eax, %eax
je 0x704c
movq 0x48(%rbx), %rax
addq %r13, %rax
movq 0x660(%rax,%rbp,8), %rdi
callq 0x3a24
testl %eax, %eax
jne 0x6fc2
movl 0x30(%rsp), %eax
movl %eax, 0x8(%rsp)
leal 0x1(%rbp), %eax
movl %eax, 0xc(%rsp)
movq %r14, %rdi
movq %r15, %rsi
callq 0x743c
movq 0x48(%rbx), %rax
addq %r13, %rax
movq 0x348(%rax,%rbp,8), %rdi
callq 0x3a24
testl %eax, %eax
jne 0x6ff1
movl %r12d, 0x8(%rsp)
leal 0x2(%rbp), %eax
movl %eax, 0xc(%rsp)
movq %r14, %rdi
movq %r15, %rsi
callq 0x743c
movq 0x48(%rbx), %rax
addq %r13, %rax
movq 0x20(%rax,%rbp,8), %rdi
callq 0x3a24
testl %eax, %eax
jne 0x7020
movl 0x2c(%rsp), %eax
movl %eax, 0x8(%rsp)
leal 0x1(%rbp), %eax
movl %eax, 0xc(%rsp)
movq %r14, %rdi
movq %r15, %rsi
callq 0x743c
movq 0x48(%rbx), %rax
addq %r13, %rax
movq 0x338(%rax,%rbp,8), %rdi
callq 0x3a24
testl %eax, %eax
jne 0x704c
movl %r12d, 0x8(%rsp)
movl %ebp, 0xc(%rsp)
movq %r14, %rdi
movq %r15, %rsi
callq 0x743c
movq 0x48(%rbx), %rdi
callq 0x4f8a
decq %rax
leaq 0x1(%rbp), %rcx
addq $0x2, %rbp
cmpq %rax, %rbp
movq %rcx, %rbp
jb 0x6f74
incq %r12
movq 0x48(%rbx), %rdi
callq 0x4f8a
decq %rax
addq $0x320, %r13            # imm = 0x320
cmpq %rax, %r12
jb 0x6f49
movq 0x78(%rbx), %r14
xorl %edi, %edi
callq 0x3290
movl %eax, %edi
callq 0x32b0
callq 0x30b0
movslq %eax, %r15
testq %r14, %r14
je 0x7148
movq %r15, %rax
xorl %edx, %edx
divq 0x78(%rbx)
movq 0x68(%rbx), %r15
movq %r15, %rax
testq %rdx, %rdx
je 0x70d6
movq %rdx, %r14
movq %r15, %rax
movq %rax, %rdi
callq 0x3380
decq %r14
jne 0x70c9
movl 0x20(%rax), %r14d
movl 0x24(%rax), %eax
movl %eax, (%rsp)
leal 0x3(%r14), %eax
movl %eax, 0x4(%rsp)
leaq 0x58(%rbx), %rbp
cmpq %rbp, %r15
je 0x71a2
leaq 0x2323(%rip), %r12      # 0x941f
leaq 0x240b(%rip), %r13      # 0x950e
callq 0x3f9a
movl 0x20(%r15), %esi
movq %rax, %rdi
callq 0x4d1c
movq %rax, %rdi
movq %r12, %rsi
callq 0x4cb0
movl 0x24(%r15), %esi
movq %rax, %rdi
callq 0x4d1c
movq %rax, %rdi
movq %r13, %rsi
callq 0x4cb0
movq %r15, %rdi
callq 0x3380
movq %rax, %r15
cmpq %rbp, %rax
jne 0x7103
jmp 0x71a2
imulq $0x66666667, %r15, %rax # imm = 0x66666667
movq %rax, %rcx
shrq $0x3f, %rcx
sarq $0x22, %rax
addl %ecx, %eax
addl %eax, %eax
leal (%rax,%rax,4), %r12d
movl %r15d, %r14d
subl %r12d, %r14d
incl %r14d
callq 0x30b0
cltq
imulq $0x66666667, %rax, %rcx # imm = 0x66666667
movq %rcx, %rdx
shrq $0x3f, %rdx
sarq $0x22, %rcx
addl %edx, %ecx
addl %ecx, %ecx
leal (%rcx,%rcx,4), %ecx
negl %ecx
addl %ecx, %eax
incl %eax
movl %eax, (%rsp)
negl %r12d
leal (%r15,%r12), %eax
addl $0x4, %eax
movl %eax, 0x4(%rsp)
movq 0x48(%rbx), %r15
movq %rbx, %rdi
callq 0x6234
movl (%rsp), %edx
leaq 0x34(%rsp), %r8
movq %r15, %rdi
movl %eax, %esi
movl %r14d, %ecx
callq 0x53c6
testb %al, %al
je 0x7366
movl $0x186a0, %edi          # imm = 0x186A0
callq 0x33f0
callq 0x39ee
movq %rax, %r14
movq %rbx, %rdi
callq 0x6234
movq %rsp, %rdx
leaq 0x4(%rsp), %rcx
movq %r14, %rdi
movl %eax, %esi
callq 0x8be6
movq 0x48(%rbx), %rdi
callq 0x4f8e
movl 0x34(%rsp), %ebp
testl %ebp, %ebp
je 0x728c
movq %rbx, %rdi
callq 0x6234
cmpl %eax, %ebp
jne 0x7366
leaq 0x18(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x21f5(%rip), %rsi      # 0x9421
leaq 0x21fb(%rip), %rdx      # 0x942e
leaq 0x8(%rsp), %rdi
callq 0x4048
leaq 0x58(%rsp), %rdi
movq %rbx, %rsi
callq 0x6238
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
leaq 0x58(%rsp), %rdi
callq 0x30f0
leaq 0x88(%rsp), %r12
movq %r12, -0x10(%r12)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x7294
movq %rdx, 0x78(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x88(%rsp)
jmp 0x729c
movb $0x1, %bpl
jmp 0x7368
movups (%rcx), %xmm0
movups %xmm0, (%r12)
movq 0x8(%rax), %rdx
leaq 0x78(%rsp), %r14
movq %rdx, 0x8(%r14)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0x33b0
movq (%r14), %rdi
cmpq %r12, %rdi
je 0x72dd
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x32f0
leaq 0x68(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x72f8
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x32f0
movl $0x3, %edi
callq 0x3230
callq 0x39ee
movq %rax, %r14
leaq 0x48(%rsp), %r12
movq %r12, -0x10(%r12)
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
addq %rsi, %rdx
leaq 0x38(%rsp), %rdi
callq 0x3fb8
leaq 0x38(%rsp), %rsi
movq %r14, %rdi
callq 0x82c0
movq 0x38(%rsp), %rdi
cmpq %r12, %rdi
je 0x734f
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x32f0
movq 0x8(%rsp), %rdi
cmpq %r15, %rdi
je 0x7366
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x32f0
xorl %ebp, %ebp
addq $0x50, %rbx
movq %rbx, %rdi
callq 0x7590
movl %ebp, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x38(%rsp), %rdi
cmpq %r12, %rdi
je 0x73c1
movq 0x48(%rsp), %rsi
jmp 0x73b2
movq %rax, %rbx
leaq 0x68(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x73c1
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x32f0
jmp 0x73c1
jmp 0x73be
movq %rax, %rbx
movq 0x8(%rsp), %rdi
cmpq %r15, %rdi
je 0x73d8
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x32f0
movq %rbx, %rdi
callq 0x34b0