正文

五子棋AI(III)2005-08-07 11:04:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/elva6401/3590.html

分享到:

//        ChangeStatus(ptempboard);
        pi=i;
        pj=j;
        while(SearchBlank(i,j,ptempboard))
        {//进行第二不查找
            ptempboard[i][j]=3;//标记已被查找
            ptemp=GiveScore(0,i,j);
            if(pscore>ptemp)//此时为玩家下子,运用极小极大法时应选取最小值
                pscore=ptemp;
        }
        for(m=0;m<n;m++)
        {//恢复玩家信息
            ptable[pi][pj][temp1[m]]=true;
            win[0][temp1[m]]=temp2[m];
        }
        //ctemp+pscore为实际估价函数
        if(ctemp+pscore>cscore) //此时为计算机下子,运用极小极大法时应选取最最大值
        {
            cscore=ctemp+pscore;
            bestx=pi;
            besty=pj;
        }
    }
    }
    board[bestx][besty]=1;
    if(m_pclastpos.x!=-1&&m_pclastpos.y!=-1)
    {//画前一棋子
    if(!m_bwfirst)
        DrawBlackChess(m_pclastpos.x,m_pclastpos.y);
    else
        DrawWhiteChess(m_pclastpos.x,m_pclastpos.y);
    }

    if(!m_bwfirst)
        DrawNowBlack(bestx,besty);
    else
        DrawNowWhite(bestx,besty);
    m_pclastpos.x=bestx;
    m_pclastpos.y=besty;
    for(i=0;i<572;i++)
    {//修改计算机下子后,棋盘的变化状况
        if(ctable[bestx][besty][i]&&win[1][i]!=7)
            win[1][i]++;
        if(ptable[bestx][besty][i])
        {
            ptable[bestx][besty][i]=false;
            win[0][i]=7;
        }
    }
    computer=false;
    player=true;
}
(8)    玩家下棋:(其中坐标(9)    加减目的是调整图片的位置和棋盘对齐)
void CMyChessDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
    int x,y,tx,ty;
    // TODO: Add your message handler code here and/or call default
    if(player&&point.x<=535&&point.y<=535)//判断是否在有效区域
    {
        tx=x=point.x-24;
        ty=y=point.y-25;
        while(tx>=36)
            tx-=36;
        while(ty>=36)
            ty-=36;
        tx+=x/36;
        ty+=y/36;
        if(tx>18)
            x=x/36+1;
        else
            x=x/36;
        if(ty>18)
            y=y/36+1;
        else
            y=y/36;
        if(board[x][y]==2)
        {
            board[x][y]=0;//设为玩家的棋子
            if(m_pplastpos.x!=-1&&m_pplastpos.y!=-1)
            {
                if(!m_bwfirst)
                    DrawWhiteChess(m_pplastpos.x,m_pplastpos.y);
                else
                    DrawBlackChess(m_pplastpos.x,m_pplastpos.y);
            }

            if(!m_bwfirst)
                DrawNowWhite(x,y);
            else
                DrawNowBlack(x,y);
            m_pplastpos.x=x;
            m_pplastpos.y=y;
            for(int i=0;i<572;i++)//修改玩家下子后棋盘状态的变化
            {
                if(i==80)
                    i=80;
                if(ptable[x][y][i]&&win[0][i]!=7)
                    win[0][i]++;
                if(ctable[x][y][i])
                {
                    ctable[x][y][i]=false;
                    win[1][i]=7;
                }
            }
            player=false;
            computer=true;
        }
    }
    CDialog::OnLButtonDown(nFlags, point);
}

阅读(3470) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册