Ejemplo n.º 1
0
 public SimpleQSearch(IEvaluator evaluator, IMoveGenerator moveGenerator, CompositeMoveOrderer quiescenceMoveOrderer, CompositeMoveOrderer checkEvasionsMoveOrderer)
 {
     _evaluator                = evaluator;
     _moveGenerator            = moveGenerator;
     _quiescenceMoveOrderer    = quiescenceMoveOrderer;
     _checkEvasionsMoveOrderer = checkEvasionsMoveOrderer;
 }
Ejemplo n.º 2
0
 public MoveHandler(IMoveGenerator moveGenerator, IUserMoveReader userMoveReader, IMoveValidator moveValidator, IBoardHandler boardHandler)
 {
     _moveGenerator  = moveGenerator;
     _userMoveReader = userMoveReader;
     _moveValidator  = moveValidator;
     _boardHandler   = boardHandler;
 }
Ejemplo n.º 3
0
        public IMoveGenerator Build(PieceType type)
        {
            IMoveGenerator generator = null;

            switch (type)
            {
            case PieceType.Bishop:
                generator = new BishopMoveGenerator(gameState, pregeneratedAttacks);
                break;

            case PieceType.Rook:
                generator = new RookMoveGenerator(gameState, pregeneratedAttacks);
                break;

            case PieceType.Knight:
                generator = new KnightMoveGenerator(gameState, pregeneratedAttacks);
                break;

            case PieceType.Pawn:
                generator = new PawnMoveGenerator(gameState, pregeneratedAttacks);
                break;

            case PieceType.Queen:
                generator = new QueenMoveGenerator(gameState, pregeneratedAttacks);
                break;

            case PieceType.King:
                generator = new KingMoveGenerator(gameState, pregeneratedAttacks);
                break;
            }
            return(generator);
        }
        private void UpdateMoveOperators()
        {
            IMoveGenerator oldMoveGenerator = MoveGenerator;
            IMoveMaker     oldMoveMaker     = MoveMaker;
            ISingleObjectiveMoveEvaluator oldMoveEvaluator = MoveEvaluator;

            ClearMoveParameters();

            if (problem != null)
            {
                foreach (IMultiMoveGenerator generator in problem.Operators.OfType <IMultiMoveGenerator>().OrderBy(x => x.Name))
                {
                    MoveGeneratorParameter.ValidValues.Add(generator);
                }
                foreach (IExhaustiveMoveGenerator generator in problem.Operators.OfType <IExhaustiveMoveGenerator>().OrderBy(x => x.Name))
                {
                    MoveGeneratorParameter.ValidValues.Add(generator);
                }

                if (oldMoveGenerator != null)
                {
                    IMoveGenerator newMoveGenerator = MoveGeneratorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMoveGenerator.GetType());
                    if (newMoveGenerator != null)
                    {
                        MoveGenerator = newMoveGenerator;
                    }
                }

                ChooseMoveOperators(oldMoveMaker, oldMoveEvaluator);
            }
        }
Ejemplo n.º 5
0
 public IidAlphaBetaSearch(IMoveGenerator moveGenerator, IEvaluator evaluator, IQSearch qSearch, CompositeMoveOrderer moveOrderer)
 {
     _moveGenerator = moveGenerator;
     _evaluator     = evaluator;
     _qSearch       = qSearch;
     _moveOrderer   = moveOrderer;
 }
Ejemplo n.º 6
0
 public MoveHandler(IMoveGenerator moveGenerator, IUserMoveReader userMoveReader, IMoveValidator moveValidator, IBoardHandler boardHandler)
 {
     _moveGenerator = moveGenerator;
     _userMoveReader = userMoveReader;
     _moveValidator = moveValidator;
     _boardHandler = boardHandler;
 }
Ejemplo n.º 7
0
        private void UpdateMoveGenerator()
        {
            IMoveGenerator oldMoveGenerator     = MoveGenerator;
            IMoveGenerator defaultMoveGenerator = Problem.Operators.OfType <IMoveGenerator>().FirstOrDefault();

            MoveGeneratorParameter.ValidValues.Clear();
            if (Problem != null)
            {
                foreach (IMoveGenerator generator in Problem.Operators.OfType <IMoveGenerator>().OrderBy(x => x.Name))
                {
                    MoveGeneratorParameter.ValidValues.Add(generator);
                }
            }
            if (oldMoveGenerator != null)
            {
                IMoveGenerator newMoveGenerator = MoveGeneratorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMoveGenerator.GetType());
                if (newMoveGenerator != null)
                {
                    MoveGenerator = newMoveGenerator;
                }
                else
                {
                    oldMoveGenerator = null;
                }
            }
            if (oldMoveGenerator == null && defaultMoveGenerator != null)
            {
                MoveGenerator = defaultMoveGenerator;
            }
            if (MoveGenerator == null)
            {
                ClearMoveParameters();
            }
        }
