// File: /workspace/github_cpp_makefile_star2_O2/smallHW89[P]gobang/GoBang_ChessBoard.cpp
// Function: ChessBoard::CalculateBoard(char, int&, bool)
int64_t ChessBoard::CalculateBoard( ChessSide side , int &win, bool isMax)
{
    int  blackWin=0, whiteWin=0, blackScore=0, whiteScore=0; 
    memset(m_bType, 0 , sizeof(m_bType));
    memset(m_wType, 0 , sizeof(m_wType));

    for(int i=0; i<m_chessList.size(); i++ ) 
    {
        int t = CalculateOnePos(m_chessList[i].x, m_chessList[i].y,
                m_chessList[i].side, 1);                    

        if( Chess_Black ==  m_chessList[i].side )
        {
            blackScore += t;
        }
        else
        {
            whiteScore += t;
        }
    }     

    int64_t  score;
    if( side == Chess_Black )
        score = blackScore - whiteScore;
    else
        score = whiteScore - blackScore;

    win = 0;
    if( side == Chess_Black  && isMax) //这里是AI下子后
    {
        if(m_bType[Board_FIVE] > 0  ) 
            win = 1; 
        else if(m_bType[Board_FOUR] > 0   &&
                m_wType[Board_FOUR]==0  && m_wType[Board_SFOUR] == 0 ) 
            win = 1;
        if(win == 1)
            score =  MM_MAX;
    }
    else if( side == Chess_Black &&  !isMax ) //对方下完后
    {
        if(m_wType[Board_FIVE] >0     ) 
            win =1;
        else if(m_wType[Board_FOUR] >0  &&
                m_bType[Board_FOUR] ==0 && m_bType[Board_SFOUR] ==0 ) 
            win = 1;
        if(win == 1)
            score = MM_MIN;
    }
    else if(side == Chess_White  && isMax) //AI白，AI下子后
    {
        if(m_wType[Board_FIVE ]> 0 ) 
            win = 1;
        if(m_wType[Board_FOUR] > 0   &&
                m_bType[Board_FOUR]==0  && m_bType[Board_SFOUR] == 0 ) 
            win = 1;
        if(win==1  )
            score == MM_MAX;
    }
    else if(side == Chess_White && !isMax)
    {
         if(m_bType[Board_FIVE] > 0  ) 
            win = 1; 
        else if(m_bType[Board_FOUR] > 0   &&
                m_wType[Board_FOUR]==0  && m_wType[Board_SFOUR] == 0 ) 
            win = 1;
        if(win == 1)
            score =  MM_MIN;
    }
    return score;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %ecx, -0x30(%rbp)
movq %rdx, -0x38(%rbp)
movl %esi, -0x2c(%rbp)
movq %rdi, %r14
addq $0x19c, %rdi            # imm = 0x19C
movl $0xa0, %edx
xorl %esi, %esi
callq 0x1090
movq 0x240(%r14), %rcx
pushq $0x10
popq %r12
xorl %r13d, %r13d
xorl %r15d, %r15d
xorl %ebx, %ebx
movq 0x248(%r14), %rax
subq %rcx, %rax
cqto
pushq $0x18
popq %rsi
idivq %rsi
cmpq %r13, %rax
jbe 0x2a0f
movl -0x10(%rcx,%r12), %esi
movl -0xc(%rcx,%r12), %edx
movsbl (%rcx,%r12), %ecx
movq %r14, %rdi
pushq $0x1
popq %r8
callq 0x2860
movq 0x240(%r14), %rcx
cmpb $0x62, (%rcx,%r12)
movl %eax, %edx
movl $0x0, %esi
cmovel %esi, %edx
cmovnel %esi, %eax
addl %edx, %r15d
addl %ebx, %eax
incq %r13
addq $0x18, %r12
movl %eax, %ebx
jmp 0x29b4
movl %ebx, %eax
subl %r15d, %eax
subl %ebx, %r15d
movl -0x2c(%rbp), %r8d
cmpb $0x62, %r8b
cmovel %eax, %r15d
setne %dl
movq -0x38(%rbp), %rsi
andl $0x0, (%rsi)
movslq %r15d, %rax
movl -0x30(%rbp), %edi
movl %edi, %ecx
xorb $0x1, %cl
orb %cl, %dl
je 0x2a70
cmpb $0x62, %r8b
setne %dl
orb %dil, %dl
je 0x2aa7
cmpb $0x77, %r8b
setne %dl
orb %cl, %dl
je 0x2ad8
cmpb $0x77, %r8b
setne %cl
orb %dil, %cl
jne 0x2b12
movq $-0x77359400, %rcx      # imm = 0x88CA6C00
jmp 0x2a75
movl $0x77359400, %ecx       # imm = 0x77359400
cmpl $0x0, 0x1a0(%r14)
jg 0x2b09
cmpl $0x0, 0x1a4(%r14)
jle 0x2b12
cmpl $0x0, 0x1f4(%r14)
jne 0x2b12
cmpl $0x0, 0x1f8(%r14)
je 0x2b09
jmp 0x2b12
movq $-0x77359400, %rcx      # imm = 0x88CA6C00
cmpl $0x0, 0x1f0(%r14)
jg 0x2b09
cmpl $0x0, 0x1f4(%r14)
jle 0x2b12
cmpl $0x0, 0x1a4(%r14)
jne 0x2b12
cmpl $0x0, 0x1a8(%r14)
je 0x2b09
jmp 0x2b12
cmpl $0x0, 0x1f0(%r14)
jle 0x2ae8
movl $0x1, (%rsi)
cmpl $0x0, 0x1f4(%r14)
jle 0x2b12
cmpl $0x0, 0x1a4(%r14)
jne 0x2b12
cmpl $0x0, 0x1a8(%r14)
movq %rax, %rcx
jne 0x2b12
movl $0x1, (%rsi)
movq %rcx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop