⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 samplesearcher.java

📁 一个黑白棋的源代码(尚未完成
💻 JAVA
字号:
/* * SampleSearcher.java * * Created on 2007-9-29, 14:49:45 * * To change this template, choose Tools | Templates * and open the template in the editor. */package org.yangcq.logic.search;import org.yangcq.logic.chessboard.ChessboardImpl;import org.yangcq.logic.step.Step;/** * * @author Administrator * 采用最差的极大极小值搜索算法 */public class SampleSearcher extends BasicSearcher{    private Step bestStep = null;    public Step getBestStep()    {        Step[][] steps = ChessboardImpl.getChessboard().getBoardCopy();        this.search(steps, ChessboardImpl.getChessboard().getCurrentPlayer().isChessmanBlack(), this.getSearchDepth(), false);        return this.bestStep;    }    protected long search(Step[][] steps, boolean side, long depth, boolean prevPass)//极大极小算法    {        long val;        long current;        boolean opSide = !side;        if (depth <= 0)        {            return this.getValuer().getValue(steps, side); //估值        }        Step[] validStep = this.getStepGenerater().getValidSteps(steps, side); //走法生成        if (validStep != null)        {            val = Long.MIN_VALUE; //初始化为极小值            for (int i = 0; i < validStep.length; i++)            {                long count = this.getStepGenerater().chess(steps, validStep[i].getX(), validStep[i].getY(), validStep[i].getColor().booleanValue()); //走一步                current = -search(steps, opSide, depth - 1, false); //让对手估值                this.getStepGenerater().unChess(steps, validStep[i].getX(), validStep[i].getY(), count); //撤销一步                if (current > val)                {                    val = current;                    if (depth == this.getSearchDepth())                    {                        this.bestStep = validStep[i].clone();                    }                }            }        } else        {            if (prevPass)            {                val = 0;                for (int x = 0; x < steps.length; x++)                {                    for (int y = 0; y < steps.length; y++)                    {                        if (steps[x][y] != null && steps[x][y].getColor().booleanValue() == side)                        {                            val++;                        } else if (steps[x][y] != null && steps[x][y].getColor().booleanValue() == opSide)                        {                            val--;                        }                    }                }            } else            {                val = -search(steps, opSide, depth, true);                if (depth == this.getSearchDepth())                {                    this.bestStep = null; //以前可能已经对最好着法赋值,但是那是对手赋的值,所以要擦除                }            }        }        return val;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -