Laboratory Exercises For Computer Science 153

Project: Racquetball/Volleyball Simulation

Project: Racquetball/Volleyball Simulation

This project investigates several approaches to one of the following problems:

  1. Racquetball: Racquetball is a game played by two players on an indoor, enclosed court. Scoring proceeds as follows: A player can only score points while she has the serve. A player loses the serve when she loses a volley, but no points are scored on the change of serve. Play continues until either the score is 11-0, which is a shut-out, or one player scores 21 points. (The rules do not require a player to win by two points.)

    Write a procedure that has the probability of Player A winning a volley as parameter and simulates the playing of 1000 games with Player A having the first serve on each game. Record the number of wins (including shut-outs) for each player and the percentage of wins. Also record the number of shut-outs for each player.

  2. Volleyball:Volleyball is a game played on a court by two teams, separated by a net. Scoring proceeds much the same way as in racquetball (as explained above). In particular, scoring starts at 0-0. A team can only score points while it serves. A team loses the serve when it loses a volley, but no points are scored on the change of serve. Play continues until one team scores 15 points, and a team must win by at least two points (if the score is 15-14, play must continue until one team leads by 2 points). There is no special rule for ending a game due to a shut-out.

    Write a procedure that has as parameter the probability of Team A winning a volley and then simulates the playing of 1000 games with Team A having the first serve on each game. The procedure should print the number of wins for each team and the percentage of wins. The procedure also should print the number of shut-outs for each team.

Observation

These two problems are quite similar: they both involve the simulation of 1000 games, where each game involves a similar flow of events. Play proceeds from volley to volley, and similar rules govern who serves and how points are scored. From the standpoint of simulation, the major difference involves the determination that someone has one a game:

With these similarities, the following discussion will apply to either sport. Only the ending condition changes from one game to another.

Approaches for Solution

Solutions to this problem typically follow one of two general approaches:

  1. A solution may treat the serve as the basic unit of activity.
  2. A solution may consider A serving for awhile and then B serving for awhile. This A-B combination then continues until someone wins.

Beyond the above general approaches, a solution may proceed either iteratively or recursively.

Either iterative or recursive implementation of the general approach leads to an solution outline. Two of the four possible, high-level outlines follow:

Approach 1:  A game is a sequence of serves
Recursive solution for a game:
     I.  Initialization
             A.  Initialize the Random Nunber Seed
             B.  Determine probability of A winning a volley
             C.  At start of simulation, neither A nor B have won,
                 and neither have scored any shut-outs

   II.  Repeat for 1000 games
            A.  Initially, A and B have 0 points
            B.  A has first serve
                    1.  Use A's probability for probability of server
            C.  If neither A nor B has won:
                    1.  Determine if server wins volley
                        If so,
                            Server wins point
                            Update score of correct person, 
                                depending on server
                            Call II.C with same server, 
                                probabilities, etc.
                        If not,
                            Server looses volley; other person 
                                wins volley
                            Change probability of server to other  
                                person
                            Call II.C with new server,  
                                probabilities, etc.
            D.  Record who has won 
            E.  Record if a shut-out has occured

  III.  Print results
            A.  Print the number of wins and shut-outs for A

Approach 2:  A game is a sequence of A-serves then B-serves
Iterative solution for a game:
     I.  Initialization
             A.  Initialize the Random Nunber Seed
             B.  Determine probability of A winning a volley
             C.  At start of simulation, neither A nor B have won,
                 and neither have scored any shut-outs

   II.  Repeat for 1000 games
            A.  Initially, A and B have 0 points
            B.  Continue until A wins or B wins
                    1.  A serves
                        Continue until A wins or looses serve
                                If A wins volley
                                    then A scores point
                                    otherwise, B gets serve
                    2.  If A has not won, then B serves
                        Continue until B wins or looses serve
                                If B wins volley
                                    then B scores point
                                    otherwise, A gets serve
            C.  Record who has won 
            D.  Record if a shut-out has occured

  III.  Print results
            A.  Print the number of wins and shut-outs for A
            B.  Print the number of wins and shut-outs for B
Steps for this Project:

  1. Write the two other outlines to solve this problem (e.g., an iterative solution for approach 1 and a recursive solution for approach 2).

  2. Write four programs to solve this problem -- an iterative and a recursive solution for each general approach.

  3. Write a comparison of the resulting four programs: which code seems simplest, most readable, most efficient?

  4. Discuss how the structure of your programs compares to the various steps of the outline(s)?

  5. Discuss your testing of these programs.

Collaboration for this Project:


This document is available on the World Wide Web as

http://www.math.grin.edu/~walker/courses/153/project-racquetball.html

created January 20, 1998
last revised February 2, 1998