public void MainLoop() { PrintStartUpInfo(); Board board = new Board(); S_SearchInfo info = new S_SearchInfo(); // Infinite loop, will only break once user quits. while (true) { string stdin = null; // https://daveaglick.com/posts/capturing-standard-input-in-csharp stdin = Console.ReadLine(); if (stdin == null) { continue; } // First check length to avoid OutOfRangeException. if (stdin.Length >= 7 && stdin.Substring(0, 7).Equals("isready")) { Console.Write("readyok\n"); continue; } else if (stdin.Length >= 8 && stdin.Substring(0, 8).Equals("position")) { ParsePosition(stdin, board); } else if (stdin.Length >= 10 && stdin.Substring(0, 10).Equals("ucinewgame")) { ParsePosition("position startpos\n", board); } else if (stdin.Length >= 2 && stdin.Substring(0, 2).Equals("go")) { ParseGo(stdin, board, ref info); } else if (stdin.Length >= 4 && stdin.Substring(0, 4).Equals("quit")) { info.Quit = true; break; } else if (stdin.Length >= 3 && stdin.Substring(0, 3).Equals("uci")) { PrintStartUpInfo(); } else if (stdin.Length >= 4 && stdin.Substring(0, 4).Equals("stop")) { info.Stopped = true; } if (info.Quit) { break; } } }
public static void ReadInput(ref S_SearchInfo info) { string stdin = null; if (Console.In.Peek() != -1) { stdin = Console.ReadLine(); if (stdin != null) { if (stdin.Length >= 4 && stdin.Substring(0, 4).Equals("quit")) { info.Quit = true; } } } }
// should read "go depth int wtime int btime int binc int winc int movetime int movestogo int // etc: // "go depth 8 wtime 10000 btime 8000 binc 1000 winc 1000 movetime 1000 movestogo 50" // Tactic: // Find each variable by String.IndexOf method, and process them. public void ParseGo(string lineIn, Board board, ref S_SearchInfo info) { info.isTimeSet = false; int depth = -1, movesToGo = 30, moveTime = -1, time = -1, inc = 0, indexOf = -1; // If binc variable exists. if (((indexOf = lineIn.IndexOf("binc")) != -1) && board.Side == (int)Colour.BLACK) { indexOf += 5; var endIndex = EndOfInteger(lineIn, indexOf); inc = Int32.Parse(lineIn.Substring(indexOf, endIndex - indexOf + 1)); } // If winc variable exists. if (((indexOf = lineIn.IndexOf("winc")) != -1) && board.Side == (int)Colour.WHITE) { indexOf += 5; var endIndex = EndOfInteger(lineIn, indexOf); inc = Int32.Parse(lineIn.Substring(indexOf, endIndex - indexOf + 1)); } if (((indexOf = lineIn.IndexOf("btime")) != -1) && board.Side == (int)Colour.BLACK) { indexOf += 6; var endIndex = EndOfInteger(lineIn, indexOf); time = Int32.Parse(lineIn.Substring(indexOf, endIndex - indexOf + 1)); } if (((indexOf = lineIn.IndexOf("wtime")) != -1) && board.Side == (int)Colour.WHITE) { indexOf += 6; var endIndex = EndOfInteger(lineIn, indexOf); time = Int32.Parse(lineIn.Substring(indexOf, endIndex - indexOf + 1)); } if ((indexOf = lineIn.IndexOf("movestogo")) != -1) { indexOf += 10; var endIndex = EndOfInteger(lineIn, indexOf); movesToGo = Int32.Parse(lineIn.Substring(indexOf, endIndex - indexOf + 1)); } if ((indexOf = lineIn.IndexOf("movetime")) != -1) { indexOf += 9; var endIndex = EndOfInteger(lineIn, indexOf); moveTime = Int32.Parse(lineIn.Substring(indexOf, endIndex - indexOf + 1)); } // If depth variable exists. if ((indexOf = lineIn.IndexOf("depth")) != -1) { indexOf += 6; var endIndex = EndOfInteger(lineIn, indexOf); depth = Int32.Parse(lineIn.Substring(indexOf, endIndex - indexOf + 1)); } if (moveTime != -1) // if the movetime were set. { time = moveTime; movesToGo = 1; } info.StartTime = Variables.Watch.ElapsedMilliseconds; info.Depth = depth; if (time != -1) // if time or movetime was set. { info.isTimeSet = true; time /= movesToGo; // time per move time -= 50; // decrement by 50 to avoid overrunning. info.StopTime = info.StartTime + time + inc; } if (depth == -1) { info.Depth = Variables.MAX_DEPTH; // if no depth was specificed, we set it to max depth. } Console.Write("time:{0} start:{1} stop:{2} depth:{3} timeset:{4}\n", time, info.StartTime, info.StopTime, info.Depth, info.isTimeSet); board.Searcher.SearchPosition(ref info); }