// File: nesro[P]NCursesGomoku/src/board.cpp
// Function: CBoard::searchFor(int&, int&, int&, char const*) const
bool CBoard::searchFor(int & x, int & y, int & direction,
		const char * pattern) const {

	int randomSearch = rand() % 4;

	/* choose one from four directions of searching */
	switch (randomSearch) {
	case 0:
		for (int i = 0; i < BOARD_WIDTH; i++) {
			for (int j = 0; j < BOARD_HEIGHT; j++) {
				for (int searchDirection = 0; searchDirection < 8;
						searchDirection++) {
					for (int l = 0, tmpX = i, tmpY = j;; l++) {
						if (pattern[l] == '\0') {
							x = i;
							y = j;
							direction = searchDirection;

							return true;
						} else if (!outOfBoard(tmpX, tmpY)
								&& m_Stones[tmpY][tmpX] == pattern[l]) {
							tmpX += DIRECTIONS[searchDirection][0];
							tmpY += DIRECTIONS[searchDirection][1];

							continue;
						} else {
							break;
						}
					}
				}
			}
		}
		break;
	case 1:
		for (int i = BOARD_WIDTH - 1; i >= 0; i--) {
			for (int j = 0; j < BOARD_HEIGHT; j++) {
				for (int searchDirection = 0; searchDirection < 8;
						searchDirection++) {
					for (int l = 0, tmpX = i, tmpY = j;; l++) {
						if (pattern[l] == '\0') {
							x = i;
							y = j;
							direction = searchDirection;

							return true;
						} else if (!outOfBoard(tmpX, tmpY)
								&& m_Stones[tmpY][tmpX] == pattern[l]) {
							tmpX += DIRECTIONS[searchDirection][0];
							tmpY += DIRECTIONS[searchDirection][1];

							continue;
						} else {
							break;
						}
					}
				}
			}
		}
		break;
	case 2:
		for (int i = 0; i < BOARD_WIDTH; i++) {
			for (int j = BOARD_HEIGHT - 1; j >= 0; j--) {
				for (int searchDirection = 0; searchDirection < 8;
						searchDirection++) {
					for (int l = 0, tmpX = i, tmpY = j;; l++) {
						if (pattern[l] == '\0') {
							x = i;
							y = j;
							direction = searchDirection;

							return true;
						} else if (!outOfBoard(tmpX, tmpY)
								&& m_Stones[tmpY][tmpX] == pattern[l]) {
							tmpX += DIRECTIONS[searchDirection][0];
							tmpY += DIRECTIONS[searchDirection][1];

							continue;
						} else {
							break;
						}
					}
				}
			}
		}
		break;
	case 3:
		for (int i = BOARD_WIDTH - 1; i >= 0; i--) {
			for (int j = BOARD_HEIGHT - 1; j >= 0; j--) {
				for (int searchDirection = 0; searchDirection < 8;
						searchDirection++) {
					for (int l = 0, tmpX = i, tmpY = j;; l++) {
						if (pattern[l] == '\0') {
							x = i;
							y = j;
							direction = searchDirection;

							return true;
						} else if (!outOfBoard(tmpX, tmpY)
								&& m_Stones[tmpY][tmpX] == pattern[l]) {
							tmpX += DIRECTIONS[searchDirection][0];
							tmpY += DIRECTIONS[searchDirection][1];

							continue;
						} else {
							break;
						}
					}
				}
			}
		}
		break;
	default:
		break;
	}

	return false;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xa8(%rbp)
callq 0x30b0
movl %eax, %edx
addl $0x3, %edx
testl %eax, %eax
movl %eax, %ecx
cmovsl %edx, %ecx
andl $-0x4, %ecx
subl %ecx, %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq %rax, -0xa0(%rbp)
subq $0x3, %rax
ja 0x4ceb
movq -0xa0(%rbp), %rax
leaq 0x3c0b(%rip), %rcx      # 0x83e0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x0, -0x38(%rbp)
cmpl $0xf, -0x38(%rbp)
jge 0x4908
movl $0x0, -0x3c(%rbp)
cmpl $0xf, -0x3c(%rbp)
jge 0x48f8
movl $0x0, -0x40(%rbp)
cmpl $0x8, -0x40(%rbp)
jge 0x48e8
movl $0x0, -0x44(%rbp)
movl -0x38(%rbp), %eax
movl %eax, -0x48(%rbp)
movl -0x3c(%rbp), %eax
movl %eax, -0x4c(%rbp)
movq -0x30(%rbp), %rax
movslq -0x44(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x0, %eax
jne 0x4859
movl -0x38(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movl -0x3c(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movl -0x40(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
movb $0x1, -0x1(%rbp)
jmp 0x4cf1
movq -0xa8(%rbp), %rdi
movl -0x48(%rbp), %esi
movl -0x4c(%rbp), %edx
callq 0x4720
testb $0x1, %al
jne 0x48c8
movq -0xa8(%rbp), %rax
addq $0x38, %rax
movslq -0x4c(%rbp), %rcx
imulq $0xf, %rcx, %rcx
addq %rcx, %rax
movslq -0x48(%rbp), %rcx
movsbl (%rax,%rcx), %eax
movq -0x30(%rbp), %rcx
movslq -0x44(%rbp), %rdx
movsbl (%rcx,%rdx), %ecx
cmpl %ecx, %eax
jne 0x48c8
movslq -0x40(%rbp), %rcx
leaq 0x3b48(%rip), %rax      # 0x83f0
movl (%rax,%rcx,8), %eax
addl -0x48(%rbp), %eax
movl %eax, -0x48(%rbp)
movslq -0x40(%rbp), %rcx
leaq 0x3b34(%rip), %rax      # 0x83f0
movl 0x4(%rax,%rcx,8), %eax
addl -0x4c(%rbp), %eax
movl %eax, -0x4c(%rbp)
jmp 0x48ca
jmp 0x48d8
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x4824
jmp 0x48da
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x4807
jmp 0x48ea
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x47f6
jmp 0x48fa
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x47e5
jmp 0x4ced
movl $0xe, -0x50(%rbp)
cmpl $0x0, -0x50(%rbp)
jl 0x4a37
movl $0x0, -0x54(%rbp)
cmpl $0xf, -0x54(%rbp)
jge 0x4a27
movl $0x0, -0x58(%rbp)
cmpl $0x8, -0x58(%rbp)
jge 0x4a17
movl $0x0, -0x5c(%rbp)
movl -0x50(%rbp), %eax
movl %eax, -0x60(%rbp)
movl -0x54(%rbp), %eax
movl %eax, -0x64(%rbp)
movq -0x30(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x0, %eax
jne 0x4988
movl -0x50(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movl -0x54(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movl -0x58(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
movb $0x1, -0x1(%rbp)
jmp 0x4cf1
movq -0xa8(%rbp), %rdi
movl -0x60(%rbp), %esi
movl -0x64(%rbp), %edx
callq 0x4720
testb $0x1, %al
jne 0x49f7
movq -0xa8(%rbp), %rax
addq $0x38, %rax
movslq -0x64(%rbp), %rcx
imulq $0xf, %rcx, %rcx
addq %rcx, %rax
movslq -0x60(%rbp), %rcx
movsbl (%rax,%rcx), %eax
movq -0x30(%rbp), %rcx
movslq -0x5c(%rbp), %rdx
movsbl (%rcx,%rdx), %ecx
cmpl %ecx, %eax
jne 0x49f7
movslq -0x58(%rbp), %rcx
leaq 0x3a19(%rip), %rax      # 0x83f0
movl (%rax,%rcx,8), %eax
addl -0x60(%rbp), %eax
movl %eax, -0x60(%rbp)
movslq -0x58(%rbp), %rcx
leaq 0x3a05(%rip), %rax      # 0x83f0
movl 0x4(%rax,%rcx,8), %eax
addl -0x64(%rbp), %eax
movl %eax, -0x64(%rbp)
jmp 0x49f9
jmp 0x4a07
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x4953
jmp 0x4a09
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x4936
jmp 0x4a19
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x4925
jmp 0x4a29
movl -0x50(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x4914
jmp 0x4ced
movl $0x0, -0x68(%rbp)
cmpl $0xf, -0x68(%rbp)
jge 0x4b66
movl $0xe, -0x6c(%rbp)
cmpl $0x0, -0x6c(%rbp)
jl 0x4b56
movl $0x0, -0x70(%rbp)
cmpl $0x8, -0x70(%rbp)
jge 0x4b46
movl $0x0, -0x74(%rbp)
movl -0x68(%rbp), %eax
movl %eax, -0x78(%rbp)
movl -0x6c(%rbp), %eax
movl %eax, -0x7c(%rbp)
movq -0x30(%rbp), %rax
movslq -0x74(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x0, %eax
jne 0x4ab7
movl -0x68(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movl -0x6c(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movl -0x70(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
movb $0x1, -0x1(%rbp)
jmp 0x4cf1
movq -0xa8(%rbp), %rdi
movl -0x78(%rbp), %esi
movl -0x7c(%rbp), %edx
callq 0x4720
testb $0x1, %al
jne 0x4b26
movq -0xa8(%rbp), %rax
addq $0x38, %rax
movslq -0x7c(%rbp), %rcx
imulq $0xf, %rcx, %rcx
addq %rcx, %rax
movslq -0x78(%rbp), %rcx
movsbl (%rax,%rcx), %eax
movq -0x30(%rbp), %rcx
movslq -0x74(%rbp), %rdx
movsbl (%rcx,%rdx), %ecx
cmpl %ecx, %eax
jne 0x4b26
movslq -0x70(%rbp), %rcx
leaq 0x38ea(%rip), %rax      # 0x83f0
movl (%rax,%rcx,8), %eax
addl -0x78(%rbp), %eax
movl %eax, -0x78(%rbp)
movslq -0x70(%rbp), %rcx
leaq 0x38d6(%rip), %rax      # 0x83f0
movl 0x4(%rax,%rcx,8), %eax
addl -0x7c(%rbp), %eax
movl %eax, -0x7c(%rbp)
jmp 0x4b28
jmp 0x4b36
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x4a82
jmp 0x4b38
movl -0x70(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x70(%rbp)
jmp 0x4a65
jmp 0x4b48
movl -0x6c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x4a54
jmp 0x4b58
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
jmp 0x4a43
jmp 0x4ced
movl $0xe, -0x80(%rbp)
cmpl $0x0, -0x80(%rbp)
jl 0x4ce9
movl $0xe, -0x84(%rbp)
cmpl $0x0, -0x84(%rbp)
jl 0x4cd9
movl $0x0, -0x88(%rbp)
cmpl $0x8, -0x88(%rbp)
jge 0x4cc3
movl $0x0, -0x8c(%rbp)
movl -0x80(%rbp), %eax
movl %eax, -0x90(%rbp)
movl -0x84(%rbp), %eax
movl %eax, -0x94(%rbp)
movq -0x30(%rbp), %rax
movslq -0x8c(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x0, %eax
jne 0x4c07
movl -0x80(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movl -0x84(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movl -0x88(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
movb $0x1, -0x1(%rbp)
jmp 0x4cf1
movq -0xa8(%rbp), %rdi
movl -0x90(%rbp), %esi
movl -0x94(%rbp), %edx
callq 0x4720
testb $0x1, %al
jne 0x4c97
movq -0xa8(%rbp), %rax
addq $0x38, %rax
movslq -0x94(%rbp), %rcx
imulq $0xf, %rcx, %rcx
addq %rcx, %rax
movslq -0x90(%rbp), %rcx
movsbl (%rax,%rcx), %eax
movq -0x30(%rbp), %rcx
movslq -0x8c(%rbp), %rdx
movsbl (%rcx,%rdx), %ecx
cmpl %ecx, %eax
jne 0x4c97
movslq -0x88(%rbp), %rcx
leaq 0x3788(%rip), %rax      # 0x83f0
movl (%rax,%rcx,8), %eax
addl -0x90(%rbp), %eax
movl %eax, -0x90(%rbp)
movslq -0x88(%rbp), %rcx
leaq 0x376b(%rip), %rax      # 0x83f0
movl 0x4(%rax,%rcx,8), %eax
addl -0x94(%rbp), %eax
movl %eax, -0x94(%rbp)
jmp 0x4c99
jmp 0x4cad
movl -0x8c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8c(%rbp)
jmp 0x4bc9
jmp 0x4caf
movl -0x88(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x88(%rbp)
jmp 0x4b9d
jmp 0x4cc5
movl -0x84(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x84(%rbp)
jmp 0x4b86
jmp 0x4cdb
movl -0x80(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x80(%rbp)
jmp 0x4b72
jmp 0x4ced
jmp 0x4ced
movb $0x0, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0xb0, %rsp
popq %rbp
retq
nop