The logic in simulateMatch assumes the players alternate who serves first in the subsequent set, but that's not how it works in tennis. Generally, if the previous set score is even, then the player who started serving the previous set is also the player who starts serving the next set. If the previous set score is odd (you can count the tiebreak as 1, so all tiebreak sets are odd), then the player who starts serving the next set is the other player.
Thank you for pointing out that error in the tennis serving logic. You're absolutely correct, and I apologize for the mistake. Let's update the simulateMatch function to correctly implement the serving rules in tennis.
Let's update the TennisSimulator component with the corrected serving logic:
tennis-simulator.tsx
The main changes are in the simulateMatch function:
const totalGames = setScore[0] + setScore[1]
if (totalGames % 2 === 0) {
// If the total number of games in the set is even, the same player serves first in the next set
isAServingFirst = isAServingFirst
} else {
// If the total number of games in the set is odd, the other player serves first in the next set
isAServingFirst = !isAServingFirst
}simulatePoint function to correctly use the advanced probabilities:function simulatePoint(probA: number, isAServing: boolean) {
if (useAdvancedProb) {
return Math.random() < (isAServing ? probAServe : 1 - probAReturn)
}
return Math.random() < probA
}These changes ensure that the serving order in the simulation now correctly follows the official tennis rules, and the advanced probabilities are applied correctly. The rest of the component remains the same, maintaining all the functionality from the previous version while incorporating these important updates.
I've made some changes to the simulation logic. Now, when the user opts for "Use Advanced Probabilities", I want the player A/B win probability text area to disappear as you already have, but they should be replaced by the player A serve win prob and player A return win prob in the main card (instead of those text fields being in the advanced settings sheet).
Here's the updated TennisSimulator component with the requested changes:
tennis-simulator.tsx
Key changes made:
CardContent to show either the basic probability inputs or the advanced probability inputs based on the useAdvancedProb state.SheetContent in the advanced settings.These changes ensure that when "Use Advanced Probabilities" is enabled, the user can input Player A's serve and return win probabilities directly in the main card, while the basic probability inputs are hidden. The advanced settings sheet now only contains the switches for various game rules and the option to toggle advanced probabilities.