// File: /workspace/github_cpp_makefile_star2_O1/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 0x872e
movl %eax, %r12d
movb %al, (%r15)
shlb $0x4, %al
addb $0x31, %al
movq %r15, -0x60(%rbp)
movb %al, 0x1(%r15)
movq %rbx, -0x40(%rbp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x800c
movl %eax, %ecx
movl $0x0, -0x30(%rbp)
xorl %eax, %eax
movl %ecx, -0x54(%rbp)
testl %ecx, %ecx
jle 0x622b
movq $0x0, -0x50(%rbp)
movzbl %r12b, %eax
incl %eax
movl %eax, -0x58(%rbp)
movl $0xc4653600, -0x44(%rbp) # imm = 0xC4653600
leaq -0x38(%rbp), %r14
movl $0x0, -0x2c(%rbp)
movq -0x40(%rbp), %r12
movq %r12, %rdi
leaq -0x30(%rbp), %rsi
callq 0x86ca
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 0x8118
testb %al, %al
je 0x61b3
leaq -0x150(%rbp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x7a74
movq %rbx, %rdi
movq %r14, %r12
movq %r14, %rsi
callq 0x818e
movl %eax, %r14d
movq %rbx, %rdi
callq 0x8316
cmpl -0x58(%rbp), %r14d
jne 0x61b7
movl -0x38(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x34(%rbp), %eax
movq %rax, -0x50(%rbp)
movl $0x5, %ecx
jmp 0x61c1
xorl %ecx, %ecx
jmp 0x61c8
movl $0x7, %ecx
testl %r14d, %r14d
je 0x61e0
movq %r12, %r14
movq -0x40(%rbp), %r12
cmpl $0x7, %ecx
je 0x61d1
testl %ecx, %ecx
jne 0x620c
incl %r13d
cmpl $0x3, %r13d
jne 0x6157
jmp 0x620c
movq -0x60(%rbp), %rdi
leaq -0x150(%rbp), %rsi
callq 0x623e
xorl %ecx, %ecx
cmpl -0x44(%rbp), %eax
movq %r12, %r14
jl 0x61c4
movl -0x38(%rbp), %edx
movl %edx, -0x2c(%rbp)
movl -0x34(%rbp), %edx
movq %rdx, -0x50(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x61c4
movl -0x30(%rbp), %eax
incl %eax
movl %eax, -0x30(%rbp)
cmpl -0x54(%rbp), %eax
jl 0x6139
movq -0x50(%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
nop