| Department of Computer Science | University of San Francisco |
|---|
For programming assignment 1, you should write a Java program that allows the user to play a game of Pig. In the game of Pig, a player rolls the dice repeatedly until she rolls at least one 1 or voluntarily gives up the dice. Each time she rolls the dice the total on the faces of the dice is added to her score, except when she rolls a 1. If she rolls one 1, she loses all the points she's accumulated so far in her turn. If she rolls two 1's, she loses all of her points so far in the game. The first player to get a total of 100 wins. For assignment 1, the user should play against the computer. The computer follows the same rules, except that it turns over the dice as soon as its score for the current turn is at least 20.
Here's a brief excerpt from a working program.
Welcome to the game of Pig. It's you against the computer.
You play by rolling the dice. The first player
to get 100 points wins. However, if you roll one 1
you lose all the points you've accumulated in your
turn. If you roll two 1's, you lose all your
points. You can turn the dice over at any time.
However, if you roll one or two 1's, you lose your
turn. I (the computer) play by the same rules,
except I'll always turn over the dice when I've
rolled 20 or more points in a single turn.
Good luck! May the better object win!
Ready to begin? (Type 'y' when you're ready)
y
You're rolling the dice . . .
You rolled 2 4
This gives you a turn total of
6
and a grand total of
6
The computer has a total of
0
Do you want to continue rolling? (Type 'y' or 'n')
y
You're rolling the dice . . .
You rolled 3 3
This gives you a turn total of
12
and a grand total of
12
The computer has a total of
0
Do you want to continue rolling? (Type 'y' or 'n')
y
You're rolling the dice . . .
You rolled 1 3
You got a 1!
Continue? (Type 'y' when you're ready to
turn the dice over to me)
y
The score is
You: 0
Computer: 0
I'm rolling the dice . . .
I rolled 1 4
I got a 1!
Continue? (Type 'y' when you're ready)
y
The score is
You: 0
Computer: 0
You're rolling the dice . . .
You rolled 5 5
This gives you a turn total of
10
and a grand total of
10
The computer has a total of
0
Do you want to continue rolling? (Type 'y' or 'n')
n
I'm rolling the dice . . .
I rolled 1 1
I got two 1's!
Continue? (Type 'y' when you're ready)
y
The score is
You: 10
Computer: 0
[Many turns deleted . . .]
You're rolling the dice . . .
You rolled 1 6
You got a 1!
Continue? (Type 'y' when you're ready to
turn the dice over to me)
y
The score is
You: 0
Computer: 93
I'm rolling the dice . . .
I rolled 5 4
This gives me a turn total of
9
and a grand total of
102
The score is
You: 0
Computer: 102
Better luck next time!
Your program doesn't have to print exactly the same messages,
but its output should contain the same information and it
should run in the same way. You can assume that the user
will make no mistakes when she types in her input.
Program Design. You should use at least three different objects: one for the dice, one for the user, and one for the computer. You will also need a class for the main method. So your program should contain at least four different classes: a Dice class, a PigUser class, a PigComputer class, and the class containing main, which should be called Pig.
The only static method in the program should be the main method. This means that every other public method you write needs to be called or invoked as an object method - i.e., the invocation will look something like object.method(params) instead of Class.method(paramss) or method(params).
I've written a Dice class that you can use in your program. However, it's OK for you to write your own version of this class. No method should be longer than 40 lines. (This limit will be reduced in future programs.)
Due Date. In order to receive full credit, your program must be submitted electronically by 10:00 am on Friday, February 9th, and you must turn in printouts of your programs to the professor's mailbox in Harney 545 by 5 pm on the 9th. You can receive half credit if you submit your program and turn in a print out after 10:00 am Friday but before 10:00 am Saturday. Programs submitted and printouts turned in later than 10:00 am Saturday, February 10 will receive no credit.
Grading. Correctness will be 40% of your grade. Does your program keep track of the total scores and turn scores? Does it correctly determine when a turn is over? Does it correctly determine when to quit?
The following static features will be graded.
Collaboration. It is OK for you to discuss solutions to this program with your classmates. However, no collaboration should ever involve looking at one of your classmate's source programs! It is usually extremely easy to determine that someone has copied a program, even when the indivdual doing the copying has changed identifiers and comments. If we discover that someone has copied a program, the authors of both programs will receive an F in the course.