Ejemplo n.º 8
0
 public SimpleUci(IMoveGenerator moveGenerator, ISearch search, TextReader input, TextWriter output)
 {
     _position           = BoardParsing.PositionFromFen(InitialFen);
     _moveGenerator      = moveGenerator;
     _search             = search;
     _timeStrategy       = new DefaultTimeStrategy();
     _input              = input;
     _output             = output;
     _searchWorkerThread = new SearchWorkerThread();
 }
Ejemplo n.º 9
0
 private static IEnumerable <T> GetCompatibleOperators <T>(IMoveGenerator generator, IEnumerable <IOperator> operators) where T : IOperator
 {
     foreach (Type type in GetMostSpecificMoveOperatorTypes(generator))
     {
         foreach (T op in operators.Where(x => type.IsAssignableFrom(x.GetType())).OfType <T>())
         {
             yield return(op);
         }
     }
 }
Ejemplo n.º 10
0
 public static Move GetMoveFromCoordinateString(IMoveGenerator moveGenerator, Position b, string coordinateString)
 {
     if (TryGetMoveFromCoordinateString(moveGenerator, b, coordinateString, out Move move))
     {
         return(move);
     }
     else
     {
         throw new Exception($"Could not find move: \"{coordinateString}\"");
     }
 }
Ejemplo n.º 11
0
 public Perft(IMoveGenerator moveGenerator, int?tableSize = null)
 {
     _moveGenerator = moveGenerator;
     if (tableSize != null)
     {
         _perftTable = new PerftTable(tableSize.Value);
     }
     else
     {
         _perftTable = null;
     }
 }
Ejemplo n.º 12
0
        private static List <Type> GetMostSpecificMoveOperatorTypes(IMoveGenerator generator)
        {
            List <Type> moveTypes = generator.GetType().GetInterfaces().Where(x => typeof(IMoveOperator).IsAssignableFrom(x)).ToList();

            foreach (Type type in moveTypes.ToList())
            {
                if (moveTypes.Any(t => t != type && type.IsAssignableFrom(t)))
                {
                    moveTypes.Remove(type);
                }
            }
            return(moveTypes);
        }
Ejemplo n.º 13
0
        private void UpdateMoveGenerator()
        {
            IMoveGenerator oldMoveGenerator     = MoveGenerator;
            IMoveGenerator defaultMoveGenerator = Problem.Operators.OfType <IMoveGenerator>().FirstOrDefault();

            MoveGeneratorParameter.ValidValues.Clear();
            if (Problem != null)
            {
                // only add move generators that also have a corresponding tabu-checker and tabu-maker
                foreach (IMoveGenerator generator in Problem.Operators.OfType <IMoveGenerator>().OrderBy(x => x.Name))
                {
                    // get all interfaces equal to or derived from IMoveOperator that this move generator implements
                    var moveTypes = generator.GetType().GetInterfaces().Where(x => typeof(IMoveOperator).IsAssignableFrom(x)).ToList();
                    // keep only the most specific interfaces (e.g. IPermutationTranslocationMoveOperator);
                    // by removing all interfaces for which a derived interface is also contained in the moveTypes set
                    foreach (var type in moveTypes.ToList())
                    {
                        if (moveTypes.Any(t => t != type && type.IsAssignableFrom(t)))
                        {
                            moveTypes.Remove(type);
                        }
                    }
                    // keep move generator only if there is a tabu checker and a tabu maker that is derived from the same move interface
                    if (Problem.Operators.OfType <ITabuChecker>().Any(op => moveTypes.Any(m => m.IsInstanceOfType(op))) &&
                        Problem.Operators.OfType <ITabuMaker>().Any(op => moveTypes.Any(m => m.IsInstanceOfType(op))))
                    {
                        MoveGeneratorParameter.ValidValues.Add(generator);
                    }
                }
            }
            if (oldMoveGenerator != null && MoveGeneratorParameter.ValidValues.Any(x => x.GetType() == oldMoveGenerator.GetType()))
            {
                MoveGenerator = MoveGeneratorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMoveGenerator.GetType());
            }
            if (MoveGenerator == null && defaultMoveGenerator != null)
            {
                MoveGenerator = defaultMoveGenerator;
            }

            if (MoveGenerator == null)
            {
                ClearMoveParameters();
            }
        }
        private void ChooseMoveOperators(IMoveMaker oldMoveMaker = null, ISingleObjectiveMoveEvaluator oldMoveEvaluator = null)
        {
            if (oldMoveMaker == null)
            {
                oldMoveMaker = MoveMaker;
            }
            if (oldMoveEvaluator == null)
            {
                oldMoveEvaluator = MoveEvaluator;
            }
            MoveMakerParameter.ValidValues.Clear();
            MoveEvaluatorParameter.ValidValues.Clear();

            if (MoveGenerator != null && Problem != null)
            {
                IMoveGenerator generator = MoveGeneratorParameter.Value;
                foreach (IMoveMaker moveMaker in MoveHelper.GetCompatibleMoveMakers(generator, Problem.Operators.OfType <IOperator>()).OrderBy(x => x.Name))
                {
                    MoveMakerParameter.ValidValues.Add(moveMaker);
                }
                foreach (ISingleObjectiveMoveEvaluator moveEvaluator in MoveHelper.GetCompatibleSingleObjectiveMoveEvaluators(generator, Problem.Operators.OfType <IOperator>()).OrderBy(x => x.Name))
                {
                    MoveEvaluatorParameter.ValidValues.Add(moveEvaluator);
                }

                if (oldMoveMaker != null)
                {
                    IMoveMaker mm = MoveMakerParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMoveMaker.GetType());
                    if (mm != null)
                    {
                        MoveMaker = mm;
                    }
                }
                if (oldMoveEvaluator != null)
                {
                    ISingleObjectiveMoveEvaluator me = MoveEvaluatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMoveEvaluator.GetType());
                    if (me != null)
                    {
                        MoveEvaluator = me;
                    }
                }
            }
        }
