Okay, so today I decided to mess around with tracking a tennis score, specifically for a fictional match between “Paul” and, let’s say, “Opponent.” Just wanted to see if I could build something simple that worked. Nothing fancy, just a basic scorekeeper.
First, I brainstormed what I needed. Obviously, I needed to keep track of points, games, and sets. Tennis scoring is a bit weird, with that 15, 30, 40, game thing, and then deuce and advantage, so I had to get that logic right.
The Basic Setup
I created variables which I named as Paul and opponent’s variables to track their scores. I started with their points:
paulPoints = 0
opponentPoints = 0
Then, I did the same for games and sets:
paulGames = 0
opponentGames = 0
paulSets = 0
opponentSets = 0
I created some functions like, “Paul scores!” or “Opponent scores!” that will increase the scores. When I click “Paul scores!”, I added 15 to paulPoints
, and so on. That was the easy part, that is:
- if click Paul scores: add 15 to
paulPoints
The trickier thing was handling when points went above 40. So I put in a bunch of ‘if’ checks. Like:
- If both players are at 40, it is considered a deuce.
- From deuce, if Paul scores, he goes to “advantage”.
- If Paul has “advantage” and scores again, he wins the game.
- If Paul has “advantage” and the opponent scores, it goes back to deuce.
I had to do a lot of, “If this is true, then that happens, otherwise, do something else.” It was a bit of a headache to make sure I got all the possible situations covered.
After someone won a game, I had to reset the points back to zero and increase their game count. And then, when someone won six games (and was two games ahead), they won a set, so I had to add to their set count and reset the games.
I kept running my little program and testing different scoring scenarios. “Okay, Paul scores, then opponent scores, then Paul scores twice…” just to make sure my logic was solid and didn’t break. It took a few tries to catch all the edge cases, especially around deuce and advantage. Finally, it works! I can now successfully keep track of Paul’s tennis score.