void AddFilePgn(string p) { List <string> listPgn = File.ReadAllLines(p).ToList(); string movesUci = String.Empty; Chess.SetFen(); foreach (string m in listPgn) { string cm = m.Trim(); if (String.IsNullOrEmpty(cm)) { continue; } if (cm[0] == '[') { continue; } cm = Regex.Replace(cm, @"\.(?! |$)", ". "); if (cm.StartsWith("1. ")) { AddUci(movesUci); ShowMoves(); movesUci = String.Empty; Chess.SetFen(); } string[] arrMoves = cm.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); foreach (string san in arrMoves) { if (Char.IsDigit(san[0])) { continue; } string umo = Chess.SanToUmo(san); if (umo == String.Empty) { errors++; break; } movesUci += $" {umo}"; int emo = Chess.UmoToEmo(umo); Chess.MakeMove(emo); } } AddUci(movesUci); ShowMoves(); }
static void Main(string[] args) { ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; /// <summary> /// Book can write new moves. /// </summary> bool isWritable = false; /// <summary> /// Load Before Add new moves. /// </summary> bool lba = false; int bookLimitW = 0; int bookLimitR = 0; int rnd = 0; CUci Uci = new CUci(); CBookMem Book = new CBookMem(); CChessExt Chess = CBookMem.Chess; string ax = "-bn"; List <string> listBn = new List <string>(); List <string> listEf = new List <string>(); List <string> listEa = new List <string>(); for (int n = 0; n < args.Length; n++) { string ac = args[n]; switch (ac) { case "-bn": case "-ef": case "-ea": case "-rnd": case "-lr": case "-lw": ax = ac; break; case "-w": ax = ac; isWritable = true; break; case "-lba": ax = ac; lba = true; break; default: switch (ax) { case "-bn": listBn.Add(ac); break; case "-ef": listEf.Add(ac); break; case "-ea": listEa.Add(ac); break; case "-rnd": rnd = int.TryParse(ac, out int r) ? r : 0; break; case "-w": ac = ac.Replace("K", "000").Replace("M", "000000"); Book.maxRecords = int.TryParse(ac, out int m) ? m : 0; break; case "-lr": bookLimitR = int.TryParse(ac, out int lr) ? lr : 0; break; case "-lw": bookLimitW = int.TryParse(ac, out int lw) ? lw : 0; break; } break; } } string bookName = String.Join(" ", listBn); string engineName = String.Join(" ", listEf); string arguments = String.Join(" ", listEa); Process myProcess = new Process(); if (File.Exists(engineName)) { myProcess.StartInfo.FileName = engineName; myProcess.StartInfo.WorkingDirectory = Path.GetDirectoryName(engineName); myProcess.StartInfo.UseShellExecute = false; myProcess.StartInfo.RedirectStandardInput = true; myProcess.StartInfo.Arguments = arguments; myProcess.Start(); } else { if (engineName != "") { Console.WriteLine($"info string missing engine [{engineName}]"); } engineName = ""; } if (!Book.LoadFromFile(bookName)) { Book.LoadFromFile($"{bookName}{CBookMem.defExt}"); } Console.WriteLine($"info string book {Book.recList.Count:N0} moves"); while (true) { string msg = Console.ReadLine().Trim(); if (String.IsNullOrEmpty(msg) || (msg == "help") || (msg == "book")) { Console.WriteLine("book load [filename].[mem|pgn|uci|fen] - clear and add moves from file"); Console.WriteLine("book save [filename].[mem] - save book to the file"); Console.WriteLine("book delete [number x] - delete x moves from the book"); Console.WriteLine("book addfile [filename].[mem|png|uci|fen] - add moves to the book from file"); Console.WriteLine("book adduci [uci] - add moves in uci format to the book"); Console.WriteLine("book addfen [fen] - add position in fen format"); Console.WriteLine("book clear - clear all moves from the book"); Console.WriteLine("book moves [uci] - make sequence of moves in uci format and shows possible continuations"); Console.WriteLine("book structure - show structure of current book"); continue; } Uci.SetMsg(msg); int count = Book.recList.Count; if (Uci.command == "book") { switch (Uci.tokens[1]) { case "addfen": if (Book.AddFen(Uci.GetValue(2, 0))) { Console.WriteLine("Fen have been added"); } else { Console.WriteLine("Wrong fen"); } break; case "addfile": if (!Book.AddFile(Uci.GetValue(2, 0))) { Console.WriteLine("File not found"); } else { Book.ShowMoves(true); } break; case "adduci": if (!Book.AddUci(Uci.GetValue(2, 0))) { Console.WriteLine("Wrong uci moves"); } else { Console.WriteLine($"{(Book.recList.Count - count):N0} moves have been added"); } break; case "clear": Book.Clear(); Console.WriteLine("Book is empty"); break; case "delete": int c = Book.Delete(Uci.GetInt(2)); Console.WriteLine($"{c:N0} moves was deleted"); break; case "load": if (!Book.LoadFromFile(Uci.GetValue(2, 0))) { Console.WriteLine("File not found"); } else { Book.ShowMoves(true); } break; case "moves": Book.InfoMoves(Uci.GetValue(2, 0)); break; case "structure": Book.InfoStructure(); break; case "save": if (Book.SaveToFile(Uci.GetValue(2, 0))) { Console.WriteLine("The book has been saved"); } else { Console.WriteLine("Writing to the file has failed"); } break; default: Console.WriteLine($"Unknown command [{Uci.tokens[1]}]"); break; } continue; } if ((Uci.command != "go") && (engineName != "")) { myProcess.StandardInput.WriteLine(msg); } switch (Uci.command) { case "position": string fen = Uci.GetValue("fen", "moves"); string moves = Uci.GetValue("moves", "fen"); Chess.SetFen(fen); Chess.MakeMoves(moves); if (isWritable && String.IsNullOrEmpty(fen) && Chess.Is2ToEnd(out string myMove, out string enMove)) { string[] am = moves.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); List <string> movesUci = new List <string>(); foreach (string m in am) { movesUci.Add(m); } movesUci.Add(myMove); movesUci.Add(enMove); int c = bookLimitW > 0 ? bookLimitW : movesUci.Count; List <string> l = movesUci.GetRange(0, c); string lm = String.Join(" ", l); if (lba) { Book.AddFile(bookName); } Book.AddUci(lm); Book.SaveToFile(); } break; case "go": string move = String.Empty; if ((Chess.g_moveNumber < bookLimitR) || (bookLimitR == 0)) { move = Book.GetMove(rnd); } if (move != String.Empty) { Console.WriteLine($"bestmove {move}"); } else if (engineName == "") { Console.WriteLine("enginemove"); } else { myProcess.StandardInput.WriteLine(msg); } break; } } }
static void Main(string[] args) { bool setSql = true; List <string> movesEng = new List <string>(); CChessExt Chess = new CChessExt(); CUci Uci = new CUci(); const string getMove = "getmove"; const string delMove = "deleteMove"; const string addMoves = "setEmo"; string ax = "-sc"; List <string> listSc = new List <string>(); List <string> listEf = new List <string>(); List <string> listEa = new List <string>(); for (int n = 0; n < args.Length; n++) { string ac = args[n]; switch (ac) { case "-sc": case "-ef": case "-ea": ax = ac; break; default: switch (ax) { case "-sc": listSc.Add(ac); break; case "-ef": listEf.Add(ac); break; case "-ea": listEa.Add(ac); break; } break; } } string script = String.Join(" ", listSc); string engine = String.Join(" ", listEf); string arguments = String.Join(" ", listEa); Process myProcess = new Process(); if (File.Exists(engine)) { myProcess.StartInfo.FileName = engine; myProcess.StartInfo.WorkingDirectory = Path.GetDirectoryName(engine); myProcess.StartInfo.UseShellExecute = false; myProcess.StartInfo.RedirectStandardInput = true; myProcess.StartInfo.Arguments = arguments; myProcess.Start(); } else { if (String.IsNullOrEmpty(engine)) { Console.WriteLine("info string missing engine"); } engine = String.Empty; } if (String.IsNullOrEmpty(script)) { Console.WriteLine("info string missing script"); return; } int MatToMate(sbyte mat) { if (mat >= 0) { return(128 - mat); } else { return(-129 - mat); } } string GetMov5() { NameValueCollection reqparm; if (!setSql) { return(""); } string boaS = Chess.GetBoaS(); if (!Chess.whiteTurn) { boaS = Chess.FlipVBoaS(boaS); } string boa5 = Chess.BoaSToBoa5(boaS); reqparm = new NameValueCollection { { "action", getMove }, { "boa5", boa5 } }; byte[] data; try { data = new WebClient().UploadValues(script, "POST", reqparm); } catch { setSql = false; return(""); } string response = Encoding.UTF8.GetString(data).Trim(); string[] arrRes = response.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (arrRes.Length == 0) { return(String.Empty); } string mov5 = arrRes[0]; string umo = Chess.Mov5ToUmo(mov5); if (Chess.IsValidMove(umo, out _)) { int mate = arrRes.Length > 1 ? MatToMate(Convert.ToSByte(arrRes[1])) : 0; string m = mate != 0 ? $" {mate:+#;-#}M" : String.Empty; string c = arrRes.Length > 2 ? $" ({arrRes[2]})" : String.Empty; Console.WriteLine($"info string book {umo}{m}{c}"); return(umo); } reqparm = new NameValueCollection { { "action", delMove }, { "boa5", boa5 }, { "mov5", mov5 } }; try { new WebClient().UploadValues(script, "POST", reqparm); } catch { } return(String.Empty); } while (true) { string msg = Console.ReadLine(); Uci.SetMsg(msg); if ((Uci.command != "go") && (engine != String.Empty)) { myProcess.StandardInput.WriteLine(msg); } switch (Uci.command) { case "ucinewgame": setSql = !String.IsNullOrEmpty(script); break; case "position": Chess.SetFen(); movesEng.Clear(); if (Uci.GetIndex("fen") > 0) { setSql = false; } else { int m = Uci.GetIndex("moves", Uci.tokens.Length); for (int n = m + 1; n < Uci.tokens.Length; n++) { string umo = Uci.tokens[n]; movesEng.Add(umo); Chess.MakeMove(umo, out _); } if (setSql && Chess.Is2ToEnd(out string mm, out string em)) { movesEng.Add(mm); movesEng.Add(em); var reqparm = new NameValueCollection { { "action", addMoves }, { "moves", String.Join(" ", movesEng) } }; try { new WebClient().UploadValues(script, "POST", reqparm); } catch { } setSql = false; } } break; case "go": string move = GetMov5(); if (move != "") { Console.WriteLine($"bestmove {move}"); } else if (String.IsNullOrEmpty(engine)) { Console.WriteLine("enginemove"); } else { myProcess.StandardInput.WriteLine(msg); } break; } } }
static void Main(string[] args) { CChessExt Chess = new CChessExt(); CUci Uci = new CUci(); string ax = "-rnd"; List <string> listEf = new List <string>(); List <string> listEa = new List <string>(); for (int n = 0; n < args.Length; n++) { string ac = args[n]; switch (ac) { case "-rnd": case "-ef": case "-ea": ax = ac; break; default: switch (ax) { case "-rnd": Chess.optRandom = int.Parse(ac); break; case "-ef": listEf.Add(ac); break; case "-ea": listEa.Add(ac); break; } break; } } string engine = String.Join(" ", listEf); string arguments = String.Join(" ", listEa); Process myProcess = new Process(); if (File.Exists(engine)) { myProcess.StartInfo.FileName = engine; myProcess.StartInfo.WorkingDirectory = Path.GetDirectoryName(engine); myProcess.StartInfo.UseShellExecute = false; myProcess.StartInfo.RedirectStandardInput = true; myProcess.StartInfo.Arguments = arguments; myProcess.Start(); } else { if (engine != "") { Console.WriteLine("info string missing engine"); } engine = ""; } do { string msg = Console.ReadLine(); Uci.SetMsg(msg); if ((Uci.command != "go") && (engine != "")) { myProcess.StandardInput.WriteLine(msg); } switch (Uci.command) { case "position": string fen = Uci.GetValue("fen", "moves"); string moves = Uci.GetValue("moves", "fen"); Chess.SetFen(fen); Chess.MakeMoves(moves); break; case "go": string move = Chess.GetMove(); if (move != String.Empty) { Console.WriteLine("info string book"); Console.WriteLine($"bestmove {move}"); } else if (engine == "") { Console.WriteLine("enginemove"); } else { myProcess.StandardInput.WriteLine(msg); } break; } } while (Uci.command != "quit"); }
static void Main(string[] args) { ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; /// <summary> /// Book can write new moves. /// </summary> bool isW = false; /// <summary> /// Limit ply to read. /// </summary> int bookLimitR = 32; /// <summary> /// Limit ply to write. /// </summary> int bookLimitW = 32; CUci Uci = new CUci(); CPolyglot Book = new CPolyglot(); CChessExt chess = CPolyglot.Chess; string ax = "-bf"; List <string> listBf = new List <string>(); List <string> listEf = new List <string>(); List <string> listEa = new List <string>(); for (int n = 0; n < args.Length; n++) { string ac = args[n]; switch (ac) { case "-bf": case "-ef": case "-ea": case "-lr": //limit read in half moves case "-lw": //limit write in half moves ax = ac; break; case "-w": ax = ac; isW = true; break; default: switch (ax) { case "-bf": listBf.Add(ac); break; case "-ef": listEf.Add(ac); break; case "-ea": listEa.Add(ac); break; case "-lr": bookLimitR = int.TryParse(ac, out int lr) ? lr : 0; break; case "-lw": bookLimitW = int.TryParse(ac, out int lw) ? lw : 0; break; case "-w": ac = ac.Replace("K", "000").Replace("M", "000000"); Book.maxRecords = int.TryParse(ac, out int m) ? m : 0; break; } break; } } string bookFile = String.Join(" ", listBf); string engineFile = String.Join(" ", listEf); string arguments = String.Join(" ", listEa); string ext = Path.GetExtension(bookFile); if (String.IsNullOrEmpty(ext)) { bookFile = $"{bookFile}{CPolyglot.defExt}"; } bool fileLoaded = Book.LoadFromFile(bookFile); if (fileLoaded) { Console.WriteLine($"info string book on"); } Process engineProcess = null; if (File.Exists(engineFile)) { engineProcess = new Process(); engineProcess.StartInfo.FileName = engineFile; engineProcess.StartInfo.WorkingDirectory = Path.GetDirectoryName(engineFile); engineProcess.StartInfo.UseShellExecute = false; engineProcess.StartInfo.RedirectStandardInput = true; engineProcess.StartInfo.Arguments = arguments; engineProcess.Start(); Console.WriteLine($"info string engine on"); } else { if (engineFile != String.Empty) { Console.WriteLine($"info string missing engine [{engineFile}]"); } engineFile = String.Empty; } Console.WriteLine($"info string book {Book.recList.Count:N0} moves"); do { string msg = Console.ReadLine().Trim(); if ((msg == "help") || (msg == "book")) { Console.WriteLine("book load [filename].[bin|pgn|uci] - clear and add moves from file"); Console.WriteLine("book save [filename].[bin] - save book to the file"); Console.WriteLine("book addfile [filename].[bin|pgn|uci] - add moves to the book"); Console.WriteLine("book adduci [uci] - add moves in uci format to the book"); Console.WriteLine("book clear - clear all moves from the book"); continue; } Uci.SetMsg(msg); if (Uci.command == "book") { if (Uci.tokens.Length > 1) { switch (Uci.tokens[1]) { case "addfile": string fn = Uci.GetValue(2, 0); if (File.Exists(fn)) { Book.AddFile(fn); Book.ShowMoves(true); } else { Console.WriteLine("File not found"); } break; case "adduci": string movesUci = Uci.GetValue(2, 0); Book.AddUci(movesUci); break; case "clear": Book.Clear(); break; case "load": if (!Book.LoadFromFile(Uci.GetValue(2, 0))) { Console.WriteLine("File not found"); } else { Book.ShowMoves(true); } break; case "save": Book.SaveToFile(Uci.GetValue(2, 0)); break; default: Console.WriteLine($"Unknown command [{Uci.tokens[1]}]"); break; } } continue; } if ((Uci.command != "go") && !String.IsNullOrEmpty(engineFile)) { engineProcess.StandardInput.WriteLine(msg); } switch (Uci.command) { case "position": List <string> movesUci = new List <string>(); string fen = Uci.GetValue("fen", "moves"); chess.SetFen(fen); int lo = Uci.GetIndex("moves"); if (lo++ > 0) { int hi = Uci.GetIndex("fen", Uci.tokens.Length); if (hi < lo) { hi = Uci.tokens.Length; } for (int n = lo; n < hi; n++) { string m = Uci.tokens[n]; movesUci.Add(m); chess.MakeMove(m, out _); } } if (isW && fileLoaded && String.IsNullOrEmpty(fen) && chess.Is2ToEnd(out string myMove, out string enMove)) { movesUci.Add(myMove); movesUci.Add(enMove); Book.AddUci(movesUci, bookLimitW, false); Book.SaveToFile(); } break; case "go": string move = String.Empty; if ((bookLimitR == 0) || (bookLimitR > chess.g_moveNumber)) { move = Book.GetMove(); if (!chess.IsValidMove(move, out _)) { move = String.Empty; } } if (!String.IsNullOrEmpty(move)) { Console.WriteLine($"bestmove {move}"); } else if (engineProcess == null) { Console.WriteLine("enginemove"); } else { engineProcess.StandardInput.WriteLine(msg); } break; } } while (Uci.command != "quit"); }