public float Minimax(Grid g, out GridCells best, int depth = 1) { miniMaxCount++; best = GridCells.None; var bestResult = -10f; GridCells garbage; if (g.IsDraw) { return(0f); } if (g.CurrentIsWinner) { return(1f / depth); } if (g.CurrentIsLoser) { return(-1f / depth); } foreach (var move in g.GetMoves()) { var other = g.MakeMove(move); var result = -Minimax(other, out garbage, depth + 1); if (result > bestResult) { best = move; bestResult = result; } } return(bestResult); }
public int Minimax(Grid g, out GridCells best) { best = GridCells.None; var bestResult = -10; GridCells garbage; if (g.IsDraw) { return(0); } if (g.CurrentIsWinner) { return(1); } if (g.CurrentIsLoser) { return(-1); } foreach (var move in g.GetMoves()) { var other = g.MakeMove(move); var result = -Minimax(other, out garbage); if (result > bestResult) { best = move; bestResult = result; } } return(bestResult); }
public float Minimax(Grid g, out GridCells best, float alpha, float beta, int depth = 1) { miniMaxCount++; best = GridCells.None; var bestResult = -10f; GridCells garbage; if (g.IsDraw) return 0f; if (g.CurrentIsWinner) return 1f / depth; if (g.CurrentIsLoser) return -1f / depth; foreach (var move in g.GetMoves()) { var other = g.MakeMove(move); alpha = -Minimax(other, out garbage, -beta, -alpha, depth + 1); if (beta <= alpha) return alpha; if (alpha > bestResult) { best = move; bestResult = alpha; } } return bestResult; }
public int Minimax(Grid g, out GridCells best) { best = GridCells.None; var bestResult = -10; GridCells garbage; if (g.IsDraw) return 0; if (g.CurrentIsWinner) return 1; if (g.CurrentIsLoser) return -1; foreach (var move in g.GetMoves()) { var other = g.MakeMove(move); var result = -Minimax(other, out garbage); if (result > bestResult) { best = move; bestResult = result; } } return bestResult; }