CS 152 Computer Programming Fundamentals Lab 3: Pig

advertisement
CS 152 Computer Programming Fundamentals
Lab 3: Pig∗
Fall 2014
1
Game Rules
Players: 2 or more (our version works for two players).
Object: Be the first to score 100 points.
Play: On your turn, roll the die. If you get a 1, your turn is over. Otherwise, your roll
is the number of points youve accumulated. You can then either stop and score the
points you’ve accumulated, or roll again trying to accumulate more points. If you ever
roll a 1, your turn ends and you lose all the points you’ve accumulated (but not those
you scored on previous turns).
2
Program Description
You will write a program in a class named Pig. To complete this program, you will need to
use a Scanner for user input, the Math.random() method to select a random number, and
if statements for (as you did in GuessNumber). In addition, you will need to use a loop (or
possibly nested loops!) to repeat behavior (this is the new part).
• The game will have two players. In my program, I identified them as Player 0 and
Player 1. (You may refer to them with some other labels, as long as we can tell whose
turn it is.)
• The game will continue as long as neither player has won.
• When a player’s turn begins, print the current score and identify which player’s turn
it is.
– A player’s turn continues until a 1 is rolled or until the player decides to stop and
score the points accumulated on this turn.
∗
This lab is based on the project at http://ljing.org/games/pig/
1
– Select a random integer between 1 and 6 for the current die roll.
– Report the value rolled.
– If the player rolled a 1, the turn is over with no points scored.
– If the player rolled some other number, add the die roll to the points accumulated
for this turn. Report the total points accumulated and ask if the player would
like to keep going.
∗ If the player wants to keep going, roll the die again.
∗ If player wants to stop, add the accumulated points to the player’s score and
end the turn.
– It is now the other player’s turn.
• When the game is over, report that fact and display the final score.
There is an example transcript on the course web site.
3
Turning in your assignment
Submit your Pig.java file to the Lab 3 assignment in UNM Learn. Do not attach .class
files or any other files.
4
Extra Challenge – Computer Player
If you have completed this lab and would like an additional challenge, try adding a computer
player.
• Add a prompt at the beginning of the game to allow the user to enable a computer
player. (You should still be able to decline and play the original two player game.)
• Make one of the players be a computer player. It doesn’t have to be very smart, but
must make legal plays. (No cheating by continuing roll after a 1, for example.) What
would be a good strategy to win this game?
• Make sure that you do not break the original assignment in the process of adding a
computer player! (I would suggest you just turn in your plain two human player version
before you work on the extra challenge. Learn will let you submit again later.)
• Enjoy your game!
2
Download