protected int findBestMove(int layer, Move move, int alpha, int beta) { if (layer == 0) { evaluatedPositions++; return(evaluateBoard(move.BoardAfter)); //return evaluateBoardPlus(move); } else { Move newerMove; List <UInt64[]> possibleMove; int movecount; if (transpTable.TryGetValue(move.toString(), out possibleMove)) { // Key was in dictionary; "value" contains corresponding value } else { possibleMove = Calculator.AllPossible(move, move.turn); transpTable.Add(move.toString(), possibleMove); } movecount = possibleMove.Count; if (move.turn)//weiss { for (var i = 0; i < movecount; i++) { newerMove = move.MakeAMove(possibleMove[i][0] , possibleMove[i][1] , false); if (move.num >= 9 && move.toString() == move.previousMove.previousMove.previousMove.previousMove.toString() && move.toString() == move.previousMove.previousMove.previousMove.previousMove.previousMove.previousMove.previousMove.previousMove.toString() ) //der dritte move in folge ist gleich { return(0); //patt } if (newerMove.promotion) { newerMove.promote("queen"); } alpha = Math.Max(alpha, findBestMove(layer - 1, newerMove, alpha, beta)); move.reset(); if (beta < alpha) { break; } } if (movecount == 0) { if (move.AmIInCheck()) { return(-999999999); } else { return(0); } } return(alpha); } else { for (var i = 0; i < movecount; i++) { newerMove = move.MakeAMove(possibleMove[i][0] , possibleMove[i][1] , false); beta = Math.Min(beta, findBestMove(layer - 1, newerMove, alpha, beta)); move.reset(); if (beta < alpha) { break; } } if (movecount == 0) { if (move.AmIInCheck()) { return(999999999); } else { return(0); } } return(beta); } } }
public virtual UInt64[] giveBestMove(int layers, Move move) { List <UInt64[]> possibleMove; if (transpTable.TryGetValue(move.toString(), out possibleMove)) { // Key was in dictionary; } else { possibleMove = Calculator.AllPossible(move, this.Color); transpTable.Add(move.toString(), possibleMove); } int i = 0; int best; if (Color) { best = -999999999; } else { best = 999999999; } Move newerMove; int besti = 0; movesCount = possibleMove.Count; firstLayer = movesCount; int ret; while (i < movesCount) { newerMove = move.MakeAMove(possibleMove[i][0] , possibleMove[i][1] , false); if (move.num >= 9 && move.toString() == move.previousMove.previousMove.previousMove.previousMove.toString() && move.toString() == move.previousMove.previousMove.previousMove.previousMove.previousMove.previousMove.previousMove.previousMove.toString() ) //der dritte move in folge ist gleich { ret = 0; //patt Console.WriteLine("instant patt"); Console.Read(); } else { if (move.promotion) { move.promote("queen"); } ret = findBestMove(layers - 1, newerMove, -999999999, 999999999); } if (!Color) { if (ret < best) { besti = i; if (ret == -999999999) { return(possibleMove[i]); } best = ret; } } else { if (ret > best) { besti = i; if (ret == 999999999) { return(possibleMove[i]); } best = ret; } } move.reset(); this.percentFinished = (float)i / movesCount * 100; myForm.Invoke(myForm.DelegateCount); i++; } this.percentFinished = 100; myForm.Invoke(myForm.DelegateCount); if (i != 0) { return(possibleMove[besti]); } else { return(new UInt64[] { 0, 0 }); } }