// File: /workspace/github_cpp_makefile_star2_O3/tyhsu[P]Ewn/minimax.cpp
// Function: Minimax::AI_move(Game&, int)
Movement Minimax::AI_move(Game& cur_game, int dice)
{
    //cerr << "calculating..." ;
    //cerr.flush();
	clock_t init_time = clock();
	this->ai_side = cur_game.get_is_switch();
	this->ai_symbol = this->ai_side? 'A': '1';

	Movement answer;	//the best move will return
	int best_val = -1e9, child_val;
	int win = this->ai_side? 2: 1;
	int next_move_cnt = cur_game.count_movable_chs(dice);

	for (int i=0; i<next_move_cnt; i++) {
		int chs_index = cur_game.get_movable_chs(i).symbol - this->ai_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 == win)
					child_val = evaluate_feature(child_game);
				else if (game_status != 0)	// lose the game
					continue;
				else {
					child_game.switch_player();
					child_val = minimax(child_game, HEIGHT);
				}
				// update the best value and the best movement
				if (child_val >= best_val) {
					best_val = child_val;
					answer = mvmt;
				}
			}
		}
	}
	double timeCost = (double)(clock() - init_time) / CLOCKS_PER_SEC;
	//printf("\ntime cost: %.2fs\n", timeCost);
	return answer;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movq %rsi, %rbx
movq %rdi, %r14
leaq -0x5c(%rbp), %r15
movl %edx, (%r15)
callq 0x20b0
movq %rbx, %rdi
callq 0x855c
movl %eax, %r12d
movb %al, 0x10(%r14)
shlb $0x4, %al
addb $0x31, %al
movq %r14, -0x48(%rbp)
movb %al, 0x11(%r14)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x7e52
movl $0x0, -0x2c(%rbp)
xorl %r14d, %r14d
movl %eax, -0x4c(%rbp)
testl %eax, %eax
jle 0x6d80
movq $0x0, -0x58(%rbp)
movzbl %r12b, %eax
incl %eax
movl %eax, -0x50(%rbp)
movl $0xc4653600, -0x34(%rbp) # imm = 0xC4653600
leaq -0x3c(%rbp), %r13
leaq -0x148(%rbp), %r12
movl $0x0, -0x30(%rbp)
movq %rbx, %rdi
leaq -0x2c(%rbp), %rsi
callq 0x84f8
movsbl %al, %r15d
movq -0x48(%rbp), %rax
movsbl 0x11(%rax), %eax
subl %eax, %r15d
xorl %r14d, %r14d
movl %r15d, -0x3c(%rbp)
movl %r14d, -0x38(%rbp)
movq %rbx, %rdi
movq %r13, %rsi
callq 0x7f74
testb %al, %al
je 0x6d58
movq %r12, %rdi
movq %rbx, %rsi
callq 0x78e2
movq %r12, %rdi
movq %r13, %rsi
callq 0x7fea
cmpl -0x50(%rbp), %eax
jne 0x6d28
movq -0x48(%rbp), %rdi
movq %r12, %rsi
callq 0x6da0
jmp 0x6d43
testl %eax, %eax
jne 0x6d58
movq %r12, %rdi
callq 0x8150
movq -0x48(%rbp), %rdi
movl 0x8(%rdi), %edx
movq (%rdi), %rax
movq %r12, %rsi
callq *(%rax)
cmpl -0x34(%rbp), %eax
jl 0x6d58
movl -0x3c(%rbp), %ecx
movl %ecx, -0x30(%rbp)
movl -0x38(%rbp), %ecx
movq %rcx, -0x58(%rbp)
movl %eax, -0x34(%rbp)
incl %r14d
cmpl $0x3, %r14d
jne 0x6ce8
movl -0x2c(%rbp), %eax
incl %eax
movl %eax, -0x2c(%rbp)
cmpl -0x4c(%rbp), %eax
jl 0x6cca
movq -0x58(%rbp), %rax
shlq $0x20, %rax
movl -0x30(%rbp), %r14d
jmp 0x6d82
xorl %eax, %eax
orq %rax, %r14
callq 0x20b0
movq %r14, %rax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop