% Simulate racquetball

reps=1000
rec=NaN(1,reps); % Record results here

playto=21
pserve=.6 % Prob of winning rally when I serve
preceive=.5 % Prob of winning rally when I receive

for k=1:reps
    done=false
    myserve=true
    score=[0 0]
    while max(score)<playto
        if myserve
            win=rand<pserve
            if win
                score=score+[1 0]
            else
                myserve=false
            end
        else
            win=rand<preceive
            if win
                myserve=true
            else
                score=score+[0 1]
            end
        end
    end
    game=(score(1)==playto) % Did I win?
    
    rec(k)=game
end

sum(rec)/reps
