コード例 #1
0
ファイル: ConfigManager.cs プロジェクト: Spongie/Chess
        public static ConcurrentDictionary <EvalCacheKey, float> LoadCache(IBoardEvaluator boardEvaluator, int depth, Color color)
        {
            string directory = $"BotConfigs\\Cache\\";

            CreateDirectoryIfNotExists(directory);

            if (boardEvaluator == null || !File.Exists(directory + boardEvaluator.GetBotId(depth) + ".cache"))
            {
                return(new ConcurrentDictionary <EvalCacheKey, float>());
            }

            var xmlSerializer = new XmlSerializer(typeof(List <CacheFileEntry>));
            var cacheList     = (List <CacheFileEntry>)xmlSerializer.Deserialize(File.OpenRead(directory + boardEvaluator.GetBotId(depth) + ".cache"));

            var cache = new ConcurrentDictionary <EvalCacheKey, float>();

            foreach (var cacheFileEntry in cacheList)
            {
                cache.TryAdd(cacheFileEntry.CacheKey, cacheFileEntry.Score);
            }

            return(cache);

            //return
            //    JsonConvert.DeserializeObject<ConcurrentDictionary<EvalCacheKey, float>>(
            //        File.ReadAllText(directory + boardEvaluator.GetBotId(depth) + ".cache"));
        }
コード例 #2
0
ファイル: ConfigManager.cs プロジェクト: Spongie/Chess
        public static void SaveCache(IBoardEvaluator boardEvaluator, ConcurrentDictionary <EvalCacheKey, float> cache, int depth, Color color)
        {
            string directory = $"BotConfigs\\Cache\\";

            CreateDirectoryIfNotExists(directory);

            var cacheList = cache.Select(s => new CacheFileEntry {
                CacheKey = s.Key, Score = s.Value
            }).ToList();

            var xmlSerializer = new XmlSerializer(typeof(List <CacheFileEntry>));

            using (var stream = new FileStream(directory + boardEvaluator.GetBotId(depth) + ".cache", FileMode.Create))
            {
                xmlSerializer.Serialize(stream, cacheList);
            }
            //File.WriteAllText(directory + boardEvaluator.GetBotId(depth) + ".cache", JsonConvert.SerializeObject(cache));
        }