Example #1
0
    private static float AlphaBeta(GameState gs, int depth, float alpha, float beta, bool isMax)
    {
        if (depth == 0 || gs.IsEndGame()) {
        float val = EvaluateMove(gs);
        return isMax ? val : -val;
        }

        Point p = null;
        if (isMax) {
        p = new Point(gs.MyX(), gs.MyY());
        } else {
        p = new Point(gs.OpponentX(), gs.OpponentY());
        }

        GameState newState = null;
        foreach (Point child in gs.PossibleMoves(p.X, p.Y, true)) {
        if (isMax) {
             newState = gs.ApplyMoveToMeAndCreate(child.GetDirectionFromPoint(p.X, p.Y));
        } else {
             newState = gs.ApplyMoveToOpponentAndCreate(child.GetDirectionFromPoint(p.X, p.Y));
        }

        alpha = Math.Max (alpha, -AlphaBeta(newState, depth-1, -beta, -alpha, !isMax));
        if (beta <= alpha) {
            break;
        }
        }

        return alpha;
    }