// File: /workspace/github_cpp_makefile_star2_O3/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 $0xb8, %rsp
movq %rdi, %rbx
movq 0x48(%rdi), %rdi
callq 0x4f7e
addq $-0x3, %rax
cmpq $-0x3, %rax
ja 0x7079
leaq 0x50(%rbx), %r14
movl $0x1, %r12d
movq 0x48(%rbx), %rdi
callq 0x4f7e
addq $-0x3, %rax
leaq 0x1(%r12), %rcx
movq %rcx, 0x30(%rsp)
cmpq $-0x3, %rax
ja 0x705c
movl $0x1, %r15d
imulq $0x320, %r12, %rbp      # imm = 0x320
movq %r12, %rax
decq %rax
imulq $0x320, 0x30(%rsp), %rcx # imm = 0x320
movq %rcx, 0x50(%rsp)
movq %rax, 0x40(%rsp)
imulq $0x320, %rax, %rax      # imm = 0x320
movq %rax, 0x48(%rsp)
movq 0x48(%rbx), %rax
addq %rbp, %rax
movq 0x18(%rax,%r15,8), %rdi
callq 0x3a24
testl %eax, %eax
je 0x703e
movq 0x48(%rbx), %rax
addq 0x50(%rsp), %rax
movq 0x18(%rax,%r15,8), %rdi
callq 0x3a24
testl %eax, %eax
jne 0x6fab
movq 0x30(%rsp), %rax
movl %eax, 0x10(%rsp)
movl %r15d, 0x14(%rsp)
movq %r14, %rdi
leaq 0x10(%rsp), %rsi
callq 0x7440
movq 0x48(%rbx), %rax
addq %rbp, %rax
leaq 0x1(%r15), %r13
movq 0x20(%rax,%r15,8), %rdi
callq 0x3a24
testl %eax, %eax
jne 0x6fdb
movl %r12d, 0x10(%rsp)
movl %r13d, 0x14(%rsp)
movq %r14, %rdi
leaq 0x10(%rsp), %rsi
callq 0x7440
movq 0x48(%rbx), %rax
addq 0x48(%rsp), %rax
movq 0x18(%rax,%r15,8), %rdi
callq 0x3a24
testl %eax, %eax
jne 0x700d
movq 0x40(%rsp), %rax
movl %eax, 0x10(%rsp)
movl %r15d, 0x14(%rsp)
movq %r14, %rdi
leaq 0x10(%rsp), %rsi
callq 0x7440
movq 0x48(%rbx), %rax
addq %rbp, %rax
movq 0x10(%rax,%r15,8), %rdi
callq 0x3a24
testl %eax, %eax
jne 0x7044
decq %r15
movl %r12d, 0x10(%rsp)
movl %r15d, 0x14(%rsp)
movq %r14, %rdi
leaq 0x10(%rsp), %rsi
callq 0x7440
jmp 0x7044
incq %r15
movq %r15, %r13
movq 0x48(%rbx), %rdi
callq 0x4f7e
decq %rax
movq %r13, %r15
cmpq %rax, %r13
jb 0x6f60
movq 0x48(%rbx), %rdi
callq 0x4f7e
decq %rax
movq 0x30(%rsp), %rcx
movq %rcx, %r12
cmpq %rax, %rcx
jb 0x6f0d
movq 0x78(%rbx), %r14
xorl %edi, %edi
callq 0x3290
movl %eax, %edi
callq 0x32b0
callq 0x30b0
movslq %eax, %r15
testq %r14, %r14
je 0x7137
movq %r15, %rax
xorl %edx, %edx
divq 0x78(%rbx)
movq 0x68(%rbx), %r15
movq %r15, %rax
testq %rdx, %rdx
je 0x70c4
movq %rdx, %r14
movq %r15, %rax
movq %rax, %rdi
callq 0x3380
decq %r14
jne 0x70b7
movl 0x20(%rax), %r14d
movl 0x24(%rax), %eax
movl %eax, 0x8(%rsp)
leal 0x3(%r14), %eax
movl %eax, 0xc(%rsp)
leaq 0x58(%rbx), %rbp
cmpq %rbp, %r15
je 0x7192
leaq 0x2334(%rip), %r12      # 0x941f
leaq 0x241c(%rip), %r13      # 0x950e
callq 0x3f98
movl 0x20(%r15), %esi
movq %rax, %rdi
callq 0x4d10
movq %rax, %rdi
movq %r12, %rsi
callq 0x4ca4
movl 0x24(%r15), %esi
movq %rax, %rdi
callq 0x4d10
movq %rax, %rdi
movq %r13, %rsi
callq 0x4ca4
movq %r15, %rdi
callq 0x3380
movq %rax, %r15
cmpq %rbp, %rax
jne 0x70f2
jmp 0x7192
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, 0x8(%rsp)
negl %r12d
leal (%r15,%r12), %eax
addl $0x4, %eax
movl %eax, 0xc(%rsp)
movq 0x48(%rbx), %r15
movq %rbx, %rdi
callq 0x6244
movl 0x8(%rsp), %edx
leaq 0x3c(%rsp), %r8
movq %r15, %rdi
movl %eax, %esi
movl %r14d, %ecx
callq 0x53ce
testb %al, %al
je 0x7365
movl $0x186a0, %edi          # imm = 0x186A0
callq 0x33f0
callq 0x39ee
movq %rax, %r14
movq %rbx, %rdi
callq 0x6244
leaq 0x8(%rsp), %rdx
leaq 0xc(%rsp), %rcx
movq %r14, %rdi
movl %eax, %esi
callq 0x8bd6
movq 0x48(%rbx), %rdi
callq 0x4f82
movl 0x3c(%rsp), %ebp
testl %ebp, %ebp
je 0x7282
movq %rbx, %rdi
callq 0x6244
cmpl %eax, %ebp
jne 0x7365
leaq 0x20(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x2202(%rip), %rsi      # 0x9421
leaq 0x2208(%rip), %rdx      # 0x942e
leaq 0x10(%rsp), %rdi
callq 0x404c
leaq 0x78(%rsp), %rdi
movq %rbx, %rsi
callq 0x6248
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
leaq 0x78(%rsp), %rdi
callq 0x30f0
leaq 0xa8(%rsp), %r12
movq %r12, -0x10(%r12)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x728a
movq %rdx, 0x98(%rsp)
movq (%rcx), %rdx
movq %rdx, 0xa8(%rsp)
jmp 0x7292
movb $0x1, %bpl
jmp 0x7367
movups (%rcx), %xmm0
movups %xmm0, (%r12)
movq 0x8(%rax), %rdx
leaq 0x98(%rsp), %r14
movq %rdx, 0x8(%r14)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0x33b0
movq (%r14), %rdi
cmpq %r12, %rdi
je 0x72d6
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x32f0
leaq 0x88(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x72f7
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x32f0
movl $0x3, %edi
callq 0x3230
callq 0x39ee
movq %rax, %r14
leaq 0x68(%rsp), %r12
movq %r12, -0x10(%r12)
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
addq %rsi, %rdx
leaq 0x58(%rsp), %rdi
callq 0x3fb6
leaq 0x58(%rsp), %rsi
movq %r14, %rdi
callq 0x82b0
movq 0x58(%rsp), %rdi
cmpq %r12, %rdi
je 0x734e
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x32f0
movq 0x10(%rsp), %rdi
cmpq %r15, %rdi
je 0x7365
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x32f0
xorl %ebp, %ebp
addq $0x50, %rbx
movq %rbx, %rdi
callq 0x7580
movl %ebp, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x58(%rsp), %rdi
cmpq %r12, %rdi
je 0x73c6
movq 0x68(%rsp), %rsi
jmp 0x73b7
movq %rax, %rbx
leaq 0x88(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x73c6
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x32f0
jmp 0x73c6
jmp 0x73c3
movq %rax, %rbx
movq 0x10(%rsp), %rdi
cmpq %r15, %rdi
je 0x73dd
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x32f0
movq %rbx, %rdi
callq 0x34b0