// File: /workspace/github_cpp_makefile_star2_O2/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, %r14
leaq 0x50(%rdi), %rax
movq %rax, 0x8(%rsp)
pushq $0x1
popq %rbx
movq 0x48(%r14), %rdi
callq 0x4778
decq %rax
cmpq %rax, %rbx
jae 0x603a
leaq 0x1(%rbx), %rcx
imulq $0x320, %rbx, %r12      # imm = 0x320
movq %rbx, 0x18(%rsp)
decq %rbx
movq %rcx, 0x48(%rsp)
imulq $0x320, %rcx, %rbp      # imm = 0x320
movq %rbx, 0x50(%rsp)
imulq $0x320, %rbx, %rbx      # imm = 0x320
pushq $0x1
popq %r13
movq %r13, %r15
movq 0x48(%r14), %rdi
callq 0x4778
decq %rax
cmpq %rax, %r13
jae 0x6030
movq 0x48(%r14), %rax
addq %r12, %rax
movq 0x18(%rax,%r15,8), %rdi
callq 0x3868
testl %eax, %eax
je 0x6025
movq 0x48(%r14), %rax
addq %rbp, %rax
movq 0x18(%rax,%r15,8), %rdi
callq 0x3868
testl %eax, %eax
jne 0x5f7f
movq 0x48(%rsp), %rax
movl %eax, 0x20(%rsp)
movl %r15d, 0x24(%rsp)
movq 0x8(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x62f0
movq 0x48(%r14), %rax
addq %r12, %rax
leaq 0x1(%r15), %r13
movq 0x20(%rax,%r15,8), %rdi
callq 0x3868
testl %eax, %eax
jne 0x5fb5
movq 0x18(%rsp), %rax
movl %eax, 0x20(%rsp)
movl %r13d, 0x24(%rsp)
movq 0x8(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x62f0
movq 0x48(%r14), %rax
addq %rbx, %rax
movq 0x18(%rax,%r15,8), %rdi
callq 0x3868
testl %eax, %eax
jne 0x5fe7
movq 0x50(%rsp), %rax
movl %eax, 0x20(%rsp)
movl %r15d, 0x24(%rsp)
movq 0x8(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x62f0
movq 0x48(%r14), %rax
addq %r12, %rax
movq 0x10(%rax,%r15,8), %rdi
callq 0x3868
testl %eax, %eax
jne 0x5f1c
decq %r15
movq 0x18(%rsp), %rax
movl %eax, 0x20(%rsp)
movl %r15d, 0x24(%rsp)
movq 0x8(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x62f0
jmp 0x5f1c
incq %r15
movq %r15, %r13
jmp 0x5f1c
movq 0x48(%rsp), %rbx
jmp 0x5ed8
movq 0x78(%r14), %rbx
xorl %edi, %edi
callq 0x3290
movl %eax, %edi
callq 0x32b0
callq 0x30a0
testq %rbx, %rbx
je 0x60f2
cltq
xorl %edx, %edx
divq 0x78(%r14)
movq %rdx, %r15
movq 0x68(%r14), %r12
movq %r12, %rax
subq $0x1, %r15
jb 0x607c
movq %rax, %rdi
callq 0x3370
jmp 0x606c
movl 0x20(%rax), %ecx
movl 0x24(%rax), %ebp
movl %ebp, 0x14(%rsp)
movq %rcx, 0x18(%rsp)
leal 0x3(%rcx), %eax
movl %eax, 0x10(%rsp)
leaq 0x58(%r14), %rbx
leaq 0x2382(%rip), %r13      # 0x841f
leaq 0x246a(%rip), %r15      # 0x850e
cmpq %rbx, %r12
je 0x60eb
callq 0x3c5a
movl 0x20(%r12), %esi
movq %rax, %rdi
callq 0x45c0
movq %rax, %rdi
movq %r13, %rsi
callq 0x4576
movl 0x24(%r12), %esi
movq %rax, %rdi
callq 0x45c0
movq %rax, %rdi
movq %r15, %rsi
callq 0x4576
movq %r12, %rdi
callq 0x3370
movq %rax, %r12
jmp 0x60a4
movq 0x18(%rsp), %r12
jmp 0x6118
pushq $0xa
popq %rbx
cltd
idivl %ebx
movl %edx, %r15d
leal 0x1(%r15), %r12d
callq 0x30a0
cltd
idivl %ebx
movl %edx, %ebp
incl %ebp
movl %ebp, 0x14(%rsp)
addl $0x4, %r15d
movl %r15d, 0x10(%rsp)
movq 0x48(%r14), %r15
movq %r14, %rdi
callq 0x557c
leaq 0x44(%rsp), %r8
movq %r15, %rdi
movl %eax, %esi
movl %ebp, %edx
movl %r12d, %ecx
callq 0x4a92
testb %al, %al
je 0x6237
movl $0x186a0, %edi          # imm = 0x186A0
callq 0x3410
callq 0x381e
movq %rax, %r15
movq %r14, %rdi
callq 0x557c
leaq 0x14(%rsp), %rdx
leaq 0x10(%rsp), %rcx
movq %r15, %rdi
movl %eax, %esi
callq 0x76be
movq 0x48(%r14), %rdi
callq 0x477c
movl 0x44(%rsp), %ebx
testl %ebx, %ebx
je 0x6257
movq %r14, %rdi
callq 0x557c
cmpl %eax, %ebx
jne 0x6237
leaq 0x2287(%rip), %rsi      # 0x8421
leaq 0x20(%rsp), %rdi
leaq 0x98(%rsp), %rdx
callq 0x3c1e
leaq 0x78(%rsp), %rdi
movq %r14, %rsi
callq 0x5580
leaq 0x98(%rsp), %rdi
leaq 0x78(%rsp), %rsi
leaq 0x20(%rsp), %rdx
callq 0x448e
leaq 0x20(%rsp), %rdi
leaq 0x98(%rsp), %r14
movq %r14, %rsi
callq 0x33b0
movq %r14, %rdi
callq 0x3170
leaq 0x78(%rsp), %rdi
callq 0x3170
pushq $0x3
popq %rdi
callq 0x3220
callq 0x381e
movq %rax, %r14
leaq 0x58(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x33f0
leaq 0x58(%rsp), %rsi
movq %r14, %rdi
callq 0x7060
leaq 0x58(%rsp), %rdi
callq 0x3170
leaq 0x20(%rsp), %rdi
callq 0x3170
xorl %ebp, %ebp
movq 0x8(%rsp), %rdi
callq 0x649a
movl %ebp, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb $0x1, %bpl
jmp 0x6239
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
jmp 0x626e
movq %rax, %rbx
leaq 0x78(%rsp), %rdi
callq 0x3170
jmp 0x627a
jmp 0x6277
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x3170
movq %rbx, %rdi
callq 0x34d0