// File: 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
subq $0x160, %rsp            # imm = 0x160
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x158(%rbp)
movl $0x3e8, 0x4(%rax)       # imm = 0x3E8
movq -0x18(%rbp), %rdi
callq 0xb910
movb %al, %cl
movq -0x158(%rbp), %rax
andb $0x1, %cl
movb %cl, (%rax)
movb (%rax), %cl
movb $0x41, %al
movb $0x31, %dl
movb %dl, -0x14e(%rbp)
testb $0x1, %cl
movb %al, -0x14d(%rbp)
jne 0x6b72
movb -0x14e(%rbp), %al
movb %al, -0x14d(%rbp)
movq -0x158(%rbp), %rax
movb -0x14d(%rbp), %cl
movb %cl, 0x1(%rax)
movl $0xc4653600, -0x20(%rbp) # imm = 0xC4653600
movb (%rax), %dl
movl $0x1, %eax
movl $0x2, %ecx
testb $0x1, %dl
cmovnel %ecx, %eax
movl %eax, -0x28(%rbp)
movq -0x18(%rbp), %rdi
leaq -0x1c(%rbp), %rsi
callq 0xada0
movl %eax, -0x2c(%rbp)
leaq -0x8(%rbp), %rdi
callq 0x4c60
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0x6ce4
movq -0x18(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0xb7c0
movq -0x158(%rbp), %rcx
movl %edx, -0x48(%rbp)
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0x40(%rbp)
movl -0x48(%rbp), %eax
movl %eax, -0x38(%rbp)
movsbl -0x40(%rbp), %eax
movsbl 0x1(%rcx), %ecx
subl %ecx, %eax
movl %eax, -0x34(%rbp)
movl $0x0, -0x54(%rbp)
cmpl $0x3, -0x54(%rbp)
jge 0x6cd4
leaq -0x5c(%rbp), %rdi
leaq -0x34(%rbp), %rsi
leaq -0x54(%rbp), %rdx
callq 0x5c40
movq -0x18(%rbp), %rdi
leaq -0x5c(%rbp), %rsi
callq 0xafe0
testb $0x1, %al
jne 0x6c38
jmp 0x6cc4
movq -0x18(%rbp), %rsi
leaq -0x148(%rbp), %rdi
callq 0xa520
leaq -0x148(%rbp), %rdi
leaq -0x5c(%rbp), %rsi
callq 0xb120
movl %eax, -0x14c(%rbp)
leaq -0x148(%rbp), %rdi
callq 0xb440
movl -0x14c(%rbp), %eax
cmpl -0x28(%rbp), %eax
jne 0x6c84
leaq -0x8(%rbp), %rdi
leaq -0x5c(%rbp), %rsi
callq 0x6a30
jmp 0x6cd4
cmpl $0x0, -0x14c(%rbp)
je 0x6c8f
jmp 0x6cc6
movq -0x158(%rbp), %rdi
leaq -0x148(%rbp), %rsi
callq 0x6d00
movl %eax, -0x24(%rbp)
jmp 0x6ca7
movl -0x24(%rbp), %eax
cmpl -0x20(%rbp), %eax
jl 0x6cc2
movl -0x24(%rbp), %eax
movl %eax, -0x20(%rbp)
leaq -0x8(%rbp), %rdi
leaq -0x5c(%rbp), %rsi
callq 0x6a30
jmp 0x6cc4
jmp 0x6cc6
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x6c07
jmp 0x6cd6
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x6bbe
movq -0x8(%rbp), %rax
addq $0x160, %rsp            # imm = 0x160
popq %rbp
retq
nopw %cs:(%rax,%rax)