// File: /workspace/github_cpp_makefile_star2_O2/vygr[P]C-Chess/chess.cpp
// Function: main
int main(int, const char *[])
{
	//setup first board, loop for white..black..white..black...
	auto game_start_time = std::chrono::high_resolution_clock::now();
	auto brd = board("rnbqkbnrpppppppp                                PPPPPPPPRNBQKBNR");
	//auto brd = board("rnb kbnrpppppppp                                PPPPPPPPRNBQKBNR");
	//auto brd = board(" k                         Q P     Q P  K                       ");
	//auto brd = board(" k                           P     Q P  K                       ");
	//auto brd = board("        p         k    p   rb         p      r              K   ");
	//auto brd = board("        p         k    p   r          p      r              K   ");
	//auto brd = board("                   k               K         Q                  ");
	//auto brd = board("    k     R                               K                      ");
	//auto brd = board("   k              KBB                                            ");
	auto history = boards{};
	auto colour = white;
	display_board(brd);
	for (;;)
	{
		auto end_time = std::chrono::high_resolution_clock::now();
		std::chrono::duration<double> elapsed = end_time - game_start_time;
		std::cout << "\nElapsed Time: " << elapsed.count() << "\n";
		if (colour == white)
		{
			std::cout << "White to move:\n";
		}
		else
		{
			std::cout << "Black to move:\n";
		}
		auto new_brd = best_move(brd, colour, history);
		if (new_brd == "")
		{
			std::size_t king_index = 0;
			if (in_check(brd, colour, king_index))
			{
				std::cout << "\n** Checkmate **\n";
			}
			else
			{
				std::cout << "\n** Stalemate **\n";
			}
			break;
		}
		auto rep = std::count(begin(history), end(history), brd);
		if (rep >= 3)
		{
			std::cout << "\n** Draw **\n";
			break;
		}
		history.push_back(new_brd);
		for (auto i = 0; i < 3; ++i)
		{
			display_board(brd);
			std::this_thread::sleep_for(std::chrono::duration<float>(0.1));
			display_board(new_brd);
			std::this_thread::sleep_for(std::chrono::duration<float>(0.1));
		}
		colour = -colour;
		brd = new_brd;
	}
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
callq 0x2040
movq %rax, 0x30(%rsp)
leaq 0x4d2c(%rip), %rsi      # 0x8c4a
leaq 0x38(%rsp), %rbx
leaq 0x58(%rsp), %rdx
movq %rbx, %rdi
callq 0x20c0
xorpd %xmm0, %xmm0
movapd %xmm0, 0x10(%rsp)
andq $0x0, 0x20(%rsp)
movq %rbx, %rdi
callq 0x2f4f
pushq $0x1
popq %rbp
movq 0xa086(%rip), %r14      # 0xdfd8
leaq 0x58(%rsp), %r13
leaq 0x38(%rsp), %r15
leaq 0x8(%rsp), %r12
callq 0x2040
movq %rax, %rbx
movq %r14, %rdi
leaq 0x4d18(%rip), %rsi      # 0x8c8b
callq 0x21c0
subq 0x30(%rsp), %rbx
cvtsi2sd %rbx, %xmm0
divsd 0x4086(%rip), %xmm0     # 0x8010
movq %rax, %rdi
callq 0x2290
movq %rax, %rdi
leaq 0x4d41(%rip), %rsi      # 0x8cdd
callq 0x21c0
cmpl $0x1, %ebp
leaq 0x4d00(%rip), %rsi      # 0x8cab
leaq 0x4ce9(%rip), %rax      # 0x8c9b
cmoveq %rax, %rsi
movq %r14, %rdi
callq 0x21c0
movq %r13, %rdi
movq %r15, %rsi
movl %ebp, %edx
leaq 0x10(%rsp), %rcx
callq 0x3bf6
movq %r13, %rdi
leaq 0x4d04(%rip), %rsi      # 0x8cde
callq 0x502c
testb %al, %al
je 0x4014
andq $0x0, 0x8(%rsp)
movq %r15, %rdi
movl %ebp, %esi
movq %r12, %rdx
callq 0x313a
testb %al, %al
leaq 0x4cce(%rip), %rsi      # 0x8ccd
leaq 0x4cb5(%rip), %rax      # 0x8cbb
cmovneq %rax, %rsi
movq %r14, %rdi
callq 0x21c0
jmp 0x403b
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq %r15, %rdx
callq 0x7813
cmpq $0x3, %rax
jl 0x403f
movq %r14, %rdi
leaq 0x4ca9(%rip), %rsi      # 0x8cdf
callq 0x21c0
xorl %ebx, %ebx
jmp 0x4095
leaq 0x10(%rsp), %rdi
movq %r13, %rsi
callq 0x503a
pushq $0x3
popq %rbx
subl $0x1, %ebx
jb 0x4086
movq %r15, %rdi
callq 0x2f4f
movl $0x3dcccccd, 0x8(%rsp)  # imm = 0x3DCCCCCD
movq %r12, %rdi
callq 0x5066
movq %r13, %rdi
callq 0x2f4f
movl $0x3dcccccd, 0x8(%rsp)  # imm = 0x3DCCCCCD
movq %r12, %rdi
callq 0x5066
jmp 0x404f
movq %r15, %rdi
movq %r13, %rsi
callq 0x2110
negl %ebp
movb $0x1, %bl
movq %r13, %rdi
callq 0x20f0
testb %bl, %bl
jne 0x3f61
leaq 0x10(%rsp), %rdi
callq 0x50bc
leaq 0x38(%rsp), %rdi
callq 0x20f0
xorl %eax, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x40d2
jmp 0x40db
jmp 0x40d2
jmp 0x40db
movq %rax, %rbx
jmp 0x40e8
jmp 0x40db
jmp 0x40db
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x20f0
leaq 0x10(%rsp), %rdi
callq 0x50bc
leaq 0x38(%rsp), %rdi
callq 0x20f0
movq %rbx, %rdi
callq 0x22e0