// File: /workspace/github_cpp_makefile_star2_O2/nesro[P]NCursesGomoku/src/ai.cpp
// Function: CAI::nextMove()
void CAI::nextMove(void) {

	/* nothing to do here */
	if (m_IsWinner == true) {
		return;
	}

	int x = 0;
	int y = 0;
	int direction = 0;

	char message[CBoard::MESSAGE_LENGTH];

	for (int i = 0; i < STRATEGIES; i++) {

		memset(message, 0, CBoard::MESSAGE_LENGTH);
		sprintf(message, " AI (%c) think about: %s", m_Mark, m_Patterns[i]);
		m_Board->printMessage(message);

		usleep(100000);

		if (m_Board->searchFor(x, y, direction, m_Patterns[i])) {

			/* AI can make mistake*/
			if ((rand() % (42 - i)) == 0) {
				memset(message, 0, CBoard::MESSAGE_LENGTH);
				sprintf(message, " AI (%c): Oh no!", m_Mark);
				m_Board->printMessage(message);
				usleep(1500000);

				memset(message, 0, CBoard::MESSAGE_LENGTH);
				sprintf(message, " AI (%c): I made a mistake! :(", m_Mark);
				m_Board->printMessage(message);
				usleep(1500000);

				continue;
			}

			for (int j = 0; j < m_Moves[i]; j++) {
				x += CBoard::DIRECTIONS[direction][0];
				y += CBoard::DIRECTIONS[direction][1];

			}

			if (m_Board->addStone(m_Mark, x, y)) {
				return;
			}
		}
	}

	memset(message, 0, CBoard::MESSAGE_LENGTH);
	sprintf(message, " AI (%c): I go random.", m_Mark);
	m_Board->printMessage(message);
	usleep(1000000);

	goRandom();
}

// Assembly:
cmpb $0x0, 0xa(%rdi)
jne 0x2a49
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %rbx
leaq 0x4(%rsp), %rax
andl $0x0, (%rax)
leaq 0x8(%rsp), %r15
andl $0x0, (%r15)
leaq 0xc(%rsp), %r12
andl $0x0, (%r12)
leaq 0x10(%rsp), %rbp
leaq 0x2b38(%rip), %r14      # 0x5400
xorl %r13d, %r13d
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movaps %xmm0, 0x10(%rsp)
movsbl 0x9(%rbx), %edx
cmpq $0x15, %r13
je 0x2a07
movq 0x18(%rbx), %rax
movq (%rax,%r13,8), %rcx
movq %rbp, %rdi
leaq 0x2a75(%rip), %rsi      # 0x536d
xorl %eax, %eax
callq 0x2060
movq 0x10(%rbx), %rdi
movq %rbp, %rsi
callq 0x30f4
movl $0x186a0, %edi          # imm = 0x186A0
callq 0x22b0
movq 0x10(%rbx), %rdi
movq 0x18(%rbx), %rax
movq (%rax,%r13,8), %r8
leaq 0x4(%rsp), %rsi
movq %r15, %rdx
movq %r12, %rcx
callq 0x2eae
testb %al, %al
je 0x29ff
callq 0x2090
pushq $0x2a
popq %rcx
subl %r13d, %ecx
cltd
idivl %ecx
testl %edx, %edx
je 0x298f
movq 0x20(%rbx), %rax
movslq 0xc(%rsp), %rcx
xorl %edx, %edx
cmpl (%rax,%r13,4), %edx
jge 0x2971
movl (%r14,%rcx,8), %esi
movl 0x4(%r14,%rcx,8), %edi
addl %esi, 0x4(%rsp)
addl %edi, 0x8(%rsp)
incl %edx
jmp 0x2956
movq 0x10(%rbx), %rdi
movsbl 0x9(%rbx), %esi
movl 0x4(%rsp), %edx
movl 0x8(%rsp), %ecx
callq 0x2dd4
testb %al, %al
je 0x29ff
jmp 0x2a3b
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movaps %xmm0, 0x10(%rsp)
movsbl 0x9(%rbx), %edx
movq %rbp, %rdi
leaq 0x29dc(%rip), %rsi      # 0x5386
xorl %eax, %eax
callq 0x2060
movq 0x10(%rbx), %rdi
movq %rbp, %rsi
callq 0x30f4
movl $0x16e360, %edi         # imm = 0x16E360
callq 0x22b0
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movaps %xmm0, 0x20(%rsp)
movsbl 0x9(%rbx), %edx
movq %rbp, %rdi
leaq 0x29b5(%rip), %rsi      # 0x5397
xorl %eax, %eax
callq 0x2060
movq 0x10(%rbx), %rdi
movq %rbp, %rsi
callq 0x30f4
movl $0x16e360, %edi         # imm = 0x16E360
callq 0x22b0
incq %r13
jmp 0x28cb
leaq 0x29a8(%rip), %rsi      # 0x53b6
leaq 0x10(%rsp), %r14
movq %r14, %rdi
xorl %eax, %eax
callq 0x2060
movq 0x10(%rbx), %rdi
movq %r14, %rsi
callq 0x30f4
movl $0xf4240, %edi          # imm = 0xF4240
callq 0x22b0
movq %rbx, %rdi
callq 0x4022
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop