// File: /workspace/github_cpp_makefile_star2_O1/tyhsu[P]Ewn/./minimax1.h
// Function: Minimax1::minimax(Game&, int)
int minimax(Game& cur_game, int height) {
		// check if end;
		if (height == 0) {
			return evaluate_simulation(cur_game);
		}
		int best_val;
		int chs_val_list[6];
		int lookahead_cnt = 0;

		bool is_ai_side;
		int cur_win;
		char cur_symbol;
		if (cur_game.get_is_switch() == this->ai_side) {
			// ai's cur_side		=> the largest, the better
			is_ai_side = true;
			best_val = -1e9;
			cur_win = this->ai_side ? 2 : 1;
			cur_symbol = this->ai_symbol;
			for (int i=0; i<6; i++)
				chs_val_list[i] = -1e9;
		}
		else {
			// opponent's cur_side	=> the smallest, the better
			is_ai_side = false;
			best_val = 1e9;
			cur_win = this->ai_side ? 1 : 2;
			cur_symbol = this->ai_side? '1' : 'A';
			for (int i=0; i<6; i++)
				chs_val_list[i] = 1e9;
		}

		// ai's cur_side, find the max
		// opponent's cur_side, find the min
		for (int dice = 0; dice < 6; dice++) {
			Chess cur_chs = cur_game.get_cur_chs_list(dice);
			if (cur_chs.exist) {
				int chs_index = cur_chs.symbol - cur_symbol;
				for (int direct = 0; direct < 3; direct++) {
					Movement mvmt(chs_index, direct);
					if (cur_game.check_in_board(mvmt)) {
						Game child_game = cur_game;
						int game_status = child_game.update_game_status(mvmt);
						// check if the game ends
						if (game_status == cur_win) return evaluate_feature(child_game);
						else if (game_status != 0) continue;
						child_game.switch_player();

						// return child value.
						int child_val = minimax(child_game, height-1);
						if (is_ai_side) {
							best_val = max(best_val, child_val);
						}
						else {
							best_val = min(best_val, child_val);
						}
					}
					else {
					}
				}
				chs_val_list[dice] = best_val;

				// if there is lookahead_cnt, compare those values of the non-existing with the existing
				// check if the current value is bigger than the previous one.
				if (lookahead_cnt) {
					int offset;
					if ((is_ai_side && best_val>chs_val_list[dice-1]) || (!is_ai_side && best_val<chs_val_list[dice-1])) {
						for (int j = 1; j <= lookahead_cnt; j++) {
							offset = dice - j;
							chs_val_list[offset] = best_val;
						}
					}
					lookahead_cnt = 0;
				}
			}
			else {
				// this chess can't move, use other's value
				lookahead_cnt++;
				if (dice - lookahead_cnt < 0) {
					// there's no chess exists before the selected one
					continue;
				}
				else {
					chs_val_list[dice] = chs_val_list[dice - lookahead_cnt];
				}
			}
		}

		// return the sum of the best values in the chs_val_list (chance node)
		best_val = 0;
		for(int i = 0; i < 6; i++)
			best_val += chs_val_list[i];

		return best_val;
	}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x148, %rsp            # imm = 0x148
