人机博弈之(六)------代码实现(3)搜索引擎

package  searchengine;

import  chessmovedata.ChessMoveData;
import  constdata.ConstData;
import  movegenerator.MoveGenerator;
import  eveluation.Eveluation;


public   class  SearchEngine
{    //表示棋盘棋的分布
    public     byte m_curPosition[][] = new byte[10][9];
    
//记录最佳走棋
    public ChessMoveData m_bestMove = new ChessMoveData();
    
//走法产生七器
    public MoveGenerator m_moveGen = new MoveGenerator();
    
//估值产生器
    public Eveluation m_eve = new Eveluation();
    
//搜索深度
    public int m_searchDepth;
    
//当前搜索深度
    public int m_maxDepth;
    
    
public  void searchAgoodMove(byte position[][]){}
    
//移动棋子
    public byte makeMove(ChessMoveData data)
    
{
        
byte chessID;
        chessID 
= m_curPosition[data.to.y][data.to.x];
        m_curPosition[data.to.y][data.to.x] 
= m_curPosition[data.from.y][data.from.x];
        m_curPosition[data.from.y][data.from.x] 
= ConstData.NOCHESS;
        
        
return chessID;
    }

    
//取消移动
    public void unMakeMove(ChessMoveData move, byte chessID)
    
{
        m_curPosition[move.from.y][move.from.x] 
= m_curPosition[move.to.y][move.to.x];
        m_curPosition[move.to.y][move.to.x] 
= chessID;
    }

    
    
//判断游戏时候结束
    public int isGameOve(byte position[][], int depth)
    
{
        
int i, j;
        
boolean redLive = false, blackLive = false;
        
        
for(i = 0; i < 3++i)
            
for(j = 3; j < 6++j)
            
{
                
if(position[i][j] == ConstData.B_KING)
                    blackLive 
= true;
                
if(position[i][j] == ConstData.R_KING)
                    redLive 
= true;
            }

        
        
for(i = 7; i < 10++i)
            
for(j = 3; j < 6++j)
            
{
                
if(position[i][j] == ConstData.B_KING)
                    blackLive 
= true;
                
if(position[i][j] == ConstData.R_KING)
                    redLive 
= true;
            }

        
        i 
= (m_maxDepth - depth + 1 )%2;//取当前奇偶标志
        
        
if(!redLive)//红将不在了
        {
            
if(i != 0)
                
return 19990 + depth;
            
else 
                
return -19990-depth;
        }

        
        
if(!blackLive)
            
if(0 != i)
                
return -19990-depth;
            
else
                
return 19990+depth;
        
        
return 0;//两将都在,返回0
    }

    
    
    
}

上面是通用的搜索引擎,可以通过继承他,改写此函数

public    void  searchAgoodMove( byte  position[][]) {}  

来实现具体的搜索。下面是负极大值搜索算法:

 

package  negamaxEngine;

import  searchengine.SearchEngine;

public   class  NegamaxEngine  extends  SearchEngine
{
    
//对传入的position状态找出一步最佳的走法
    public  void searchAgoodMove(byte position[][])
    
{System.out.println("search.......");
        m_maxDepth 
= m_searchDepth;
        
int i, j;
        
for(i = 0; i < 10++i)
            
for(j = 0; j < 9++j)
                m_curPosition[i][j] 
= position[i][j];
        
        negaMax(m_maxDepth);
        makeMove(m_bestMove);
        
        
for(i = 0; i < 10++i)
            
for(j = 0; j < 9++j)
                position[i][j] 
= m_curPosition[i][j];
    }

    
//负极大值搜索引擎
    public int negaMax(int depth)
    
{
        
int current = -20000;
        
int score = 0;
        
int count = 0 , i;
        
byte type;
        
        i 
= isGameOve(m_curPosition, depth);
        
if(i != 0)//不等于0表示某方的王不存在了,直接返回
        {
            
return i;
        }

        
boolean side;
        
if((m_maxDepth-depth)%2 == 1) side = true;
        
else side = false;
        
//搜索到叶子节点,返回估值信息
        if(depth <= 0)
            
return m_eve.eveluation(m_curPosition, side);
        
//生产所有可能的走法,
        count = m_moveGen.createPossibleMove(m_curPosition, depth, side);
        
        
for(i = 0; i < count ; ++i)
        
{    //尝试每种走法
            type = makeMove(m_moveGen.moveList[depth][i]);
            
//返回此种走法的估值分数
            score = -negaMax(depth-1);
            
//取消上次走法
            unMakeMove(m_moveGen.moveList[depth][i], type);
            
//若当前走法更优,则记录下来
            if(score > current)
            
{
                current 
= score;
                
//如果到了根节点
                if(depth == m_maxDepth)
                
{//记录最优的走棋,把它保存到m_bestMove中
                    m_bestMove = m_moveGen.moveList[depth][i];
                }

            }

        }

        
return current;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值