Пример #1
0
 public bool AddFileBin(string path)
 {
     try
     {
         using (FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read))
         {
             using (BinaryReader reader = new BinaryReader(fs))
             {
                 while (reader.BaseStream.Position != reader.BaseStream.Length)
                 {
                     CRec rec = new CRec
                     {
                         hash   = ReadUInt64(reader),
                         move   = ReadUInt16(reader),
                         weight = ReadUInt16(reader),
                         learn  = ReadUInt32(reader)
                     };
                     recList.Add(rec);
                 }
             }
         }
     }
     catch
     {
         return(false);
     }
     ShowCountMoves();
     return(true);
 }
Пример #2
0
        public string GetMove(int rnd)
        {
            CEmoList emoList = GetEmoList();

            if (emoList.Count == 0)
            {
                return(String.Empty);
            }
            CRec rec = new CRec();

            rec.hash = GetHash();
            int mat = -emoList[0].mat;

            if (mat > 0)
            {
                mat--;
            }
            rec.mat = (sbyte)mat;
            recList.RecUpdate(rec);
            CEmo   bst  = emoList.GetRnd(rnd);
            string umo  = Chess.EmoToUmo(bst.emo);
            int    mate = MatToMate(bst.mat);

            Console.WriteLine($"info string book {umo} {mate:+#;-#;0} ({emoList.Count})");
            return(umo);
        }
Пример #3
0
        public CEmoList GetEmoList()
        {
            CEmoList   emoList = new CEmoList();
            List <int> moves   = Chess.GenerateValidMoves(out _, true);

            foreach (int m in moves)
            {
                Chess.MakeMove(m);
                ulong hash = GetHash();
                CRec  rec  = recList.GetRec(hash);
                if (rec != null)
                {
                    CEmo emo = new CEmo
                    {
                        emo = m,
                        mat = rec.mat,
                        age = rec.age
                    };
                    emoList.Add(emo);
                }
                Chess.UnmakeMove(m);
            }
            emoList.SortMat();
            return(emoList);
        }
Пример #4
0
 public bool AddFen(string fen)
 {
     if (Chess.SetFen(fen))
     {
         CRec rec = new CRec
         {
             hash = GetHash()
         };
         recList.AddRec(rec);
         return(true);
     }
     return(false);
 }
Пример #5
0
        public void SaveToFile(string path, int reduction = 0)
        {
            if ((maxRecords > 0) && (recList.Count > maxRecords))
            {
                Delete(recList.Count - maxRecords);
            }
            bool r = false;

            try
            {
                using (FileStream fs = File.Open(path, FileMode.Create, FileAccess.Write))
                {
                    using (BinaryWriter writer = new BinaryWriter(fs))
                    {
                        CRec last = new CRec();
                        recList.SortHash();
                        foreach (CRec rec in recList)
                        {
                            rec.weight >>= reduction;
                            if (rec.weight == 0)
                            {
                                continue;
                            }
                            if ((rec.hash == last.hash) && (rec.move == last.move))
                            {
                                int weight = rec.weight + last.weight;
                                if (weight > 0xffff)
                                {
                                    r      = true;
                                    weight = 0xffff;
                                }
                                fs.Position = fs.Length - 16;
                                rec.weight  = (ushort)weight;
                            }
                            WriteUInt64(writer, rec.hash);
                            WriteUInt16(writer, rec.move);
                            WriteUInt16(writer, rec.weight);
                            WriteUInt32(writer, rec.learn);
                            last = rec;
                        }
                    }
                }
            }
            catch
            {
            }
            if (r)
            {
                SaveToFile(path, 1);
            }
        }
Пример #6
0
        List <CRec> GetRecList(ulong hash)
        {
            int         first = recList.FindHash(hash);
            List <CRec> rl    = new List <CRec>();

            for (int n = first; n < recList.Count; n++)
            {
                CRec r = recList[n];
                if (r.hash == hash)
                {
                    rl.Add(r);
                }
                if (r.hash > hash)
                {
                    break;
                }
            }
            return(rl);
        }
Пример #7
0
        public bool AddUci(string[] moves)
        {
            bool update = true;
            int  count  = moves.Length;

            Chess.SetFen();
            if (!Chess.MakeMoves(moves))
            {
                return(false);
            }
            CGameState gs = Chess.GetGameState();

            Chess.SetFen();
            for (int n = 0; n < moves.Length; n++)
            {
                string m = moves[n];
                Chess.MakeMove(m, out _);
                CRec rec = new CRec();
                rec.hash = GetHash();
                if (gs == CGameState.mate)
                {
                    int mate = GetMate(n, count);
                    rec.mat = MateToMat(mate);
                    if (mate > 0)
                    {
                        recList.AddRec(rec);
                    }
                    else if (update)
                    {
                        update = recList.RecUpdate(rec);
                    }
                }
                else
                {
                    recList.AddHash(rec);
                }
            }
            return(true);
        }
Пример #8
0
        bool AddFileMem(string p)
        {
            FileStream fs;

            try
            {
                fs = File.Open(p, FileMode.Open, FileAccess.Read, FileShare.Read);
            }
            catch
            {
                return(false);
            }
            if (fs != null)
            {
                using (BinaryReader reader = new BinaryReader(fs))
                {
                    string header = GetHeader();
                    if (reader.ReadString() != header)
                    {
                        Console.WriteLine($"This program only supports version  [{header}]");
                    }
                    else
                    {
                        while (reader.BaseStream.Position != reader.BaseStream.Length)
                        {
                            CRec rec = new CRec
                            {
                                hash = reader.ReadUInt64(),
                                mat  = reader.ReadSByte(),
                                age  = reader.ReadByte()
                            };
                            recList.Add(rec);
                        }
                        return(true);
                    }
                }
            }
            return(false);
        }
Пример #9
0
        public void AddUci(string[] moves, int limit = 0, bool all = true)
        {
            int count = moves.Length;

            Chess.SetFen();
            for (int n = 0; n < moves.Length; n++)
            {
                if ((limit > 0) && (limit <= n))
                {
                    break;
                }
                string umo = moves[n];
                if (IsWinner(n, count) || all)
                {
                    CRec rec = new CRec
                    {
                        hash = GetHash(),
                        move = UmoToBmo(umo)
                    };
                    recList.Add(rec);
                }
                Chess.MakeMove(umo, out _);
            }
        }