BenchmarksGenerator.BenchmarkGenerator.GenerateSituation C# (CSharp) Method

GenerateSituation() public static method

public static GenerateSituation ( int depth ) : List
depth int
return List
        public static List<Move> GenerateSituation(int depth)
        {
            int curr_depth = 0;
            Color myColor = Color.White;
            PlayerPosition myPosition = PlayerPosition.Down;

            GameProvider provider = new GameProvider(new MovesArrayAllocator());

            Color color = myColor;
            Random rand = new Random(DateTime.Now.Millisecond);

            while (curr_depth < depth)
            {
                var player = provider.PlayerBoards[(int)color];
                var opponent = provider.PlayerBoards[1 - (int)color];

                var lastMove = new Move(Square.A1, Square.A1);
                if (provider.History.HasItems())
                    lastMove = provider.History.GetLastMove();

                var moves = player.GetMoves(
                    opponent,
                    lastMove,
                    MovesMask.AllMoves);
                provider.FilterMoves(moves, color);

                if (moves.Size == 0)
                {
                    // some checkmate found
                    break;
                }
                int index = rand.Next(moves.Size);
                // just get random move
                var move = new Move(moves.InnerArray[index]);

                provider.ProcessMove(move, color);

                bool needsPromotion = (int)move.Type >= (int)MoveType.Promotion;
                if (needsPromotion)
                    provider.PromotePawn(
                        color,
                        move.To,
                        move.Type.GetPromotionFigure());

                color = (Queem.Core.Color)(1 - (int)color);
                curr_depth += 1;

                provider.Allocator.ReleaseLast();
            }

            return provider.History.Moves;
        }

Usage Example

Example #1
0
        static void Main(string[] args)
        {
            string help = @"
Usage: BenchmarkGenerator [-h|--help] [-v|--verbose] test_count depth results_path

     -h, --help : display this help
  -v, --verbose : be verbose
    tests_count : number of tests to generate
          depth : number of half-moves of each player 
                  to generate in each test
   results_path : path to folder where to save benchmarks
                  (if folder does not exist, 
                   it will be created)
";

            // will fit both -h and --help
            if (args.Any((s) => s.Contains("-h")) ||
                (args.Length == 0))
            {
                Console.WriteLine(help);
                return;
            }

            bool verbose = false;

            int index = 0;

            if (args[0].Contains("-v"))
            {
                verbose = true;
                index  += 1;
            }

            string testsCountStr = args[index];
            string depthStr      = args[index + 1];
            string path          = args[index + 2];

            int depth = 0;

            int.TryParse(depthStr, out depth);

            if (depth <= 0)
            {
                Console.WriteLine("<depth> needs to be a positive integer.");
                return;
            }

            if (!Directory.Exists(path))
            {
                // create specified path with subdirs
                Directory.CreateDirectory(path);
                Console.WriteLine("Tests directory created");
            }

            int testsCount = 0;

            int.TryParse(testsCountStr, out testsCount);
            int testNumber = 1;

            while (testNumber <= testsCount)
            {
                var           moves = BenchmarkGenerator.GenerateSituation(depth);
                StringBuilder sb    = new StringBuilder();
                for (int i = 0; i < moves.Count; ++i)
                {
                    sb.AppendLine(moves[i].ToString());
                }

                string filePath = path;
                if (!filePath.EndsWith(Path.DirectorySeparatorChar.ToString()))
                {
                    filePath += Path.DirectorySeparatorChar;
                }

                filePath += string.Format("test{0}_{1}.cbi",
                                          testNumber,
                                          DateTime.Now.Millisecond//ToString("dd_MM_yyyy")
                                          );

                File.WriteAllText(filePath, sb.ToString());

                if (verbose)
                {
                    Console.WriteLine(
                        string.Format("Test {0} of {1}...",
                                      testNumber,
                                      testsCount)
                        );
                }

                testNumber += 1;
            }
        }
BenchmarkGenerator