// File: /workspace/github_cpp_makefile_star2_O3/tyhsu[P]Ewn/mcts.cpp
// Function: MCTS::AI_move(Game&, int)
Movement MCTS::AI_move(Game& cur_game, int dice) {
	this->max_iterations = 1000;
	this->ai_side = cur_game.get_is_switch();
	this->ai_symbol = this->ai_side? 'A': '1';

	int best_val = -1e9, child_val;
	int win = this->ai_side? 2: 1;
	int next_move_cnt = cur_game.count_movable_chs(dice);
	Movement answer;
	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);
				child_game.switch_player();

				// check if the game ends
				if (game_status == win) {
					answer = mvmt;
					break;
				}
				else if (game_status != 0)	// lose the game
					continue;
				else {
					child_val = this->run(child_game);
				}

				// update the best value and the best movement
				if (child_val >= best_val) {
					best_val = child_val;
					answer = mvmt;
				}
			}
		}
	}
	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, %r15
leaq -0x64(%rbp), %r14
movl %edx, (%r14)
movl $0x3e8, 0x4(%rdi)       # imm = 0x3E8
movq %rsi, %rdi
callq 0x855c
movl %eax, %r12d
movb %al, (%r15)
shlb $0x4, %al
addb $0x31, %al
movq %r15, -0x60(%rbp)
movb %al, 0x1(%r15)
movq %rbx, -0x50(%rbp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x7e52
movl %eax, %ecx
movl $0x0, -0x30(%rbp)
xorl %eax, %eax
movl %ecx, -0x54(%rbp)
testl %ecx, %ecx
jle 0x6128
movq $0x0, -0x48(%rbp)
movzbl %r12b, %eax
incl %eax
movl %eax, -0x58(%rbp)
movl $0xc4653600, -0x3c(%rbp) # imm = 0xC4653600
leaq -0x38(%rbp), %r14
movl $0x0, -0x2c(%rbp)
movq -0x50(%rbp), %r12
movq %r12, %rdi
leaq -0x30(%rbp), %rsi
callq 0x84f8
movsbl %al, %r15d
movq -0x60(%rbp), %rax
movsbl 0x1(%rax), %eax
subl %eax, %r15d
xorl %r13d, %r13d
movl %r15d, -0x38(%rbp)
movl %r13d, -0x34(%rbp)
movq %r12, %rdi
movq %r14, %rsi
callq 0x7f74
testb %al, %al
je 0x60e6
leaq -0x150(%rbp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x78e2
movq %rbx, %rdi
movq %r14, %r12
movq %r14, %rsi
callq 0x7fea
movl %eax, %r14d
movq %rbx, %rdi
callq 0x8150
cmpl -0x58(%rbp), %r14d
je 0x60f5
testl %r14d, %r14d
jne 0x60df
movq -0x60(%rbp), %rdi
leaq -0x150(%rbp), %rsi
callq 0x613a
movl -0x3c(%rbp), %edx
cmpl %edx, %eax
movl -0x2c(%rbp), %ecx
cmovgel -0x38(%rbp), %ecx
movl %ecx, -0x2c(%rbp)
movq -0x48(%rbp), %rcx
cmovgel -0x34(%rbp), %ecx
movq %rcx, -0x48(%rbp)
cmovgl %eax, %edx
movl %edx, -0x3c(%rbp)
movq %r12, %r14
movq -0x50(%rbp), %r12
incl %r13d
cmpl $0x3, %r13d
jne 0x6061
jmp 0x6109
movl -0x38(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x34(%rbp), %eax
movq %rax, -0x48(%rbp)
movq %r12, %r14
movq -0x50(%rbp), %r12
movl -0x30(%rbp), %eax
incl %eax
movl %eax, -0x30(%rbp)
cmpl -0x54(%rbp), %eax
jl 0x6043
movq -0x48(%rbp), %rax
shlq $0x20, %rax
movl -0x2c(%rbp), %ecx
orq %rcx, %rax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq