static void Main(string[] args) { Process.GetCurrentProcess().ProcessorAffinity = new IntPtr(2); Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High; Thread.CurrentThread.Priority = ThreadPriority.Highest; boardStruct brett = new boardStruct(); movelist list = new movelist(); suchInfo info = new suchInfo(); init.AllInit(ref brett); bool loop = true; while (loop) { loop = false; Console.Write("\nGeben sie \"uci\" für den uci modus und \"vwa\" für den Konsolen modus ein: "); string input = Console.ReadLine(); if (input.Length > 2 && string.Equals(input.Substring(0, 3), "uci", StringComparison.CurrentCultureIgnoreCase)) { uci.Uci_Loop(ref brett, ref info); } if (input.Length > 2 && string.Equals(input.Substring(0, 3), "vwa", StringComparison.CurrentCultureIgnoreCase)) { Console.Clear(); xboard.Console_Loop(ref brett, ref info); } else { loop = true; } } }
static void ClearForSearch(ref boardStruct brett, ref suchInfo info) { int index = 0; int index2 = 0; for (index = 0; index < 13; ++index) { for (index2 = 0; index2 < defs.BRD_SQ_NUM; ++index2) { brett.searchHistory[index, index2] = 0; } } for (index = 0; index < 2; ++index) { for (index2 = 0; index2 < defs.MAXTIEFE; ++index2) { brett.searchKillers[index, index2] = 0; } } brett.PvTable.overWrite = 0; brett.PvTable.hit = 0; brett.PvTable.cut = 0; brett.ply = 0; PvTable.ClearPvTable(ref brett.PvTable); info.stopped = false; info.nodes = 0; info.fh = 0; info.fhf = 0; }
static void CheckUp(ref suchInfo info) { if (info.timeset && info.timer.ElapsedMilliseconds > info.stoptime) { info.timer.Stop(); info.stopped = true; } }
public static void ParseGo(string input, ref suchInfo info, ref boardStruct brett) { input += " "; int depth = -1, movestogo = 30, movetime = -1; int time = -1, inc = 0; info.timeset = false; int pointer = 0; int pointer2 = 0; pointer = input.IndexOf("infinite"); if (pointer != -1) { ; } pointer = input.IndexOf("binc"); if (pointer != -1 && brett.seite == (int)defs.Farben.SCHWARZ) { pointer2 = input.Substring(pointer + 5).IndexOf(" "); inc = Convert.ToInt32(input.Substring(pointer + 5, pointer2)); } pointer = input.IndexOf("winc"); if (pointer != -1 && brett.seite == (int)defs.Farben.WEISS) { pointer2 = input.Substring(pointer + 5).IndexOf(" "); inc = Convert.ToInt32(input.Substring(pointer + 5, pointer2)); } pointer = input.IndexOf("wtime"); if (pointer != -1 && brett.seite == (int)defs.Farben.WEISS) { pointer2 = input.Substring(pointer + 6).IndexOf(" "); time = Convert.ToInt32(input.Substring(pointer + 6, pointer2)); } pointer = input.IndexOf("btime"); if (pointer != -1 && brett.seite == (int)defs.Farben.SCHWARZ) { pointer2 = input.Substring(pointer + 6).IndexOf(" "); time = Convert.ToInt32(input.Substring(pointer + 6, pointer2)); } pointer = input.IndexOf("movestogo"); if (pointer != -1) { pointer2 = input.Substring(pointer + 10).IndexOf(" "); movestogo = Convert.ToInt32(input.Substring(pointer + 10, pointer2)); } pointer = input.IndexOf("movetime"); if (pointer != -1) { pointer2 = input.Substring(pointer + 9).IndexOf(" "); movetime = Convert.ToInt32(input.Substring(pointer + 9, pointer2)); } pointer = input.IndexOf("depth"); if (pointer != -1) { pointer2 = input.Substring(pointer + 6).IndexOf(" "); depth = Convert.ToInt32(input.Substring(pointer + 6, pointer2)); } if (movetime != -1) { time = movetime; movestogo = 1; } info.depth = depth; if (time != -1) { info.timer.Reset(); info.timer.Start(); info.timeset = true; time /= movestogo; time -= 50; info.stoptime = time + inc; } if (depth == -1) { info.depth = defs.MAXTIEFE; } if (info.timer.IsRunning) { Console.WriteLine(String.Format("timer: Running stop:{0} tiefe:{1} timeset:{2}\n", info.stoptime, info.depth, info.timeset)); } else { Console.WriteLine(String.Format("timer: Not Running stop:{0} tiefe:{1} timeset:{2}\n", info.stoptime, info.depth, info.timeset)); } search.SearchPosition(ref brett, ref info); }
public static void Uci_Loop(ref boardStruct brett, ref suchInfo info) { info.GAME_MODE = (int)defs.Engine.UCIMODE; //setbuf(stdin, NULL); //setbuf(stdout, NULL); //char line[INPUTBUFFER]; Console.Write("id name VWA\n"); Console.Write("id author Anton Lang\n"); Console.Write("option name Hash type spin default 64 min 4 max {0}\n", defs.MaxHash); Console.Write("option name Book type check default true\n"); Console.Write("uciok\n"); quitsearch = true; TextReader a = Console.In; while (true) { string input = Console.ReadLine(); if (string.IsNullOrEmpty(input)) { continue; } if (input[0] == '\n') { continue; } if (input.Length > 6 && input.Substring(0, 7) == "isready") { Console.WriteLine("readyok"); continue; } else if (input.Length > 7 && input.Substring(0, 8) == "position") { ParsePosition(input, ref brett); } else if (input.Length > 9 && input.Substring(0, 10) == "ucinewgame") { ParsePosition("position startpos\n", ref brett); } else if (input.Length > 1 && input.Substring(0, 2) == "go") { Console.WriteLine("Main thread is: {0}", Thread.CurrentThread.ManagedThreadId); ParseGo(input, ref info, ref brett); } else if (input.Length > 3 && input.Substring(0, 4) == "quit") { Console.WriteLine("Seen Quit"); info.quit = true; break; } else if (input.Length > 2 && input.Substring(0, 3) == "uci") { Console.Write("id name VWA\n"); Console.Write("id author Anton Lang\n"); Console.Write("uciok\n"); } else if (input.Length > 4 && input.Substring(0, 5) == "debug") { //DebugAnalysisTest(pos, info); break; } else if (input.Length > 25 && input.Substring(0, 26) == "setoption name Hash value ") { /*sscanf(line, "%*s %*s %*s %*s %d", &MB); * if (MB < 4) MB = 4; * if (MB > MAX_HASH) MB = MAX_HASH; * printf("Set Hash to %d MB\n", MB); * InitHashTable(pos->HashTable, MB);*/ } else if (input.Length > 25 && input.Substring(0, 26) == "setoption name Book value ") { int index = input.IndexOf("true"); if (index != -1) { options.UseBook = true; } else { options.UseBook = false; } } if (info.quit) { break; } } }
public static void Console_Loop(ref boardStruct brett, ref suchInfo info) { Console.WriteLine("Welcome to Vwa In Console Mode!\n"); Console.WriteLine("Type help for commands\n"); info.GAME_MODE = (int)defs.Engine.CONSOLEMODE; info.POST_THINKING = true; int depth = defs.MAXTIEFE, movetime = 3000; int engineSide = (int)defs.Farben.BEIDE; int move = defs.NOMOVE; engineSide = (int)defs.Farben.SCHWARZ; board.ParseFen(defs.START, ref brett); while (true) { if (brett.seite == engineSide && !checkresult(ref brett)) { info.timer.Reset(); info.timer.Start(); info.starttime = 1; info.depth = depth; if (movetime != 0) { info.timeset = true; info.stoptime = movetime; } search.SearchPosition(ref brett, ref info); } Console.Write("\nVwa > "); string input = Console.ReadLine(); if (input.Length > 3 && input.Substring(0, 4) == "help") { Console.Write("\nCommands:\n"); Console.Write("quit - quit game\n"); Console.Write("force - computer will not think\n"); Console.Write("print - show board\n"); Console.Write("post - show thinking\n"); Console.Write("nopost - do not show thinking\n"); Console.Write("new - start new game\n"); Console.Write("go - set computer thinking\n"); Console.Write("depth x - set depth to x\n"); Console.Write("time x - set thinking time to x seconds (depth still applies if set)\n"); Console.Write("view - show current depth and movetime settings\n"); Console.Write("setboard x - set position to fen x\n"); Console.Write("** note ** - to reset time and depth, set to 0\n"); Console.Write("enter moves using b7b8q notation\n\n\n"); continue; } if (input.Length > 3 && input.Substring(0, 4) == "eval") { Console.WriteLine(io.PrintBoard(ref brett)); Console.WriteLine("Eval:" + evaluate.EvalPosition(ref brett)); board.MirrorBoard(ref brett); Console.WriteLine(io.PrintBoard(ref brett)); Console.WriteLine("Eval:" + evaluate.EvalPosition(ref brett)); continue; } if (input.Length > 7 && input.Substring(0, 8) == "setboard") { engineSide = (int)defs.Farben.BEIDE; board.ParseFen(input.Substring(9), ref brett); continue; } if (input.Length > 3 && input.Substring(0, 4) == "quit") { info.quit = true; break; } if (input.Length > 3 && input.Substring(0, 4) == "post") { info.POST_THINKING = true; continue; } if (input.Length > 4 && input.Substring(0, 5) == "print") { Console.WriteLine(io.PrintBoard(ref brett)); continue; } if (input.Length > 5 && input.Substring(0, 6) == "nopost") { info.POST_THINKING = false; continue; } if (input.Length > 4 && input.Substring(0, 5) == "force") { engineSide = (int)defs.Farben.BEIDE; continue; } if (input.Length > 3 && input.Substring(0, 4) == "view") { if (depth == defs.MAXTIEFE) { Console.WriteLine("depth not set "); } else { Console.WriteLine("depth " + depth); } if (movetime != 0) { Console.WriteLine("movetime " + movetime / 1000 + "\n"); } else { Console.WriteLine("movetime not set\n"); } continue; } if (input.Length > 4 && input.Substring(0, 5) == "depth") { if (input.Length > 6) { depth = Convert.ToInt32(input.Substring(6)); } if (depth == 0) { depth = defs.MAXTIEFE; } continue; } if (input.Length > 3 && input.Substring(0, 4) == "time") { if (input.Length > 5) { movetime = Convert.ToInt32(input.Substring(5)); } movetime *= 1000; continue; } if (input.Length > 2 && input.Substring(0, 3) == "new") { PvTable.ClearPvTable(ref brett.PvTable); engineSide = (int)defs.Farben.SCHWARZ; board.ParseFen(defs.START, ref brett); continue; } if (input.Length > 1 && input.Substring(0, 2) == "go") { engineSide = brett.seite; continue; } move = io.ParseMove(input, ref brett); if (move == defs.NOMOVE) { Console.WriteLine("Command unknown:" + input + "\n"); continue; } makemove.MakeMove(move, ref brett); brett.ply = 0; } }
public static void XBoard_Loop(ref boardStruct brett, ref suchInfo info) { PrintOptions(); // HACK int[] movestogo = { 30, 30 }; int depth = -1, movetime = -1; int time = -1, inc = 0; int engineSide = (int)defs.Farben.BEIDE; int timeLeft; int sec; int mps = 0; int move = defs.NOMOVE; char[] inBuf = new char[80], command = new char[80]; int MB; engineSide = (int)defs.Farben.SCHWARZ; board.ParseFen(defs.START, ref brett); depth = -1; time = -1; while (true) { if (brett.seite == engineSide && !checkresult(ref brett)) { info.timer.Start(); //info.starttime = GetTimeMs(); info.depth = depth; if (time != -1) { info.timeset = true; time /= movestogo[brett.seite]; time -= 50; info.stoptime = time + inc; } if (depth == -1 || depth > defs.MAXTIEFE) { info.depth = defs.MAXTIEFE; } Console.WriteLine(String.Format("time:{0} start:{1} stop:{2} depth:{3} timeset:{4} movestogo:{5} mps:{6}\n", time, 0, info.stoptime, info.depth, Convert.ToInt32(info.timeset), movestogo[brett.seite], mps)); search.SearchPosition(ref brett, ref info); if (mps != 0) { movestogo[brett.seite ^ 1]--; if (movestogo[brett.seite ^ 1] < 1) { movestogo[brett.seite ^ 1] = mps; } } } //if (!fgets(inBuf, 80, stdin))continue; string input = Console.ReadLine(); //sscanf(inBuf, "%s", command); Console.WriteLine(string.Format("command seen:{0}\n", input)); if (input.Length > 3 && input.Substring(0, 4) == "quit") { info.quit = true; break; } if (input.Length > 4 && input.Substring(0, 5) == "force") { engineSide = (int)defs.Farben.BEIDE; continue; } if (input.Length > 7 && input.Substring(0, 8) == "protover") { PrintOptions(); continue; } if (input.Length > 6 && input.Substring(0, 7) == "polykey") { Console.WriteLine(io.PrintBoard(ref brett)); //polybook.GetBookMove(ref brett); continue; } if (input.Length > 1 && input.Substring(0, 2) == "sd") { //sscanf(inBuf, "sd %d", &depth); //Console.WriteLine("DEBUG depth:" + depth + "\n"); continue; } /*if (!strcmp(command, "st")) * { * sscanf(inBuf, "st %d", &movetime); * printf("DEBUG movetime:%d\n", movetime); * continue; * } * * if (!strcmp(command, "time")) * { * sscanf(inBuf, "time %d", &time); * time *= 10; * printf("DEBUG time:%d\n", time); * continue; * }*/ if (input.Length > 6 && input.Substring(0, 7) == "polykey") { Console.WriteLine(io.PrintBoard(ref brett)); //GetBookMove(pos); continue; } /*if (!strcmp(command, "memory")) * { * sscanf(inBuf, "memory %d", &MB); * if (MB < 4) MB = 4; * if (MB > MAX_HASH) MB = MAX_HASH; * printf("Set Hash to %d MB\n", MB); * InitHashTable(pos->HashTable, MB); * continue; * }*/ /*if (!strcmp(command, "level")) * { * sec = 0; * movetime = -1; * if (sscanf(inBuf, "level %d %d %d", &mps, &timeLeft, &inc) != 3) * { * sscanf(inBuf, "level %d %d:%d %d", &mps, &timeLeft, &sec, &inc); * printf("DEBUG level with :\n"); * } * else * { * printf("DEBUG level without :\n"); * } * timeLeft *= 60000; * timeLeft += sec * 1000; * movestogo[0] = movestogo[1] = 30; * if (mps != 0) * { * movestogo[0] = movestogo[1] = mps; * } * time = -1; * printf("DEBUG level timeLeft:%d movesToGo:%d inc:%d mps%d\n", timeLeft, movestogo[0], inc, mps); * continue; * } * * if (!strcmp(command, "ping")) * { * printf("pong%s\n", inBuf + 4); * continue; * }*/ if (input.Length > 2 && input.Substring(0, 3) == "new") { PvTable.ClearPvTable(ref brett.PvTable); engineSide = (int)defs.Farben.SCHWARZ; board.ParseFen(defs.START, ref brett); depth = -1; time = -1; continue; } if (input.Length > 7 && input.Substring(0, 8) == "setboard") { engineSide = (int)defs.Farben.BEIDE; board.ParseFen(input.Substring(9), ref brett); continue; } if (input.Length > 1 && input.Substring(0, 2) == "go") { engineSide = brett.seite; continue; } if (input.Length > 7 && input.Substring(0, 8) == "usermove") { movestogo[brett.seite]--; move = io.ParseMove(input.Substring(9), ref brett); if (move == defs.NOMOVE) { continue; } makemove.MakeMove(move, ref brett); brett.ply = 0; } } }
public static void SearchPosition(ref boardStruct brett, ref suchInfo info) { int bestMove = defs.NOMOVE; int bestScore = -defs.INFINITE; int currentDepth = 0; int pvMoves = 0; int pvNum = 0; ClearForSearch(ref brett, ref info); if (options.UseBook) { bestMove = polybook.GetBookMove(ref brett); } //Console.WriteLine(String.Format("Such tiefe:{0}\n",info.depth)); // iterative deepening if (bestMove == defs.NOMOVE) { for (currentDepth = 1; currentDepth <= info.depth; ++currentDepth) { // alpha beta rootDepth = currentDepth; bestScore = AlphaBeta(-defs.INFINITE, defs.INFINITE, currentDepth, ref brett, ref info, true); if (info.stopped) { break; } pvMoves = PvTable.GetPvLine(currentDepth, ref brett); bestMove = brett.PvArray[0]; if (info.GAME_MODE == (int)defs.Engine.UCIMODE) { //Console.Write(String.Format("Score: {0} Tief: {1} Nodes: {2} Time: {3}", bestScore, currentDepth , info.nodes, info.timer.Elapsed)); Console.Write(String.Format("\ninfo score cp {0} depth {1} nodes {2} time {3} ", bestScore, currentDepth, info.nodes, info.timer.ElapsedMilliseconds)); } else if (info.GAME_MODE == (int)defs.Engine.XBOARDMODE && info.POST_THINKING) { Console.WriteLine("{0} {1} {2} {3} ", currentDepth, bestScore, info.timer.ElapsedMilliseconds / 10, info.nodes); } else if (info.POST_THINKING) { Console.Write(String.Format("\nscore:{0} depth:{1} nodes:{2} time:{3}ms ", bestScore, currentDepth, info.nodes, info.timer.ElapsedMilliseconds)); } if (info.GAME_MODE == (int)defs.Engine.UCIMODE || info.POST_THINKING) { pvMoves = PvTable.GetPvLine(currentDepth, ref brett); if (info.GAME_MODE != (int)defs.Engine.XBOARDMODE) { Console.Write("pv"); } for (pvNum = 0; pvNum < pvMoves; pvNum++) { Console.Write(" " + io.PrMove(brett.PvArray[pvNum])); } Console.Write(""); } //printf("Hits:%d Overwrite:%d NewWrite:%d Cut:%d\nOrdering %.2f NullCut:%d\n",pos.HashTable.hit,pos.HashTable.overWrite,pos.HashTable.newWrite,pos.HashTable.cut, //(info.fhf/info.fh)*100,info.nullCut); } } if (info.GAME_MODE == (int)defs.Engine.UCIMODE) { Console.WriteLine("\nbestmove {0}\n", io.PrMove(bestMove)); } else if (info.GAME_MODE == (int)defs.Engine.XBOARDMODE) { Console.WriteLine("move " + io.PrMove(bestMove) + "\n"); makemove.MakeMove(bestMove, ref brett); } else { Console.WriteLine("\n\n***!! Vwa makes move " + io.PrMove(bestMove) + "!!***\n\n"); makemove.MakeMove(bestMove, ref brett); Console.WriteLine(io.PrintBoard(ref brett)); } }
public static int Quiescence(int alpha, int beta, ref boardStruct brett, ref suchInfo info) { Debug.Assert(board.CheckBoard(ref brett)); Debug.Assert(beta > alpha); if ((info.nodes & 2047) == 0) { CheckUp(ref info); } info.nodes++; if (IsRepetition(ref brett) || brett.fiftyMove >= 100) { return(0); } if (brett.ply > defs.MAXTIEFE - 1) { return(evaluate.EvalPosition(ref brett)); } int Score = evaluate.EvalPosition(ref brett); Debug.Assert(Score > -defs.INFINITE && Score < defs.INFINITE); if (Score >= beta) { return(beta); } if (Score > alpha) { alpha = Score; } movelist list = new movelist(); movegen.GenerateAllCaps(ref list, ref brett); int MoveNum = 0; int Legal = 0; Score = -defs.INFINITE; for (MoveNum = 0; MoveNum < list.anzahlZüge; ++MoveNum) { PickNextMove(MoveNum, ref list); if (!makemove.MakeMove(list.zugliste[MoveNum].Zug, ref brett)) { continue; } Legal++; Score = -Quiescence(-beta, -alpha, ref brett, ref info); makemove.TakeMove(ref brett); if (info.stopped) { return(0); } if (Score > alpha) { if (Score >= beta) { if (Legal == 1) { info.fhf++; } info.fh++; return(beta); } alpha = Score; } } return(alpha); }
public static int AlphaBeta(int alpha, int beta, int depth, ref boardStruct brett, ref suchInfo info, bool DoNull) { Debug.Assert(board.CheckBoard(ref brett)); Debug.Assert(alpha < beta); Debug.Assert(depth >= 0); if (depth <= 0) { return(Quiescence(alpha, beta, ref brett, ref info)); //return evaluate.EvalPosition(ref brett); } if ((info.nodes % 2047) == 0) { CheckUp(ref info); } info.nodes++; if ((IsRepetition(ref brett) || brett.fiftyMove >= 100) && brett.ply != 0) { return(0); } if (brett.ply > defs.MAXTIEFE - 1) { return(evaluate.EvalPosition(ref brett)); } bool inSchach = attack.SqAttacked(brett.KönigSq[brett.seite], brett.seite ^ 1, ref brett); //wenn könig in schach ist gleich tiefer weil das sonst unnötig ist weil eh in schach if (inSchach) { depth++; } int Score = -defs.INFINITE; int PvMove = defs.NOMOVE; if (PvTable.ProbeHashEntry(ref PvMove, ref Score, alpha, beta, depth, ref brett)) { brett.PvTable.cut++; return(Score); } if (DoNull && !inSchach && brett.ply > 0 && (brett.bigPce[brett.seite] > 0) && depth >= 4) { makemove.MakeNullMove(ref brett); Score = -AlphaBeta(-beta, -beta + 1, depth - 4, ref brett, ref info, false); makemove.TakeNullMove(ref brett); if (info.stopped) { return(0); } if (Score >= beta && Math.Abs(Score) < defs.ISMATE) { info.nullCut++; return(beta); } } movelist list = new movelist(); movegen.GenerateAllMoves(ref list, ref brett); int MoveNum = 0; int Legal = 0; int OldAlpha = alpha; int BestMove = defs.NOMOVE; Score = -defs.INFINITE; int BestScore = -defs.INFINITE; //checkt ob man in der best möglichewn line ist dann sucht es diesesen zug zuerst if (PvMove != defs.NOMOVE) { for (MoveNum = 0; MoveNum < list.anzahlZüge; ++MoveNum) { if (list.zugliste[MoveNum].Zug == PvMove) { list.zugliste[MoveNum].Wertung = 2000000; break; } } } for (MoveNum = 0; MoveNum < list.anzahlZüge; ++MoveNum) { PickNextMove(MoveNum, ref list); if (!makemove.MakeMove(list.zugliste[MoveNum].Zug, ref brett)) { continue; } Legal++; Score = -AlphaBeta(-beta, -alpha, depth - 1, ref brett, ref info, true); makemove.TakeMove(ref brett); if (info.stopped) { return(0); } if (Score > BestScore) { BestScore = Score; BestMove = list.zugliste[MoveNum].Zug; if (Score > alpha) { if (Score >= beta) { if (Legal == 1) { info.fhf++; } info.fh++; if (!makros.MoveFlagCapture(list.zugliste[MoveNum].Zug)) { brett.searchKillers[1, brett.ply] = brett.searchKillers[0, brett.ply]; brett.searchKillers[0, brett.ply] = list.zugliste[MoveNum].Zug; } PvTable.StoreHashEntry(BestMove, beta, (int)defs.HASH.HFBETA, depth, ref brett); return(beta); } alpha = Score; if (!makros.MoveFlagCapture(list.zugliste[MoveNum].Zug)) { brett.searchHistory[brett.figuren[makros.FromSq(BestMove)], makros.ToSq(BestMove)] += depth; } } } } if (Legal == 0) { if (inSchach) { return(-defs.MATE + brett.ply); } else { return(0); } } if (alpha != OldAlpha) { PvTable.StoreHashEntry(BestMove, BestScore, (int)defs.HASH.HFEXACT, depth, ref brett); } else { PvTable.StoreHashEntry(BestMove, alpha, (int)defs.HASH.HFALPHA, depth, ref brett); } return(alpha); }