|
|
先上菜:
- Move MinMax_AI::FindBestStep()
- {
- int maxVal = -99999999;
- Move bestMove;
- int tmp=0;
- char* curStep;
- int cur;
- actionInfo info;
- for(int j=0; j<8 ;j++)
- {
- for(int i=0; i<8 ;i++)
- {
- if((_board->GetBoardInfo(i,j)^AI_PLAYER) < 0x10)//只控制是自己的棋子就够了。。。
- {
- cur = 0;
- info = _board->GetPieceBonusPoints(i,j);//获得棋盘上这个点的所有移动可能
- curStep = (char*)&info.moveList;//moveList是short数组,前面8个字是x,后8个字是y
- while(info.moveList[cur] != 0)//对所有可以移动的可能
- {
- _board->MovePieceWithOutCheck(i,j,*curStep,*(curStep+1));//直接无需检查就移动,因为这次移动是通过检查后得到的。
- tmp = Min(_depth-1);//轮到对方走了。。。
- if(tmp > maxVal)
- {
- maxVal = tmp;
- bestMove.srcPt = MyPOINT(i,j);
- bestMove.dstPt = MyPOINT(*curStep,*(curStep+1));
- }
- _board->MovePieceWithOutCheck(*curStep,*(curStep+1),i,j);//然后把棋子弄回来。
- curStep+=2;//继续下个移动
- cur++;
- }
- }
- }
- }
- return bestMove;
- }
- int MinMax_AI::Min(int dp)
- {
- if(dp==0)
- {
- //返回棋盘估价,2是代表 AI分-人类分,eval是一个类的实现
- //把棋盘传过去
- return _eval.GetVal(_board,2);
- }
- int min = 99999999;
- int tmp=0;
- char* curStep;
- int cur;
- actionInfo info;
- for(int j=0;j<8;j++)
- {
- for(int i=0; i<8; i++)
- {
- if((_board->GetBoardInfo(i,j)^HUMAN_PLAYER) < 0x10)//只用模拟人类防。。。
- {
- cur = 0;
- info = _board->GetPieceBonusPoints(i,j);
- curStep = (char*)&info.moveList;
- while(info.moveList[cur] != 0)
- {
- _board->MovePieceWithOutCheck(i,j,*curStep,*(curStep+1));
- tmp = Max(dp-1);
- if(tmp < min)
- {
- min = tmp;
- }
- _board->MovePieceWithOutCheck(*curStep,*(curStep+1),i,j);
- curStep+=2;
- cur++;
- }
- }
- }
- }
- return min;
- }
- int MinMax_AI::Max(int dp)
- {
- if(dp == 0)
- return _eval.GetVal(_board,2);
- int max = -99999999;
- int tmp=0;
- char* curStep;
- int cur;
- actionInfo info;
- for(int j=0; j<8 ;j++)
- {
- for(int i=0; i<8; i++)
- {
- if((_board->GetBoardInfo(i,j)^AI_PLAYER) < 0x10)
- {
- cur = 0;
- info = _board->GetPieceBonusPoints(i,j);
- curStep = (char*)&info.moveList;
- while(info.moveList[cur] != 0)
- {
- _board->MovePieceWithOutCheck(i,j,*curStep,*(curStep+1));
- tmp = Min(dp-1);
- if(tmp > max)
- {
- max = tmp;
- }
- _board->MovePieceWithOutCheck(*curStep,*(curStep+1),i,j);
- curStep+=2;
- cur++;
- }
- }
- }
- }
- return max;
- }
复制代码
对于上面的算法类,不知道为什么。在计算时会改变棋盘的布局,也就是没办法在模拟对战后把布局改回原来的样子。。
我觉得应该是下面这个的问题,因为我不记得除了MovePieceWithOutCheck这个函数外还调用过其他能改变棋盘布局的函数,,。。。
- _board->MovePieceWithOutCheck(i,j,*curStep,*(curStep+1));
- tmp = Min(dp-1);
- if(tmp > max)
- {
- max = tmp;
- }
- _board->MovePieceWithOutCheck(*curStep,*(curStep+1),i,j);
复制代码
这个部分的问题。。。因为移动后的棋子因为深度的原因没有办法很好的回到它自己的原地。。。 [em17]
MovePieceWithOutCheck的实现:
- void chessboard::MovePieceWithOutCheck(int srcX, int srcY, int dstX, int dstY)
- {
- if( _board[srcX][srcY]!=EMPTYPLACE)
- {
- _board[dstX][dstY] = _board[srcX][srcY];
- _board[srcX][srcY] = EMPTYPLACE;
- }
- else std::cout<<"!";//如果是错误的移动。。。也就是移动空格子就打印!
- }
复制代码
而在深度为>=3的情况下打印了很多"!!!!!!!!!!!!!!"。。。也就是说很多错误的移动。。。
不知道哪里出问题了 哎 [em4] |
|