Ejemplo n.º 15
0
        public NormalGameMode()
        {
            AttackMapGenerator = new AttackMapGenerator();
            MoveGenerator      = new MoveGenerator();
            PiecePlacement     = new Dictionary <Vector2Int, Tuple <Type, PlayerColor> >
            {
                { new Vector2Int(0, 0), new Tuple <Type, PlayerColor>(typeof(Rook), PlayerColor.White) },
                { new Vector2Int(1, 0), new Tuple <Type, PlayerColor>(typeof(Knight), PlayerColor.White) },
                { new Vector2Int(2, 0), new Tuple <Type, PlayerColor>(typeof(Bishop), PlayerColor.White) },
                { new Vector2Int(3, 0), new Tuple <Type, PlayerColor>(typeof(Queen), PlayerColor.White) },
                { new Vector2Int(4, 0), new Tuple <Type, PlayerColor>(typeof(King), PlayerColor.White) },
                { new Vector2Int(5, 0), new Tuple <Type, PlayerColor>(typeof(Bishop), PlayerColor.White) },
                { new Vector2Int(6, 0), new Tuple <Type, PlayerColor>(typeof(Knight), PlayerColor.White) },
                { new Vector2Int(7, 0), new Tuple <Type, PlayerColor>(typeof(Rook), PlayerColor.White) },
                { new Vector2Int(0, 1), new Tuple <Type, PlayerColor>(typeof(Pawn), PlayerColor.White) },
                { new Vector2Int(1, 1), new Tuple <Type, PlayerColor>(typeof(Pawn), PlayerColor.White) },
                { new Vector2Int(2, 1), new Tuple <Type, PlayerColor>(typeof(Pawn), PlayerColor.White) },
                { new Vector2Int(3, 1), new Tuple <Type, PlayerColor>(typeof(Pawn), PlayerColor.White) },
                { new Vector2Int(4, 1), new Tuple <Type, PlayerColor>(typeof(Pawn), PlayerColor.White) },
                { new Vector2Int(5, 1), new Tuple <Type, PlayerColor>(typeof(Pawn), PlayerColor.White) },
                { new Vector2Int(6, 1), new Tuple <Type, PlayerColor>(typeof(Pawn), PlayerColor.White) },
                { new Vector2Int(7, 1), new Tuple <Type, PlayerColor>(typeof(Pawn), PlayerColor.White) },

                { new Vector2Int(0, 7), new Tuple <Type, PlayerColor>(typeof(Rook), PlayerColor.Black) },
                { new Vector2Int(1, 7), new Tuple <Type, PlayerColor>(typeof(Knight), PlayerColor.Black) },
                { new Vector2Int(2, 7), new Tuple <Type, PlayerColor>(typeof(Bishop), PlayerColor.Black) },
                { new Vector2Int(3, 7), new Tuple <Type, PlayerColor>(typeof(Queen), PlayerColor.Black) },
                { new Vector2Int(4, 7), new Tuple <Type, PlayerColor>(typeof(King), PlayerColor.Black) },
                { new Vector2Int(5, 7), new Tuple <Type, PlayerColor>(typeof(Bishop), PlayerColor.Black) },
                { new Vector2Int(6, 7), new Tuple <Type, PlayerColor>(typeof(Knight), PlayerColor.Black) },
                { new Vector2Int(7, 7), new Tuple <Type, PlayerColor>(typeof(Rook), PlayerColor.Black) },
                { new Vector2Int(0, 6), new Tuple <Type, PlayerColor>(typeof(Pawn), PlayerColor.Black) },
                { new Vector2Int(1, 6), new Tuple <Type, PlayerColor>(typeof(Pawn), PlayerColor.Black) },
                { new Vector2Int(2, 6), new Tuple <Type, PlayerColor>(typeof(Pawn), PlayerColor.Black) },
                { new Vector2Int(3, 6), new Tuple <Type, PlayerColor>(typeof(Pawn), PlayerColor.Black) },
                { new Vector2Int(4, 6), new Tuple <Type, PlayerColor>(typeof(Pawn), PlayerColor.Black) },
                { new Vector2Int(5, 6), new Tuple <Type, PlayerColor>(typeof(Pawn), PlayerColor.Black) },
                { new Vector2Int(6, 6), new Tuple <Type, PlayerColor>(typeof(Pawn), PlayerColor.Black) },
                { new Vector2Int(7, 6), new Tuple <Type, PlayerColor>(typeof(Pawn), PlayerColor.Black) },
            };
        }
