// File: tewalds[P]morat/hex/agentmctsthread.cpp
// Function: Morat::Hex::AgentMCTS::AgentThread::test_bridge_probe(Morat::Hex::Board const&, Morat::Move const&, Morat::Move const&) const
bool AgentMCTS::AgentThread::test_bridge_probe(const Board & board, const Move & move, const Move & test) const {
	//TODO: switch to the same method as policy_bridge.h, maybe even share code
	if(board.dist(move, test) != 1)
		return false;

	bool equals = false;

	int state = 0;
	Side piece = ~board.get(move);
	const MoveValid* neighbors = board.neighbors(move);
	for(int i = 0; i < 8; i++){
		MoveValid cur = neighbors[i % 6];

		bool on = board.on_board(cur);
		Side v = Side::NONE;
		if(on)
			v = board.get(cur);

	//state machine that progresses when it see the pattern, but counting borders as part of the pattern
		if(state == 0){
			if(!on || v == piece)
				state = 1;
			//else state = 0;
		}else if(state == 1){
			if(on){
				if(v == Side::NONE){
					state = 2;
					equals = (test == cur);
				}else if(v != piece)
					state = 0;
				//else (v==piece) => state = 1;
			}
			//else state = 1;
		}else{ // state == 2
			if(!on || v == piece){
				if(equals)
					return true;
				state = 1;
			}else{
				state = 0;
			}
		}
	}
	return false;
}

// Assembly:
subq $0x58, %rsp
movq %rdi, 0x48(%rsp)
movq %rsi, 0x40(%rsp)
movq %rdx, 0x38(%rsp)
movq %rcx, 0x30(%rsp)
movq 0x40(%rsp), %rdi
movq 0x38(%rsp), %rsi
movq 0x30(%rsp), %rdx
callq 0x298a0
cmpl $0x1, %eax
je 0x289bb
movb $0x0, 0x57(%rsp)
jmp 0x28b65
movb $0x0, 0x2f(%rsp)
movl $0x0, 0x28(%rsp)
movq 0x40(%rsp), %rdi
movq 0x38(%rsp), %rsi
callq 0x29b90
movb %al, 0x26(%rsp)
movzbl 0x26(%rsp), %edi
callq 0x1c680
movb %al, 0x27(%rsp)
movq 0x40(%rsp), %rdi
movq 0x38(%rsp), %rsi
callq 0x29bd0
movq %rax, 0x18(%rsp)
movl $0x0, 0x14(%rsp)
cmpl $0x8, 0x14(%rsp)
jge 0x28b60
movq 0x18(%rsp), %rax
movq %rax, (%rsp)
movl 0x14(%rsp), %eax
movl $0x6, %ecx
cltd
idivl %ecx
movq (%rsp), %rax
movslq %edx, %rcx
movl (%rax,%rcx,4), %eax
movl %eax, 0x10(%rsp)
movq 0x40(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x29c10
andb $0x1, %al
movb %al, 0xf(%rsp)
leaq 0x44a6c(%rip), %rax     # 0x6d4bb
movb (%rax), %al
movb %al, 0xe(%rsp)
testb $0x1, 0xf(%rsp)
je 0x28a77
movq 0x40(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x29c30
movb %al, 0xd(%rsp)
movb 0xd(%rsp), %al
movb %al, 0xe(%rsp)
cmpl $0x0, 0x28(%rsp)
jne 0x28aa7
testb $0x1, 0xf(%rsp)
je 0x28a9a
leaq 0xe(%rsp), %rdi
leaq 0x27(%rsp), %rsi
callq 0x1f930
testb $0x1, %al
jne 0x28a9a
jmp 0x28aa2
movl $0x1, 0x28(%rsp)
jmp 0x28b4e
cmpl $0x1, 0x28(%rsp)
jne 0x28b0e
testb $0x1, 0xf(%rsp)
je 0x28b0c
leaq 0xe(%rsp), %rdi
leaq 0x449fa(%rip), %rsi     # 0x6d4bb
callq 0x1f930
testb $0x1, %al
jne 0x28acc
jmp 0x28aeb
movl $0x2, 0x28(%rsp)
movq 0x30(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x1c3e0
andb $0x1, %al
movb %al, 0x2f(%rsp)
jmp 0x28b0a
leaq 0xe(%rsp), %rdi
leaq 0x27(%rsp), %rsi
callq 0x29c60
testb $0x1, %al
jne 0x28b00
jmp 0x28b08
movl $0x0, 0x28(%rsp)
jmp 0x28b0a
jmp 0x28b0c
jmp 0x28b4c
testb $0x1, 0xf(%rsp)
je 0x28b2a
leaq 0xe(%rsp), %rdi
leaq 0x27(%rsp), %rsi
callq 0x1f930
testb $0x1, %al
jne 0x28b2a
jmp 0x28b42
testb $0x1, 0x2f(%rsp)
je 0x28b38
movb $0x1, 0x57(%rsp)
jmp 0x28b65
movl $0x1, 0x28(%rsp)
jmp 0x28b4a
movl $0x0, 0x28(%rsp)
jmp 0x28b4c
jmp 0x28b4e
jmp 0x28b50
movl 0x14(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14(%rsp)
jmp 0x28a05
movb $0x0, 0x57(%rsp)
movb 0x57(%rsp), %al
andb $0x1, %al
addq $0x58, %rsp
retq