예제 #1
0
        private int NoParitySearch(BitBoard board, int alpha, int beta, int empties, bool prevmove = true)
        {
            searchResult.Nodes++;
            var moves = Rule.FindMoves(board);

            if (moves.Length == 0)
            {
                if (!prevmove)
                {
                    //END
                    return(EndGameEvaluation.Eval(board));
                }
                else
                {
                    return(-NoParitySearch(board.Switch(), -beta, -alpha, empties, false));
                }
            }

            var score     = minimumScore;
            var diffCount = board.DiffCount();

            foreach (var pos in moves)
            {
                var eval = 0;

                var flips         = Rule.FindFlips(board, pos);
                var oppBoard      = Rule.FlipSwitch(board, pos, flips);
                var ownFlipsCount = flips.CountBits();

                if (empties == 2)
                {
                    //the last move of opponent player
                    var lastEmptySquare = oppBoard.EmptyPieces.Index();
                    if (lastEmptySquare < 0)
                    {
                        throw new Exception($"invalid square index:{lastEmptySquare}");
                    }

                    var oppFlipsCount = Rule.CountFlips(oppBoard, lastEmptySquare);

                    if (oppFlipsCount > 0)
                    {
                        //both done.
                        eval = diffCount + 2 * (ownFlipsCount - oppFlipsCount);
                    }
                    else
                    {
                        //opp pass
                        var ownLastFlipsCount = Rule.CountFlips(oppBoard, lastEmptySquare);

                        if (ownLastFlipsCount > 0)
                        {
                            eval = diffCount + 2 * (ownFlipsCount + ownLastFlipsCount) + 2;
                        }
                        else
                        {
                            //all pass
                            eval = diffCount + 2 * ownFlipsCount;
                            //TODO: eval==0?
                            if (eval >= 0)
                            {
                                eval += 2;
                            }
                        }
                    }
                }
                else
                {
                    //empties!=2
                    eval = -NoParitySearch(oppBoard, -beta, -alpha, empties - 1);
                }

                if (eval > score)
                {
                    score = eval;
                    if (eval > alpha)
                    {
                        if (eval >= beta)
                        {
                            return(score);
                        }
                        alpha = eval;
                    }
                }
            }

            return(score);
        }
예제 #2
0
 public int Eval(BitBoard board)
 {
     return(board.DiffCount());
 }