// File: tyhsu[P]Ewn/minimax.cpp
// Function: Minimax::simulation(Game)
int Minimax::simulation(Game simu_game) {
	char cur_symbol;
	int game_status = 0, ai_win = this->ai_side ? 2 : 1;
	if (simu_game.get_is_switch() == this->ai_side)
		cur_symbol = this->ai_symbol;
	else
		cur_symbol = this->ai_side? '1' : 'A';

	while (game_status == 0) {
		Movement available_mvmt_list[18];
		int available_mvmt_cnt = 0;

		// find all avaible move (6 dice indices, 3 directions)
		for(int chs_index = 0; chs_index < 6; chs_index++) {
			Chess cur_chs = simu_game.get_cur_chs_list(chs_index);
			if (cur_chs.exist) {
				for (int direct = 0; direct < 3; direct++) {
					Movement tmp_mvmt(chs_index, direct);
					if (simu_game.check_in_board(tmp_mvmt)) {
						available_mvmt_list[available_mvmt_cnt] = tmp_mvmt;
						available_mvmt_cnt ++;
					}
				}
			}
		}
		// randomly pick a move.
		Movement next_mvmt = available_mvmt_list[rand() % available_mvmt_cnt];
		game_status = simu_game.update_game_status(next_mvmt);

		// check game status => if the game keep going, switch the player.
		if(game_status == 0) simu_game.switch_player();
		else break;
	}
	// return the result of game.
	return (game_status == ai_win? 1 : 0);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x130, %rsp            # imm = 0x130
movq %rsi, -0x108(%rbp)
movq %rdi, %rax
movq -0x108(%rbp), %rdi
movq %rdi, -0x100(%rbp)
movq %rax, -0x8(%rbp)
movq %rdi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xf8(%rbp)
movl $0x0, -0x18(%rbp)
movb 0x10(%rax), %dl
movl $0x1, %eax
movl $0x2, %ecx
testb $0x1, %dl
cmovnel %ecx, %eax
movl %eax, -0x1c(%rbp)
callq 0xb910
movq -0xf8(%rbp), %rcx
andb $0x1, %al
movzbl %al, %eax
movb 0x10(%rcx), %cl
andb $0x1, %cl
movzbl %cl, %ecx
cmpl %ecx, %eax
jne 0x8980
movq -0xf8(%rbp), %rax
movb 0x11(%rax), %al
movb %al, -0x11(%rbp)
jmp 0x89b4
movq -0xf8(%rbp), %rax
movb 0x10(%rax), %cl
movb $0x31, %al
movb $0x41, %dl
movb %dl, -0x10a(%rbp)
testb $0x1, %cl
movb %al, -0x109(%rbp)
jne 0x89ab
movb -0x10a(%rbp), %al
movb %al, -0x109(%rbp)
movb -0x109(%rbp), %al
movb %al, -0x11(%rbp)
jmp 0x89b6
cmpl $0x0, -0x18(%rbp)
jne 0x8b68
leaq -0xb0(%rbp), %rax
movq %rax, %rcx
addq $0x90, %rcx
movq %rcx, -0x120(%rbp)
movq %rax, -0x118(%rbp)
movq -0x118(%rbp), %rdi
movq %rdi, -0x128(%rbp)
callq 0x4c60
movq -0x128(%rbp), %rax
movq -0x120(%rbp), %rcx
addq $0x8, %rax
cmpq %rcx, %rax
movq %rax, -0x118(%rbp)
jne 0x89df
movl $0x0, -0xb4(%rbp)
movl $0x0, -0xb8(%rbp)
cmpl $0x6, -0xb8(%rbp)
jge 0x8b19
movq -0x100(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
callq 0xb810
movl %edx, -0xd0(%rbp)
movq %rax, -0xd8(%rbp)
movq -0xd8(%rbp), %rax
movq %rax, -0xc4(%rbp)
movl -0xd0(%rbp), %eax
movl %eax, -0xbc(%rbp)
testb $0x1, -0xc3(%rbp)
je 0x8b03
movl $0x0, -0xdc(%rbp)
cmpl $0x3, -0xdc(%rbp)
jge 0x8b01
leaq -0xe4(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
leaq -0xdc(%rbp), %rdx
callq 0x5c40
movq -0x100(%rbp), %rdi
leaq -0xe4(%rbp), %rsi
callq 0xafe0
testb $0x1, %al
jne 0x8abe
jmp 0x8aee
movslq -0xb4(%rbp), %rax
leaq -0xb0(%rbp), %rdi
shlq $0x3, %rax
addq %rax, %rdi
leaq -0xe4(%rbp), %rsi
callq 0x6a30
movl -0xb4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb4(%rbp)
jmp 0x8af0
movl -0xdc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xdc(%rbp)
jmp 0x8a82
jmp 0x8b03
jmp 0x8b05
movl -0xb8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb8(%rbp)
jmp 0x8a24
callq 0x2050
movq -0x100(%rbp), %rdi
cltd
idivl -0xb4(%rbp)
movslq %edx, %rax
movq -0xb0(%rbp,%rax,8), %rax
movq %rax, -0xec(%rbp)
leaq -0xec(%rbp), %rsi
callq 0xb120
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
jne 0x8b61
movq -0x100(%rbp), %rdi
callq 0xb440
jmp 0x8b63
jmp 0x8b68
jmp 0x89b6
movl -0x18(%rbp), %edx
movl -0x1c(%rbp), %esi
xorl %eax, %eax
movl $0x1, %ecx
cmpl %esi, %edx
cmovel %ecx, %eax
addq $0x130, %rsp            # imm = 0x130
popq %rbp
retq
nopw %cs:(%rax,%rax)
nopl (%rax)