CSC 221: Introduction to Programming
Fall 2013

HW 4: Ping Pong Simulations

You may work with one other person from the class on this assignment. You may NOT work with the same person you worked with on HW 1.


For many years, official ping pong (table tennis) games were played to 21 points. That is, a game continued until one player scored 21 points and led by at least two points. A match was typically two-out-of-three games. In 2001, the International Table Tennis Federation changed the scoring rules, moving to 11-point games and matches of three-out-of-five games (or sometimes four-out-of-seven). Their stated intent was to make games more exciting and also more TV friendly. For this assignment, you will develop functions for simulating ping pong games and matches, and collect statistics to help determine the effect of the scoring rules change.

Part 1: Simulating Games

Assume that the two players in a ping pong game are each assigned a skill level from 0 to 10. The higher the skill level, the more likely that player is to win a particular point. For example, if player 1 and player 2 have identical skill levels, say 6.5 and 6.5, each is equally likely to win a given point. However, if player 1 has a skill level of 6.5 and player 2 has a skill level of 3.25, then player 1 is twice as likely to win a given point. The following Python function can be used to simulate a single point in a ping pong game.

  1. Define a Python function named pingPongGame that simulates a complete game of ping pong by calling pingPongPoint repeatedly. The function should have three inputs, the number of points required to win a game, the skill level of player 1, and the skill level of player 2 (in that order). It should print the score after each point. For example:
    Note that a team has to win a game by at least two points. You will need to think carefully how to address this in your function.

  2. Once you have your pingPongGame function working correctly, comment out the print statement by placing a '#' character at the front of the line. Instead, have the function return a pair of values: the final score of player 1 followed by the final score of player 2. For example, if the variables score1 and score2 stored the player scores, the following would return a pair containing those scores: return (score1, score2) .

    It is possible to assign a pair in a single assignment, effectively assigning two variables at once. For example:

Part 2: Consequences of Game Length

While the skill levels of the players determine the likelihood of winning a particular point, this is not the same as the likelihood of winning a game. In fact a small advantage in skill level can translate into a huge advantage in game winning percentage. For example, a player who is only 10% better than her opponent (say skill level 6.6 vs. 6.0) is ~25% more likely to win a game to 21. Consider the following hypothesis:

HYPOTHESIS 1: Reducing the length of games from 21 points to 11 points favors the weaker player, since a shorter lucky streak is required to steal a game from the stronger player.

You are to develop code and perform experiments to either support or refute this hypothesis.

  1. Define a function named gameStats that performs repeated game simulations and keeps track of the winning percentages of the players. The function should have four inputs, the number of games to be played, the number of points required to win a game, and the skill levels of the two players (in that order). It should repeatedly call the pingPongGame function and keep track of the number of wins by each team. At the end, it should print the winning percentages of the two players, rounded to one digit to the right of the decimal place. For example:
  2. Once you have your gameStats function working, generate data to fill in the table below. Each simulation should run 10,000 games.

    player1 / player2
    skill levels
    likelihood of player 1
    winning each point
    % of 21-point games
    won by player 1
    % of 11-point games
    won by player 1
    5.0 / 5.0 50.0%    
    5.5 / 4.5 55.0%    
    7.5 / 5.0 60.0%    
    8.0 / 4.0 66.7%    
    6.0 / 2.0 75.0%    
    8.0 / 2.0 80.0%    
    9.0 / 1.0 90.0%    

    Describe any trends you see in the game winning percentages. Does your data support or refute HYPOTHESIS 1? Explain.

Part 3: Consequences of Match Length

In conjunction with shortening games, the new scoring system lengthened matches, going from two-out-of-three to three-out-of-five games. Consider the following hypothesis:

HYPOTHESIS 2: Increasing the length of matches from two-out-of-three to three-out-of-five games favors the stronger player, since more games makes it more likely that skill will win out over luck.

You are to develop code and perform experiments to either support or refute this hypothesis.

  1. Define a function named pingPongMatch that simulates a complete match of ping pong by calling pingPongGame repeatedly. The function should have four inputs, the number of games required to win a match, the number of points required to win a game, and the skill levels of the two players (in that order). It should return a pair consisting of the number of games won by each team. For example:

    Note: this function should be very similar to pingPongGame.

  2. Define a function named matchStats that performs repeated match simulations and keeps track of the winning percentages of the players. The function should have five inputs, the number of matches to be played, the number games required to win a match, the number of points required to win a game, and the skill levels of the two players (in that order). It should repeatedly call the pingPongGame function and keep track of the number of wins by each team. At the end, it should print the winning percentages of the two players, rounded to one digit to the right of the decimal place.

    Note: this function should be very similar to gameStats.

  3. Once you have your matchStats function working, generate data to fill in the table below. Each simulation should run 10,000 matches, with games to 11 points.

    player1 / player2
    skill levels
    likelihood of player 1
    winning each point
    % of 2-out-of-3 matches
    won by player 1
    % of 3-out-of-5 matches
    won by player 1
    5.0 / 5.0 50.0%    
    5.5 / 4.5 55.0%    
    7.5 / 5.0 60.0%    
    8.0 / 4.0 66.7%    
    6.0 / 2.0 75.0%    
    8.0 / 2.0 80.0%    
    9.0 / 1.0 90.0%    

    Describe any trends you see in the game winning percentages. Does your data support or refute HYPOTHESIS 2? Explain.

Part 4: Balancing Competition

The ITTF publicly stated that the motivation for the scoring change was to add excitement and make the sport more TV friendly. In particular, the changes were not intended to significantly change the balance of games. So, if a player had a certain likelihood of winning a match under the old rules, the likelihood of winning under the new rules should be comparable.

HYPOTHESIS 3: Shortening games from 21 to 11 points while simultaneously lengthening matches from two-out-of-three to three-out-of-five does not significantly change the likelihood of players winning or losing.
  1. Generate data to fill in the table below. Each simulation should run 10,000 matches.

    player1 / player2
    skill levels
    likelihood of player 1
    winning each point
    % of (21 pt, 2-of-3) matches
    won by player 1
    % of (11 pt, 3-of-5) matches
    won by player 1
    5.0 / 5.0 50.0%    
    5.5 / 4.5 55.0%    
    7.5 / 5.0 60.0%    
    8.0 / 4.0 66.7%    
    6.0 / 2.0 75.0%    
    8.0 / 2.0 80.0%    
    9.0 / 1.0 90.0%    

    Describe any trends you see in the game winning percentages. Does your data support or refute HYPOTHESIS 3? If the winning percentages are not comparable, would going to four-out-of-seven games (to 11 points) be better? Explain.