movq %rsi, %rbx
movq %rdi, %r14
movl %edx, -0x38(%rbp)
testl %edx, %edx
je 0x553e
movq %rbx, %rdi
callq 0x872e
movb 0x10(%r14), %dl
movb %al, -0x2a(%rbp)
cmpb %al, %dl
movq %r14, -0x60(%rbp)
movb %dl, -0x29(%rbp)
jne 0x555e
movb 0x11(%r14), %al
xorl %ecx, %ecx
movl $0xc4653600, -0x80(%rbp,%rcx,4) # imm = 0xC4653600
incq %rcx
cmpq $0x6, %rcx
jne 0x551d
movzbl %dl, %ecx
incl %ecx
movl %ecx, -0x3c(%rbp)
movl $0xc4653600, %r15d      # imm = 0xC4653600
jmp 0x558d
movq (%r14), %rax
movq 0x8(%rax), %rax
movq %r14, %rdi
movq %rbx, %rsi
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmpq *%rax
movzbl %dl, %eax
movl $0x2, %ecx
subl %eax, %ecx
movl %ecx, -0x3c(%rbp)
xorl %eax, %eax
movl $0x3b9aca00, -0x80(%rbp,%rax,4) # imm = 0x3B9ACA00
incq %rax
cmpq $0x6, %rax
jne 0x556d
movl %edx, %eax
xorb $0x1, %al
shlb $0x4, %al
addb $0x31, %al
movl $0x3b9aca00, %r15d      # imm = 0x3B9ACA00
leaq -0x30(%rbp), %r12
movl $0x0, (%r12)
movsbl %al, %eax
movl %eax, -0x40(%rbp)
decl -0x38(%rbp)
xorl %r14d, %r14d
movq %rbx, %rdi
movq %r12, %rsi
callq 0x86e0
btl $0x8, %eax
jb 0x55d9
incl %r14d
movslq -0x30(%rbp), %rax
movl %eax, %ecx
subl %r14d, %ecx
js 0x56ed
movl %ecx, %ecx
movl -0x80(%rbp,%rcx,4), %ecx
movl %ecx, -0x80(%rbp,%rax,4)
xorl %ecx, %ecx
jmp 0x572f
movq %r14, -0x58(%rbp)
movl %r15d, -0x34(%rbp)
movsbl %al, %r13d
subl -0x40(%rbp), %r13d
xorl %r12d, %r12d
leaq -0x4c(%rbp), %r15
movl %r13d, -0x4c(%rbp)
movl %r12d, -0x48(%rbp)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x8118
testb %al, %al
je 0x5642
movq %r15, %r14
leaq -0x168(%rbp), %r15
movq %r15, %rdi
movq %rbx, %rsi
callq 0x7a74
movq %r15, %rdi
movq %r14, %rsi
callq 0x818e
cmpl -0x3c(%rbp), %eax
jne 0x5646
movq -0x60(%rbp), %rdi
movq %r15, %rsi
callq 0x6e8c
movl %eax, -0x44(%rbp)
movl $0x1, %ecx
jmp 0x5681
xorl %ecx, %ecx
jmp 0x5685
movl $0xd, %ecx
testl %eax, %eax
jne 0x5681
movq %r15, %rdi
callq 0x8316
movq -0x60(%rbp), %rdi
movq (%rdi), %rax
movq %r15, %rsi
movl -0x38(%rbp), %edx
callq *(%rax)
movl -0x34(%rbp), %edx
cmpl %eax, %edx
movl %edx, %ecx
cmovll %eax, %ecx
cmovgl %eax, %edx
movb -0x29(%rbp), %al
cmpb -0x2a(%rbp), %al
cmovel %ecx, %edx
movl %edx, -0x34(%rbp)
xorl %ecx, %ecx
leaq -0x4c(%rbp), %r15
cmpl $0xd, %ecx
je 0x568e
testl %ecx, %ecx
jne 0x56a0
incl %r12d
cmpl $0x3, %r12d
jne 0x55f0
movl $0xb, %ecx
cmpl $0xb, %ecx
jne 0x56df
movslq -0x30(%rbp), %rax
movl -0x34(%rbp), %r15d
movl %r15d, -0x80(%rbp,%rax,4)
xorl %ecx, %ecx
movq -0x58(%rbp), %rdi
testl %edi, %edi
leaq -0x30(%rbp), %r12
je 0x572c
movl -0x84(%rbp,%rax,4), %edx
movb -0x29(%rbp), %sil
cmpb -0x2a(%rbp), %sil
jne 0x56f4
testl %edi, %edi
setle %sil
cmpl %edx, %r15d
setle %dl
jmp 0x5700
leaq -0x30(%rbp), %r12
movl -0x34(%rbp), %r15d
movq -0x58(%rbp), %r14
jmp 0x572f
movl $0xa, %ecx
jmp 0x572f
testl %edi, %edi
setle %sil
cmpl %edx, %r15d
setge %dl
orb %sil, %dl
jne 0x572c
cmpl $0x2, %edi
movl $0x1, %edx
cmovll %edx, %edi
leaq -0x84(%rbp), %rdx
leaq (%rdx,%rax,4), %rax
negq %rdi
xorl %edx, %edx
movl %r15d, (%rax,%rdx,4)
decq %rdx
cmpq %rdx, %rdi
jne 0x5720
xorl %r14d, %r14d
cmpl $0xa, %ecx
je 0x5738
testl %ecx, %ecx
jne 0x574f
movl -0x30(%rbp), %eax
leal 0x1(%rax), %ecx
movl %ecx, -0x30(%rbp)
cmpl $0x5, %eax
jl 0x55a5
movl $0x8, %ecx
cmpl $0x8, %ecx
movl -0x44(%rbp), %eax
jne 0x5768
xorl %ecx, %ecx
xorl %eax, %eax
addl -0x80(%rbp,%rcx,4), %eax
incq %rcx
cmpq $0x6, %rcx
jne 0x575b
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq