//calculate hevristic recursive private static int GetHevristic(byte[,] state, byte currentPlayer, int level) { if (level == RecursionDeepth) { int hevr = Hevristic.GetHevristic(state); return(hevr); } int bestHevristic = 0; int tmpHevristic = 0; int min = 1000; int max = -1000; for (int i = 0; i < 19; i++) { for (int j = 0; j < 19; j++) { if (state[i, j] == PlayersInfo.Empty) { byte[,] tmpState = MatrixCopy(state, 19); //make a step tmpState[i, j] = currentPlayer; tmpHevristic = GetHevristic(tmpState, PlayersInfo.Inverse(currentPlayer), level + 1); if (currentPlayer != PlayersInfo.AI) { bestHevristic = bestHevristic < max ? max : bestHevristic; } else { bestHevristic = bestHevristic > min ? min : bestHevristic; } } } } return(bestHevristic); }
private static int CalculateHevristic(byte[,] state, byte currentPlayer, int level) { //if max recursion deepth has reached //returns current state if (level == RecursionDeepth) { //reset max = -1000; min = 1000; return(Hevristic.GetHevristic(state)); } //best Hevristic result int bestHevristic = -1000; //current Hevristic value int currentHevristic = -1000; //hevristic in the current state int currentStateHevristic = 0; //field after step byte[,] nextState = null; //iterates each one of empty cells for (int i = 0; i < 19; i++) { for (int j = 0; j < 19; j++) { //find empty position //to make an step if (state[i, j] == PlayersInfo.Empty) { //copy the field nextState = MatrixCopy(state, 19); //make an step nextState[i, j] = currentPlayer; //get the current state hevristic currentStateHevristic = Hevristic.GetHevristic(state); if (currentPlayer == PlayersInfo.AI) { min = min < currentStateHevristic ? min : currentStateHevristic; } else { max = max > currentStateHevristic ? max : currentStateHevristic; } if (max >= min) { max = -1000; min = 1000; return(currentStateHevristic); } currentHevristic = CalculateHevristic(nextState, PlayersInfo.Inverse(currentPlayer), level + 1); bestHevristic = bestHevristic > currentHevristic ? bestHevristic : currentHevristic; } } } return(bestHevristic); }