int alpha_beta(int color,int alpha,int beta,int depth){
	int i,best=-INF;
	int bestmove=-1;
	if(depth==MAX_DEEP){  
		return evaluate(color); 
	} 
	int stacklen=createmove(color,depth);
	if(stacklen==0){
		return evaluate(color);
	}
	for(i=0;i<stacklen;i++){
		movestack[depth][i].score=history[movestack[depth][i].x[0]][movestack[depth][i].y[0]][movestack[depth][i].x[1]][movestack[depth][i].y[1]][movestack[depth][i].x[2]][movestack[depth][i].y[2]];
	}
	qsort(movestack[depth],stacklen,sizeof(movestack[depth][0]),cmp);
	for(i=0;i<stacklen;i++){
		movechess(movestack[depth][i]);
		best=-alpha_beta(color^3,-beta,-alpha,depth+1);
		unmovechess(movestack[depth][i]);
		if(best>alpha){
			alpha=best;
			bestmove=i;
		}
		if(alpha>=beta){
			bestmove=i;
			break;
		}
	}
	if(bestmove!=-1){
		history[movestack[depth][bestmove].x[0]][movestack[depth][bestmove].y[0]][movestack[depth][bestmove].x[1]][movestack[depth][bestmove].y[1]][movestack[depth][bestmove].x[2]][movestack[depth][bestmove].y[2]]+=2<<(MAX_DEEP-depth);
	}
	return alpha;
}
int search_a_goodmove(int color, MoveType * bestMove,int alpha,int beta,int dep) {		//alpha-beta根节点函数
	int i,best=-INF;
	int bestmove=-1;
	memset(history,0,sizeof(history));
	int stacklen=createmove(color,dep);
	if(stacklen==0){
		return 0;
	}
	for(i=0;i<stacklen;i++){
	movestack[dep][i].score=history[movestack[dep][i].x[0]][movestack[dep][i].y[0]][movestack[dep][i].x[1]][movestack[dep][i].y[1]][movestack[dep][i].x[2]][movestack[dep][i].y[2]];
	}
	qsort(movestack[dep],stacklen,sizeof(movestack[dep][0]),cmp);
	for(i=0;i<stacklen;i++){
		movechess(movestack[dep][i]);
		best=-alpha_beta(color^3,-beta,-alpha,dep+1);
		unmovechess(movestack[dep][i]);
		if(best>alpha){
			alpha=best;
			*bestMove=movestack[dep][i];
			bestmove=i;
		}
		if(alpha>=beta){
			bestmove=i;
			break;
		}
	}
	if(bestmove!=-1){
	history[movestack[dep][bestmove].x[0]][movestack[dep][bestmove].y[0]][movestack[dep][bestmove].x[1]][movestack[dep][bestmove].y[1]][movestack[dep][bestmove].x[2]][movestack[dep][bestmove].y[2]]+=2<<(MAX_DEEP-dep);
	}
	return 1;
}
Пример #3
0
//采用博弈树来分析
//调用c语言
void AiGame::ComMoveChess()
{
    if(!win){
     QPoint whiteGo;//电脑的颜色

     int x,y;
     int level = 2;
     x = 0;
     y = 0;
     int flag = 1;
     if(clickcount == 1){ //电脑第一次落子
         while(flag){
           int  flag_randomPick = qrand()%7;
            FindFirstChess(&x,&y,chess,flag_randomPick);
        if(Position(x,y,chess) == 0) //直到落子合法才使flag=0退出循环
            flag = 0;
         }
     }
     else{
         switch(level){ //因为level是采用下拉框,所以不用做异值判断
            case 1:{
                    robot_level1(level,&x,&y,chess);//只会防守
                    break;
                }
            case 2:{
                    robot_level2(level,&x,&y,chess);//会适当进攻
                    break;
                }
            case 3:{
                    //todo
                    //采用棋谱
                    break;
               }
         }
     }
     clickcount++;
    movechess(x,y);
    win = isWin(x,y);
        if(win)
        {
            QMessageBox::information(this, "RobotWin", "RobotWin", QMessageBox::Ok);
            qDebug()<<"win";
        }
    }
    else
    {
        QMessageBox::information(this, "YouWin", "YouWin", QMessageBox::Ok);
        qDebug()<<"win";
    }
 }
Пример #4
0
void AiGame::mousePressEvent(QMouseEvent *event){
    int x;
    int y;
  if(event->button() == Qt::LeftButton){
    offset = event->pos();//获取鼠标的坐标
            //棋盘区域
            if(!win && offset.rx()>186&&offset.rx()<600&&offset.ry()>39&&offset.ry()<445)
            {
                    if(chess[(offset.rx()-210)/27][(offset.ry()-60)/27])//如果鼠标点击的地方有旗子
                        return;
                    x = (offset.rx()-210)/27;
                    y = (offset.ry()-60)/27;
                    if (offset.ry() > 415 && !chess[x][14])//补偿最后一格的误差
                        y = 14;

                    clickcount++;//记录步数

                    movechess(x,y);
                    win = isWin(x,y);
                    ComMoveChess();
                }
        }

};