public GameTreeAction <A> GetBestAction(int depth)
 {
     if (depth != 0)
     {
         GameTreeAction <A> bestAction = null;
         var nextDepth = depth - 1;
         foreach (var nextAction in NodeData.GetNextActions(NodeColor))
         {
             nextAction.score = CurrentScore + (NodeColor.IsRed ? nextAction.score : -nextAction.score);
             var nextNode       = new GameTreeNode <D, A>(this, nextAction);
             var bestNextAction = nextNode.GetBestAction(nextDepth);
             if (TestAndUpdateThreshold(bestNextAction.score))
             {
                 bestAction = bestNextAction;
                 if (NeedsCutTreeNode())
                 {
                     break;
                 }
             }
         }
         if (nodeParent == null)
         {
             return(bestAction);
         }
         if (bestAction != null)
         {
             nodeAction.score = bestAction.score;
         }
     }
     return(nodeAction);
 }
示例#2
0
        public IGameTreeData <AiAction> GetNextState(GameTreeAction <AiAction> action)
        {
            var newBoardData = (PieceInfo[, ])Board.BoardPieces.Clone();
            var newBoard     = new ChessBoard(newBoardData);

            newBoard.MovePiece(action.action.Record);
            return(new AiData(newBoard));
        }
示例#3
0
        public IEnumerable <GameTreeAction <AiAction> > GetNextActions(GameTreeNodeColor nodeColor)
        {
            var actions = new List <GameTreeAction <AiAction> >();
            var color   = nodeColor.IsRed ? PieceColor.Red : PieceColor.Black;

            foreach (var record in Board.GetChessRecords(color))
            {
                if (Board.MovePieceIsOK(record, color))
                {
                    var aiAction = new AiAction(record);
                    // TODO score 计算
                    var score  = ValueRule.GetValueOfRecord(record);
                    var action = new GameTreeAction <AiAction>(aiAction, score);
                    actions.Add(action);
                }
            }
            actions.Sort((x, y) =>
            {
                return(y.score.CompareTo(x.score));
            });
            return(actions);
        }
 private GameTreeNode(GameTreeNode <D, A> parent, GameTreeAction <A> action)
 {
     nodeParent = parent;
     nodeAction = action;
 }