CSE 4051: Project #1, Bullseye

Due: Friday, 11 January 2007

The Task

A simple dartboard consists of a flat, circular piece of cork with concentric rings drawn on it. Darts are thrown at the board by players in an attempt to hit the center of the dartboard (the Bullseye). The region between each pair of rings (or the center and the first ring) represents a certain point value. The closer the region is to the center of the dartboard, the more points the region is worth, as shown in the diagram below:

Ring radii are at 3", 6", 9", 12" and 15" (the Bullseye has a diameter of 6"). A game of darts between two players is played as follows. Each player throws three darts. A score is computed by adding up the point values of each region that a dart lands in. The player with the higher score wins the game. If they have the same score, then the games ends in a tie.

You are to write a program that computes the scores for two players, and determine who, if anyone, wins the game. For the purposes of this problem, you can assume that a dart has an infinitely fine point. If a dart lands exactly on a ring boundary, the higher point value is awarded. Any dart outside the outer ring receives no points.

Input/Output

Each line of the input represents a single game. A game has 12 values separated by one or more spaces. Ignore anything on the end of the line after the values. Player one's darts are represented by the first 3 pairs of values, and player two's by the last 3 pairs of values. Each pair of values represents the X and Y distances respectively of a dart from the center of the board in inches. The center is located at X=0, Y=0. Each value is an ordinary floating-point number with or without the decimal point and no more than than four digits to the right of the decimal point. The range of values are: -20.0 ≤ X,Y ≤ 20.0.

For each game G, print a line of the form:

Game #G: N to M, player P wins.
or:
Game #G: N to M, tie.
where N is player one's score, and M is player two's score. P is either 1 or 2 depending on which player wins. All values are non-negative integers. For the input:
 -9    0    0 -4.5  -2 2    9 0  0 4.5   2 -2    # Game 1
-19.0 19.0  0  0     0 0    3 3  6 6    12 12    # Game 2
the output would be:
Game #1: 240 to 240, tie.
Game #2: 200 to 140, player 1 wins.

Acknowledgment

Greater New York 2004; TJU #2101

Standard Input/Output

The standard input is the stream of characters that the Java program finds in the InputStream called System.in. The standard input is associated with keyboard by default, but can be associated with the file as in
java Bullseye < data
Now the characters found in the standard input come from the file named data. When the standard input comes from the keyboard a signal of some kind is needed to indicate from the interactive user when the end-of-file is reached because the length of the "file" cannot be known in advance. This signal is understood by the operating system which then passes it to the program. Different operating systems provide different mechanisms for doing this. In Unix the usual signal is typing control-D (in Dos, control-Z) on a line by itself. (Typing control-D in the middle of the line may have the effect of putting the u0004 character in the input stream instead of signaling end-of-file.)

The standard output is the stream of characters that the Java program writes to the PrintStream called System.out. The standard output is associated with the computer display by default, but can be associated with the file as in

java Bullseye > output
Now the output is collected in the file named output. Since keystrokes are echoed on the display, it is sometimes hard for the user to distinguish which characters on the display correspond to the standard input and which characters to the standard output. From the point of view of the program no such confusion exists.

Using standard input and standard output for IO is very flexible. The user of the program (instead of the program writer) can decide whether to type in the input or put the input in a some file. If the program was written to take input from a particular file, the user of the program has no choice.

Reading Line-By-Line

Here is a program fragment to read the standard input and write it to the standard output.
final Scanner s = new Scanner (new BufferedInputStream (System.in));

for (;;) {
   final String line = s.nextLine();
   if (line==null) break;             // null means end-of-file
   System.out.println (line);
}
It will be necessary to include:
import java.util.Scanner;
import java.io.BufferedInputStream;

Helpful Stuff

Turning it in

Turn in the Java source code for the program using the submission server. The file name should be Bullseye.java and the project is proj01. Be sure your name is in comments at the beginning of your program as required in the standard header for this class. For your convenience, here is a submission form for this assignment.

File 1
Control code:
Course=cse4051
Project=proj01


Ryan Stansifer <ryan@cs.fit.edu>
Last modified: Mon Dec 24 12:25:03 EST 2007