A Python Model for Ping Pong MatchesSee Python: Tips and Tricks for similar articles.
A while back, I started playing table tennis more seriously. I’m now in sort of a no-man’s land, in which I can beat most people who consider it a basement game, but can’t beat very many people who consider it a sport.
Because I’d rather write Python code than practice against my robot, I’ve created a model in Python to try to get an idea of what I should practice most to win more games. I’m unlikely to follow through on the practice, but I am likely to refine the model if people have ideas of how it could be made better. So, if there are any ping pong programmers out there, please feel free to make suggestions.
Before we get into the Python code, let me explain a little about table tennis.
Relevant Table Tennis Rules:
- Matches are best of 5 games.
- Games are played to 11 points, but you must win by 2.
- The server changes every two points up until deuce deuce (11-11), at which point it changes every point.
- The first to serve in the first game is chosen randomly and then alternated each subsequent game.
- In table tennis, you only get one fault.
How Points Play Out
The server tries to make it difficult for his opponent to return the serve well. Ideally, the opponent will not be able to return the serve at all, but if he does, the server hopes the return is weak enough so that he can easily put the third ball away. You can think of a point like this:
- Server serves
- Returner returns serve
- Server attempts putaway
- Returner volleys
- Server volleys
- Returner volleys
Once the volley has lasted for five hits, the odds of a player successfully returning a shot probably don’t change much with each successive hit.
So, when two players face each other, you can create a model to show how the matches are likely to come out based on the players’ likelihood to fail on any given shot. For example, imagine these two players failure rates when they play each other:
|Return of Serve||.1||.5|
- When Leira serves first, he almost always gets it in.
- Nat has a lot of trouble with Leira’s serve. He hits half of them into the net or off the table.
- When Nat does get it back, he sets Leira up for a putaway. And Leira generally goes for it, but he hits it out 30% of the time.
- As per above, Leira’s 3rd shot is often, but not always, an attempted putaway. Nat fails to return that shot about 40% of the time.
- If it gets to a fifth shot, the point becomes more of a volley and the chances of a putaway or an error on any given shot are lower. Leira only fails to return the fifth and subsequent shots 20% of the time.
- At this point in the volley, Nat fails to return shots 25% of the time.
- When Nat serves first, he gets 90% of his serves in.
- Leira has little trouble returning Nat’s serve. He gets 90% of them back.
- Nat doesn’t often set himself up for the 3rd shot kill, so he moves right to volley mode, in which he fails to return shots 25% of the time.
- Leira is in volley mode at this point too and fails to return shots 20% of the time.
So, let’s consider our players in Python:
#Leira player.name = 'Leira' player.fail_rates = [.05,.1,.3,.2] #Nat player.name = 'Nat' player.fail_rates = [.1,.5,.25,.4,.25]
My program assumes that the fail rate for all hits after the last one indicated in the player’s fail_rates list is the same as the fail rate for the last one listed. So, because Leira has the same fail rate for shots 4, 5, and 6 and all subsequent shots, we only have to indicate the fail rate for his first four shots.
So, those are my assumptions. You can check out the Python program here.
Feel free to play around with it and let me know if you have any suggestions for improving the model or the code.
- Scientific Notation in Python
- Understanding Python’s __main__ variable
- Associate Python Files with IDLE
- Python: isdigit() vs. isdecimal()
- Python Color Constants Module
- Python: pow(x, y, z) less efficient than x**y % z
- A Python Model for Ping Pong Matches (this article)
- Bulk Convert Python files to IPython Notebook Files (py to ipynb conversion)
- Collatz Conjecture in Python
- Finally, a use case for finally – Python Exception Handling
- Python Clocks Explained
- Python’s date.strftime() slower than str(), split, unpack, and concatenate?
- Bi-directional Dictionary in Python
- Maximum recursion depth exceeded while calling a Python object
- Basic Python Programming Exercise: A Penny Doubled Every Day
- Creating an Email Decorator with Python and AWS
- How to Create a Simple Simulation in Python – Numeric Data
- Python Coding Challenge: Two People with the Same Birthday
- How to find all your Python installations on Windows
- Change Default autosave Interval in JupyterLab
- Interactive Quiz using IPython Notebook
- When to use Static Methods in Python? Never
- Converting Leading Tabs to Spaces with Python
- Simple Python Script for Extracting Text from an SRT File
- Python Virtual Environments with venv
- Mapping python to Python 3 on Your Mac
- How to Make IDLE the Default Editor for Python Files on Windows
- How to Do Ternary Operator Assignment in Python
- How to Convert Seconds to Years with Python
- How to Create a Python Package
- How to Read a File with Python
- How to Check the Operating System with Python
- How to Use enumerate() to Print a Numbered List in Python
- How to Repeatedly Append to a String in Python
- Checking your Sitemap for Broken Links with Python
- How to do Simultaneous Assignment in Python
- Visual Studio Code - Opening Files with Python open()
- How to Slice Strings in Python
- How Python Finds Imported Modules
- How to Merge Dictionaries in Python
- How to Index Strings in Python
- How to Create a Tuple in Python