// File: 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:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x50(%rbp)
movb 0xa(%rax), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x3d56
jmp 0x3f70
movl $0x0, -0xc(%rbp)
movl $0x0, -0x10(%rbp)
movl $0x0, -0x14(%rbp)
movl $0x0, -0x44(%rbp)
cmpl $0x15, -0x44(%rbp)
jge 0x3f22
leaq -0x40(%rbp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x3180
movq -0x50(%rbp), %rax
leaq -0x40(%rbp), %rdi
movsbl 0x9(%rax), %edx
movq 0x18(%rax), %rax
movslq -0x44(%rbp), %rcx
movq (%rax,%rcx,8), %rcx
leaq 0x45c2(%rip), %rsi      # 0x836d
movb $0x0, %al
callq 0x3080
movq -0x50(%rbp), %rax
movq 0x10(%rax), %rdi
leaq -0x40(%rbp), %rsi
callq 0x4d00
movl $0x186a0, %edi          # imm = 0x186A0
callq 0x3390
movq -0x50(%rbp), %rax
movq 0x10(%rax), %rdi
movq 0x18(%rax), %rax
movslq -0x44(%rbp), %rcx
movq (%rax,%rcx,8), %r8
leaq -0xc(%rbp), %rsi
leaq -0x10(%rbp), %rdx
leaq -0x14(%rbp), %rcx
callq 0x4770
testb $0x1, %al
jne 0x3dfb
jmp 0x3f12
callq 0x30b0
movl $0x2a, %ecx
subl -0x44(%rbp), %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0x3ea0
leaq -0x40(%rbp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x3180
movq -0x50(%rbp), %rax
leaq -0x40(%rbp), %rdi
movsbl 0x9(%rax), %edx
leaq 0x454f(%rip), %rsi      # 0x8386
movb $0x0, %al
callq 0x3080
movq -0x50(%rbp), %rax
movq 0x10(%rax), %rdi
leaq -0x40(%rbp), %rsi
callq 0x4d00
movl $0x16e360, %edi         # imm = 0x16E360
callq 0x3390
leaq -0x40(%rbp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x3180
movq -0x50(%rbp), %rax
leaq -0x40(%rbp), %rdi
movsbl 0x9(%rax), %edx
leaq 0x451b(%rip), %rsi      # 0x8397
movb $0x0, %al
callq 0x3080
movq -0x50(%rbp), %rax
movq 0x10(%rax), %rdi
leaq -0x40(%rbp), %rsi
callq 0x4d00
movl $0x16e360, %edi         # imm = 0x16E360
callq 0x3390
jmp 0x3f14
movl $0x0, -0x48(%rbp)
movq -0x50(%rbp), %rcx
movl -0x48(%rbp), %eax
movq 0x20(%rcx), %rcx
movslq -0x44(%rbp), %rdx
cmpl (%rcx,%rdx,4), %eax
jge 0x3eef
movslq -0x14(%rbp), %rcx
leaq 0x452a(%rip), %rax      # 0x83f0
movl (%rax,%rcx,8), %eax
addl -0xc(%rbp), %eax
movl %eax, -0xc(%rbp)
movslq -0x14(%rbp), %rcx
leaq 0x4516(%rip), %rax      # 0x83f0
movl 0x4(%rax,%rcx,8), %eax
addl -0x10(%rbp), %eax
movl %eax, -0x10(%rbp)
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x3ea7
movq -0x50(%rbp), %rax
movq 0x10(%rax), %rdi
movb 0x9(%rax), %al
movl -0xc(%rbp), %edx
movl -0x10(%rbp), %ecx
movsbl %al, %esi
callq 0x4590
testb $0x1, %al
jne 0x3f0e
jmp 0x3f10
jmp 0x3f70
jmp 0x3f12
jmp 0x3f14
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x3d72
leaq -0x40(%rbp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x3180
movq -0x50(%rbp), %rax
leaq -0x40(%rbp), %rdi
movsbl 0x9(%rax), %edx
leaq 0x4471(%rip), %rsi      # 0x83b6
movb $0x0, %al
callq 0x3080
movq -0x50(%rbp), %rax
movq 0x10(%rax), %rdi
leaq -0x40(%rbp), %rsi
callq 0x4d00
movl $0xf4240, %edi          # imm = 0xF4240
callq 0x3390
movq -0x50(%rbp), %rdi
callq 0x6970
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)