# Homework 3: A Raquetball Simulator

Assigned: Tuesday, 1 April 2003
Due: Tuesday, 8 April 2003

Summary: Originally, object-oriented languages were designed to make simulation easier and perhaps more accurate. In this assignment, you will be developing a simple simulation of a racquetball game to consider some of the aspects of simulation and to get your feet wet with Java programming.

Citation: This assignment is based on a column by Henry M. Walker be published in some issue of the ACM SIGCSE Bulletin, although it was designed before that issue was published.

Turning it in: Use the ECA.

Collaboration: You may work in group of up to size two, and may discuss your design with any size group. You may also work with each other on general debugging issues.

Contents:

## Background: The Rules of Raquetball

Racquetball is a two-player game played by two players on an indoor, enclosed court. One player serves the ball, the other returns (or attempts to return) the ball, and they continue to volley until one player misses. If the serving player wins the volley, that player receives a point. If the serving player misses the volley, the serve passes to the other player, and no points are scored. Play continues until the score is 11-0 (a shut out) or one player scores 21 points.

## Basics of the Assignment

Your goal is twofold: First, write a program that narrates one match between two players. Next, write a program compute the liklihood of a player winning a game of Raquetball by simulating a large number of matches. Your second program should report at least four results, given two players, A and B: (1) the number of wins for A in the number of matches specified, (2) the probability of A winning, (3) the number of shut-outs (by either player) in the number of matches specified, and (4) the probability of a shut-out occuring.

## Modeling Raquetball

In order to write both programs, you'll need to find a way to model various aspects of raquetball. The simplest technique is to assign to each player a probability of winning a volley when the player has the serve. To simulate a match, you start the serve with a player, determine whether or not that player wins. If so, you assign a point to that player for the match. If not, you switch the serve to the other player.

A more complex and more realistic mechanism is to assign to each player a probability of returning the ball. I assume you can figure out how to use this probabilty.

## Required Classes

You must create at least the following five classes for the two parts of the assignment.

• `Player`, used to simulate each player,
• `Score`, used to keep track of the score of one match (and perhaps the serve, depending on your design),
• `RecordTable`, used to keep track of wins/losses/shutouts.
• `MatchReporter`, used to report on one match. This class should contain a `main` method.
• `StatisticsCalculator`, used to compute the various statistics mentioned above. This class should also contain a `main` method.

## Extra Credit

If you'd like some more to do on this assignment, you might consider one or more of the following:

• Extend your simulation with additional characteristics of players (e.g., placement of balls, basing the probability of a return on the other player's placement, etc.)
• Read in information on more than two players, and simulate a tournament between the players. You might want to consider a variety of types of tournaments (single elimination, double elimination, etc.)
• Provide a visual interfce for your program.
• Make your simulation sufficiently generic that it can support a number of similar games, such as ping-pong (see rules at http://www.geocities.com/Colosseum/7666/index1.htm).

## Related Topic: Random Sequences of Numberse

In order to simulate the match, you'll need a random sequence of numbers. Java provides at least two ways to create random sequences of numbers, one from the `Math` class, and one from a separate `Random` class.

If you call `Math.random()` (that is, the class method `random` of the `Math` class), it will return a pseudorandom number between 0.0 and 1.0.

If you create an instance, `r`, of class `Random`, using something like

```Random r = new Random()
```

you can ask for `r.nextDouble()`, `r.nextFloat()`, `r.nextInt()`,, or `r.nextLong()`.

By pseudorandom I mean that it is generated algorithmically, but it is still hard to predict. On most (all?) computers, random numbers are generated using a random number generator, an algorithm for computing a random-looking (but not truly random) sequence. For some simulations, it is important that you get the same sequence of random numbers each time. You can seed the random number generator with `r.setSeed()`.

## History

Tuesday, 1 April 2003 [Samuel A. Rebelsky]

• Created.
• Much of the content is based on assignment 2 from CS152 1997F.
• Edited significantly.
• Released.
• Evening: Made it clear that the five classes were required.

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Tue May 6 09:19:39 2003.
The source to the document was last modified on Tue Apr 1 22:29:00 2003.
This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS153/2003S/Homework/hw.03.html`.

You may wish to validate this document's HTML ; ; Check with Bobby

Samuel A. Rebelsky, rebelsky@grinnell.edu