Ejemplo n.º 16
0
        public static bool TryGetMoveFromCoordinateString(
            IMoveGenerator moveGenerator,
            Position b,
            string coordinateString,
            out Move move)
        {
            List <Move> moves = new List <Move>();

            moveGenerator.Generate(moves, b);

            foreach (var tmpMove in moves)
            {
                var potentialCoordinateString = CoordinateStringFromMove(tmpMove);
                if (!string.Equals(potentialCoordinateString, coordinateString, StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }

                // if a pseudolegal move generator, then we need to make sure that the move we're attempting is even legal
                if (!moveGenerator.OnlyLegalMoves)
                {
                    var testingBoard = Position.MakeMove(new Position(), tmpMove, b);

                    // if we moved into check, clearly it was an invalid move
                    if (testingBoard.MovedIntoCheck())
                    {
                        continue;
                    }
                }

                move = tmpMove;
                return(true);
            }

            move = default; // in practice, this is fine, because callers should never use this if returning false
            return(false);
        }
Ejemplo n.º 17
0
 public SimpleAlphaBetaSearch(IMoveGenerator moveGenerator, IEvaluator evaluator, IQSearch qSearch)
 {
     _moveGenerator = moveGenerator;
     _evaluator     = evaluator;
     _qSearch       = qSearch;
 }
Ejemplo n.º 18
0
 public MoveManager( IMoveGenerator moveGenerator )
 {
     _moveGenerator = moveGenerator;
     GameSequence = new List<GameTile>();
     LastMoveIndex = 0;
 }
Ejemplo n.º 19
0
 public static IEnumerable <ITabuMaker> GetCompatibleTabuMakers(IMoveGenerator generator, IEnumerable <IOperator> operators)
 {
     return(GetCompatibleOperators <ITabuMaker>(generator, operators));
 }
Ejemplo n.º 20
0
 public static IEnumerable <ISingleObjectiveMoveEvaluator> GetCompatibleSingleObjectiveMoveEvaluators(IMoveGenerator generator, IEnumerable <IOperator> operators)
 {
     return(GetCompatibleOperators <ISingleObjectiveMoveEvaluator>(generator, operators));
 }
Ejemplo n.º 21
0
 public ChessRunTime(IEvaluator evaluator, IMoveGenerator moveGenerator)
 {
     _evaluator     = evaluator;
     _moveGenerator = moveGenerator;
 }
Ejemplo n.º 22
0
 public Evaluator(IRuleSet knowledge, IMoveGenerator moveGenerator)
 {
     _knowledge     = knowledge;
     _moveGenerator = moveGenerator;
 }